【OpenGL】Shader技巧集合

这篇文章将收集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的例子:

[cpp]  view plain  copy
  1. Shader "shaderToy/LolCrap" {  
  2.     Properties {  
  3.         _MainTex ("image", 2D) = "white" {}  
  4.         _NoiseTex("noise", 2D) = "bump" {}  
  5.         _percent("percent", Range(-0.3, 1)) = 0  
  6.         _DefColor ("defalutColor", COLOR)  = ( 0, .8, .4, 1)  
  7.     }  
  8.       
  9.     CGINCLUDE  
  10.         #include "UnityCG.cginc"             
  11.         #pragma target 3.0    
  12.         #pragma glsl      
  13.                     
  14.         float mod289(float x) {  
  15.             return x - floor(x * (1.0 / 289.0)) * 289.0;  
  16.         }  
  17.   
  18.         float4 mod289(float4 x) {  
  19.             return x - floor(x * (1.0 / 289.0)) * 289.0;  
  20.         }  
  21.   
  22.         float4 perm(float4 x) {  
  23.             return mod289(((x * 34.0) + 1.0) * x);  
  24.         }  
  25.   
  26.         float noise3d(float3 p) {  
  27.             float3 a = floor(p);  
  28.             float3 d = p - a;  
  29.             d = d * d * (3.0 - 2.0 * d);  
  30.   
  31.             float4 b = a.xxyy + float4(0.0, 1.0, 0.0, 1.0);  
  32.             float4 k1 = perm(b.xyxy);  
  33.             float4 k2 = perm(k1.xyxy + b.zzww);  
  34.   
  35.             float4 c = k2 + a.zzzz;  
  36.             float4 k3 = perm(c);  
  37.             float4 k4 = perm(c + 1.0);  
  38.   
  39.             float4 o1 = frac(k3 * (1.0 / 41.0));  
  40.             float4 o2 = frac(k4 * (1.0 / 41.0));  
  41.   
  42.             float4 o3 = o2 * d.z + o1 * (1.0 - d.z);  
  43.             float2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);  
  44.   
  45.             return o4.y * d.y + o4.x * (1.0 - d.y);  
  46.         }  
  47.   
  48.         struct v2f {      
  49.             half4 pos:SV_POSITION;      
  50.             half4 uv : TEXCOORD0;     
  51.         };    
  52.   
  53.         v2f vert(appdata_base v) {  
  54.             v2f o;    
  55.             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);    
  56.             return o;    
  57.         }  
  58.     
  59.         fixed4 frag(float4 sp:WPOS) : COLOR0 {  
  60.                   
  61.             float2 uv = 2.0 * sp.xy / _ScreenParams.xy - 1.0;  
  62.               
  63.             float3 water[4];  
  64.             float3 fire[4];  
  65.   
  66.             float3x3 r = float3x3(0.36, 0.48, -0.8, -0.8, 0.60, 0.0, 0.48, 0.64, 0.60);  
  67.             float3 p_pos = mul(float3(uv * float2(16.0, 9.0), 0.0), r);  
  68.             float3 p_time = mul(float3(0.0, 0.0, _Time.y * 2.0), r);  
  69.   
  70.         //    /* Noise sampling points for water */  
  71.             water[0] = p_pos / 2.0 + p_time;  
  72.             water[1] = p_pos / 4.0 + p_time;  
  73.             water[2] = p_pos / 8.0 + p_time;  
  74.             water[3] = p_pos / 16.0 + p_time;  
  75.   
  76.         //    /* Noise sampling points for fire */  
  77.             p_pos = 16.0 * p_pos - mul( float3(0.0, mod289(_Time.y) * 128.0, 0.0), r);  
  78.             fire[0] = p_pos / 2.0 + p_time * 2.0;  
  79.             fire[1] = p_pos / 4.0 + p_time * 1.5;  
  80.             fire[2] = p_pos / 8.0 + p_time;  
  81.             fire[3] = p_pos / 16.0 + p_time;  
  82.   
  83.             float2x2 rot = float2x2(cos(_Time.y), sin(_Time.y), -sin(_Time.y), cos(_Time.y));  
  84.   
  85.             float2 poszw = mul(uv, rot);  
  86.   
  87.         //  /* Dither the transition between water and fire */  
  88.             float test = poszw.x * poszw.y + 1.5 * sin(_Time.y);  
  89.             float2 d = float2(16.0, 9.0) * uv;  
  90.             test += 0.5 * (length(frac(d) - 0.5) - length(frac(d + 0.5) - 0.5));  
  91.   
  92.         //    /* Compute 4 octaves of noise */  
  93.             float3 points[4];  
  94.             points[0] = (test > 0.0) ? fire[0] : water[0];  
  95.             points[1] = (test > 0.0) ? fire[1] : water[1];  
  96.             points[2] = (test > 0.0) ? fire[2] : water[2];  
  97.             points[3] = (test > 0.0) ? fire[3] : water[3];  
  98.               
  99.             float4 n = float4(noise3d(points[0]),  
  100.                           noise3d(points[1]),  
  101.                           noise3d(points[2]),  
  102.                           noise3d(points[3]));  
  103.   
  104.             float4 color;  
  105.   
  106.             if (test > 0.0)  
  107.             {  
  108.         //        /* Use noise results for fire */  
  109.                 float p = dot(n, float4(0.125, 0.125, 0.25, 0.5));  
  110.   
  111.         //        /* Fade to black on top of screen */  
  112.                 p -= uv.y * 0.8 + 0.25;  
  113.                 p = max(p, 0.0);  
  114.                 p = min(p, 1.0);  
  115.   
  116.                 float q = p * p * (3.0 - 2.0 * p);  
  117.                 float r = q * q * (3.0 - 2.0 * q);  
  118.                 color = float4(min(q * 2.0, 1.0),  
  119.                              max(r * 1.5 - 0.5, 0.0),  
  120.                              max(q * 8.0 - 7.3, 0.0),  
  121.                              1.0);  
  122.             }  
  123.             else  
  124.             {  
  125.         //        /* Use noise results for water */  
  126.                 float p = dot(abs(2.0 * n - 1.0),  
  127.                               float4(0.5, 0.25, 0.125, 0.125));  
  128.                 float q = sqrt(p);  
  129.   
  130.                 color = float4(1.0 - q,  
  131.                              1.0 - 0.5 * q,  
  132.                              1.0,  
  133.                              1.0);  
  134.             }  
  135.   
  136.             return color;  
  137.         }    
  138.   
  139.     ENDCG      
  140.     
  141.     SubShader {     
  142.         Tags {"Queue" = "Transparent"}       
  143.         ZWrite Off       
  144.         Blend SrcAlpha OneMinusSrcAlpha       
  145.         Pass {      
  146.             CGPROGRAM      
  147.             #pragma vertex vert      
  148.             #pragma fragment frag      
  149.             #pragma fragmentoption ARB_precision_hint_fastest       
  150.     
  151.             ENDCG      
  152.         }  
  153.     }  
  154.     FallBack Off    
  155. }  


[cpp]  view plain  copy
  1. // Modified by stalendp  
  2. // Created by inigo quilez - iq/2013  
  3. // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.  
  4.   
  5. // A list of usefull distance function to simple primitives, and an example on how to   
  6. // do some interesting boolean operations, repetition and displacement.  
  7. //  
  8. // More info here: http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm  
  9.   
  10. Shader "shaderToy/raymarchingPrimitives" {  
  11.   
  12.    Properties {    
  13.         _NoiseTex("noise", 2D) = "bump" {}    
  14.     }    
  15.   
  16.     CGINCLUDE    
  17.         #include "UnityCG.cginc"               
  18.           
  19.         #pragma target 3.0  
  20.         #pragma glsl    
  21.           
  22.           
  23.           
  24.   
  25.   
  26. float sdPlane( float3 p )  
  27. {  
  28.     return p.y;  
  29. }  
  30.   
  31. float sdSphere( float3 p, float s )  
  32. {  
  33.     return length(p)-s;  
  34. }  
  35.   
  36. float sdBox( float3 p, float3 b )  
  37. {  
  38.   float3 d = abs(p) - b;  
  39.   return min(max(d.x,max(d.y,d.z)),0.0) +  
  40.          length(max(d,0.0));  
  41. }  
  42.   
  43. float udRoundBox( float3 p, float3 b, float r )  
  44. {  
  45.   return length(max(abs(p)-b,0.0))-r;  
  46. }  
  47.   
  48. float sdTorus( float3 p, float3 t )  
  49. {  
  50.   float2 q = float2(length(p.xz)-t.x,p.y);  
  51.   return length(q)-t.y;  
  52. }  
  53.   
  54. float sdHexPrism( float3 p, float2 h )  
  55. {  
  56.     float3 q = abs(p);  
  57.     return max(q.z-h.y,max(q.x+q.y*0.57735,q.y*1.1547)-h.x);  
  58. }  
  59.   
  60. float sdCapsule( float3 p, float3 a, float3 b, float r )  
  61. {  
  62.     float3 pa = p - a;  
  63.     float3 ba = b - a;  
  64.     float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );  
  65.       
  66.     return length( pa - ba*h ) - r;  
  67. }  
  68.   
  69. float sdTriPrism( float3 p, float2 h )  
  70. {  
  71.     float3 q = abs(p);  
  72.     return max(q.z-h.y,max(q.x*0.866025+p.y*0.5,-p.y)-h.x*0.5);  
  73. }  
  74.   
  75. float sdCylinder( float3 p, float2 h )  
  76. {  
  77.   float2 d = abs(float2(length(p.xz),p.y)) - h;  
  78.   return min(max(d.x,d.y),0.0) + length(max(d,0.0));  
  79. }  
  80.   
  81.   
  82. float sdCone( in float3 p, in float3 c )  
  83. {  
  84.     float2 q = float2( length(p.xz), p.y );  
  85.     return max( max( dot(q,c.xy), p.y), -p.y-c.z );  
  86. }  
  87.   
  88. float length2( float2 p )  
  89. {  
  90.     return sqrt( p.x*p.x + p.y*p.y );  
  91. }  
  92.   
  93. float length6( float2 p )  
  94. {  
  95.     p = p*p*p; p = p*p;  
  96.     return pow( p.x + p.y, 1.0/6.0 );  
  97. }  
  98.   
  99. float length8( float2 p )  
  100. {  
  101.     p = p*p; p = p*p; p = p*p;  
  102.     return pow( p.x + p.y, 1.0/8.0 );  
  103. }  
  104.   
  105. float sdTorus82( float3 p, float2 t )  
  106. {  
  107.   float2 q = float2(length2(p.xz)-t.x,p.y);  
  108.   return length8(q)-t.y;  
  109. }  
  110.   
  111. float sdTorus88( float3 p, float2 t )  
  112. {  
  113.   float2 q = float2(length8(p.xz)-t.x,p.y);  
  114.   return length8(q)-t.y;  
  115. }  
  116.   
  117. float sdCylinder6( float3 p, float2 h )  
  118. {  
  119.   return max( length6(p.xz)-h.x, abs(p.y)-h.y );  
  120. }  
  121.   
  122. //----------------------------------------------------------------------  
  123.   
  124. float opS( float d1, float d2 )  
  125. {  
  126.     return max(-d2,d1);  
  127. }  
  128.   
  129. float2 opU( float2 d1, float2 d2 )  
  130. {  
  131.     return (d1.x<d2.x) ? d1 : d2;  
  132. }  
  133.   
  134. float3 opRep( float3 p, float3 c )  
  135. {  
  136.     return fmod(p,c)-0.5*c;  
  137. }  
  138.   
  139. //float3 opTwist( float3 p )  
  140. //{  
  141. //    float  c = cos(10.0*p.y+10.0);  
  142. //    float  s = sin(10.0*p.y+10.0);  
  143. //    float2x2   m = float2x2(c,-s,s,c);  
  144. //    return float3(m*p.xz,p.y);  
  145. //}  
  146.   
  147. //----------------------------------------------------------------------  
  148. //  
  149. float2 map( in float3 pos )  
  150. {  
  151.     float2 res = opU( float2( sdPlane(     pos), 1.0 ),  
  152.                     float2( sdSphere(    pos-float3( 0.0,0.25, 0.0), 0.25 ), 46.9 ) );  
  153. //    res = opU( res, float2( sdBox(       pos-float3( 1.0,0.25, 0.0), float3(0.25) ), 3.0 ) );  
  154. //    res = opU( res, float2( udRoundBox(  pos-float3( 1.0,0.25, 1.0), float3(0.15), 0.1 ), 41.0 ) );  
  155. //  res = opU( res, float2( sdTorus(     pos-float3( 0.0,0.25, 1.0), float2(0.20,0.05) ), 25.0 ) );  
  156. //    res = opU( res, float2( sdCapsule(   pos,float3(-1.3,0.20,-0.1), float3(-1.0,0.20,0.2), 0.1  ), 31.9 ) );  
  157. //  res = opU( res, float2( sdTriPrism(  pos-float3(-1.0,0.25,-1.0), float2(0.25,0.05) ),43.5 ) );  
  158. //  res = opU( res, float2( sdCylinder(  pos-float3( 1.0,0.30,-1.0), float2(0.1,0.2) ), 8.0 ) );  
  159. //  res = opU( res, float2( sdCone(      pos-float3( 0.0,0.50,-1.0), float3(0.8,0.6,0.3) ), 55.0 ) );  
  160. //  res = opU( res, float2( sdTorus82(   pos-float3( 0.0,0.25, 2.0), float2(0.20,0.05) ),50.0 ) );  
  161. //  res = opU( res, float2( sdTorus88(   pos-float3(-1.0,0.25, 2.0), float2(0.20,0.05) ),43.0 ) );  
  162. //  res = opU( res, float2( sdCylinder6( pos-float3( 1.0,0.30, 2.0), float2(0.1,0.2) ), 12.0 ) );  
  163. //  res = opU( res, float2( sdHexPrism(  pos-float3(-1.0,0.20, 1.0), float2(0.25,0.05) ),17.0 ) );  
  164.   
  165. //#if 1  
  166. //    res = opU( res, float2( opS(  
  167. //                   udRoundBox(  pos-float3(-2.0,0.2, 1.0), float3(0.15),0.05),  
  168. //                   sdSphere(    pos-float3(-2.0,0.2, 1.0), 0.25)), 13.0 ) );  
  169. //    res = opU( res, float2( opS(  
  170. //                   sdTorus82(  pos-float3(-2.0,0.2, 0.0), float2(0.20,0.1)),  
  171. //                   sdCylinder(  opRep( float3(atan(pos.x+2.0,pos.z)/6.2831 + 0.1*iGlobalTime,  
  172. //                                            pos.y,  
  173. //                                            0.02+0.5*length(pos-float3(-2.0,0.2, 0.0))),  
  174. //                                       float3(0.05,1.0,0.05)), float2(0.02,0.6))), 51.0 ) );  
  175. //  res = opU( res, float2( sdSphere(    pos-float3(-2.0,0.25,-1.0), 0.2 ) +   
  176. //                                     0.03*sin(50.0*pos.x)*sin(50.0*pos.y+8.0*iGlobalTime)*sin(50.0*pos.z),   
  177. //                                       65.0 ) );  
  178. //  
  179. //  res = opU( res, float2( 0.5*sdTorus( opTwist(pos-float3(-2.0,0.25, 2.0)),float2(0.20,0.05)), 46.7 ) );  
  180. //#endif  
  181.   
  182.     return res;  
  183. }  
  184.   
  185.   
  186.   
  187.   
  188. float2 castRay( in float3 ro, in float3 rd, in float maxd )  
  189. {  
  190.     float precis = 0.001;  
  191.     float h=precis*2.0;  
  192.     float t = 0.0;  
  193.     float m = -1.0;  
  194.     forint i=0; i<60; i++ )  
  195.     {  
  196.         if( abs(h)<precis||t>maxd ) continue;//break;  
  197.         t += h;  
  198.         float2 res = map( ro+rd*t );  
  199.         h = res.x;  
  200.         m = res.y;  
  201.     }  
  202.   
  203.     if( t>maxd ) m=-1.0;  
  204.     return float2( t, m );  
  205. }  
  206.   
  207.   
  208. float softshadow( in float3 ro, in float3 rd, in float mint, in float maxt, in float k )  
  209. {  
  210.     float res = 1.0;  
  211.     float t = mint;  
  212.     forint i=0; i<30; i++ )  
  213.     {  
  214.         if( t<maxt )  
  215.         {  
  216.         float h = map( ro + rd*t ).x;  
  217.         res = min( res, k*h/t );  
  218.         t += 0.02;  
  219.         }  
  220.     }  
  221.     return clamp( res, 0.0, 1.0 );  
  222.   
  223. }  
  224.   
  225. float3 calcNormal( in float3 pos )  
  226. {  
  227.     float3 eps = float3( 0.001, 0.0, 0.0 );  
  228.     float3 nor = float3(  
  229.         map(pos+eps.xyy).x - map(pos-eps.xyy).x,  
  230.         map(pos+eps.yxy).x - map(pos-eps.yxy).x,  
  231.         map(pos+eps.yyx).x - map(pos-eps.yyx).x );  
  232.     return normalize(nor);  
  233. }  
  234.   
  235. float calcAO( in float3 pos, in float3 nor )  
  236. {  
  237.     float totao = 0.0;  
  238.     float sca = 1.0;  
  239.     forint aoi=0; aoi<5; aoi++ )  
  240.     {  
  241.         float hr = 0.01 + 0.05*float(aoi);  
  242.         float3 aopos =  nor * hr + pos;  
  243.         float dd = map( aopos ).x;  
  244.         totao += -(dd-hr)*sca;  
  245.         sca *= 0.75;  
  246.     }  
  247.     return clamp( 1.0 - 4.0*totao, 0.0, 1.0 );  
  248. }  
  249.   
  250. float3 render( in float3 ro, in float3 rd )  
  251. {   
  252.     float3 col = float3(0.0);  
  253.     float2 res = castRay(ro,rd,20.0);  
  254.     float t = res.x;  
  255.     float m = res.y;  
  256.     if( m>-0.5 )  
  257.     {  
  258.         float3 pos = ro + t*rd;  
  259.         float3 nor = calcNormal( pos );  
  260.   
  261.         //col = float3(0.6) + 0.4*sin( float3(0.05,0.08,0.10)*(m-1.0) );  
  262.         col = float3(0.6) + 0.4*sin( float3(0.05,0.08,0.10)*(m-1.0) );  
  263.           
  264.         float ao = calcAO( pos, nor );  
  265.   
  266.         float3 lig = normalize( float3(-0.6, 0.7, -0.5) );  
  267.         float amb = clamp( 0.5+0.5*nor.y, 0.0, 1.0 );  
  268.         float dif = clamp( dot( nor, lig ), 0.0, 1.0 );  
  269.         float bac = clamp( dot( nor, normalize(float3(-lig.x,0.0,-lig.z))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0);  
  270.   
  271.         float sh = 1.0;  
  272.         if( dif>0.02 ) { sh = softshadow( pos, lig, 0.02, 10.0, 7.0 ); dif *= sh; }  
  273.   
  274.         float3 brdf = float3(0.0);  
  275.         brdf += 0.20*amb*float3(0.10,0.11,0.13)*ao;  
  276.         brdf += 0.20*bac*float3(0.15,0.15,0.15)*ao;  
  277.         brdf += 1.20*dif*float3(1.00,0.90,0.70);  
  278.   
  279.         float pp = clamp( dot( reflect(rd,nor), lig ), 0.0, 1.0 );  
  280.         float spe = sh*pow(pp,16.0);  
  281.         float fre = ao*pow( clamp(1.0+dot(nor,rd),0.0,1.0), 2.0 );  
  282.   
  283.         col = col*brdf + float3(1.0)*col*spe + 0.2*fre*(0.5+0.5*col);  
  284.           
  285.     }  
  286.   
  287.     col *= exp( -0.01*t*t );  
  288.   
  289.   
  290.     return float3( clamp(col,0.0,1.0) );  
  291. }  
  292.   
  293.         struct v2f {        
  294.             half4 pos:SV_POSITION;        
  295.             half4 uv : TEXCOORD0;       
  296.         };      
  297.       
  298.         v2f vert(appdata_full v) {      
  299.             v2f o;      
  300.             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);      
  301.             return o;      
  302.         }      
  303.       
  304.         float4 frag(float4 sp:WPOS) : COLOR0 {    
  305.             float2 q = sp.xy/_ScreenParams.xy;  
  306.             float2 p = -1.0+2.0*q;  
  307.             p.x *= _ScreenParams.x/_ScreenParams.y;  
  308.             float2 mo = float2(1)/_ScreenParams.xy;  
  309.             //         
  310.             float time = 15.0 +  _Time.y;  
  311.   
  312.             // camera     
  313.             float3 ro = float3( -0.5+3.2*cos(0.1*time + 6.0*mo.x), 1.0 + 2.0*mo.y, 0.5 + 3.2*sin(0.1*time + 6.0*mo.x) );  
  314.             float3 ta = float3( -0.5, -0.4, 0.5 );  
  315.   
  316.             // camera tx  
  317.             float3 cw = normalize( ta-ro );  
  318.             float3 cp = float3( 0.0, 1.0, 0.0 );  
  319.             float3 cu = normalize( cross(cw,cp) );  
  320.             float3 cv = normalize( cross(cu,cw) );  
  321.             float3 rd = normalize( p.x*cu + p.y*cv + 2.5*cw );  
  322.               
  323.             float3 col = render( ro, rd );  
  324.   
  325.             col = sqrt( col );  
  326.             return  float4( col, 1.0 );  
  327.         }      
  328.     ENDCG        
  329.       
  330.     SubShader {       
  331.         Tags {"Queue" = "Transparent"}         
  332.         ZWrite Off         
  333.         Blend SrcAlpha OneMinusSrcAlpha         
  334.         Pass {        
  335.             CGPROGRAM        
  336.             #pragma vertex vert        
  337.             #pragma fragment frag        
  338.             #pragma fragmentoption ARB_precision_hint_fastest         
  339.       
  340.             ENDCG        
  341.         }    
  342.     }   
  343. }  




[cpp]  view plain  copy
  1. Shader "shaderToy/Interstellar" {  
  2.   
  3.    Properties {    
  4.         _NoiseTex("noise", 2D) = "bump" {}    
  5.     }    
  6.   
  7.     CGINCLUDE    
  8.         #include "UnityCG.cginc"               
  9.           
  10.         #pragma target 3.0  
  11.         #pragma glsl    
  12.         sampler2D _NoiseTex;            
  13.           
  14.   
  15.         float3 ToLinear( in float3 col )  
  16.         {  
  17.             // simulate a monitor, converting colour values into light values  
  18.             return pow( col, float3(2.2) );  
  19.         }  
  20.   
  21.         float3 ToGamma( in float3 col )  
  22.         {  
  23.             // convert back into colour values, so the correct light will come out of the monitor  
  24.             return pow( col, float3(1.0/2.2) );  
  25.         }  
  26.   
  27.         float4 Noise( in float2 x )  
  28.         {  
  29.             return tex2D( _NoiseTex, (float2(x)+0.5)/256.0 );  
  30.         }  
  31.   
  32.         float4 Rand( in int x )  
  33.         {  
  34.             float2 uv;  
  35.             uv.x = (float(x)+0.5)/256.0;  
  36.             uv.y = (floor(uv.x)+0.5)/256.0;  
  37.             return tex2D( _NoiseTex, uv );  
  38.         }  
  39.   
  40.         struct v2f {        
  41.             half4 pos:SV_POSITION;        
  42.             half4 uv : TEXCOORD0;       
  43.         };      
  44.       
  45.         v2f vert(appdata_full v) {      
  46.             v2f o;      
  47.             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);      
  48.             return o;      
  49.         }      
  50.       
  51.         float4 frag(float4 sp:WPOS) : COLOR0 {    
  52.             float3 ray;  
  53.             ray.xy = 2.0*(sp.xy-_ScreenParams.xy*.5)/_ScreenParams.x;  
  54.             ray.z = 1.0;  
  55.   
  56.             float offset =  _Time.y*.5;   
  57.             float speed2 = (cos(offset)+1.0)*8.0;  
  58.             float speed = speed2+.1;  
  59.             offset += sin(offset)*.96;  
  60.             offset *= 2.0;  
  61.   
  62.             float3 col = float3(0);  
  63.             float3 stp = ray/max(abs(ray.x),abs(ray.y));  
  64.   
  65.             float3 pos = 2.0*stp+.5;  
  66.             float3 c = float3(0);  
  67.             for ( int i=0; i < 20; i++ ) {  
  68.                 float z = Noise(float2(pos.xy)).x;  
  69.                 z = frac(z-offset);  
  70.                 float d = 50.0*z-pos.z;  
  71.                 float w = pow(max(0.0,1.0-10.0*length(frac(pos.xy)-.5)),2.0);  
  72.                 float3 c = max(float3(0),float3(1.0-abs(d+speed2*.5)/speed,1.0-abs(d)/speed,1.0-abs(d-speed2*.5)/speed));  
  73.                 col += 1.5*(1.0-z)*c*w;  
  74.                 pos += stp;  
  75.             }  
  76.   
  77.             return  float4(ToGamma(col),1.0);  
  78.         }      
  79.     ENDCG        
  80.       
  81.     SubShader {       
  82.         Tags {"Queue" = "Transparent"}         
  83.         ZWrite Off         
  84.         Blend SrcAlpha OneMinusSrcAlpha         
  85.         Pass {        
  86.             CGPROGRAM        
  87.             #pragma vertex vert        
  88.             #pragma fragment frag        
  89.             #pragma fragmentoption ARB_precision_hint_fastest         
  90.       
  91.             ENDCG        
  92.         }    
  93.     }   
  94. }  



