Search Unity

Help Combining 2 Shaders into 1

Discussion in 'Shaders' started by MehDaegan, Jul 14, 2019.

  1. MehDaegan

    MehDaegan

    Joined:
    Jul 14, 2019
    Posts:
    1
    Bit lost on how to combine these. I'm not great at writing my own shaders. I want to make wireframe waves on a plane. But I have no idea how to combine it, if there is a way. Any help would be greatly appreciated. Apologies for odd format, first time posting.

    Wireframe Shader:

    Code (CSharp):
    1. Shader "SuperSystems/Wireframe-Shaded-Unlit"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("MainTex", 2D) = "white" {}
    6.         _WireThickness ("Wire Thickness", RANGE(0, 800)) = 100
    7.         _WireSmoothness ("Wire Smoothness", RANGE(0, 20)) = 3
    8.         _WireColor ("Wire Color", Color) = (0.0, 1.0, 0.0, 1.0)
    9.         _BaseColor ("Base Color", Color) = (0.0, 0.0, 0.0, 1.0)
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags {
    15.             "RenderType"="Opaque"
    16.         }
    17.  
    18.         Pass
    19.         {
    20.             // Wireframe shader based on the the following
    21.             // http://developer.download.nvidia.com/SDK/10/direct3d/Source/SolidWireframe/Doc/SolidWireframe.pdf
    22.  
    23.             CGPROGRAM
    24.             #pragma vertex vert
    25.             #pragma geometry geom
    26.             #pragma fragment frag
    27.  
    28.             #include "UnityCG.cginc"
    29.  
    30.             uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
    31.             uniform float _WireThickness;
    32.             uniform float _WireSmoothness;
    33.             uniform float4 _WireColor;
    34.             uniform float4 _BaseColor;
    35.  
    36.             struct appdata
    37.             {
    38.                 float4 vertex : POSITION;
    39.                 float2 texcoord0 : TEXCOORD0;
    40.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    41.             };
    42.  
    43.             struct v2g
    44.             {
    45.                 float4 projectionSpaceVertex : SV_POSITION;
    46.                 float2 uv0 : TEXCOORD0;
    47.                 float4 worldSpacePosition : TEXCOORD1;
    48.                 UNITY_VERTEX_OUTPUT_STEREO
    49.             };
    50.  
    51.             struct g2f
    52.             {
    53.                 float4 projectionSpaceVertex : SV_POSITION;
    54.                 float2 uv0 : TEXCOORD0;
    55.                 float4 worldSpacePosition : TEXCOORD1;
    56.                 float4 dist : TEXCOORD2;
    57.                 UNITY_VERTEX_OUTPUT_STEREO
    58.             };
    59.            
    60.             v2g vert (appdata v)
    61.             {
    62.                 v2g o;
    63.                 UNITY_SETUP_INSTANCE_ID(v);
    64.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    65.                 o.projectionSpaceVertex = UnityObjectToClipPos(v.vertex);
    66.                 o.worldSpacePosition = mul(unity_ObjectToWorld, v.vertex);
    67.                 o.uv0 = TRANSFORM_TEX(v.texcoord0, _MainTex);
    68.                 return o;
    69.             }
    70.            
    71.             [maxvertexcount(3)]
    72.             void geom(triangle v2g i[3], inout TriangleStream<g2f> triangleStream)
    73.             {
    74.                 float2 p0 = i[0].projectionSpaceVertex.xy / i[0].projectionSpaceVertex.w;
    75.                 float2 p1 = i[1].projectionSpaceVertex.xy / i[1].projectionSpaceVertex.w;
    76.                 float2 p2 = i[2].projectionSpaceVertex.xy / i[2].projectionSpaceVertex.w;
    77.  
    78.                 float2 edge0 = p2 - p1;
    79.                 float2 edge1 = p2 - p0;
    80.                 float2 edge2 = p1 - p0;
    81.  
    82.                 // To find the distance to the opposite edge, we take the
    83.                 // formula for finding the area of a triangle Area = Base/2 * Height,
    84.                 // and solve for the Height = (Area * 2)/Base.
    85.                 // We can get the area of a triangle by taking its cross product
    86.                 // divided by 2.  However we can avoid dividing our area/base by 2
    87.                 // since our cross product will already be double our area.
    88.                 float area = abs(edge1.x * edge2.y - edge1.y * edge2.x);
    89.                 float wireThickness = 800 - _WireThickness;
    90.  
    91.                 g2f o;
    92.                
    93.                 o.uv0 = i[0].uv0;
    94.                 o.worldSpacePosition = i[0].worldSpacePosition;
    95.                 o.projectionSpaceVertex = i[0].projectionSpaceVertex;
    96.                 o.dist.xyz = float3( (area / length(edge0)), 0.0, 0.0) * o.projectionSpaceVertex.w * wireThickness;
    97.                 o.dist.w = 1.0 / o.projectionSpaceVertex.w;
    98.                 UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[0], o);
    99.                 triangleStream.Append(o);
    100.  
    101.                 o.uv0 = i[1].uv0;
    102.                 o.worldSpacePosition = i[1].worldSpacePosition;
    103.                 o.projectionSpaceVertex = i[1].projectionSpaceVertex;
    104.                 o.dist.xyz = float3(0.0, (area / length(edge1)), 0.0) * o.projectionSpaceVertex.w * wireThickness;
    105.                 o.dist.w = 1.0 / o.projectionSpaceVertex.w;
    106.                 UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[1], o);
    107.                 triangleStream.Append(o);
    108.  
    109.                 o.uv0 = i[2].uv0;
    110.                 o.worldSpacePosition = i[2].worldSpacePosition;
    111.                 o.projectionSpaceVertex = i[2].projectionSpaceVertex;
    112.                 o.dist.xyz = float3(0.0, 0.0, (area / length(edge2))) * o.projectionSpaceVertex.w * wireThickness;
    113.                 o.dist.w = 1.0 / o.projectionSpaceVertex.w;
    114.                 UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[2], o);
    115.                 triangleStream.Append(o);
    116.             }
    117.  
    118.             fixed4 frag (g2f i) : SV_Target
    119.             {
    120.                 float minDistanceToEdge = min(i.dist[0], min(i.dist[1], i.dist[2])) * i.dist[3];
    121.  
    122.                 float4 baseColor = _BaseColor * tex2D(_MainTex, i.uv0);
    123.  
    124.                 // Early out if we know we are not on a line segment.
    125.                 if(minDistanceToEdge > 0.9)
    126.                 {
    127.                     return fixed4(baseColor.rgb,0);
    128.                 }
    129.  
    130.                 // Smooth our line out
    131.                 float t = exp2(_WireSmoothness * -1.0 * minDistanceToEdge * minDistanceToEdge);
    132.                 fixed4 finalColor = lerp(baseColor, _WireColor, t);
    133.                 finalColor.a = t;
    134.  
    135.                 return finalColor;
    136.             }
    137.             ENDCG
    138.         }
    139.     }
    140. }
    141.  

    Wave Shader

    Code (CSharp):
    1. Shader "Custom/PlanToWave" {
    2.     Properties {
    3.         _Color ("Color", Color) = (0, 0, 0, 1)
    4.         _Amplitude ("Amplitude", Range(0,4)) = 1.0
    5.         _Movement ("Movement", Range(-100,100)) = 0
    6.     }
    7.  
    8.     SubShader {
    9.         Tags { "RenderType"="transparent" }
    10.        
    11.         Pass
    12.         {
    13.             Cull Off
    14.  
    15.             CGPROGRAM
    16.  
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.  
    20.             float4 _Color;
    21.             float _Amplitude;
    22.             float _Movement;
    23.  
    24.             struct vertexInput
    25.             {
    26.                 float4 vertex : POSITION;
    27.             };
    28.        
    29.             struct vertexOutput
    30.             {
    31.                 float4 pos : SV_POSITION;
    32.             };
    33.  
    34.             vertexOutput vert(vertexInput input)
    35.             {
    36. [B]                [/B]float4x4 Matrice = unity_ObjectToWorld;
    37.                 vertexOutput output;
    38.  
    39.                 float4 posWorld = mul(Matrice, input.vertex);
    40.  
    41.                 float displacement = (cos(posWorld.y) + cos(posWorld.x + _Movement * _Time));
    42.                 posWorld.y = posWorld.y + _Amplitude * displacement;
    43.  
    44.                 output.pos = mul(UNITY_MATRIX_VP, posWorld);
    45.                 return output;
    46.             }
    47.  
    48.             float4 frag(vertexOutput input) : COLOR
    49.             {
    50.                 return _Color;
    51.             }
    52.             ENDCG
    53.         }
    54.     }
    55.     FallBack "Diffuse"
    56. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,994
    Try to make an attempt to combine them yourself. We can help you out with the problems you run into, but otherwise we're generally not going to do the work for you.