찾다
데이터 베이스MySQL 튜토리얼【OpenGL】Shader技巧集合

【OpenGL】Shader技巧集合

Jun 07, 2016 pm 03:15 PM
openglu기능모으다기사모으다

这篇文章将收集unity中使用shader的相关技巧和特效,会不断地更新内容。关于在Unity中使用shader的介绍,请参考《【OpenGL】使用Unity来学习OpenGL》 常用的内置uniform iResolution =》_ScreenParams iGlobalTime = _Time.y glFragCoord = f loat4 sp:WPOS

这篇文章将收集unity中使用shader的相关技巧和特效,会不断地更新内容。关于在Unity中使用shader的介绍,请参考《【OpenGL】使用Unity来学习OpenGL》

常用的内置uniform

iResolution =》_ScreenParams

iGlobalTime => _Time.y

glFragCoord => float4 sp:WPOS  // 需要 #pragma target 3.0, 另外的方式请见下面

vec2 => float2

mix => lerp

mod => fmod

texture2D => tex2D

textureCube => texCUBE

mat2=>float2x2

fract=>frac

========

关于glFragCoord, 可以使用另外一种方式计算(支持3.0之前的)参考官方例子

o.scrPos = ComputeScreenPos(o.pos);

float2 wcoord = (i.scrPos.xy/i.scrPos.w);

-------

float2 wcoord = sp.xy/_ScreenParams.xy;


关于数学的Shader:https://www.shadertoy.com/view/ldlSD2    https://www.shadertoy.com/view/ldlSWj


很好的一个教程:http://ogldev.atspace.co.uk/index.html


Deferred Shading 原理: http://ogldev.atspace.co.uk/www/tutorial35/tutorial35.html 


关于Stencil Buffer 的理解:http://www.cnblogs.com/mikewolf2002/archive/2012/05/15/2500867.html

更多文章:1)http://docs.unity3d.com/Manual/SL-Stencil.html

2) http://answers.unity3d.com/questions/590800/how-to-cullrender-to-through-a-window.html


Stencil Shadow Volume : http://ogldev.atspace.co.uk/www/tutorial40/tutorial40.html

http://en.wikipedia.org/wiki/Shadow_volume


镜面反射的实现原理:

ftp://ftp.sgi.com/sgi/opengl/contrib/blythe/advanced99/notes/node158.html

其它镜面反射:

http://en.wikibooks.org/wiki/Cg_Programming/Unity/Mirrors


在unity cg中可以使用[HideInInspector]来隐藏uniform属性,这样就可以用作自定义常量。

Physically Based Rendering:   Tutorial: Physically Based Rendering, And you can too!

边缘检测:1) http://www.codeproject.com/Articles/94817/Pixel-Shader-for-Edge-Detection-and-Cartoon-Effect

2) http://coding-experiments.blogspot.hk/2010/06/edge-detection.html

3) http://en.wikipedia.org/wiki/Edge_detection

Cg函数表:http://http.developer.nvidia.com/CgTutorial/cg_tutorial_appendix_e.html

heat effect : http://forum.unity3d.com/threads/50132-Heat-Distortion,   http://www.cnblogs.com/geoffyange/archive/2013/06/06/3122570.html

skin shading in unity: http://www.altdevblogaday.com/2011/12/31/skin-shading-in-unity3d/

http://http.developer.nvidia.com/GPUGems3/gpugems3_ch14.html

http://gamedev.stackexchange.com/questions/31308/algorithm-for-creating-spheres

RenderMan University: http://renderman.pixar.com/view/renderman-university

一些shader的例子:













