Advertisement
Guest User

World Triplanar Mapped Particle.shader

a guest
Jan 30th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // by Neitri, free of charge, free to redistribute
  2. // downloaded from https://github.com/netri/Neitri-Unity-Shaders
  3.  
  4. Shader "Neitri/World Triplanar Mapped Particle"
  5. {
  6.     Properties
  7.     {
  8.         _MainTex ("_MainTex", 2D) = "black" {}
  9.         _Scale ("_Scale", Range(0.1, 10)) = 0.2
  10.         _Range ("_Range", Range(0, 10)) = 10
  11.     }
  12.     SubShader
  13.     {
  14.         Tags
  15.         {
  16.             "Queue" = "Transparent+1000"
  17.             "RenderType" = "Transparent"
  18.         }
  19.  
  20.         Cull Off
  21.         Blend SrcAlpha OneMinusSrcAlpha
  22.  
  23.         Pass
  24.         {
  25.             // based on "Neitri/World Normal Ugly Fast"
  26.  
  27.             CGPROGRAM
  28.             #pragma vertex vert
  29.             #pragma fragment frag
  30.             #include "UnityCG.cginc"
  31.  
  32.             struct appdata
  33.             {
  34.                 float4 vertex : POSITION;
  35.                 float3 normal : NORMAL;
  36.                 float3 center : TEXCOORD0;
  37.             };
  38.             struct v2f
  39.             {
  40.                 float4 vertex : SV_POSITION;
  41.                 float3 modelCenterPos : TEXCOORD0;
  42.                 float4 projPos : TEXCOORD1;
  43.                 float3 ray : TEXCOORD2;
  44.             };
  45.  
  46.             UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
  47.  
  48.             sampler2D _MainTex; float4 _MainTex_ST;
  49.             float _Scale;
  50.             float _Range;
  51.  
  52.             float4 neitriTriPlanar1(sampler2D tex, float3 position, float3 modelPos, float3 normal, float scale)
  53.             {
  54.                 position = (position - modelPos) / scale + 0.5;
  55.                            
  56.                 float3 blendWeights = pow(abs(normal), 3);
  57.                 blendWeights /= blendWeights.x + blendWeights.y + blendWeights.z;
  58.  
  59.                 return  blendWeights.x * tex2D(tex, position.zy) +
  60.                         blendWeights.y * tex2D(tex, position.xz) +
  61.                         blendWeights.z * tex2D(tex, position.xy);
  62.             }
  63.            
  64.             float4 neitriTriPlanar2(sampler2D tex, float3 position, float3 modelPos, float3 normal, float scale)
  65.             {
  66.                 position = (position - modelPos) / scale + 0.5;
  67.  
  68.                 float3 blendWeights = pow(abs(normal), 3);
  69.                 blendWeights /= dot(blendWeights, 1);
  70.  
  71.                 const float threshold = 0.05;
  72.  
  73.                 float3 finalWeights = 0;
  74.                 if(blendWeights.x > threshold) finalWeights.x = blendWeights.x;
  75.                 if(blendWeights.y > threshold) finalWeights.y = blendWeights.y;
  76.                 if(blendWeights.z > threshold) finalWeights.z = blendWeights.z;
  77.                 finalWeights /= finalWeights.x + finalWeights.y + finalWeights.z;
  78.  
  79.                 float4 result = 0;
  80.                 if(finalWeights.x > 0) result += finalWeights.x * tex2D(tex, position.zy);
  81.                 if(finalWeights.y > 0) result += finalWeights.y * tex2D(tex, position.xz);
  82.                 if(finalWeights.z > 0) result += finalWeights.z * tex2D(tex, position.xy);
  83.                 return result;
  84.             }
  85.  
  86.             float4 errorTriPlanar(sampler2D tex, float3 position, float3 modelPos, float3 normal, float scale)
  87.             {
  88.                 position = (position - modelPos) / scale + 0.5;
  89.                 normal = abs(normal);
  90.  
  91.                 float2 coords =
  92.                     step(normal.y,normal.x) * step(normal.z,normal.x) * position.zy +
  93.                     step(normal.x,normal.y) * step(normal.z,normal.y) * position.xz +
  94.                     step(normal.x,normal.z) * step(normal.y,normal.z) * position.xy;    
  95.  
  96.                 return tex2D(tex, coords);
  97.             }
  98.            
  99.  
  100.             v2f vert (appdata v)
  101.             {
  102.                 v2f o;
  103.                 float4 worldPos = mul(UNITY_MATRIX_M, v.vertex);
  104.                 o.modelCenterPos = v.center;
  105.                 o.ray = worldPos.xyz - _WorldSpaceCameraPos;
  106.                 o.vertex = mul(UNITY_MATRIX_VP, worldPos);
  107.                 o.projPos = ComputeScreenPos (o.vertex);
  108.                 o.projPos.z = -mul(UNITY_MATRIX_V, worldPos).z;
  109.                 return o;
  110.                 return o;
  111.             }
  112.  
  113.             float4 frag (v2f i) : SV_Target
  114.             {
  115.                 float sceneDepth = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
  116.                 float3 worldPosition = sceneDepth * i.ray / i.projPos.z + _WorldSpaceCameraPos;
  117.                 fixed3 worldNormal = normalize(cross(-ddx(worldPosition), ddy(worldPosition)));
  118.                 clip(_Range - distance(worldPosition, i.modelCenterPos));
  119.  
  120.                 //fixed4 color = neitriTriPlanar1(_MainTex, worldPosition, i.modelCenterPos, worldNormal, _Scale);
  121.                 //fixed4 color = neitriTriPlanar2(_MainTex, worldPosition, i.modelCenterPos, worldNormal, _Scale);
  122.                 fixed4 color = errorTriPlanar(_MainTex, worldPosition, i.modelCenterPos, worldNormal, _Scale);
  123.  
  124.                 clip(color.a - 0.01);
  125.                 return color;
  126.             }
  127.  
  128.             ENDCG
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement