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

Combine Two Shaders Into One - Help

Discussion in 'Shaders' started by Deleted User, May 28, 2017.

  1. Deleted User

    Deleted User

    Guest

    Ok, so I own shaderforge, because I'm pretty crappy at writing my own shaders, I have a basic Emissive Shader that can also receive Shadows that I developed. I also have a seperate Curved Horizon shader that I modified to my own needs from the web.

    I would like to combine the curved shader into my emissive shader, but each time I've tried, I've clearly not understood how both shaders relate to each other and how I can merge the curved shader in with shaderforge shader, and so, final result, a complete mess that does not work, I'm hoping someone could take a look at both shaders, and possibly help ?

    The curved shader :

    Code (CSharp):
    1. Shader "playmint/curved/curved"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Colour", Color) = (1,1,1,1)
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags { "RenderType" = "Opaque" }
    12.         LOD 200
    13.  
    14.         CGPROGRAM
    15.         #pragma surface surf Lambert vertex:vert addshadow
    16.  
    17.         // Global Shader values
    18.         uniform float2 _BendAmount;
    19.         uniform float3 _BendOrigin;
    20.         uniform float _BendFalloff;
    21.  
    22.         sampler2D _MainTex;
    23.         fixed4 _Color;
    24.  
    25.         struct Input
    26.         {
    27.               float2 uv_MainTex;
    28.         };
    29.  
    30.         float4 Curve(float4 v)
    31.         {
    32.               //HACK: Considerably reduce amount of Bend
    33.               _BendAmount *= .0001;
    34.  
    35.               float4 world = mul(unity_ObjectToWorld, v);
    36.  
    37.               float dist = length(world.xz-_BendOrigin.xz);
    38.  
    39.               dist = max(0, dist-_BendFalloff);
    40.  
    41.               // Distance squared
    42.               dist = dist*dist;
    43.  
    44.               world.xy += dist*_BendAmount;
    45.               return mul(unity_WorldToObject, world);
    46.         }
    47.  
    48.         void vert(inout appdata_full v)
    49.         {
    50.               v.vertex = Curve(v.vertex);
    51.         }
    52.  
    53.         void surf(Input IN, inout SurfaceOutput o)
    54.         {
    55.               fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    56.               o.Albedo = c.rgb;
    57.               o.Alpha = c.a;
    58.         }
    59.  
    60.         ENDCG
    61.     }
    62.       Fallback "Mobile/Diffuse"
    63. }
    and, my Emissive shader :

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. // Shader created with Shader Forge v1.36
    4. // Shader Forge (c) Neat Corporation / Joachim Holmer - http://www.acegikmo.com/shaderforge/
    5. // Note: Manually altering this data may prevent you from opening it in Shader Forge
    6. /*SF_DATA;ver:1.36;sub:START;pass:START;ps:flbk:,iptp:0,cusa:False,bamd:0,cgin:,lico:1,lgpr:1,limd:1,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,hqlp:False,rprd:False,enco:False,rmgx:True,imps:True,rpth:0,vtps:0,hqsc:True,nrmq:1,nrsp:0,vomd:0,spxs:False,tesm:0,olmd:1,culm:0,bsrc:0,bdst:1,dpts:2,wrdp:True,dith:0,atcv:False,rfrpo:True,rfrpn:Refraction,coma:15,ufog:True,aust:True,igpj:False,qofs:0,qpre:1,rntp:1,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,stcl:False,stva:128,stmr:255,stmw:255,stcp:6,stps:0,stfa:0,stfz:0,ofsf:0,ofsu:0,f2p0:False,fnsp:False,fnfb:False,fsmp:False;n:type:ShaderForge.SFN_Final,id:4013,x:32719,y:32712,varname:node_4013,prsc:2|emission-2128-OUT;n:type:ShaderForge.SFN_Tex2d,id:3122,x:32333,y:32486,ptovrint:False,ptlb:Diffuse Texture,ptin:_DiffuseTexture,varname:node_3122,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Multiply,id:2128,x:32537,y:32670,varname:node_2128,prsc:2|A-3122-RGB,B-5967-OUT;n:type:ShaderForge.SFN_LightAttenuation,id:5967,x:32333,y:32670,varname:node_5967,prsc:2;proporder:3122;pass:END;sub:END;*/
    7.  
    8. Shader "playmint/emissive/Em + Shade" {
    9.     Properties {
    10.         _DiffuseTexture ("Diffuse Texture", 2D) = "white" {}
    11.     }
    12.     SubShader {
    13.         Tags {
    14.             "RenderType"="Opaque"
    15.         }
    16.         Pass {
    17.             Name "FORWARD"
    18.             Tags {
    19.                 "LightMode"="ForwardBase"
    20.             }
    21.            
    22.            
    23.             CGPROGRAM
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.             #define UNITY_PASS_FORWARDBASE
    27.             #include "UnityCG.cginc"
    28.             #include "AutoLight.cginc"
    29.             #include "Lighting.cginc"
    30.             #pragma multi_compile_fwdbase_fullshadows
    31.             #pragma multi_compile_fog
    32.             #pragma only_renderers d3d9 d3d11 glcore gles
    33.             #pragma target 3.0
    34.             uniform sampler2D _DiffuseTexture; uniform float4 _DiffuseTexture_ST;
    35.             struct VertexInput {
    36.                 float4 vertex : POSITION;
    37.                 float2 texcoord0 : TEXCOORD0;
    38.             };
    39.             struct VertexOutput {
    40.                 float4 pos : SV_POSITION;
    41.                 float2 uv0 : TEXCOORD0;
    42.                 LIGHTING_COORDS(1,2)
    43.                 UNITY_FOG_COORDS(3)
    44.             };
    45.             VertexOutput vert (VertexInput v) {
    46.                 VertexOutput o = (VertexOutput)0;
    47.                 o.uv0 = v.texcoord0;
    48.                 o.pos = UnityObjectToClipPos(v.vertex );
    49.                 UNITY_TRANSFER_FOG(o,o.pos);
    50.                 TRANSFER_VERTEX_TO_FRAGMENT(o)
    51.                 return o;
    52.             }
    53.             float4 frag(VertexOutput i) : COLOR {
    54. ////// Lighting:
    55.                 float attenuation = LIGHT_ATTENUATION(i);
    56.                 float3 attenColor = attenuation * _LightColor0.xyz;
    57. ////// Emissive:
    58.                 float4 _DiffuseTexture_var = tex2D(_DiffuseTexture,TRANSFORM_TEX(i.uv0, _DiffuseTexture));
    59.                 float3 emissive = (_DiffuseTexture_var.rgb*attenuation);
    60.                 float3 finalColor = emissive;
    61.                 fixed4 finalRGBA = fixed4(finalColor,1);
    62.                 UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
    63.                 return finalRGBA;
    64.             }
    65.             ENDCG
    66.         }
    67.         Pass {
    68.             Name "FORWARD_DELTA"
    69.             Tags {
    70.                 "LightMode"="ForwardAdd"
    71.             }
    72.             Blend One One
    73.            
    74.            
    75.             CGPROGRAM
    76.             #pragma vertex vert
    77.             #pragma fragment frag
    78.             #define UNITY_PASS_FORWARDADD
    79.             #include "UnityCG.cginc"
    80.             #include "AutoLight.cginc"
    81.             #include "Lighting.cginc"
    82.             #pragma multi_compile_fwdadd_fullshadows
    83.             #pragma multi_compile_fog
    84.             #pragma only_renderers d3d9 d3d11 glcore gles
    85.             #pragma target 3.0
    86.             uniform sampler2D _DiffuseTexture; uniform float4 _DiffuseTexture_ST;
    87.             struct VertexInput {
    88.                 float4 vertex : POSITION;
    89.                 float2 texcoord0 : TEXCOORD0;
    90.             };
    91.             struct VertexOutput {
    92.                 float4 pos : SV_POSITION;
    93.                 float2 uv0 : TEXCOORD0;
    94.                 LIGHTING_COORDS(1,2)
    95.                 UNITY_FOG_COORDS(3)
    96.             };
    97.             VertexOutput vert (VertexInput v) {
    98.                 VertexOutput o = (VertexOutput)0;
    99.                 o.uv0 = v.texcoord0;
    100.                 o.pos = UnityObjectToClipPos(v.vertex );
    101.                 UNITY_TRANSFER_FOG(o,o.pos);
    102.                 TRANSFER_VERTEX_TO_FRAGMENT(o)
    103.                 return o;
    104.             }
    105.             float4 frag(VertexOutput i) : COLOR {
    106. ////// Lighting:
    107.                 float attenuation = LIGHT_ATTENUATION(i);
    108.                 float3 attenColor = attenuation * _LightColor0.xyz;
    109.                 float3 finalColor = 0;
    110.                 fixed4 finalRGBA = fixed4(finalColor * 1,0);
    111.                 UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
    112.                 return finalRGBA;
    113.             }
    114.             ENDCG
    115.         }
    116.     }
    117.     FallBack "Diffuse"
    118.     CustomEditor "ShaderForgeMaterialInspector"
    119. }
     
  2. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    972
    That should be very simple.
    1. put these variable declarations for _BendAmount, _BendOrigin, _BendFalloff into the passes of the second shader.
    2. put the Curve function into nto the passes of the second shader.
    3. put this statement: "v.vertex = Curve(v.vertex);" at the beginning of the vert function in both passes of the second shader.
    4. Use second shader modified that way as the combined shader.
     
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

    Thanks, I still haven't got it working yet, but I feel like I'm on the right track a bit more now.

    I 'think' I've acted upon your advice, but the shader does not work at all, literally just get an invisible object in my scene that the shader is applied to. Maybe I've put something in the wrong section ?

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. // Shader created with Shader Forge v1.36
    4. // Shader Forge (c) Neat Corporation / Joachim Holmer - http://www.acegikmo.com/shaderforge/
    5. // Note: Manually altering this data may prevent you from opening it in Shader Forge
    6. /*SF_DATA;ver:1.36;sub:START;pass:START;ps:flbk:,iptp:0,cusa:False,bamd:0,cgin:,lico:1,lgpr:1,limd:1,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,hqlp:False,rprd:False,enco:False,rmgx:True,imps:True,rpth:0,vtps:0,hqsc:True,nrmq:1,nrsp:0,vomd:0,spxs:False,tesm:0,olmd:1,culm:0,bsrc:0,bdst:1,dpts:2,wrdp:True,dith:0,atcv:False,rfrpo:True,rfrpn:Refraction,coma:15,ufog:True,aust:True,igpj:False,qofs:0,qpre:1,rntp:1,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,stcl:False,stva:128,stmr:255,stmw:255,stcp:6,stps:0,stfa:0,stfz:0,ofsf:0,ofsu:0,f2p0:False,fnsp:False,fnfb:False,fsmp:False;n:type:ShaderForge.SFN_Final,id:4013,x:32719,y:32712,varname:node_4013,prsc:2|emission-2128-OUT;n:type:ShaderForge.SFN_Tex2d,id:3122,x:32333,y:32486,ptovrint:False,ptlb:Diffuse Texture,ptin:_DiffuseTexture,varname:node_3122,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Multiply,id:2128,x:32537,y:32670,varname:node_2128,prsc:2|A-3122-RGB,B-5967-OUT;n:type:ShaderForge.SFN_LightAttenuation,id:5967,x:32333,y:32670,varname:node_5967,prsc:2;proporder:3122;pass:END;sub:END;*/
    7.  
    8. Shader "playmint/curved/Curve + Em + Shade" {
    9.     Properties {
    10.         _DiffuseTexture ("Diffuse Texture", 2D) = "white" {}
    11.     }
    12.     SubShader {
    13.         Tags {
    14.             "RenderType"="Opaque"
    15.         }
    16.         Pass {
    17.             Name "FORWARD"
    18.             Tags {
    19.                 "LightMode"="ForwardBase"
    20.             }
    21.            
    22.             CGPROGRAM
    23.             #pragma vertex vert
    24.             #pragma fragment frag
    25.             #define UNITY_PASS_FORWARDBASE
    26.             #include "UnityCG.cginc"
    27.             #include "AutoLight.cginc"
    28.             #include "Lighting.cginc"
    29.             #pragma multi_compile_fwdbase_fullshadows
    30.             #pragma multi_compile_fog
    31.             #pragma only_renderers d3d9 d3d11 glcore gles
    32.             #pragma target 3.0
    33.  
    34.             uniform float _BendFalloff;
    35.             uniform float2 _BendAmount;
    36.             uniform float3 _BendOrigin;
    37.  
    38.             uniform sampler2D _DiffuseTexture; uniform float4 _DiffuseTexture_ST;
    39.  
    40.             float Curve(float4 v)
    41.             {
    42.             // Hack : Considerably reduce amount of Bend
    43.             _BendAmount *= .0001;
    44.             float4 world = mul(unity_ObjectToWorld, v);
    45.             float dist = length(world.xz-_BendOrigin.xz);
    46.             dist = max(0, dist-_BendFalloff);
    47.             dist = dist*dist;
    48.             world.xy += dist*_BendAmount;
    49.             return mul(unity_WorldToObject, world);
    50.             }
    51.  
    52.             struct VertexInput {
    53.                 float4 vertex : POSITION;
    54.                 float2 texcoord0 : TEXCOORD0;
    55.             };
    56.  
    57.             struct VertexOutput {
    58.                 float4 pos : SV_POSITION;
    59.                 float2 uv0 : TEXCOORD0;
    60.                 LIGHTING_COORDS(1,2)
    61.                 UNITY_FOG_COORDS(3)
    62.             };
    63.             VertexOutput vert (VertexInput v) {
    64.  
    65.                 v.vertex = Curve(v.vertex);
    66.  
    67.                 VertexOutput o = (VertexOutput)0;
    68.                 o.uv0 = v.texcoord0;
    69.                 o.pos = UnityObjectToClipPos(v.vertex );
    70.                 UNITY_TRANSFER_FOG(o,o.pos);
    71.                 TRANSFER_VERTEX_TO_FRAGMENT(o)
    72.                 return o;
    73.             }
    74.             float4 frag(VertexOutput i) : COLOR {
    75. ////// Lighting:
    76.                 float attenuation = LIGHT_ATTENUATION(i);
    77.                 float3 attenColor = attenuation * _LightColor0.xyz;
    78. ////// Emissive:
    79.                 float4 _DiffuseTexture_var = tex2D(_DiffuseTexture,TRANSFORM_TEX(i.uv0, _DiffuseTexture));
    80.                 float3 emissive = (_DiffuseTexture_var.rgb*attenuation);
    81.                 float3 finalColor = emissive;
    82.                 fixed4 finalRGBA = fixed4(finalColor,1);
    83.                 UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
    84.                 return finalRGBA;
    85.             }
    86.  
    87.             ENDCG
    88.         }
    89.         Pass {
    90.             Name "FORWARD_DELTA"
    91.             Tags {
    92.                 "LightMode"="ForwardAdd"
    93.             }
    94.             Blend One One
    95.            
    96.            
    97.             CGPROGRAM
    98.             #pragma vertex vert
    99.             #pragma fragment frag
    100.             #define UNITY_PASS_FORWARDADD
    101.             #include "UnityCG.cginc"
    102.             #include "AutoLight.cginc"
    103.             #include "Lighting.cginc"
    104.             #pragma multi_compile_fwdadd_fullshadows
    105.             #pragma multi_compile_fog
    106.             #pragma only_renderers d3d9 d3d11 glcore gles
    107.             #pragma target 3.0
    108.  
    109.                uniform float _BendFalloff;
    110.             uniform float2 _BendAmount;
    111.             uniform float3 _BendOrigin;
    112.  
    113.             uniform sampler2D _DiffuseTexture; uniform float4 _DiffuseTexture_ST;
    114.  
    115.             float Curve(float4 v)
    116.             {
    117.             // Hack : Considerably reduce amount of Bend
    118.             _BendAmount *= .0001;
    119.             float4 world = mul(unity_ObjectToWorld, v);
    120.             float dist = length(world.xz-_BendOrigin.xz);
    121.             dist = max(0, dist-_BendFalloff);
    122.             dist = dist*dist;
    123.             world.xy += dist*_BendAmount;
    124.             return mul(unity_WorldToObject, world);
    125.             }
    126.  
    127.             struct VertexInput {
    128.                 float4 vertex : POSITION;
    129.                 float2 texcoord0 : TEXCOORD0;
    130.             };
    131.  
    132.             struct VertexOutput {
    133.                 float4 pos : SV_POSITION;
    134.                 float2 uv0 : TEXCOORD0;
    135.                 LIGHTING_COORDS(1,2)
    136.                 UNITY_FOG_COORDS(3)
    137.             };
    138.             VertexOutput vert (VertexInput v) {
    139.  
    140.                 v.vertex = Curve(v.vertex);
    141.  
    142.                 VertexOutput o = (VertexOutput)0;
    143.                 o.uv0 = v.texcoord0;
    144.                 o.pos = UnityObjectToClipPos(v.vertex );
    145.                 UNITY_TRANSFER_FOG(o,o.pos);
    146.                 TRANSFER_VERTEX_TO_FRAGMENT(o)
    147.                 return o;
    148.             }
    149.             float4 frag(VertexOutput i) : COLOR {
    150. ////// Lighting:
    151.                 float attenuation = LIGHT_ATTENUATION(i);
    152.                 float3 attenColor = attenuation * _LightColor0.xyz;
    153.                 float3 finalColor = 0;
    154.                 fixed4 finalRGBA = fixed4(finalColor * 1,0);
    155.                 UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
    156.                 return finalRGBA;
    157.             }
    158.             ENDCG
    159.         }
    160.     }
    161.     FallBack "Diffuse"
    162.     CustomEditor "ShaderForgeMaterialInspector"
    163. }
    If I remove the v.vertex = Curve(v.vertex); line, then my shader becomes visible again, minus any curve abilities, only the emissive and shadows part of the shader then work.
     
    Last edited by a moderator: May 29, 2017
  4. Deleted User

    Deleted User

    Guest

    Anybody can offer any insight on this, as to what I may be doing wrong ?
     
    Last edited by a moderator: May 30, 2017
  5. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    972
    Maybe a real expert could tell what is wrong almost straightaway from the code - but I would try adding the emissive shader features one by one to the curved shader.
     
  6. Deleted User

    Deleted User

    Guest

    Well, through lots of experimentation and hair pulling, finally managed to figure it out ! :)

    Shadows are not right when curvature takes place, but I think I can figure that out, I'll post a fixed version as soon as I finish it.

    Code below :

    Code (CSharp):
    1. Shader "playmint/curved/Curve + Unlit + Shade" {
    2.     Properties {
    3.         _BaseRGB ("Base (RGB)", 2D) = "white" {}
    4.     }
    5.     SubShader {
    6.         Tags {
    7.             "RenderType"="Opaque"
    8.         }
    9.         Pass {
    10.             Name "FORWARD"
    11.             Tags {
    12.                 "LightMode"="ForwardBase"
    13.             }
    14.            
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.             #define UNITY_PASS_FORWARDBASE
    19.             #include "UnityCG.cginc"
    20.             #include "AutoLight.cginc"
    21.             #include "Lighting.cginc"
    22.             #pragma multi_compile_fwdbase_fullshadows
    23.             #pragma multi_compile_fog
    24.             #pragma only_renderers d3d9 d3d11 glcore gles
    25.             #pragma target 2.0
    26.  
    27.             // Global Shader Values
    28.  
    29.             uniform float2 _BendAmount;
    30.             uniform float3 _BendOrigin;
    31.             uniform float _BendFalloff;
    32.  
    33.             uniform sampler2D _BaseRGB; uniform float4 _BaseRGB_ST;
    34.  
    35.             float4 Curve(float4 v)
    36.             {
    37.                 // Hack : Considerably reduce amount of Bend
    38.                   _BendAmount *= .0001;
    39.  
    40.                   float4 world = mul(unity_ObjectToWorld, v);
    41.                   float dist = length(world.xz-_BendOrigin.xz);
    42.                   dist = max(0, dist-_BendFalloff);
    43.  
    44.                   // Distance squared
    45.                   dist = dist*dist;
    46.  
    47.                   world.xy += dist*_BendAmount;
    48.                   return mul(unity_WorldToObject, world);
    49.             }
    50.  
    51.             struct VertexInput {
    52.                 float4 vertex : POSITION;
    53.                 float2 texcoord0 : TEXCOORD0;
    54.             };
    55.             struct VertexOutput {
    56.                 float4 pos : SV_POSITION;
    57.                 float2 uv0 : TEXCOORD0;
    58.                 LIGHTING_COORDS(1,2)
    59.                 UNITY_FOG_COORDS(3)
    60.             };
    61.             VertexOutput vert (VertexInput v) {
    62.  
    63.                 v.vertex = Curve(v.vertex);
    64.  
    65.                 VertexOutput o = (VertexOutput)0;
    66.                 o.uv0 = v.texcoord0;
    67.                 o.pos = UnityObjectToClipPos(v.vertex );
    68.                 UNITY_TRANSFER_FOG(o,o.pos);
    69.                 TRANSFER_VERTEX_TO_FRAGMENT(o)
    70.                 return o;
    71.             }
    72.             float4 frag(VertexOutput i) : COLOR {
    73. ////// Lighting:
    74.                 float attenuation = LIGHT_ATTENUATION(i);
    75. ////// Emissive:
    76.                 float4 _BaseRGB_var = tex2D(_BaseRGB,TRANSFORM_TEX(i.uv0, _BaseRGB));
    77.                 float3 emissive = (_BaseRGB_var.rgb*attenuation);
    78.                 float3 finalColor = emissive;
    79.                 fixed4 finalRGBA = fixed4(finalColor,1);
    80.                 UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
    81.                 return finalRGBA;
    82.             }
    83.             ENDCG
    84.         }
    85.     }
    86.     FallBack "Diffuse"
    87. }
    88.  
     
    Fibonaccov and tomekkie2 like this.
  7. Deleted User

    Deleted User

    Guest

    Well, that all took some figuring out, NEW version of my unlit curved shader, support textures with transparency and shadows correctly now, single sided version :

    Code (CSharp):
    1. Shader "playmint/curve unlit/CurveUnlit + Shade + Alpha" {
    2.     Properties {
    3.         _BaseRGBTransA ("Base (RGB) Trans (A)", 2D) = "white" {}
    4.         [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    5.     }
    6.     SubShader {
    7.         Tags {
    8.             "IgnoreProjector"="True"
    9.             "Queue"="Transparent"
    10.             "RenderType"="Transparent"
    11.         }
    12.         Pass {
    13.             Name "FORWARD"
    14.             Tags {
    15.                 "LightMode"="ForwardBase"
    16.             }
    17.             Blend SrcAlpha OneMinusSrcAlpha
    18.             ZWrite Off
    19.          
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             #define UNITY_PASS_FORWARDBASE
    24.             #include "UnityCG.cginc"
    25.             #include "Lighting.cginc"
    26.             #pragma multi_compile_fwdbase
    27.             #pragma only_renderers d3d9 d3d11 glcore gles
    28.             #pragma target 2.0
    29.  
    30.             uniform float2 _BendAmount;
    31.             uniform float3 _BendOrigin;
    32.             uniform float _BendFalloff;
    33.  
    34.             uniform sampler2D _BaseRGBTransA; uniform float4 _BaseRGBTransA_ST;
    35.  
    36.             float4 Curve(float4 v)
    37.             {
    38.                 // Hack : Considerably reduce amount of Bend
    39.                   _BendAmount *= .0001;
    40.                   float4 world = mul(unity_ObjectToWorld, v);
    41.                   float dist = length(world.xz-_BendOrigin.xz);
    42.                   dist = max(0, dist-_BendFalloff);
    43.                   // Distance squared
    44.                   dist = dist*dist;
    45.                   world.xy += dist*_BendAmount;
    46.                   return mul(unity_WorldToObject, world);
    47.             }
    48.  
    49.             struct VertexInput {
    50.                 float4 vertex : POSITION;
    51.                 float2 texcoord0 : TEXCOORD0;
    52.             };
    53.             struct VertexOutput {
    54.                 float4 pos : SV_POSITION;
    55.                 float2 uv0 : TEXCOORD0;
    56.             };
    57.             VertexOutput vert (VertexInput v) {
    58.  
    59.                 v.vertex = Curve(v.vertex);
    60.  
    61.                 VertexOutput o = (VertexOutput)0;
    62.                 o.uv0 = v.texcoord0;
    63.                 o.pos = UnityObjectToClipPos(v.vertex );
    64.                 return o;
    65.             }
    66.             float4 frag(VertexOutput i) : COLOR {
    67.                 float4 _BaseRGBTransA_var = tex2D(_BaseRGBTransA,TRANSFORM_TEX(i.uv0, _BaseRGBTransA));
    68.                 clip(_BaseRGBTransA_var.a - 0.5);
    69. ////// Lighting:
    70.                 float attenuation = 1;
    71. ////// Emissive:
    72.                 float3 emissive = (_BaseRGBTransA_var.rgb*attenuation);
    73.                 float3 finalColor = emissive;
    74.                 return fixed4(finalColor,_BaseRGBTransA_var.a);
    75.             }
    76.             ENDCG
    77.         }
    78.         Pass {
    79.             Name "ShadowCaster"
    80.             Tags {
    81.                 "LightMode"="ShadowCaster"
    82.             }
    83.             Offset 1, 1
    84.             Cull Back
    85.          
    86.             CGPROGRAM
    87.             #pragma vertex vert
    88.             #pragma fragment frag
    89.             #define UNITY_PASS_SHADOWCASTER
    90.             #include "UnityCG.cginc"
    91.             #include "Lighting.cginc"
    92.             #pragma fragmentoption ARB_precision_hint_fastest
    93.             #pragma multi_compile_shadowcaster
    94.             #pragma only_renderers d3d9 d3d11 glcore gles
    95.             #pragma target 2.0
    96.  
    97.             uniform float2 _BendAmount;
    98.             uniform float3 _BendOrigin;
    99.             uniform float _BendFalloff;
    100.  
    101.             uniform sampler2D _BaseRGBTransA; uniform float4 _BaseRGBTransA_ST;
    102.  
    103.             float4 Curve(float4 v)
    104.             {
    105.                 // Hack : Considerably reduce amount of Bend
    106.                   _BendAmount *= .0001;
    107.                   float4 world = mul(unity_ObjectToWorld, v);
    108.                   float dist = length(world.xz-_BendOrigin.xz);
    109.                   dist = max(0, dist-_BendFalloff);
    110.                   // Distance squared
    111.                   dist = dist*dist;
    112.                   world.xy += dist*_BendAmount;
    113.                   return mul(unity_WorldToObject, world);
    114.             }
    115.  
    116.             struct VertexInput {
    117.                 float4 vertex : POSITION;
    118.                 float2 texcoord0 : TEXCOORD0;
    119.             };
    120.             struct VertexOutput {
    121.                 V2F_SHADOW_CASTER;
    122.                 float2 uv0 : TEXCOORD1;
    123.             };
    124.             VertexOutput vert (VertexInput v) {
    125.  
    126.                 v.vertex = Curve(v.vertex);
    127.  
    128.                 VertexOutput o = (VertexOutput)0;
    129.                 o.uv0 = v.texcoord0;
    130.                 o.pos = UnityObjectToClipPos(v.vertex );
    131.                 TRANSFER_SHADOW_CASTER(o)
    132.                 return o;
    133.             }
    134.             float4 frag(VertexOutput i) : COLOR {
    135.                 float4 _BaseRGBTransA_var = tex2D(_BaseRGBTransA,TRANSFORM_TEX(i.uv0, _BaseRGBTransA));
    136.                 clip(_BaseRGBTransA_var.a - 0.5);
    137.                 SHADOW_CASTER_FRAGMENT(i)
    138.             }
    139.             ENDCG
    140.         }
    141.     }
    142.     FallBack "Diffuse"
    143. }
    And, a version that supports double sided :

    Code (CSharp):
    1. Shader "playmint/curve unlit/CurveUnlit + Shade + AlphaDs" {
    2.     Properties {
    3.         _BaseRGBTransA ("Base (RGB) Trans (A)", 2D) = "white" {}
    4.         [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    5.     }
    6.     SubShader {
    7.         Tags {
    8.             "IgnoreProjector"="True"
    9.             "Queue"="Transparent"
    10.             "RenderType"="Transparent"
    11.         }
    12.         Pass {
    13.             Name "FORWARD"
    14.             Tags {
    15.                 "LightMode"="ForwardBase"
    16.             }
    17.             Blend SrcAlpha OneMinusSrcAlpha
    18.             Cull Off
    19.             ZWrite Off
    20.          
    21.             CGPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.             #define UNITY_PASS_FORWARDBASE
    25.             #include "UnityCG.cginc"
    26.             #include "Lighting.cginc"
    27.             #pragma multi_compile_fwdbase
    28.             #pragma only_renderers d3d9 d3d11 glcore gles
    29.             #pragma target 2.0
    30.  
    31.             uniform float2 _BendAmount;
    32.             uniform float3 _BendOrigin;
    33.             uniform float _BendFalloff;
    34.  
    35.             uniform sampler2D _BaseRGBTransA; uniform float4 _BaseRGBTransA_ST;
    36.  
    37.             float4 Curve(float4 v)
    38.             {
    39.                 // Hack : Considerably reduce amount of Bend
    40.                   _BendAmount *= .0001;
    41.                   float4 world = mul(unity_ObjectToWorld, v);
    42.                   float dist = length(world.xz-_BendOrigin.xz);
    43.                   dist = max(0, dist-_BendFalloff);
    44.                   // Distance squared
    45.                   dist = dist*dist;
    46.                   world.xy += dist*_BendAmount;
    47.                   return mul(unity_WorldToObject, world);
    48.             }
    49.  
    50.             struct VertexInput {
    51.                 float4 vertex : POSITION;
    52.                 float2 texcoord0 : TEXCOORD0;
    53.             };
    54.             struct VertexOutput {
    55.                 float4 pos : SV_POSITION;
    56.                 float2 uv0 : TEXCOORD0;
    57.             };
    58.             VertexOutput vert (VertexInput v) {
    59.  
    60.                 v.vertex = Curve(v.vertex);
    61.  
    62.                 VertexOutput o = (VertexOutput)0;
    63.                 o.uv0 = v.texcoord0;
    64.                 o.pos = UnityObjectToClipPos(v.vertex );
    65.                 return o;
    66.             }
    67.             float4 frag(VertexOutput i, float facing : VFACE) : COLOR {
    68.                 float isFrontFace = ( facing >= 0 ? 1 : 0 );
    69.                 float faceSign = ( facing >= 0 ? 1 : -1 );
    70.                 float4 _BaseRGBTransA_var = tex2D(_BaseRGBTransA,TRANSFORM_TEX(i.uv0, _BaseRGBTransA));
    71.                 clip(_BaseRGBTransA_var.a - 0.5);
    72. ////// Lighting:
    73.                 float attenuation = 1;
    74. ////// Emissive:
    75.                 float3 emissive = (_BaseRGBTransA_var.rgb*attenuation);
    76.                 float3 finalColor = emissive;
    77.                 return fixed4(finalColor,_BaseRGBTransA_var.a);
    78.             }
    79.             ENDCG
    80.         }
    81.         Pass {
    82.             Name "ShadowCaster"
    83.             Tags {
    84.                 "LightMode"="ShadowCaster"
    85.             }
    86.             Offset 1, 1
    87.             Cull Off
    88.          
    89.             CGPROGRAM
    90.             #pragma vertex vert
    91.             #pragma fragment frag
    92.             #define UNITY_PASS_SHADOWCASTER
    93.             #include "UnityCG.cginc"
    94.             #include "Lighting.cginc"
    95.             #pragma fragmentoption ARB_precision_hint_fastest
    96.             #pragma multi_compile_shadowcaster
    97.             #pragma only_renderers d3d9 d3d11 glcore gles
    98.             #pragma target 2.0
    99.  
    100.             uniform float2 _BendAmount;
    101.             uniform float3 _BendOrigin;
    102.             uniform float _BendFalloff;
    103.  
    104.             uniform sampler2D _BaseRGBTransA; uniform float4 _BaseRGBTransA_ST;
    105.  
    106.             float4 Curve(float4 v)
    107.             {
    108.                 // Hack : Considerably reduce amount of Bend
    109.                   _BendAmount *= .0001;
    110.                   float4 world = mul(unity_ObjectToWorld, v);
    111.                   float dist = length(world.xz-_BendOrigin.xz);
    112.                   dist = max(0, dist-_BendFalloff);
    113.                   // Distance squared
    114.                   dist = dist*dist;
    115.                   world.xy += dist*_BendAmount;
    116.                   return mul(unity_WorldToObject, world);
    117.             }
    118.  
    119.             struct VertexInput {
    120.                 float4 vertex : POSITION;
    121.                 float2 texcoord0 : TEXCOORD0;
    122.             };
    123.             struct VertexOutput {
    124.                 V2F_SHADOW_CASTER;
    125.                 float2 uv0 : TEXCOORD1;
    126.             };
    127.             VertexOutput vert (VertexInput v) {
    128.  
    129.                 v.vertex = Curve(v.vertex);
    130.  
    131.                 VertexOutput o = (VertexOutput)0;
    132.                 o.uv0 = v.texcoord0;
    133.                 o.pos = UnityObjectToClipPos(v.vertex );
    134.                 TRANSFER_SHADOW_CASTER(o)
    135.                 return o;
    136.             }
    137.             float4 frag(VertexOutput i, float facing : VFACE) : COLOR {
    138.                 float isFrontFace = ( facing >= 0 ? 1 : 0 );
    139.                 float faceSign = ( facing >= 0 ? 1 : -1 );
    140.                 float4 _BaseRGBTransA_var = tex2D(_BaseRGBTransA,TRANSFORM_TEX(i.uv0, _BaseRGBTransA));
    141.                 clip(_BaseRGBTransA_var.a - 0.5);
    142.                 SHADOW_CASTER_FRAGMENT(i)
    143.             }
    144.             ENDCG
    145.         }
    146.     }
    147.     FallBack "Diffuse"
    148. }
    And, lastly, same but with Alpha Cutoff enabled ( Double Sided ) :

    Code (CSharp):
    1. Shader "playmint/curve unlit/CurveUnlit + Shade + AlphaCutDs" {
    2.     Properties {
    3.         _BaseRGB ("Base (RGB)", 2D) = "white" {}
    4.         _Cutoff ("Cutoff", Range(0, 1)) = 0.5
    5.         [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    6.     }
    7.     SubShader {
    8.         Tags {
    9.             "Queue"="AlphaTest"
    10.             "RenderType"="TransparentCutout"
    11.         }
    12.         Pass {
    13.             Name "FORWARD"
    14.             Tags {
    15.                 "LightMode"="ForwardBase"
    16.             }
    17.             Cull Off
    18.          
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.             #define UNITY_PASS_FORWARDBASE
    23.             #include "UnityCG.cginc"
    24.             #include "AutoLight.cginc"
    25.             #include "Lighting.cginc"
    26.             #pragma multi_compile_fwdbase_fullshadows
    27.             #pragma multi_compile_fog
    28.             #pragma only_renderers d3d9 d3d11 glcore gles
    29.             #pragma target 2.0
    30.  
    31.             uniform float2 _BendAmount;
    32.             uniform float3 _BendOrigin;
    33.             uniform float _BendFalloff;
    34.  
    35.             uniform sampler2D _BaseRGB; uniform float4 _BaseRGB_ST;
    36.             uniform float _Cutoff;
    37.  
    38.             float4 Curve(float4 v)
    39.             {
    40.                 // Hack : Considerably reduce amount of Bend
    41.                   _BendAmount *= .0001;
    42.                   float4 world = mul(unity_ObjectToWorld, v);
    43.                   float dist = length(world.xz-_BendOrigin.xz);
    44.                   dist = max(0, dist-_BendFalloff);
    45.                   // Distance squared
    46.                   dist = dist*dist;
    47.                   world.xy += dist*_BendAmount;
    48.                   return mul(unity_WorldToObject, world);
    49.             }
    50.  
    51.             struct VertexInput {
    52.                 float4 vertex : POSITION;
    53.                 float2 texcoord0 : TEXCOORD0;
    54.             };
    55.             struct VertexOutput {
    56.                 float4 pos : SV_POSITION;
    57.                 float2 uv0 : TEXCOORD0;
    58.                 LIGHTING_COORDS(1,2)
    59.                 UNITY_FOG_COORDS(3)
    60.             };
    61.             VertexOutput vert (VertexInput v) {
    62.  
    63.                 v.vertex = Curve(v.vertex);
    64.  
    65.                 VertexOutput o = (VertexOutput)0;
    66.                 o.uv0 = v.texcoord0;
    67.                 o.pos = UnityObjectToClipPos(v.vertex );
    68.                 UNITY_TRANSFER_FOG(o,o.pos);
    69.                 TRANSFER_VERTEX_TO_FRAGMENT(o)
    70.                 return o;
    71.             }
    72.             float4 frag(VertexOutput i, float facing : VFACE) : COLOR {
    73.                 float isFrontFace = ( facing >= 0 ? 1 : 0 );
    74.                 float faceSign = ( facing >= 0 ? 1 : -1 );
    75.                 float4 _BaseRGB_var = tex2D(_BaseRGB,TRANSFORM_TEX(i.uv0, _BaseRGB));
    76.                 clip(((_BaseRGB_var.a+_Cutoff)*_Cutoff) - 0.5);
    77. ////// Lighting:
    78.                 float attenuation = LIGHT_ATTENUATION(i);
    79. ////// Emissive:
    80.                 float3 emissive = (_BaseRGB_var.rgb*attenuation);
    81.                 float3 finalColor = emissive;
    82.                 fixed4 finalRGBA = fixed4(finalColor,1);
    83.                 UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
    84.                 return finalRGBA;
    85.             }
    86.             ENDCG
    87.         }
    88.         Pass {
    89.             Name "ShadowCaster"
    90.             Tags {
    91.                 "LightMode"="ShadowCaster"
    92.             }
    93.             Offset 1, 1
    94.             Cull Off
    95.          
    96.             CGPROGRAM
    97.             #pragma vertex vert
    98.             #pragma fragment frag
    99.             #define UNITY_PASS_SHADOWCASTER
    100.             #include "UnityCG.cginc"
    101.             #include "Lighting.cginc"
    102.             #pragma fragmentoption ARB_precision_hint_fastest
    103.             #pragma multi_compile_shadowcaster
    104.             #pragma multi_compile_fog
    105.             #pragma only_renderers d3d9 d3d11 glcore gles
    106.             #pragma target 2.0
    107.  
    108.             uniform float2 _BendAmount;
    109.             uniform float3 _BendOrigin;
    110.             uniform float _BendFalloff;
    111.  
    112.             uniform sampler2D _BaseRGB; uniform float4 _BaseRGB_ST;
    113.             uniform float _Cutoff;
    114.  
    115.             float4 Curve(float4 v)
    116.             {
    117.                 // Hack : Considerably reduce amount of Bend
    118.                   _BendAmount *= .0001;
    119.                   float4 world = mul(unity_ObjectToWorld, v);
    120.                   float dist = length(world.xz-_BendOrigin.xz);
    121.                   dist = max(0, dist-_BendFalloff);
    122.                   // Distance squared
    123.                   dist = dist*dist;
    124.                   world.xy += dist*_BendAmount;
    125.                   return mul(unity_WorldToObject, world);
    126.             }
    127.  
    128.             struct VertexInput {
    129.                 float4 vertex : POSITION;
    130.                 float2 texcoord0 : TEXCOORD0;
    131.             };
    132.             struct VertexOutput {
    133.                 V2F_SHADOW_CASTER;
    134.                 float2 uv0 : TEXCOORD1;
    135.             };
    136.             VertexOutput vert (VertexInput v) {
    137.  
    138.                 v.vertex = Curve(v.vertex);
    139.  
    140.                 VertexOutput o = (VertexOutput)0;
    141.                 o.uv0 = v.texcoord0;
    142.                 o.pos = UnityObjectToClipPos(v.vertex );
    143.                 TRANSFER_SHADOW_CASTER(o)
    144.                 return o;
    145.             }
    146.             float4 frag(VertexOutput i, float facing : VFACE) : COLOR {
    147.                 float isFrontFace = ( facing >= 0 ? 1 : 0 );
    148.                 float faceSign = ( facing >= 0 ? 1 : -1 );
    149.                 float4 _BaseRGB_var = tex2D(_BaseRGB,TRANSFORM_TEX(i.uv0, _BaseRGB));
    150.                 clip(((_BaseRGB_var.a+_Cutoff)*_Cutoff) - 0.5);
    151.                 SHADOW_CASTER_FRAGMENT(i)
    152.             }
    153.             ENDCG
    154.         }
    155.     }
    156.     FallBack "Diffuse"
    157. }
    would seem a bit remiss if I didn't post the control script :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class curveController : MonoBehaviour
    6. {
    7.     private float currentCurvatureX;
    8.     private float newCurvatureX;
    9.     private float currentCurvatureY;
    10.     private float newCurvatureY;
    11.  
    12.     [Header("Time Before Curvature Begins")]
    13.     public float timeToWait = 30f;
    14.  
    15.     [Header("Time Between Curvature Changes")]
    16.     public float timeInterval = 30f;
    17.  
    18.     [Header("Curve Origin - Main Camera")]
    19.     public Transform CurveOrigin;
    20.  
    21.     [Header("Curvature")]
    22.  
    23.     [Range(-500f, 500f)]
    24.     [SerializeField]
    25.     float curvatureX = 0f;
    26.  
    27.     [Range(-500f, 500f)]
    28.     [SerializeField]
    29.     float curvatureY = 0f;
    30.  
    31.     [Range(0f, 50f)]
    32.     [SerializeField]
    33.     [Header("Falloff Effect")]
    34.     float falloff = 0f;
    35.  
    36.     private Vector2 bendAmount = Vector2.zero;
    37.  
    38.     // Global shader property ids
    39.     private int bendAmountId;
    40.     private int bendOriginId;
    41.     private int bendFalloffId;
    42.  
    43.     void Start ()
    44.     {
    45.         bendAmountId = Shader.PropertyToID("_BendAmount");
    46.         bendOriginId = Shader.PropertyToID("_BendOrigin");
    47.         bendFalloffId = Shader.PropertyToID("_BendFalloff");
    48.  
    49.         // Make sure we start with default X and Y curvature
    50.         curvatureX = 0f;
    51.         curvatureY = 0f;
    52.  
    53.         StartCoroutine(InitiateCoroutinesInOrder());
    54.     }
    55.  
    56.     void Update ()
    57.     {
    58.         bendAmount.x=curvatureX;
    59.         bendAmount.y=curvatureY;
    60.  
    61.         Shader.SetGlobalVector(bendAmountId, bendAmount);
    62.         Shader.SetGlobalVector(bendOriginId, CurveOrigin.position);
    63.         Shader.SetGlobalFloat(bendFalloffId, falloff);
    64.     }
    65.  
    66.     IEnumerator InitiateCoroutinesInOrder () {
    67.  
    68.         yield return StartCoroutine(waitUntilStart());
    69.         yield return StartCoroutine(changePathCurvature());
    70.     }
    71.  
    72.     IEnumerator waitUntilStart() {
    73.  
    74.         yield return new WaitForSeconds(timeToWait);
    75.     }
    76.  
    77.     IEnumerator changePathCurvature() {
    78.         // Keep repeating
    79.         while(true) {
    80.          
    81.             // currentCurvatureX - Get current value of CurvatureX
    82.             float currentCurvatureX = curvatureX;
    83.             // newCurvatureX = Random within a range of -50 and 50
    84.             float newCurvatureX = (Random.Range(-50.0f, 50.0f));
    85.  
    86.             // currentCurvatureY - Get current value of CurvatureY
    87.             float currentCurvatureY = curvatureY;
    88.             // newCurvatureY = Random within a range of -50 and 50
    89.             float newCurvatureY = (Random.Range(-50.0f, 50.0f));
    90.  
    91.             // t = reference to Time
    92.             float t = 0;
    93.  
    94.             // t = 0, if t is less than or equal to timeInterval, then increase t until we reach TimeInterval
    95.  
    96.             for(t = 0; t <= timeInterval; t+=Time.deltaTime) {
    97.                 curvatureX = Mathf.Lerp(currentCurvatureX, newCurvatureX, t / timeInterval);
    98.                 curvatureY = Mathf.Lerp(currentCurvatureY, newCurvatureY, t / timeInterval);
    99.                 yield return null;
    100.             }
    101.         }
    102.     }
    103. }
     
    Last edited by a moderator: Jun 2, 2017
    Fibonaccov likes this.
  8. davitsedrakian

    davitsedrakian

    Joined:
    Jun 23, 2018
    Posts:
    30
    Hey man! Can you help me with mixing 2 my shaders, curved and low poly water?