Facebook
From Soiled Baboon, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 86
  1. // This shader fills the mesh shape with a color that a user can change using the
  2. // Inspector window on a Material.
  3. Shader "Example/Disintegration"
  4. {
  5.     // The _BaseColor variable is visible in the Material's Inspector, as a field
  6.     // called Base Color. You can use it to select a custom color. This variable
  7.     // has the default value (1, 1, 1, 1).
  8.     Properties
  9.     {
  10.         _MainTex ("Texture", 2D) = "white" {}
  11.         _Color("Color", Color) = (1, 1, 1, 1)
  12.         [HDR]_AmbientColor("Ambient Color", Color) = (0.4,0.4,0.4,1)
  13.         _BumpMap("Normal Map", 2D) = "bump" {}
  14.         _BumpStr("Normal Map Strenght", float) = 1
  15.  
  16.         _FlowMap("Flow (RG)", 2D) = "black" {}
  17.         _DissolveTexture("Dissolve Texutre", 2D) = "white" {}
  18.         _DissolveColor("Dissolve Color Border", Color) = (1, 1, 1, 1)
  19.         _DissolveBorder("Dissolve Border", float) = 0.05
  20.  
  21.  
  22.         _Exapnd("Expand", float) = 1
  23.         _Weight("Weight", Range(0,1)) = 0
  24.         _Direction("Direction", Vector) = (0, 0, 0, 0)
  25.         [HDR]_DisintegrationColor("Disintegration Color", Color) = (1, 1, 1, 1)
  26.         _Glow("Glow", float) = 1
  27.  
  28.         _Shape("Shape Texutre", 2D) = "white" {}
  29.         _R("Radius", float) = .1
  30.     }
  31.  
  32.     SubShader
  33.     {
  34.         Tags
  35.         {
  36.             "RenderType" = "Opaque" "RenderPipeline" = "UniversalRenderPipeline"
  37.         }
  38.  
  39.         Pass
  40.         {
  41.             HLSLPROGRAM
  42.             #pragma vertex vert
  43.             #pragma geometry geom
  44.             #pragma fragment frag
  45.  
  46.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  47.             // #include "UnityCG.cginc"
  48.  
  49.             struct Attributes
  50.             {
  51.                 float4 vertex : POSITION;
  52.                 float3 normal : NORMAL;
  53.                 float2 uv : TEXCOORD0;
  54.             };
  55.  
  56.             struct Varyings
  57.             {
  58.                 float4 objPos : SV_POSITION;
  59.                 float2 uv : TEXCOORD0;
  60.                 float3 normal : NORMAL;
  61.                 float3 worldPos : TEXCOORD1;
  62.             };
  63.  
  64.             struct g2f
  65.             {
  66.                 float4 worldPos : SV_POSITION;
  67.                 float2 uv : TEXCOORD0;
  68.                 half4 color : COLOR;
  69.                 float3 normal : NORMAL;
  70.             };
  71.  
  72.             // This macro declares _BaseMap as a Texture2D object.
  73.             TEXTURE2D(_MainTex);
  74.             // This macro declares the sampler for the _BaseMap texture.
  75.             SAMPLER(sampler_MainTex);
  76.             // To make the Unity shader SRP Batcher compatible, declare all
  77.             // properties related to a Material in a a single CBUFFER block with
  78.             // the name UnityPerMaterial.
  79.             CBUFFER_START(UnityPerMaterial)
  80.            
  81.             // sampler2D _MainTex;
  82.             float4 _MainTex_ST;
  83.             float4 _Color;
  84.             float4 _AmbientColor;
  85.             sampler2D _BumpMap;
  86.             float _BumpStr;
  87.             float _Metallic;
  88.  
  89.             sampler2D _FlowMap;
  90.             float4 _FlowMap_ST;
  91.             sampler2D _DissolveTexture;
  92.             float4 _DissolveColor;
  93.             float _DissolveBorder;
  94.  
  95.  
  96.             float _Exapnd;
  97.             float _Weight;
  98.             float4 _Direction;
  99.             float4 _DisintegrationColor;
  100.             float _Glow;
  101.             sampler2D _Shape;
  102.             float _R;
  103.             CBUFFER_END
  104.  
  105.             float remap(float value, float from1, float to1, float from2, float to2)
  106.             {
  107.                 return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
  108.             }
  109.  
  110.             float4 remapFlowTexture(float4 tex)
  111.             {
  112.                 return float4(
  113.                     remap(tex.x, 0, 1, -1, 1),
  114.                     remap(tex.y, 0, 1, -1, 1),
  115.                     0,
  116.                     remap(tex.w, 0, 1, -1, 1)
  117.                 );
  118.             }
  119.  
  120.             Varyings vert(Attributes IN)
  121.             {
  122.                 Varyings OUT;
  123.                 OUT.objPos = IN.vertex;
  124.                 OUT.uv = IN.uv;
  125.                 OUT.normal = TransformObjectToWorldNormal(IN.normal);
  126.                 OUT.worldPos = mul(unity_ObjectToWorld, IN.vertex).xyz;
  127.                 return OUT;
  128.             }
  129.  
  130.             [maxvertexcount(7)]
  131.             void geom(triangle Varyings IN[3], inout TriangleStream<g2f> triStream)
  132.             {
  133.                 float2 avgUV = (IN[0].uv + IN[1].uv + IN[2].uv) / 3;
  134.                 float3 avgPos = (IN[0].objPos + IN[1].objPos + IN[2].objPos) / 3;
  135.                 float3 avgNormal = (IN[0].normal + IN[1].normal + IN[2].normal) / 3;
  136.  
  137.                 float dissolve_value = tex2Dlod(_DissolveTexture, float4(avgUV, 0, 0)).r;
  138.                 float t = clamp(_Weight * 2 - dissolve_value, 0, 1);
  139.  
  140.                 float2 flowUV = TRANSFORM_TEX(mul(unity_ObjectToWorld, avgPos).xz, _FlowMap);
  141.                 float4 flowVector = remapFlowTexture(tex2Dlod(_FlowMap, float4(flowUV, 0, 0)));
  142.  
  143.                 float3 pseudoRandomPos = (avgPos) + _Direction;
  144.                 pseudoRandomPos += (flowVector.xyz * _Exapnd);
  145.  
  146.                 float3 p = lerp(avgPos, pseudoRandomPos, t);
  147.                 float radius = lerp(_R, 0, t);
  148.  
  149.  
  150.                 if (t > 0)
  151.                 {
  152.                     float3 look = _WorldSpaceCameraPos - p;
  153.                     look = normalize(look);
  154.  
  155.                     float3 right = UNITY_MATRIX_IT_MV[0].xyz;
  156.                     float3 up = UNITY_MATRIX_IT_MV[1].xyz;
  157.  
  158.                     float halfS = 0.5f * radius;
  159.  
  160.                     float4 v[4];
  161.                     v[0] = float4(p + halfS * right - halfS * up, 1.0f);
  162.                     v[1] = float4(p + halfS * right + halfS * up, 1.0f);
  163.                     v[2] = float4(p - halfS * right - halfS * up, 1.0f);
  164.                     v[3] = float4(p - halfS * right + halfS * up, 1.0f);
  165.  
  166.  
  167.                     g2f vert;
  168.                     vert.worldPos = TransformObjectToHClip(v[0]);
  169.                     vert.uv = mul(float4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1), float2(1.0f, 0.0f));
  170.                     vert.color = float4(1, 1, 1, 1);
  171.                     vert.normal = avgNormal;
  172.                     triStream.Append(vert);
  173.  
  174.                     vert.worldPos = TransformObjectToHClip(v[1]);
  175.                     vert.uv = mul(float4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1), float2(1.0f, 1.0f));
  176.                     vert.color = float4(1, 1, 1, 1);
  177.                     vert.normal = avgNormal;
  178.                     triStream.Append(vert);
  179.  
  180.                     vert.worldPos = TransformObjectToHClip(v[2]);
  181.                     vert.uv = mul(float4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1), float2(0.0f, 0.0f));
  182.                     vert.color = float4(1, 1, 1, 1);
  183.                     vert.normal = avgNormal;
  184.                     triStream.Append(vert);
  185.  
  186.                     vert.worldPos = TransformObjectToHClip(v[3]);
  187.                     vert.uv = mul(float4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1), float2(0.0f, 1.0f));
  188.                     vert.color = float4(1, 1, 1, 1);
  189.                     vert.normal = avgNormal;
  190.                     triStream.Append(vert);
  191.  
  192.                     triStream.RestartStrip();
  193.                 }
  194.  
  195.                 for (int j = 0; j < 3; j++)
  196.                 {
  197.                     g2f o;
  198.                     o.worldPos = TransformObjectToHClip(IN[j].objPos);
  199.                     o.uv = TRANSFORM_TEX(IN[j].uv, _MainTex);
  200.                     o.color = half4(0, 0, 0, 0);
  201.                     o.normal = IN[j].normal;
  202.                     triStream.Append(o);
  203.                 }
  204.  
  205.                 triStream.RestartStrip();
  206.                
  207.             }
  208.  
  209.             half4 frag(Varyings IN) : SV_Target
  210.             {
  211.                 half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
  212.                 return color;
  213.             }
  214.             ENDHLSL
  215.         }
  216.     }
  217. }
  218.