[cpp]  view plain  copy
  1. Shader "stalendp/shaderTest02" { //see https://www.shadertoy.com/view/4sj3zy  
  2.     Properties {  
  3.         _MainTex ("Base (RGB)", 2D) = "white" {}  
  4.     }  
  5.     SubShader {  
  6.         Pass {  
  7.             CGPROGRAM  
  8.             #pragma vertex vert  
  9.             #pragma fragment frag  
  10.             #pragma target 3.0  
  11.               
  12.             #include "UnityCG.cginc"  
  13.   
  14.             sampler2D _MainTex;  
  15.           
  16.             //Variable declarations  
  17.               
  18.             struct myvars {  
  19.                 float3 bgColor;  
  20.                 float sphereScale;  
  21.                 float sphereShine;  
  22.                 float3 sphereDiff;  
  23.                 float3 sphereSpec;  
  24.                 float2 specPoint;  
  25.             };  
  26.   
  27.             float4 vert(appdata_base v) : POSITION {  
  28.                 return mul(UNITY_MATRIX_MVP, v.vertex);  
  29.             }  
  30.               
  31.             float4 frag(float4 sp:WPOS): COLOR {  
  32.                 myvars mv;  
  33.                 mv.bgColor = float3(0.6, 0.5, 0.6);  
  34.                 mv.sphereScale = 0.7;  
  35.                 mv.sphereShine = 0.5;  
  36.                 mv.sphereDiff = float3(0.5, 0.0, 0.5);  
  37.                 mv.sphereSpec = float3(1.0, 1.0, 1.0);  
  38.                 mv.specPoint = float2(0.2, -0.1);  
  39.               
  40.                 // creates shader pixel coordinates  
  41.                 float2 uv = sp.xy/_ScreenParams.xy;  
  42.                 // sets the position of the camera  
  43.                 float2 p = uv * 2.5 - float2(1.0, 1.0);  
  44.                 p.x *= _ScreenParams.x / _ScreenParams.y;  
  45.                   
  46.                 // Rotates the sphere in a circle  
  47.                 p.x += cos(-_Time.y) *0.35;  
  48.                 p.y += sin(-_Time.y) * 0.35;  
  49.                   
  50.                 // Rotates the specular point with the sphere  
  51.                 mv.specPoint.x += cos(-_Time.y) * 0.35;  
  52.                 mv.specPoint.y += sin(-_Time.y) * 0.35;  
  53.                   
  54.                 //Sets the radius of the sphere to the middle of the screen  
  55.                 float radius = length(p);//sqrt(dot(p, p));  
  56.       
  57.                 float3 col = mv.bgColor;  
  58.       
  59.                 //Sets the initial dark shadow around the edge of the sphere  
  60.                 float f = smoothstep(mv.sphereScale * 0.7, mv.sphereScale, length(p + mv.specPoint));  
  61.                 col -= lerp(col, float3(0.0,0.0,0.0), f) * 0.2;  
  62.                   
  63.                 //Only carries out the logic if the radius of the sphere is less than the scale  
  64.                 if(radius < mv.sphereScale) {  
  65.                     float3 bg = col;  
  66.                       
  67.                     //Sets the diffuse colour of the sphere (solid colour)  
  68.                     col = mv.sphereDiff;  
  69.                       
  70.                     //Adds smooth dark borders to help achieve 3D look  
  71.                     f = smoothstep(mv.sphereScale * 0.7, mv.sphereScale, radius);  
  72.                     col = lerp(col, mv.sphereDiff * 0.45, f);  
  73.                       
  74.                     //Adds specular glow to help achive 3D look  
  75.                     f = 1.0 - smoothstep(-0.2, 0.6, length(p - mv.specPoint));  
  76.                     col += f * mv.sphereShine * mv.sphereSpec;  
  77.                   
  78.                     //Smoothes the edge of the sphere  
  79.                     f = smoothstep(mv.sphereScale - 0.01, mv.sphereScale, radius);  
  80.                     col = lerp(col, bg, f);  
  81.                 }  
  82.                   
  83.                   
  84.                 //The final output of the shader logic above  
  85.                 //gl_FragColor is a vector with 4 paramaters(red, green, blue, alpha)  
  86.                 //Only 2 need to be used here, as "col" is a vector that already carries r, g, and b values  
  87.                 return float4(col, 1);  
  88.             }  
  89.               
  90.             ENDCG  
  91.         }  
  92.     }   
  93.     FallBack "Diffuse"  
  94. }  


[cpp]  view plain  copy
  1. Shader "Custom/shaderTest03" {  // https://www.shadertoy.com/view/Xdf3DS  
  2.     Properties {  
  3.         _MainTex ("Base (RGB)", 2D) = "white" {}  
  4.     }  
  5.     SubShader {  
  6.       
  7.         Pass {  
  8.             CGPROGRAM  
  9.             #pragma vertex vert  
  10.             #pragma fragment frag  
  11.             #pragma target 3.0  
  12.               
  13.             #include "UnityCG.cginc"  
  14.   
  15.             sampler2D _MainTex;  
  16.   
  17.               
  18.             struct myvars {  
  19.                 float k;  
  20.                 float f;  
  21.                 float threshold;  
  22.   
  23.                 float3 colour;  
  24.                 float3 normal;  
  25.   
  26.                 float3 lightPos;  
  27.                 float3 lightColour;  
  28.                 float3 ambient;  
  29.                 float shinyness;  
  30.                 float diffuseFactor;  
  31.                 float4 fragCoord;  
  32.             };  
  33.               
  34.   
  35.             float2 center ( float2 border , float2 _offset , float2 vel, myvars mv) {  
  36.                 float2 c = _offset + vel * _Time * 0.5;  
  37.                 c = fmod ( c , 2. - 4. * border );  
  38.                 if ( c.x > 1. - border.x ) c.x = 2. - c.x - 2. * border.x;  
  39.                 if ( c.x < border.x ) c.x = 2. * border.x - c.x;  
  40.                 if ( c.y > 1. - border.y ) c.y = 2. - c.y - 2. * border.y;  
  41.                 if ( c.y < border.y ) c.y = 2. * border.y - c.y;  
  42.                 return c;  
  43.             }  
  44.               
  45.             float field ( float b, float r , myvars mv) {  
  46.                 if ( r > b )  
  47.                     return 0.0;  
  48.                 if ( r >= b/3.0 ) {  
  49.                     float rb = 1.0 - r/b;  
  50.                     return (3.0*mv.k)/2.0 * rb * rb;  
  51.                 }  
  52.                 if ( r >= 0.0 && r <= b/3.0 ) {  
  53.                     return mv.k * ( 1.0 - ( (3.0*r*r)/(b*b) ) );      
  54.                 }  
  55.                 return 0.0;  
  56.             }  
  57.               
  58.             void circle ( float r , float2 col , float2 _offset , float2 vel, myvars mv ) {  
  59.                 float2 pos = mv.fragCoord.xy / _ScreenParams.y;  
  60.                 float aspect = _ScreenParams.x / _ScreenParams.y;  
  61.                 float2 c = center ( float2 ( r / aspect , r ) , _offset , vel, mv);  
  62.                 c.x *= aspect;  
  63.                 float d = distance ( pos , c );  
  64.                 float thisField =  field (r, d, mv);  
  65.                 mv.f += thisField;  
  66.                 mv.colour += float3(col, 0) * thisField;  
  67.                 mv.normal += normalize(float3(pos.x-c.x, pos.y-c.y,r))*thisField;  
  68.             }  
  69.                           
  70.   
  71.             float4 vert(appdata_base v) : POSITION {  
  72.                 return mul(UNITY_MATRIX_MVP, v.vertex);  
  73.             }  
  74.               
  75.             float4 frag(float4 sp:WPOS): COLOR {  
  76.                 myvars mv;  
  77.                 mv.fragCoord = sp;  
  78.                 mv.k = 100.0;  
  79.                 mv.f = 0.0;  
  80.                 mv.threshold = 10.0;  
  81.   
  82.                 mv.colour = float3(0.0,0.0,0.0);  
  83.                 mv.normal = float3(0.0,0.0,0.0);  
  84.   
  85.                 mv.lightPos = float3(_ScreenParams.xy,2000.0);  
  86.                 mv.lightColour = float3(0.9,0.9,1.0);  
  87.                 mv.ambient = float3(0.1,0.0,0.0);  
  88.                 mv.shinyness = 20.0;  
  89.                 mv.diffuseFactor = 0.0006;  
  90.               
  91.                 circle ( .10 , float3 ( 0.7 , 0.2 , 0.8 ) , float2 ( .6 ) , float2 ( .30 , .70 ), mv );  
  92.                 circle ( .09 , float3 ( 0.7 , 0.9 , 0.6 ) , float2 ( .1 ) , float2 ( .02 , .20 ), mv );  
  93.                 circle ( .12 , float3 ( 0.3 , 0.4 , 0.1 ) , float2 ( .1 ) , float2 ( .10 , .04 ), mv );  
  94.                 circle ( .15 , float3 ( 0.2 , 0.5 , 0.1 ) , float2 ( .3 ) , float2 ( .10 , .20 ), mv );  
  95.                 circle ( .20 , float3 ( 0.1 , 0.3 , 0.7 ) , float2 ( .2 ) , float2 ( .40 , .25 ), mv );  
  96.                 circle ( .30 , float3 ( 0.9 , 0.4 , 0.2 ) , float2 ( .0 ) , float2 ( .15 , .20 ), mv );  
  97.                   
  98.                 float3 c;  
  99.                   
  100.                 if (mv.f < mv.threshold)  
  101.                     c = float3(0.0,0.0,0.0);  
  102.                 else {  
  103.                     mv.colour /= mv.f;  
  104.                     mv.normal = mv.normal/mv.f;  
  105.                       
  106.                     c = mv.ambient;  
  107.                     float3 lightDir = mv.lightPos - float3(sp.xy,0.0);  
  108.                     c += mv.colour * mv.diffuseFactor * max(dot(mv.normal,lightDir), 0.0);  
  109.                     float3 r = normalize ( reflect ( lightDir, mv.normal ) );  
  110.                     c += mv.lightColour * pow(max(dot(r,float3(0.0,0.0,-1.0)), 0.0), mv.shinyness);   
  111.                 }  
  112.                 return float4(c, 1);  
  113.             }  
  114.               
  115.             ENDCG  
  116.         }  
  117.     }   
  118. }  

[cpp]  view plain  copy
  1. Shader "stalendp/shaderTest04" { //see https://www.shadertoy.com/view/Xsf3R8  
  2.     Properties {  
  3.         _MainTex ("Base (RGB)", 2D) = "white" {}  
  4.     }  
  5.     SubShader {  
  6.         Pass {  
  7.             CGPROGRAM  
  8.             #pragma vertex vert  
  9.             #pragma fragment frag  
  10.             #pragma target 3.0  
  11.               
  12.             #include "UnityCG.cginc"  
  13.   
  14.             sampler2D _MainTex;  
  15.               
  16.             struct Ray {  
  17.                 float3 org;  
  18.                 float3 dir;  
  19.             };  
  20.               
  21.             float rayPlaneIntersect( Ray ray, float4 plane ) {  
  22.                 float f = dot( ray.dir, plane.xyz );  
  23.                   
  24.                 float t = -( dot( ray.org, plane.xyz ) + plane.w );  
  25.                 t /= f;  
  26.                   
  27.                 return t;  
  28.             }  
  29.               
  30.             float3 shade( float3 pos, float3 nrm, float4 light ) {  
  31.                 float3 toLight = light.xyz - pos;  
  32.                 float toLightLen = length( toLight );  
  33.                 toLight = normalize( toLight );  
  34.                       
  35.                 float diff = dot( nrm, toLight );  
  36.                 float attn = 1.0 - pow( min( 1.0, toLightLen / light.w ), 2.0 );  
  37.                 float comb = 2.0 * diff * attn;  
  38.                   
  39.                 return float3( comb, comb, comb );  
  40.             }  
  41.   
  42.   
  43.             float4 vert(appdata_base v) : POSITION {  
  44.                 return mul(UNITY_MATRIX_MVP, v.vertex);  
  45.             }  
  46.               
  47.             float4 frag(float4 sp:WPOS): COLOR {  
  48.               
  49.                 // gl_FragCoord: location (0.5, 0.5) is returned   
  50.                 // for the lower-left-most pixel in a window  
  51.                   
  52.                 // XY of the normalized device coordinate  
  53.                 // ranged from [-1, 1]  
  54.                 float2 ndcXY = -1.0 + 2.0 * sp.xy / _ScreenParams.xy;  
  55.                   
  56.                 // aspect ratio  
  57.                 float aspectRatio = _ScreenParams.x / _ScreenParams.y;  
  58.                   
  59.                 // scaled XY which fits the aspect ratio  
  60.                 float2 scaledXY = ndcXY * float2( aspectRatio, 1.0 );  
  61.                   
  62.                 // camera XYZ in world space  
  63.                 float3 camWsXYZ = float3( 0.0, 1.0, 0.0 );  
  64.                 camWsXYZ.z += 10.0 * cos( _Time.y );  
  65.                   
  66.                 // construct the ray in world space  
  67.                 Ray ray;  
  68.                 ray.org = camWsXYZ;  
  69.                 ray.dir = float3( scaledXY, -2.0 ); // OpenGL is right handed  
  70.                   
  71.                 // define the plane in world space  
  72.                 float4 plane = float4( 0.0, 1.0, 0.0, 0.0 );  
  73.                   
  74.                 float t = rayPlaneIntersect( ray, plane );  
  75.                   
  76.                 // define the point light in world space (XYZ, range)  
  77.                 float4 lightWs = float4( 0.0, 5.0, -5.0, 10.0 );  
  78.                   
  79.                 if ( t >= 0.0 )  
  80.                 {  
  81.                     float3 sceneWsPos = ray.org + t * ray.dir;  
  82.                     float3 sceneWsNrm = plane.xyz;  
  83.                     float2 sceneUV = sceneWsPos.xz / 4.0;  
  84.                       
  85.                     float4 sceneBase = tex2D( _MainTex, sceneUV );        
  86.                     float3 sceneShade = shade( sceneWsPos, sceneWsNrm, lightWs );  
  87.                       
  88.                     return float4( sceneShade * sceneBase.xyz, 1.0 );  
  89.                 }  
  90.               
  91.                 return float4( 0.0, 0.0, 0.0, 1.0 );  
  92.             }  
  93.               
  94.             ENDCG  
  95.         }  
  96.     }   
  97.     FallBack "Diffuse"  
  98. }  


[cpp]  view plain  copy
  1. Shader "stalendp/shaderTest04" { //see https://www.shadertoy.com/view/MdB3Dw  
  2.     Properties {  
  3.         _MainTex ("Base (RGB)", 2D) = "white" {}  
  4.     }  
  5.     SubShader {  
  6.         Pass {  
  7.             CGPROGRAM  
  8.             #pragma vertex vert  
  9.             #pragma fragment frag  
  10.             #pragma target 3.0  
  11.               
  12.             #include "UnityCG.cginc"  
  13.               
  14.             #define USE_ANALYTICAL_MBLUR  
  15.   
  16.             sampler2D _MainTex;  
  17.       
  18.             // intersect a MOVING sphere  
  19.             float2 iSphere( in float3 ro, in float3 rd, in float4 sp, in float3 ve, out float3 nor )  
  20.             {  
  21.                 float t = -1.0;  
  22.                 float s = 0.0;  
  23.                 nor = float3(0.0);  
  24.                   
  25.                 float3  rc = ro - sp.xyz;  
  26.                 float A = dot(rc,rd);  
  27.                 float B = dot(rc,rc) - sp.w*sp.w;  
  28.                 float C = dot(ve,ve);  
  29.                 float D = dot(rc,ve);  
  30.                 float E = dot(rd,ve);  
  31.                 float aab = A*A - B;  
  32.                 float eec = E*E - C;  
  33.                 float aed = A*E - D;  
  34.                 float k = aed*aed - eec*aab;  
  35.                       
  36.                 if( k>0.0 )  
  37.                 {  
  38.                     k = sqrt(k);  
  39.                     float hb = (aed - k)/eec;  
  40.                     float ha = (aed + k)/eec;  
  41.                       
  42.                     float ta = max( 0.0, ha );  
  43.                     float tb = min( 1.0, hb );  
  44.                       
  45.                     if( ta < tb )  
  46.                     {  
  47.                         ta = 0.5*(ta+tb);             
  48.                         t = -(A-E*ta) - sqrt( (A-E*ta)*(A-E*ta) - (B+C*ta*ta-2.0*D*ta) );  
  49.                         nor = normalize( (ro+rd*t) - (sp.xyz+ta*ve ) );  
  50.                         s = 2.0*(tb - ta);  
  51.                     }  
  52.                 }  
  53.   
  54.                 return float2(t,s);  
  55.             }  
  56.   
  57.             // intersect a STATIC sphere  
  58.             float iSphere( in float3 ro, in float3 rd, in float4 sp, out float3 nor )  
  59.             {  
  60.                 float t = -1.0;  
  61.                 nor = float3(0.0);  
  62.                   
  63.                 float3  rc = ro - sp.xyz;  
  64.                 float b =  dot(rc,rd);  
  65.                 float c =  dot(rc,rc) - sp.w*sp.w;  
  66.                 float k = b*b - c;  
  67.                 if( k>0.0 )  
  68.                 {  
  69.                     t = -b - sqrt(k);  
  70.                     nor = normalize( (ro+rd*t) - sp.xyz );  
  71.                 }  
  72.   
  73.                 return t;  
  74.             }  
  75.   
  76.             float3 getPosition( float time ) { return float3(     2.5*sin(8.0*time), 0.0,      1.0*cos(8.0*time) ); }  
  77.             float3 getVelocity( float time ) { return float3( 8.0*2.5*cos(8.0*time), 0.0, -8.0*1.0*sin(8.0*time) ); }  
  78.   
  79.   
  80.             float4 vert(appdata_base v) : POSITION {  
  81.                 return mul(UNITY_MATRIX_MVP, v.vertex);  
  82.             }  
  83.               
  84.             float4 frag(float4 sp:WPOS): COLOR {  
  85.                 float2 q = sp.xy / _ScreenParams.xy;  
  86.                 float2 p = -1.0 + 2.0*q;  
  87.                 p.x *= _ScreenParams.x/_ScreenParams.y;   
  88.   
  89.                 // camera  
  90.                 float3  ro = float3(0.0,0.0,4.0);  
  91.                 float3  rd = normalize( float3(p.xy,-2.0) );  
  92.                   
  93.                 // sphere     
  94.                   
  95.                 // render  
  96.                 float3  col = float3(0.0);  
  97.                   
  98.                 #ifdef USE_ANALYTICAL_MBLUR  
  99.                               
  100.                 //---------------------------------------------------     
  101.                 // render with analytical motion blur  
  102.                 //---------------------------------------------------     
  103.                 float3  ce = getPosition( _Time.y );  
  104.                 float3  ve = getVelocity( _Time.y );  
  105.                       
  106.                 col = float3(0.25) + 0.3*rd.y;  
  107.                 float3 nor = float3(0.0);  
  108.                 float3 tot = float3(0.25) + 0.3*rd.y;  
  109.                 float2 res = iSphere( ro, rd, float4(ce,1.0), ve/24.0, nor );  
  110.                 float t = res.x;  
  111.                 if( t>0.0 )  
  112.                 {  
  113.                     float dif = clamp( dot(nor,float3(0.5703)), 0.0, 1.0 );  
  114.                     float amb = 0.5 + 0.5*nor.y;  
  115.                     float3  lcol = dif*float3(1.0,0.9,0.3) + amb*float3(0.1,0.2,0.3);  
  116.                     col = lerp( tot, lcol, res.y );  
  117.                 }  
  118.                   
  119.                 #else  
  120.                   
  121.                 //---------------------------------------------------     
  122.                 // render with brute force sampled motion blur  
  123.                 //---------------------------------------------------     
  124.                   
  125.                 #define NUMSAMPLES 32  
  126.                 float3 tot = float3(0.0);  
  127.                 forint i=0; i<NUMSAMPLES; i++ )  
  128.                 {  
  129.                     float fi = float(i)/float(NUMSAMPLES);  
  130.                     float3  ce = getPosition( _Time.y + fi/24.0 );  
  131.                     float3 nor = float3(0.0);  
  132.                     float3 tmp = float3(0.25) + 0.3*rd.y;  
  133.                     float t = iSphere( ro, rd, float4(ce,1.0), nor );  
  134.                     if( t>0.0 )  
  135.                     {  
  136.                         float dif = clamp( dot(nor,float3(0.5703)), 0.0, 1.0 );  
  137.                         float amb = 0.5 + 0.5*nor.y;  
  138.                         tmp = dif*float3(1.0,0.9,0.3) + amb*float3(0.1,0.2,0.3);  
  139.                     }  
  140.                     col += tmp;  
  141.                 }         
  142.                 col /= float(NUMSAMPLES);  
  143.                       
  144.                 #endif  
  145.                   
  146.                 col = pow( clamp(col,0.0,1.0), float3(0.45) );  
  147.   
  148.                 return float4( col, 1.0 );  
  149.             }  
  150.               
  151.             ENDCG  
  152.         }  
  153.     }   
  154.     FallBack "Diffuse"  
  155. }  

[cpp]  view plain  copy
  1. Shader "stalendp/shaderTest05" { //see https://www.shadertoy.com/view/XsB3DW  
  2.     Properties {  
  3.         _MainTex ("Base (RGB)", 2D) = "white" {}  
  4.         _CubeDiffuse ("Cubemap Diffuse Map", CUBE) = "" {}  
  5.         vv1("vv1"float) = -1.0  
  6.         vv2("vv2"float) = 2.0  
  7.     }  
  8.     SubShader {  
  9.         Pass {  
  10.             CGPROGRAM  
  11.             #pragma vertex vert  
  12.             #pragma fragment frag  
  13.             #pragma target 3.0  
  14.             //下面防止编译错误:instruction limit of 1024 exceed;  
  15.             #pragma glsl    
  16.               
  17.             #include "UnityCG.cginc"   
  18.               
  19.             #define MAX_STEPS 64  
  20.             #define MAX_REFLECTIONS 4  
  21.             #define PI 3.1415926536  
  22.   
  23.             sampler2D _MainTex;  
  24.             samplerCUBE _CubeDiffuse;  
  25.             float vv1, vv2;  
  26.       
  27.             struct Ray {  
  28.                 float3 o;  
  29.                 float3 d;  
  30.             };  
  31.             struct Sphere {  
  32.                 float3 o;  
  33.                 float r;  
  34.             };  
  35.             struct Box {  
  36.                 float3 o;  
  37.                 float3 s;  
  38.             };  
  39.             struct Torus {  
  40.                 float3 o;  
  41.                 float2 s;  
  42.             };  
  43.                           
  44.             float2 rotate2d(in float2 v, in float a) {  
  45.                 float sinA = sin(a);  
  46.                 float cosA = cos(a);  
  47.                 return float2(v.x * cosA - v.y * sinA, v.y * cosA + v.x * sinA);      
  48.             }  
  49.   
  50.             float sdSphere(in float3 p, in Sphere s) {  
  51.                 return length(p-s.o)-s.r;  
  52.             }  
  53.             float sdBox(in float3 p, in Box b) {  
  54.                 float3 d = abs(p-b.o) - b.s;  
  55.                 return min(max(d.x,max(d.y,d.z)),0.0) +  
  56.                     length(max(d,0.0));  
  57.             }  
  58.             float sdTorus(in float3 p, in Torus t) {  
  59.                 p -= t.o;  
  60.                 float2 q = float2(length(p.xz)-t.s.x,p.y);  
  61.                 return length(q)-t.s.y;  
  62.             }  
  63.             float world(in float3 p) {  
  64.                 float ti = fmod(_Time.y,10.);  
  65.                 if(ti > 2.) {  
  66.                     Sphere s0 = Sphere(float3(0),1.);  
  67.                     Box b0 = Box(float3(0),float3(.8));  
  68.                     if(ti < 4.) {  
  69.                         return max(-sdSphere(p,s0),sdBox(p,b0));  
  70.                     } else if(ti < 6.) {  
  71.                         return min(sdSphere(p,s0),sdBox(p,b0));  
  72.                     } else if(ti < 8.) {  
  73.                         return max(sdSphere(p,s0),sdBox(p,b0));  
  74.                     } else {  
  75.                         return max(sdSphere(p,s0),-sdBox(p,b0));  
  76.                     }  
  77.                 } else {  
  78.                     float3 pr = p.xzy;  
  79.                     return sdTorus(pr, Torus(float3(0),float2(1.,.5)));  
  80.                 }  
  81.             }  
  82.               
  83.             float3 getNormal(in float3 p) {  
  84.                 float3 d = float3(.005,0,0);  
  85.                 float3 n;  
  86.                 n.x = world(p+d.xyy);  
  87.                 n.y = world(p+d.yxy);  
  88.                 n.z = world(p+d.yyx);  
  89.                 return normalize(n);  
  90.             }  
  91.   
  92.             bool march(in Ray r, out float3 p) {  
  93.                 p = r.o;  
  94.                 float d;  
  95.                 for(int i = 0; i < MAX_STEPS; i++) {  
  96.                     d = world(p);  
  97.                     p += r.d*d;  
  98.                 }  
  99.                 return d<=0.01;  
  100.             }  
  101.   
  102.             float3 colorMarch(in Ray r) {  
  103.                 float3 p;  
  104.                 float3 col = float3(0);  
  105.                 for(int i = 0; i < MAX_REFLECTIONS; i++) {  
  106.                     if(march(r,p)) {  
  107.                         float3 ldir = normalize(float3(1,-1,.5));  
  108.                         float3 n = getNormal(p);  
  109.                         col += float3(dot(n,-ldir))*.25;  
  110.                         r = Ray(p,reflect(r.d,n));  
  111.                         r.o += r.d*0.2;  
  112.                     } else {  
  113.                         break;  
  114.                     }  
  115.                 }  
  116.                 col += texCUBE(_CubeDiffuse, r.d).rgb;  
  117.                 return col;  
  118.             }  
  119.   
  120.             float4 vert(appdata_base v) : POSITION {  
  121.                 return mul(UNITY_MATRIX_MVP, v.vertex);  
  122.             }  
  123.               
  124.             float4 frag(float4 sp:WPOS): COLOR {  
  125.                 float2 uv = 2.*sp.xy/_ScreenParams.xy-1.;  
  126.                 uv.x *= _ScreenParams.x/_ScreenParams.y;  
  127.                   
  128.                 Ray r = Ray(float3(0,0,-2),normalize(float3(uv,1)));  
  129.                 r.o.xz = rotate2d(r.o.xz,_Time.y*.5);  
  130.                 r.d.xz = rotate2d(r.d.xz,_Time.y*.5);  
  131.                 float3 cc =colorMarch(r);  
  132.   
  133.                 return float4( cc, 1.0 );  
  134.             }  
  135.               
  136.             ENDCG  
  137.         }  
  138.     }   
  139.     FallBack "Diffuse"  
  140. }  