Shader "stalendp/shaderTest02" { //see https://www.shadertoy.com/view/4sj3zy
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
	}
	SubShader {
		Pass {
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma target 3.0
			
			#include "UnityCG.cginc"

			sampler2D _MainTex;
		
			//Variable declarations
			
			struct myvars {
				float3 bgColor;
				float sphereScale;
				float sphereShine;
				float3 sphereDiff;
				float3 sphereSpec;
				float2 specPoint;
			};

			float4 vert(appdata_base v) : POSITION {
				return mul(UNITY_MATRIX_MVP, v.vertex);
			}
			
			float4 frag(float4 sp:WPOS): COLOR {
				myvars mv;
				mv.bgColor = float3(0.6, 0.5, 0.6);
				mv.sphereScale = 0.7;
				mv.sphereShine = 0.5;
				mv.sphereDiff = float3(0.5, 0.0, 0.5);
				mv.sphereSpec = float3(1.0, 1.0, 1.0);
				mv.specPoint = float2(0.2, -0.1);
			
				// creates shader pixel coordinates
				float2 uv = sp.xy/_ScreenParams.xy;
				// sets the position of the camera
				float2 p = uv * 2.5 - float2(1.0, 1.0);
				p.x *= _ScreenParams.x / _ScreenParams.y;
				
				// Rotates the sphere in a circle
				p.x += cos(-_Time.y) *0.35;
				p.y += sin(-_Time.y) * 0.35;
				
				// Rotates the specular point with the sphere
				mv.specPoint.x += cos(-_Time.y) * 0.35;
				mv.specPoint.y += sin(-_Time.y) * 0.35;
				
				//Sets the radius of the sphere to the middle of the screen
				float radius = length(p);//sqrt(dot(p, p));
	
				float3 col = mv.bgColor;
	
				//Sets the initial dark shadow around the edge of the sphere
				float f = smoothstep(mv.sphereScale * 0.7, mv.sphereScale, length(p + mv.specPoint));
				col -= lerp(col, float3(0.0,0.0,0.0), f) * 0.2;
				
				//Only carries out the logic if the radius of the sphere is less than the scale
				if(radius <br>
<br>


<pre class="brush:php;toolbar:false">Shader "Custom/shaderTest03" {  // https://www.shadertoy.com/view/Xdf3DS
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
	}
	SubShader {
	
		Pass {
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma target 3.0
			
			#include "UnityCG.cginc"

			sampler2D _MainTex;

			
			struct myvars {
				float k;
				float f;
				float threshold;

				float3 colour;
				float3 normal;

				float3 lightPos;
				float3 lightColour;
				float3 ambient;
				float shinyness;
				float diffuseFactor;
				float4 fragCoord;
			};
			

			float2 center ( float2 border , float2 _offset , float2 vel, myvars mv) {
				float2 c = _offset + vel * _Time * 0.5;
				c = fmod ( c , 2. - 4. * border );
				if ( c.x > 1. - border.x ) c.x = 2. - c.x - 2. * border.x;
				if ( c.x  1. - border.y ) c.y = 2. - c.y - 2. * border.y;
				if ( c.y  b )
					return 0.0;
				if ( r >= b/3.0 ) {
					float rb = 1.0 - r/b;
					return (3.0*mv.k)/2.0 * rb * rb;
				}
				if ( r >= 0.0 && r <br>
<pre class="brush:php;toolbar:false">Shader "stalendp/shaderTest04" { //see https://www.shadertoy.com/view/Xsf3R8
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
	}
	SubShader {
		Pass {
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma target 3.0
			
			#include "UnityCG.cginc"

			sampler2D _MainTex;
			
			struct Ray {
				float3 org;
				float3 dir;
			};
			
			float rayPlaneIntersect( Ray ray, float4 plane ) {
				float f = dot( ray.dir, plane.xyz );
				
				float t = -( dot( ray.org, plane.xyz ) + plane.w );
				t /= f;
				
				return t;
			}
			
			float3 shade( float3 pos, float3 nrm, float4 light ) {
				float3 toLight = light.xyz - pos;
				float toLightLen = length( toLight );
				toLight = normalize( toLight );
					
				float diff = dot( nrm, toLight );
				float attn = 1.0 - pow( min( 1.0, toLightLen / light.w ), 2.0 );
				float comb = 2.0 * diff * attn;
				
				return float3( comb, comb, comb );
			}


			float4 vert(appdata_base v) : POSITION {
				return mul(UNITY_MATRIX_MVP, v.vertex);
			}
			
			float4 frag(float4 sp:WPOS): COLOR {
			
				// gl_FragCoord: location (0.5, 0.5) is returned 
				// for the lower-left-most pixel in a window
				
				// XY of the normalized device coordinate
				// ranged from [-1, 1]
				float2 ndcXY = -1.0 + 2.0 * sp.xy / _ScreenParams.xy;
				
				// aspect ratio
				float aspectRatio = _ScreenParams.x / _ScreenParams.y;
				
				// scaled XY which fits the aspect ratio
				float2 scaledXY = ndcXY * float2( aspectRatio, 1.0 );
				
				// camera XYZ in world space
				float3 camWsXYZ = float3( 0.0, 1.0, 0.0 );
				camWsXYZ.z += 10.0 * cos( _Time.y );
				
				// construct the ray in world space
				Ray ray;
				ray.org = camWsXYZ;
				ray.dir = float3( scaledXY, -2.0 ); // OpenGL is right handed
				
				// define the plane in world space
				float4 plane = float4( 0.0, 1.0, 0.0, 0.0 );
				
				float t = rayPlaneIntersect( ray, plane );
				
				// define the point light in world space (XYZ, range)
				float4 lightWs = float4( 0.0, 5.0, -5.0, 10.0 );
				
				if ( t >= 0.0 )
				{
					float3 sceneWsPos = ray.org + t * ray.dir;
					float3 sceneWsNrm = plane.xyz;
					float2 sceneUV = sceneWsPos.xz / 4.0;
					
					float4 sceneBase = tex2D( _MainTex, sceneUV );		
					float3 sceneShade = shade( sceneWsPos, sceneWsNrm, lightWs );
					
					return float4( sceneShade * sceneBase.xyz, 1.0 );
				}
			
				return float4( 0.0, 0.0, 0.0, 1.0 );
			}
			
			ENDCG
		}
	} 
	FallBack "Diffuse"
}


Shader "stalendp/shaderTest04" { //see https://www.shadertoy.com/view/MdB3Dw
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
	}
	SubShader {
		Pass {
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma target 3.0
			
			#include "UnityCG.cginc"
			
			#define USE_ANALYTICAL_MBLUR

			sampler2D _MainTex;
	
			// intersect a MOVING sphere
			float2 iSphere( in float3 ro, in float3 rd, in float4 sp, in float3 ve, out float3 nor )
			{
			    float t = -1.0;
				float s = 0.0;
				nor = float3(0.0);
				
				float3  rc = ro - sp.xyz;
				float A = dot(rc,rd);
				float B = dot(rc,rc) - sp.w*sp.w;
				float C = dot(ve,ve);
				float D = dot(rc,ve);
				float E = dot(rd,ve);
				float aab = A*A - B;
				float eec = E*E - C;
				float aed = A*E - D;
				float k = aed*aed - eec*aab;
					
				if( k>0.0 )
				{
					k = sqrt(k);
					float hb = (aed - k)/eec;
					float ha = (aed + k)/eec;
					
					float ta = max( 0.0, ha );
					float tb = min( 1.0, hb );
					
					if( ta 0.0 )
				{
					t = -b - sqrt(k);
					nor = normalize( (ro+rd*t) - sp.xyz );
				}

				return t;
			}

			float3 getPosition( float time ) { return float3(     2.5*sin(8.0*time), 0.0,      1.0*cos(8.0*time) ); }
			float3 getVelocity( float time ) { return float3( 8.0*2.5*cos(8.0*time), 0.0, -8.0*1.0*sin(8.0*time) ); }


			float4 vert(appdata_base v) : POSITION {
				return mul(UNITY_MATRIX_MVP, v.vertex);
			}
			
			float4 frag(float4 sp:WPOS): COLOR {
				float2 q = sp.xy / _ScreenParams.xy;
				float2 p = -1.0 + 2.0*q;
				p.x *= _ScreenParams.x/_ScreenParams.y;	

				// camera
				float3  ro = float3(0.0,0.0,4.0);
			    float3  rd = normalize( float3(p.xy,-2.0) );
				
			    // sphere	
				
				// render
				float3  col = float3(0.0);
				
				#ifdef USE_ANALYTICAL_MBLUR
							
			    //---------------------------------------------------	
			    // render with analytical motion blur
			    //---------------------------------------------------	
				float3  ce = getPosition( _Time.y );
				float3  ve = getVelocity( _Time.y );
			    	
				col = float3(0.25) + 0.3*rd.y;
				float3 nor = float3(0.0);
				float3 tot = float3(0.25) + 0.3*rd.y;
			    float2 res = iSphere( ro, rd, float4(ce,1.0), ve/24.0, nor );
				float t = res.x;
				if( t>0.0 )
				{
					float dif = clamp( dot(nor,float3(0.5703)), 0.0, 1.0 );
					float amb = 0.5 + 0.5*nor.y;
					float3  lcol = dif*float3(1.0,0.9,0.3) + amb*float3(0.1,0.2,0.3);
					col = lerp( tot, lcol, res.y );
				}
				
				#else
				
			    //---------------------------------------------------	
			    // render with brute force sampled motion blur
			    //---------------------------------------------------	
				
			    #define NUMSAMPLES 32
				float3 tot = float3(0.0);
				for( int i=0; i<numsamples i float fi="float(i)/float(NUMSAMPLES);" float3 ce="getPosition(" _time.y nor="float3(0.0);" tmp="float3(0.25)" t="iSphere(" ro rd float4 if>0.0 )
			        {
			            float dif = clamp( dot(nor,float3(0.5703)), 0.0, 1.0 );
			            float amb = 0.5 + 0.5*nor.y;
			            tmp = dif*float3(1.0,0.9,0.3) + amb*float3(0.1,0.2,0.3);
			        }
			        col += tmp;
				}		
				col /= float(NUMSAMPLES);
					
			    #endif
				
				col = pow( clamp(col,0.0,1.0), float3(0.45) );

				return float4( col, 1.0 );
			}
			
			ENDCG
		}
	} 
	FallBack "Diffuse"
}
</numsamples>

Shader "stalendp/shaderTest05" { //see https://www.shadertoy.com/view/XsB3DW
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_CubeDiffuse ("Cubemap Diffuse Map", CUBE) = "" {}
		vv1("vv1", float) = -1.0
		vv2("vv2", float) = 2.0
	}
	SubShader {
		Pass {
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma target 3.0
			//下面防止编译错误:instruction limit of 1024 exceed;
			#pragma glsl  
			
			#include "UnityCG.cginc" 
			
			#define MAX_STEPS 64
			#define MAX_REFLECTIONS 4
			#define PI 3.1415926536

			sampler2D _MainTex;
			samplerCUBE _CubeDiffuse;
			float vv1, vv2;
	
			struct Ray {
				float3 o;
				float3 d;
			};
			struct Sphere {
				float3 o;
				float r;
			};
			struct Box {
				float3 o;
				float3 s;
			};
			struct Torus {
				float3 o;
				float2 s;
			};
						
			float2 rotate2d(in float2 v, in float a) {
				float sinA = sin(a);
				float cosA = cos(a);
				return float2(v.x * cosA - v.y * sinA, v.y * cosA + v.x * sinA);	
			}

			float sdSphere(in float3 p, in Sphere s) {
				return length(p-s.o)-s.r;
			}
			float sdBox(in float3 p, in Box b) {
				float3 d = abs(p-b.o) - b.s;
				return min(max(d.x,max(d.y,d.z)),0.0) +
					length(max(d,0.0));
			}
			float sdTorus(in float3 p, in Torus t) {
				p -= t.o;
				float2 q = float2(length(p.xz)-t.s.x,p.y);
				return length(q)-t.s.y;
			}
			float world(in float3 p) {
				float ti = fmod(_Time.y,10.);
				if(ti > 2.) {
					Sphere s0 = Sphere(float3(0),1.);
					Box b0 = Box(float3(0),float3(.8));
					if(ti <br>
<pre class="brush:php;toolbar:false">

CGINCLUDE的使用



성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
MySQL에서 사용 가능한 다른 스토리지 엔진은 무엇입니까?MySQL에서 사용 가능한 다른 스토리지 엔진은 무엇입니까?Apr 26, 2025 am 12:27 AM

mysqloffersvariousStorageEngines, 각각의 everitedforentUsecases : 1) innodbisidealforapplicationsneedingAcidCoInceandHighConcurrency, 지원 트랜잭션 및 foreignKeys.2) myIsAmisbestforread-heverworkloads, memoryengineis

MySQL의 일반적인 보안 취약점은 무엇입니까?MySQL의 일반적인 보안 취약점은 무엇입니까?Apr 26, 2025 am 12:27 AM

MySQL의 일반적인 보안 취약점에는 SQL 주입, 약한 암호, 부적절한 권한 구성 및 업데이트되지 않은 소프트웨어가 포함됩니다. 1. 전처리 명령문을 사용하여 SQL 주입을 방지 할 수 있습니다. 2. 강력한 비밀번호 전략을 사용하여 약한 암호는 피할 수 있습니다. 3. 정기적 인 검토 및 사용자 권한 조정을 통해 부적절한 권한 구성을 해결할 수 있습니다. 4. Unupdated 소프트웨어는 MySQL 버전을 정기적으로 확인하고 업데이트하여 패치 할 수 있습니다.

MySQL에서 느린 쿼리를 어떻게 식별 할 수 있습니까?MySQL에서 느린 쿼리를 어떻게 식별 할 수 있습니까?Apr 26, 2025 am 12:15 AM

느린 쿼리 로그를 활성화하고 임계 값을 설정하여 MySQL에서 느린 쿼리를 식별 할 수 있습니다. 1. 느린 쿼리 로그를 활성화하고 임계 값을 설정하십시오. 2. 느린 쿼리 로그 파일을보고 분석하고 심층 분석을 위해 MySQLDumpSlow 또는 PT-Query 소수성과 같은 도구를 사용하십시오. 3. 인덱스 최적화, 쿼리 재 작성 및 select*의 사용을 피함으로써 느린 쿼리 최적화를 달성 할 수 있습니다.

MySQL 서버 건강 및 성능을 어떻게 모니터링 할 수 있습니까?MySQL 서버 건강 및 성능을 어떻게 모니터링 할 수 있습니까?Apr 26, 2025 am 12:15 AM

MySQL 서버의 건강 및 성능을 모니터링하려면 시스템 건강, 성능 지표 및 쿼리 실행에주의를 기울여야합니다. 1) 시스템 건강 모니터링 : CPU, 메모리, 디스크 I/O 및 네트워크 활동을 볼 수 있도록 상단, HTOP 또는 ShowGlobalStatus 명령을 사용하십시오. 2) 성능 표시기 추적 : 초당 쿼리 번호, 평균 쿼리 시간 및 캐시 적중률과 같은 주요 표시기를 모니터링합니다. 3) 쿼리 실행 최적화 확인 : 실행 시간이 설정 임계 값을 초과하는 쿼리를 느린 쿼리 로그를 활성화하고 기록 및 최적화하십시오.

MySQL 및 Mariadb를 비교하고 대조하십시오.MySQL 및 Mariadb를 비교하고 대조하십시오.Apr 26, 2025 am 12:08 AM

MySQL과 Mariadb의 주요 차이점은 성능, 기능 및 라이센스입니다. 1. MySQL은 Oracle에 의해 개발되었으며 Mariadb는 포크입니다. 2. MariaDB는 높은 하중 환경에서 더 나은 성능을 발휘할 수 있습니다. 3. Mariadb는 더 많은 스토리지 엔진과 기능을 제공합니다. 4.MySQL은 듀얼 라이센스를 채택하고 MariaDB는 완전히 오픈 소스입니다. 선택할 때 기존 인프라, 성능 요구 사항, 기능 요구 사항 및 라이센스 비용을 고려해야합니다.

MySQL의 라이센스는 다른 데이터베이스 시스템과 어떻게 비교됩니까?MySQL의 라이센스는 다른 데이터베이스 시스템과 어떻게 비교됩니까?Apr 25, 2025 am 12:26 AM

MySQL은 GPL 라이센스를 사용합니다. 1) GPL 라이센스는 MySQL의 무료 사용, 수정 및 분포를 허용하지만 수정 된 분포는 GPL을 준수해야합니다. 2) 상업용 라이센스는 공개 수정을 피할 수 있으며 기밀이 필요한 상업용 응용 프로그램에 적합합니다.

MyISAM을 통해 언제 innodb를 선택 하시겠습니까?MyISAM을 통해 언제 innodb를 선택 하시겠습니까?Apr 25, 2025 am 12:22 AM

MyISAM 대신 InnoDB를 선택할 때의 상황에는 다음이 포함됩니다. 1) 거래 지원, 2) 높은 동시성 환경, 3) 높은 데이터 일관성; 반대로, MyISAM을 선택할 때의 상황에는 다음이 포함됩니다. 1) 주로 읽기 작업, 2) 거래 지원이 필요하지 않습니다. InnoDB는 전자 상거래 플랫폼과 같은 높은 데이터 일관성 및 트랜잭션 처리가 필요한 응용 프로그램에 적합하지만 MyISAM은 블로그 시스템과 같은 읽기 집약적 및 트랜잭션이없는 애플리케이션에 적합합니다.

MySQL에서 외국 키의 목적을 설명하십시오.MySQL에서 외국 키의 목적을 설명하십시오.Apr 25, 2025 am 12:17 AM

MySQL에서 외국 키의 기능은 테이블 간의 관계를 설정하고 데이터의 일관성과 무결성을 보장하는 것입니다. 외국 키는 참조 무결성 검사 및 계단식 작업을 통해 데이터의 효과를 유지합니다. 성능 최적화에주의를 기울이고 사용할 때 일반적인 오류를 피하십시오.

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

안전한 시험 브라우저

안전한 시험 브라우저

안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

DVWA

DVWA

DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기