Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Custom Shader to URPL

Discussion in 'Shaders' started by j111194, Dec 8, 2020.

  1. j111194

    j111194

    Joined:
    Mar 14, 2020
    Posts:
    20
    Hi Unity'ers!

    I'm quite stuck on a certain thing and I am trying out stuff for the last couple of days without any results. I have this custom shader of Acid fluid from a Unity Learn project, which I try to integrate in a URPL project. Also converting it with Shader Graphs was without any success.

    So I want to rewrite the code for URPL. Please find the code below. Can anyone help me how to rewrite or direct me to some instructions?

    You would be a hero to my project!

    Code (CG):
    1. Shader "Custom/Acid" {
    2.     Properties {
    3.         _Color1 ("Color1", Color) = (1,1,1,1)
    4.         _Color2 ("Color2", Color) = (1,1,1,1)
    5.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.         _Ramp ("Ramp (RGB)", 2D) = "white" {}
    7.         _Normal ("Normal", 2D) = "bump" {}
    8.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    9.         _Metallic ("Metallic", Range(0,1)) = 0.0
    10.         _Noise ("Noise)", 2D) = "white" {}
    11.  
    12.         _Edge ("Edge Size", Float) = 1.0
    13.         _Fog ("Fog", Float) = 1.0
    14.         _Fade ("Edge Fade", Float) = 1.0
    15.     }
    16.     SubShader {
    17.         Tags { "Queue"="Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
    18.         LOD 200
    19.  
    20.         GrabPass { }
    21.  
    22.         CGPROGRAM
    23.         // Physically based Standard lighting model, and enable shadows on all light types
    24.         #pragma surface surf Standard noshadow nolightmap vertex:vert alpha:blend
    25.  
    26.         // Use shader model 3.0 target, to get nicer looking lighting
    27.         #pragma target 3.0
    28.  
    29.         sampler2D _MainTex, _Normal, _Ramp, _Noise;
    30.  
    31.         UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
    32.  
    33.         struct Input {
    34.             float2 uv_MainTex;
    35.             float2 uv_Normal;
    36.  
    37.             float4 projPos;
    38.             float4 grabPos;
    39.             float3 worldPos;
    40.             float2 localDir;
    41.             float3 worldRefl;
    42.             float4 screenPos;
    43.             float3 viewDir;
    44.             float depth;
    45.  
    46.         };
    47.  
    48.         half _Glossiness;
    49.         half _Metallic;
    50.         fixed4 _Color1, _Color2;
    51.         half _Edge, _Fade, _Fog;
    52.  
    53.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    54.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    55.         // #pragma instancing_options assumeuniformscaling
    56.         UNITY_INSTANCING_BUFFER_START(Props)
    57.             // put more per-instance properties here
    58.         UNITY_INSTANCING_BUFFER_END(Props)
    59.  
    60.         inline float InverseLerp(float a, float b, float value)
    61.         {
    62.             return saturate((value - a) / (b - a));
    63.         }
    64.  
    65.  
    66.         void vert (inout appdata_full v, out Input o) {
    67.             UNITY_INITIALIZE_OUTPUT(Input,o);
    68.  
    69.             o.localDir = v.vertex.xz;
    70.             float4 vertex = UnityObjectToClipPos(v.vertex);
    71.             o.projPos = ComputeScreenPos (vertex);
    72.             COMPUTE_EYEDEPTH(o.projPos.z);
    73.             o.grabPos = ComputeGrabScreenPos(vertex);
    74.             float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
    75.             float3 relPos = worldPos.xyz - _WorldSpaceCameraPos.xyz;
    76.             o.depth = length(relPos);
    77.         }
    78.  
    79.         void surf (Input IN, inout SurfaceOutputStandard o) {
    80.             // Albedo comes from a texture tinted by color
    81.  
    82.             float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(IN.projPos)));
    83.             float partZ = IN.projPos.z;
    84.             float zDiff = abs(sceneZ-partZ);
    85.             float edge = InverseLerp(_Edge, 0, zDiff);
    86.             float fog = 1-InverseLerp(_Fog, 0, zDiff);
    87.             float fade = InverseLerp(_Fade, 0, zDiff);
    88.  
    89.             float2 UV2 = IN.uv_Normal;
    90.             UV2.x += _Time*0.2;
    91.             UV2.y += _Time*0.1;
    92.             UV2 *= 0.5;
    93.            
    94.            
    95.             float3 normal = UnpackScaleNormal(tex2D (_Normal, UV2), 1);
    96.  
    97.             float2 UV = IN.uv_MainTex;
    98.             UV.y += _Time*0.2;
    99.             UV += normal*0.2;
    100.  
    101.             float2 UV3 = IN.uv_MainTex;
    102.             UV3.y += _Time*1;
    103.             UV3 += normal;
    104.  
    105.             float noiseTex = tex2D(_Noise, UV3*0.2);
    106.             float blend = tex2D(_MainTex, UV).r;
    107.            
    108.             //fixed4 c = lerp(_Color1, _Color2, pow(blend, 2));
    109.             fixed4 c = tex2D(_Ramp, float2(blend, 0));
    110.             c = lerp(c, _Color2, edge);
    111.             float FogEdge = smoothstep(noiseTex, noiseTex + 1, fog);
    112.             //fixed4 fogc = tex2D(_Ramp, float2(fog, 0));
    113.             fixed4 fogc = lerp(_Color2, _Color1, fog * FogEdge);
    114.             c += fogc;
    115.             fixed4 emission = lerp(0, _Color2, pow(blend, 2));
    116.             fixed roughness = lerp(1, 0, blend);
    117.  
    118.             o.Albedo = c.rgb;
    119.             // Metallic and smoothness come from slider variables
    120.             o.Metallic = _Metallic;
    121.             o.Normal = normal;
    122.             o.Smoothness = roughness * _Glossiness;
    123.             //o.Alpha = c.a;
    124.             //o.Emission = emission.rgb;
    125.             //o.Alpha = zDiff;
    126.  
    127.             float distanceAlpha = 1-InverseLerp(0.5*0.7, 0.5, IN.depth);
    128.  
    129.              o.Alpha = 1-fade;
    130.         }
    131.         ENDCG
    132.     }
    133.     FallBack "Diffuse"
    134. }
    135.  
    View attachment 750529
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553