[cpp]  view plain  copy
  1. Shader "stalendp/myShaderWang" {   
  2.     Properties {  
  3.         _MainTex ("Base (RGB)", 2D) = "white" {}  
  4.         wangTex("wang texture", 2D) = "white" {}  
  5.         disHeight ("texture height"float) = 0.0  
  6.     }  
  7.     SubShader {  
  8.         Tags {"Queue" = "Transparent"}  
  9.           
  10.         Pass{  
  11.             Cull Off  
  12.             Cull Back // now render the front faces ZWrite Off // don't write to depth buffer  
  13.             // in order not to occlude other objects  
  14.             Blend SrcAlpha OneMinusSrcAlpha  
  15.             // blend based on the fragment's alpha value  
  16.         
  17.             CGPROGRAM  
  18.   
  19.             #pragma vertex vert  
  20.             #pragma fragment frag  
  21.       
  22.             uniform sampler2D _MainTex;  
  23.             uniform sampler2D wangTex;  
  24.             uniform float disHeight;  
  25.               
  26.             struct vertexInput {  
  27.                 float4 vertex : POSITION;  
  28.                 float4 texcoord : TEXCOORD0;  
  29.             };  
  30.             struct vertexOutput {  
  31.                 float4 pos : SV_POSITION;  
  32.                 float4 position_in_world_space : TEXCOORD1;  
  33.                 float4 tex : TEXCOORD0;  
  34.                 float4 sp:WPOS;  
  35.             };  
  36.           
  37.             vertexOutput vert(vertexInput input) {  
  38.                 vertexOutput output;  
  39.                 output.pos = mul(UNITY_MATRIX_MVP, input.vertex);  
  40.                 output.position_in_world_space = mul(_Object2World, input.vertex);  
  41.                 output.tex = input.texcoord;  
  42.                   
  43.                 return output;  
  44.             }  
  45.           
  46.             float4 frag(vertexOutput input) : COLOR {  
  47.                 //https://www.shadertoy.com/view/4dsGzH  
  48.                 float3 COLOR1 = float3(0.0, 0.0, 0.3);  
  49.                 float3 COLOR2 = float3(0.5, 0.0, 0.0);  
  50.                 float BLOCK_WIDTH = 0.01;  
  51.                 float2 uv = float2(input.tex);  
  52.                   
  53.                 // To create the BG pattern  
  54.                 float3 final_color = float3(1.0);  
  55.                 float3 bg_color = float3(0.0);  
  56.                 float3 wave_color = float3(0.0);  
  57.                 float c1 = fmod(uv.x, 2.0 * BLOCK_WIDTH);  
  58.                 c1 = step(BLOCK_WIDTH, c1);  
  59.                 float c2 = fmod(uv.y, 2.0 * BLOCK_WIDTH);  
  60.                 c2 = step(BLOCK_WIDTH, c2);  
  61.                 bg_color = lerp(uv.x * COLOR1, uv.y * COLOR2, c1 * c2);  
  62.                   
  63.                 // To create the waves  
  64.                 float wave_width = 0.01;  
  65.                 uv  = -1.0 + 2.0 * uv;  
  66.                 uv.y += 0.1;  
  67.                 for(float i = 0.0; i < 1.0; i++) {  
  68.                     uv.y += (0.07 * sin(uv.x + i/7.0 +  _Time.y ));  
  69.                     wave_width = abs(1.0 / (150.0 * uv.y));  
  70.                     wave_color += float3(wave_width * 1.9, wave_width, wave_width * 1.5);  
  71.                 }  
  72.                 final_color = bg_color + wave_color;  
  73.                   
  74.                 float pos = input.position_in_world_space.y;  
  75.                 float p1 = saturate((pos - disHeight)*3);    
  76.                 float p2 = saturate((pos - disHeight - 0.5)*3);    
  77.                   
  78.                 float4 non = float4(0.0);  
  79.                 float4 skin = tex2D(_MainTex, float2(input.tex));    
  80.                 float4 wang = tex2D(wangTex, float2(input.tex));    
  81.                 float4 final =  lerp(lerp(skin, wang, p1), non, p2);  
  82.                 return final + float4(final_color, 1.0);    
  83.             }  
  84.             ENDCG  
  85.         }  
  86.           
  87.     }   
  88.     FallBack "Diffuse"  
  89. }  

CGINCLUDE的使用

[cpp]  view plain  copy
  1. Shader "Self-Illumin/AngryBots/InterlacePatternAdditive" {  
  2.     Properties {  
  3.         _MainTex ("Base", 2D) = "white" {}  
  4.         _TintColor ("TintColor", Color) = (1,1,1,1) // needed simply for shader replacement     
  5.         _InterlacePattern ("InterlacePattern", 2D) = "white" {}  
  6.         _Illum ("_Illum", 2D) = "white" {}  
  7.         _EmissionLM ("Emission (Lightmapper)", Float) = 1.0   
  8.     }  
  9.       
  10.     CGINCLUDE  
  11.   
  12.         #include "UnityCG.cginc"  
  13.   
  14.         sampler2D _MainTex;  
  15.         sampler2D _InterlacePattern;  
  16.                           
  17.         half4 _InterlacePattern_ST;  
  18.         fixed4 _TintColor;                
  19.                           
  20.         struct v2f {  
  21.             half4 pos : SV_POSITION;  
  22.             half2 uv : TEXCOORD0;  
  23.             half2 uv2 : TEXCOORD1;  
  24.         };  
  25.   
  26.         v2f vert(appdata_full v)  
  27.         {  
  28.             v2f o;  
  29.               
  30.             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);     
  31.             o.uv.xy = v.texcoord.xy;  
  32.             o.uv2.xy = TRANSFORM_TEX(v.texcoord.xy, _InterlacePattern) + _Time.xx * _InterlacePattern_ST.zw;  
  33.                       
  34.             return o;   
  35.         }  
  36.           
  37.         fixed4 frag( v2f i ) : COLOR  
  38.         {     
  39.             fixed4 colorTex = tex2D (_MainTex, i.uv);  
  40.             fixed4 interlace = tex2D (_InterlacePattern, i.uv2);  
  41.             colorTex *= interlace;  
  42.               
  43.             return colorTex;  
  44.         }  
  45.       
  46.     ENDCG  
  47.       
  48.     SubShader {  
  49.         Tags {"RenderType" = "Transparent" "Queue" = "Transparent" "Reflection" = "RenderReflectionTransparentAdd" }  
  50.         Cull Off  
  51.         ZWrite Off  
  52.         Blend One One  
  53.           
  54.     Pass {  
  55.       
  56.         CGPROGRAM  
  57.           
  58.         #pragma vertex vert  
  59.         #pragma fragment frag  
  60.         #pragma fragmentoption ARB_precision_hint_fastest   
  61.           
  62.         ENDCG  
  63.            
  64.         }  
  65.                   
  66.     }   
  67.     FallBack Off  
  68. }  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值