Search Unity

Combine Two Shaders

Discussion in 'Shaders' started by DarkHorseTrolls, Oct 14, 2019.

  1. DarkHorseTrolls

    DarkHorseTrolls

    Joined:
    Aug 29, 2019
    Posts:
    13
    Hello,
    I am trying to combine two shaders for my point cloud mesh.
    1st shader is a geometry shader which has 1 texture and a way to control the point size
    2nd shader blends multiple textures beased on the height of point in the mesh.
    I am very very new to shader coding and would like to combine the two.
    If i literally copy paste them both into one file, then blending does not work. I would like to blend the textures and control the point cloud size.
    I am very lost and would really appreciate any help.

    1st shader
    Code (CSharp):
    1. Shader "Custom/TerrainShaderAll"{
    2.     Properties{
    3.         _DeepWater("DeepWater", 2D) = "white" {}
    4.         _ShallowWater("ShallowWater", 2D) = "white" {}
    5.         _Sand("Sand", 2D) = "white" {}
    6.         _Grass("Grass", 2D) = "white"{}
    7.         _Tree("Tree", 2D) = "white" {}
    8.         _Rock("Rock", 2D) = "white" {}
    9.         _Snow("Snow", 2D) = "white" {}
    10.         _e1("e1", 2D) = "white" {}
    11.         _e2("e2", 2D) = "white" {}
    12.         _e3("e3", 2D) = "white" {}
    13.         _WaterLevel("Water Level", Float) = 0
    14.         _LayerSize("LayerSize", Float) = 20
    15.         _BlendRange("BlendRange", Range(0,1.5)) = 0.1
    16.     }
    17.         Subshader{
    18.             Pass {
    19.                 CGPROGRAM
    20.                 #pragma vertex vert
    21.                 #pragma fragment frag
    22.                 #include "UnityCG.cginc"
    23.  
    24.                 uniform sampler2D _DeepWater;
    25.                 uniform sampler2D _ShallowWater;
    26.                 uniform sampler2D _Sand;
    27.                 uniform sampler2D _Grass;
    28.                 uniform sampler2D _Tree;
    29.                 uniform sampler2D _Rock;
    30.                 uniform sampler2D _Snow;
    31.                 uniform sampler2D _e1;
    32.                 uniform sampler2D _e2;
    33.                 uniform sampler2D _e3;
    34.                
    35.  
    36.                 uniform float _WaterLevel;
    37.                 uniform float _LayerSize;
    38.                 uniform float _BlendRange;
    39.  
    40.                 struct fragmentInput {
    41.                     float4 pos : SV_POSITION;
    42.                     float4 texcoord : TEXCOORD0;
    43.                     float4 blend: COLOR;
    44.                 };
    45.  
    46.                 fragmentInput vert(appdata_base v)
    47.                 {
    48.                     float NumOfTextures = 10;
    49.                     fragmentInput o;
    50.                     o.pos = UnityObjectToClipPos(v.vertex);
    51.                     o.texcoord = v.texcoord;
    52.  
    53.                     float MinValue = _WaterLevel - (NumOfTextures - 1) * _LayerSize;
    54.                     float MaxValue = (_WaterLevel + _LayerSize);
    55.                     float Blend = MaxValue - v.vertex.z;
    56.                     Blend = clamp(Blend / (NumOfTextures * _LayerSize), 0, 1);
    57.  
    58.                     o.blend.xyz = 0;
    59.                     o.blend.w = Blend;
    60.                     return o;
    61.                 }
    62.                
    63.  
    64.                 inline float CalculateBlend(float TextureFloat)
    65.                 {
    66.                     return 1 - clamp((1 - TextureFloat) / _BlendRange, 0, 1);
    67.                 }
    68.  
    69.                 inline float4 DoBlending(float TextureID, float TextureFloat, fixed4 BaseTexture, fixed4 BlendTexture)
    70.                 {
    71.                     float Blend = CalculateBlend(clamp(TextureFloat - TextureID, 0, 1));
    72.                     return lerp(BaseTexture, BlendTexture, Blend);
    73.                 }
    74.  
    75.                 float4 frag(fragmentInput i) : COLOR0
    76.                 {
    77.                     float NumOfTextures = 10;
    78.                     float TextureFloat = i.blend.w * NumOfTextures;
    79.  
    80.                     if (TextureFloat < 1)
    81.                     {
    82.                         fixed4 DeepWaterColor = tex2D(_DeepWater, i.texcoord);
    83.                         fixed4 ShallowWaterColor = tex2D(_ShallowWater, i.texcoord);
    84.  
    85.                         return DoBlending(0, TextureFloat, DeepWaterColor, ShallowWaterColor);
    86.                     }
    87.                     if (TextureFloat < 2)
    88.                     {
    89.                         fixed4 ShallowWaterColor = tex2D(_ShallowWater, i.texcoord);
    90.                         fixed4 SandColor = tex2D(_Sand, i.texcoord);
    91.  
    92.                         return DoBlending(1, TextureFloat, ShallowWaterColor, SandColor);
    93.                     }
    94.                     if (TextureFloat < 3)
    95.                     {
    96.                         fixed4 SandColor = tex2D(_Sand, i.texcoord);
    97.                         fixed4 GrassColor = tex2D(_Grass, i.texcoord);
    98.  
    99.                         return DoBlending(2, TextureFloat, SandColor, GrassColor);
    100.                     }
    101.                     if (TextureFloat < 4)
    102.                     {
    103.                         fixed4 GrassColor = tex2D(_Grass, i.texcoord);
    104.                         fixed4 TreeColor = tex2D(_Tree, i.texcoord);
    105.  
    106.                         return DoBlending(3, TextureFloat, GrassColor, TreeColor);
    107.                     }
    108.                     if (TextureFloat < 5)
    109.                     {
    110.                         fixed4 TreeColor = tex2D(_Tree, i.texcoord);
    111.                         fixed4 RockColor = tex2D(_Rock, i.texcoord);
    112.  
    113.                         return DoBlending(4, TextureFloat, TreeColor, RockColor);
    114.                     }
    115.                     if (TextureFloat < 6)
    116.                     {
    117.                         fixed4 RockColor = tex2D(_Rock, i.texcoord);
    118.                         fixed4 SnowColor = tex2D(_Snow, i.texcoord);
    119.  
    120.                         return DoBlending(5, TextureFloat, RockColor, SnowColor);
    121.                     }
    122.                     if (TextureFloat < 7)
    123.                     {
    124.                         fixed4 SnowColor = tex2D(_Snow, i.texcoord);
    125.                         fixed4 e1Color = tex2D(_e1, i.texcoord);
    126.  
    127.                         return DoBlending(6, TextureFloat, SnowColor, e1Color);
    128.                     }
    129.                     if (TextureFloat < 8)
    130.                     {
    131.                         fixed4 e1Color = tex2D(_e1, i.texcoord);
    132.                         fixed4 e2Color = tex2D(_e2, i.texcoord);
    133.  
    134.                         return DoBlending(7, TextureFloat, e1Color, e2Color);
    135.                     }
    136.                     if (TextureFloat < 9)
    137.                     {
    138.                         fixed4 e2Color = tex2D(_e2, i.texcoord);
    139.                         fixed4 e3Color = tex2D(_e3, i.texcoord);
    140.  
    141.                         return DoBlending(8, TextureFloat, e2Color, e3Color);
    142.                     }
    143.  
    144.                     fixed4 e3Color = tex2D(_e3, i.texcoord);
    145.                    
    146.                     return e3Color;
    147.  
    148.                     fixed4 DeepWaterColor = tex2D(_DeepWater, i.texcoord);
    149.                     fixed4 ShallowWaterColor = tex2D(_ShallowWater, i.texcoord);
    150.  
    151.                     return lerp(DeepWaterColor, ShallowWaterColor, i.blend.w);
    152.  
    153.                 }
    154.             ENDCG
    155.             }
    156.         }
    157.     FallBack "Diffuse"
    158. }
    2nd Shader
    Code (CSharp):
    1. Shader "Custom/PointCloudGeom" {
    2.     Properties {
    3.         [NoScaleOffset]_MainTex ("Texture", 2D) = "white" {}
    4.         [NoScaleOffset]_UVMap ("UV", 2D) = "white" {}
    5.         _PointSize("Point Size", Float) = 4.0
    6.         _Color ("PointCloud Color", Color) = (1, 1, 1, 1)
    7.         [Toggle(USE_DISTANCE)]_UseDistance ("Scale by distance?", float) = 0
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Cull Off
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma geometry geom
    18.             #pragma fragment frag
    19.             #pragma shader_feature USE_DISTANCE
    20.             #include "UnityCG.cginc"
    21.  
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.  
    28.             struct v2f
    29.             {
    30.                 float4 vertex : SV_POSITION;
    31.                 float2 uv : TEXCOORD0;
    32.             };
    33.  
    34.             float _PointSize;
    35.             fixed4 _Color;
    36.  
    37.             sampler2D _MainTex;
    38.             float4 _MainTex_TexelSize;
    39.  
    40.             sampler2D _UVMap;
    41.             float4 _UVMap_TexelSize;
    42.  
    43.  
    44.             struct g2f
    45.             {
    46.                 float4 vertex : SV_POSITION;
    47.                 float2 uv : TEXCOORD0;
    48.             };
    49.  
    50.             [maxvertexcount(4)]
    51.             void geom(point v2f i[1], inout TriangleStream<g2f> triStream)
    52.             {
    53.                 g2f o;
    54.                 float4 v = i[0].vertex;
    55.                 v.y = -v.y;
    56.  
    57.                 // TODO: interpolate uvs on quad
    58.                 float2 uv = i[0].uv;
    59.                 float2 p = _PointSize * 0.001;
    60.                 p.y *= _ScreenParams.x / _ScreenParams.y;
    61.                
    62.                 o.vertex = UnityObjectToClipPos(v);
    63.                 #ifdef USE_DISTANCE
    64.                 o.vertex += float4(-p.x, p.y, 0, 0);
    65.                 #else
    66.                 o.vertex += float4(-p.x, p.y, 0, 0) * o.vertex.w;
    67.                 #endif
    68.                 o.uv = uv;
    69.                 triStream.Append(o);
    70.  
    71.                 o.vertex = UnityObjectToClipPos(v);
    72.                 #ifdef USE_DISTANCE
    73.                 o.vertex += float4(-p.x, -p.y, 0, 0);
    74.                 #else
    75.                 o.vertex += float4(-p.x, -p.y, 0, 0) * o.vertex.w;
    76.                 #endif
    77.                 o.uv = uv;
    78.                 triStream.Append(o);
    79.  
    80.                 o.vertex = UnityObjectToClipPos(v);
    81.                 #ifdef USE_DISTANCE
    82.                 o.vertex += float4(p.x, p.y, 0, 0);
    83.                 #else
    84.                 o.vertex += float4(p.x, p.y, 0, 0) * o.vertex.w;
    85.                 #endif
    86.                 o.uv = uv;
    87.                 triStream.Append(o);
    88.  
    89.                 o.vertex = UnityObjectToClipPos(v);
    90.                 #ifdef USE_DISTANCE
    91.                 o.vertex += float4(p.x, -p.y, 0, 0);
    92.                 #else
    93.                 o.vertex += float4(p.x, -p.y, 0, 0) * o.vertex.w;
    94.                 #endif
    95.                 o.uv = uv;
    96.                 triStream.Append(o);
    97.  
    98.             }
    99.             v2f vert (appdata v)
    100.             {
    101.                 v2f o;
    102.                 o.vertex = v.vertex;
    103.                 o.uv = v.uv;
    104.                 return o;
    105.             }
    106.  
    107.             fixed4 frag (g2f i) : SV_Target
    108.             {
    109.                 float2 uv = tex2D(_UVMap, i.uv);
    110.                 if(any(uv <= 0 || uv >= 1))
    111.                     discard;
    112.                 // offset to pixel center
    113.                 uv += 0.5 * _MainTex_TexelSize.xy;
    114.                 return tex2D(_MainTex, uv) * _Color;
    115.             }
    116.             ENDCG
    117.         }
    118.     }
    119. }
    120.