Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/CalculateNormals"
  2. {
  3.     Properties
  4.     {
  5.         _Color ("Color", Color) = (1,1,1,1)
  6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
  7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
  8.         _Metallic ("Metallic", Range(0,1)) = 0.0
  9.        
  10.         _PhaseOffset("Phase Offset", Float) = .1
  11.         _Speed("Speed", Float) = 1.5
  12.         _Depth("Depth", Float) = .14
  13.         _Smoothing("Smoothing", Range(.01, .9)) = .5
  14.         _XDrift("XDrift", Float) = .1
  15.         _ZDrift("ZDrift", Float) = .1
  16.         _Scale("Scale", Float) = 5
  17.     }
  18.     SubShader
  19.     {
  20.         Tags { "RenderType"="Opaque" }
  21.         LOD 200
  22.  
  23.         CGPROGRAM
  24.         // Physically based Standard lighting model, and enable shadows on all light types
  25.         #pragma surface surf Standard fullforwardshadows vertex:vert
  26.  
  27.         // Use shader model 3.0 target, to get nicer looking lighting
  28.         #pragma target 3.0
  29.  
  30.         sampler2D _MainTex;
  31.  
  32.         struct Input
  33.         {
  34.             float2 uv_MainTex;
  35.         };
  36.  
  37.         half _Glossiness;
  38.         half _Metallic;
  39.         fixed4 _Color;
  40.         float _PhaseOffset;
  41.         float _Speed;
  42.         float _Depth;
  43.         float _Smoothing;
  44.         float _XDrift;
  45.         float _ZDrift;
  46.         float _Scale;
  47.  
  48.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
  49.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
  50.         // #pragma instancing_options assumeuniformscaling
  51.         UNITY_INSTANCING_BUFFER_START(Props)
  52.             // put more per-instance properties here
  53.         UNITY_INSTANCING_BUFFER_END(Props)
  54.  
  55.         void vert( inout appdata_full v)
  56.         {
  57.             // Note that, to start off, all work is in object (local) space. // We will eventually move normals to world space to handle arbitrary object orientation. // There is no real need for tangent space in this case.
  58.             // Do all work in world space
  59.             float3 v0 = mul( unity_ObjectToWorld, v.vertex ).xyz;
  60.             // Create two fake neighbor vertices. // The important thing is that they be distorted in the same way that a real vertex in their location would.
  61.             // This is pretty easy since we're just going to do some trig based on position, so really any samples will do.
  62.             float3 v1 = v0 + float3( 0.05, 0, 0 ); //+X
  63.             float3 v2 = v0 + float3( 0, 0, 0.05 ); //+Z
  64.  
  65.             // Some animation values
  66.             float phase  = _PhaseOffset * (3.14 * 2);
  67.             float phase2 = _PhaseOffset * (3.14 * 1.123);
  68.             float speed  = _Time.y * _Speed;
  69.             float speed2 = _Time.y * (_Speed * 0.33 );
  70.             float _Depth2 = _Depth * 1.0;
  71.             float v0alt = v0.x * _XDrift + v0.z * _ZDrift;
  72.             float v1alt = v1.x * _XDrift + v1.z * _ZDrift;
  73.             float v2alt = v2.x * _XDrift + v2.z * _ZDrift;
  74.  
  75.             // Modify the real vertex and two theoretical samples by the distortion algorithm (here a simple sine wave on Y, driven by local X pos)
  76.             v0.y += sin( phase  + speed  + ( v0.x  * _Scale  ) ) * _Depth;
  77.             v0.y += sin( phase2 + speed2 + ( v0alt * _Scale  ) ) * _Depth2; // This is just another wave being applied for a bit more complexity.
  78.            
  79.             v1.y += sin( phase  + speed  + ( v1.x  * _Scale  ) ) * _Depth;
  80.             v1.y += sin( phase2 + speed2 + ( v1alt * _Scale  ) ) * _Depth2;
  81.            
  82.             v2.y += sin( phase  + speed  + ( v2.x  * _Scale  ) ) * _Depth;
  83.             v2.y += sin( phase2 + speed2 + ( v2alt * _Scale  ) ) * _Depth2;
  84.              
  85.             // By reducing the delta on Y, we effectively restrict the amout of variation the normals will exhibit.
  86.             // This appears like a smoothing effect, separate from the actual displacement depth.
  87.             // It's basically undoing the change to the normals, leaving them straight on Y.
  88.             v1.y -= (v1.y - v0.y) * _Smoothing;
  89.             v2.y -= (v2.y - v0.y) * _Smoothing;
  90.            
  91.             // Solve worldspace normal
  92.             float3 vna = cross( v2-v0, v1-v0 );
  93.            
  94.             // OPTIONAL worldspace normal out to a custom value. Uncomment the showNormals finalcolor profile option above to see the result
  95.             //o.debugColor = ( normalize( vna ) * 0.5 ) + 0.5; //o.debugColor - ( normalize( vna ) );
  96.  
  97.             // Put normals back in object space
  98.             float3 vn = mul(unity_WorldToObject, vna );
  99.            
  100.             // Normalize
  101.             v.normal = normalize( vn );
  102.            
  103.             // Put vertex back in object space, Unity will automatically do the MVP projection
  104.             v.vertex.xyz = mul(unity_WorldToObject, v0 );
  105.         }
  106.  
  107.         void surf (Input IN, inout SurfaceOutputStandard o)
  108.         {
  109.             // Albedo comes from a texture tinted by color
  110.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
  111.             o.Albedo = c.rgb;
  112.             // Metallic and smoothness come from slider variables
  113.             o.Metallic = _Metallic;
  114.             o.Smoothness = _Glossiness;
  115.             o.Alpha = c.a;
  116.         }
  117.         ENDCG
  118.     }
  119.     FallBack "Diffuse"
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement