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

Question Help to convert shader to shader graph

Discussion in 'Shader Graph' started by saifshk17, Sep 15, 2020.

  1. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    I need help to convert the shaders given below to shader graph in Unity. Can someone help me with this?

    Code (CSharp):
    1. Shader "Wireframe/WireframeTransparentCulled"
    2. {
    3.     Properties
    4.     {
    5.         _WireThickness ("Wire Thickness", RANGE(0, 800)) = 100
    6.         _WireSmoothness ("Wire Smoothness", RANGE(0, 20)) = 3
    7.         [HDR]_WireColor ("Wire Color", Color) = (0.0, 1.0, 0.0, 1.0)
    8.         [HDR]_BaseColor ("Base Color", Color) = (0.0, 0.0, 0.0, 0.0)
    9.         _MaxTriSize ("Max Tri Size", RANGE(0, 200)) = 25
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags {
    15.             "IgnoreProjector"="True"
    16.             "Queue"="Transparent"
    17.             "RenderType"="Transparent"
    18.         }
    19.  
    20.         Pass
    21.         {
    22.             Blend SrcAlpha OneMinusSrcAlpha
    23.             ZWrite Off
    24.             Cull Back
    25.  
    26.             CGPROGRAM
    27.             #pragma vertex vert
    28.             #pragma geometry geom
    29.             #pragma fragment frag
    30.  
    31.             #include "UnityCG.cginc"
    32.             #include "Wireframe.cginc"
    33.  
    34.             ENDCG
    35.         }
    36.     }
    37. }
    38.  

    Wireframe.cginc
    Code (CSharp):
    1.  
    2. uniform float _WireThickness = 100;
    3. uniform float _WireSmoothness = 3;
    4. uniform float4 _WireColor = float4(0.0, 1.0, 0.0, 1.0);
    5. uniform float4 _BaseColor = float4(0.0, 0.0, 0.0, 0.0);
    6. uniform float _MaxTriSize = 25.0;
    7.  
    8. struct appdata
    9. {
    10.     float4 vertex : POSITION;
    11.     UNITY_VERTEX_INPUT_INSTANCE_ID
    12. };
    13.  
    14. struct v2g
    15. {
    16.     float4 projectionSpaceVertex : SV_POSITION;
    17.     float4 worldSpacePosition : TEXCOORD1;
    18.     UNITY_VERTEX_OUTPUT_STEREO
    19. };
    20.  
    21. struct g2f
    22. {
    23.     float4 projectionSpaceVertex : SV_POSITION;
    24.     float4 worldSpacePosition : TEXCOORD0;
    25.     float4 dist : TEXCOORD1;
    26.     float4 area : TEXCOORD2;
    27.     UNITY_VERTEX_OUTPUT_STEREO
    28. };
    29.  
    30. v2g vert (appdata v)
    31. {
    32.     v2g o;
    33.     UNITY_SETUP_INSTANCE_ID(v);
    34.     UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    35.     o.projectionSpaceVertex = UnityObjectToClipPos(v.vertex);
    36.     o.worldSpacePosition = mul(unity_ObjectToWorld, v.vertex);
    37.     return o;
    38. }
    39.  
    40. [maxvertexcount(3)]
    41. void geom(triangle v2g i[3], inout TriangleStream<g2f> triangleStream)
    42. {
    43.     float2 p0 = i[0].projectionSpaceVertex.xy / i[0].projectionSpaceVertex.w;
    44.     float2 p1 = i[1].projectionSpaceVertex.xy / i[1].projectionSpaceVertex.w;
    45.     float2 p2 = i[2].projectionSpaceVertex.xy / i[2].projectionSpaceVertex.w;
    46.  
    47.     float2 edge0 = p2 - p1;
    48.     float2 edge1 = p2 - p0;
    49.     float2 edge2 = p1 - p0;
    50.  
    51.     float4 worldEdge0 = i[0].worldSpacePosition - i[1].worldSpacePosition;
    52.     float4 worldEdge1 = i[1].worldSpacePosition - i[2].worldSpacePosition;
    53.     float4 worldEdge2 = i[0].worldSpacePosition - i[2].worldSpacePosition;
    54.  
    55.     // To find the distance to the opposite edge, we take the
    56.     // formula for finding the area of a triangle Area = Base/2 * Height,
    57.     // and solve for the Height = (Area * 2)/Base.
    58.     // We can get the area of a triangle by taking its cross product
    59.     // divided by 2.  However we can avoid dividing our area/base by 2
    60.     // since our cross product will already be double our area.
    61.     float area = abs(edge1.x * edge2.y - edge1.y * edge2.x);
    62.     float wireThickness = 800 - _WireThickness;
    63.  
    64.     g2f o;
    65.  
    66.     o.area = float4(0, 0, 0, 0);
    67.     o.area.x = max(length(worldEdge0), max(length(worldEdge1), length(worldEdge2)));
    68.  
    69.     o.worldSpacePosition = i[0].worldSpacePosition;
    70.     o.projectionSpaceVertex = i[0].projectionSpaceVertex;
    71.     o.dist.xyz = float3( (area / length(edge0)), 0.0, 0.0) * o.projectionSpaceVertex.w * wireThickness;
    72.     o.dist.w = 1.0 / o.projectionSpaceVertex.w;
    73.     UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[0], o);
    74.     triangleStream.Append(o);
    75.  
    76.     o.worldSpacePosition = i[1].worldSpacePosition;
    77.     o.projectionSpaceVertex = i[1].projectionSpaceVertex;
    78.     o.dist.xyz = float3(0.0, (area / length(edge1)), 0.0) * o.projectionSpaceVertex.w * wireThickness;
    79.     o.dist.w = 1.0 / o.projectionSpaceVertex.w;
    80.     UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[1], o);
    81.     triangleStream.Append(o);
    82.  
    83.     o.worldSpacePosition = i[2].worldSpacePosition;
    84.     o.projectionSpaceVertex = i[2].projectionSpaceVertex;
    85.     o.dist.xyz = float3(0.0, 0.0, (area / length(edge2))) * o.projectionSpaceVertex.w * wireThickness;
    86.     o.dist.w = 1.0 / o.projectionSpaceVertex.w;
    87.     UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[2], o);
    88.     triangleStream.Append(o);
    89. }
    90.  
    91. fixed4 frag(g2f i) : SV_Target
    92. {
    93.     float minDistanceToEdge = min(i.dist[0], min(i.dist[1], i.dist[2])) * i.dist[3];
    94.  
    95.     // Early out if we know we are not on a line segment.
    96.     if(minDistanceToEdge > 0.9 || i.area.x > _MaxTriSize)
    97.     {
    98.         return fixed4(_BaseColor.rgb,0);
    99.     }
    100.  
    101.     // Smooth our line out
    102.     float t = exp2(_WireSmoothness * -1.0 * minDistanceToEdge * minDistanceToEdge);
    103.     fixed4 finalColor = lerp(_BaseColor, _WireColor, t);
    104.  
    105.     return finalColor;
    106. }
     
  2. DearUnityPleaseAddSerializableDictionaries

    DearUnityPleaseAddSerializableDictionaries

    Joined:
    Sep 12, 2014
    Posts:
    135
    Did you find a way to do this
     
  3. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @EGA Shader Graph does not support geometry shaders.