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 Unable to convert legacy shader to shader graph

Discussion in 'Shader Graph' started by X-BLaZeKiLL-X, Aug 16, 2021.

  1. X-BLaZeKiLL-X

    X-BLaZeKiLL-X

    Joined:
    Apr 9, 2013
    Posts:
    6
    I have this shader which is working in the default render pipeline. basically i am generating ao values for runtime generated blocky voxel mesh and the ao values are passed to the shader as uv1. in the vertex shader the ao value is nomalized and the 4 ao values of the face are passed to surf per vertex where the ao value is interpolated according to uv and applied to albedo

    Code (Shader):
    1. Shader "VloxyEngine/Lit/VertexColorAO" {
    2.     Properties {
    3.         _Glossiness ("Smoothness", Range(0,1)) = 0.0
    4.         _Metallic ("Metallic", Range(0,1)) = 0.0
    5.         _AOColor ("AO Color", Color) = (0,0,0,1)
    6.         _AOCurve ("AO Curve", Vector) = (0.75, 0.825, 0.9, 1.0)
    7.         _AOIntensity ("AO Intensity", Range(0, 1)) = 1.0
    8.         _AOPower ("AO Power", Range(0, 1)) = 1.0
    9.     }
    10.     SubShader {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 200
    13.        
    14.         CGPROGRAM
    15.         #pragma surface surf Standard fullforwardshadows
    16.         #pragma vertex vert
    17.         #pragma target 3.0
    18.  
    19.         struct Input {
    20.             float4 color : COLOR;
    21.             float2 aocoords;
    22.             float4 aovector;
    23.         };
    24.  
    25.         half _Glossiness;
    26.         half _Metallic;
    27.        
    28.         half4 _AOColor;
    29.         float4 _AOCurve;
    30.         float _AOIntensity;
    31.         float _AOPower;
    32.  
    33.         float compute_ao(float index) {
    34.             return pow(_AOCurve[index] * _AOIntensity, _AOPower);
    35.         }
    36.        
    37.         void vert (inout appdata_full v, out Input o) {
    38.             UNITY_INITIALIZE_OUTPUT(Input, o);
    39.             o.aocoords = v.texcoord.xy;
    40.             o.aovector = float4(compute_ao(v.texcoord1.x), compute_ao(v.texcoord1.y), compute_ao(v.texcoord1.z), compute_ao(v.texcoord1.w));
    41.         }
    42.        
    43.         void surf (Input IN, inout SurfaceOutputStandard o) {
    44.             float ao1 = lerp(IN.aovector.x, IN.aovector.z, IN.aocoords.x);
    45.             float ao2 = lerp(IN.aovector.y, IN.aovector.w, IN.aocoords.x);
    46.             float ao = lerp(ao1, ao2, IN.aocoords.y);
    47.        
    48.             o.Albedo = lerp(_AOColor.rgb, IN.color.rgb, ao);
    49.             o.Metallic = _Metallic;
    50.             o.Smoothness = _Glossiness;
    51.             o.Alpha = IN.color.a;
    52.         }
    53.        
    54.         ENDCG
    55.     }
    56.     FallBack "Diffuse"
    57. }
    Now I tried converting this to shader graph and i don't understand how to get somthing like the Input struct above, basically pass custom data from vert to surf. Shader graph does have bock context for vertex and fragment shader but i am limited to the types of nodes permitted in those contexts. is there any way I can pass custom data ?

    for now i have created this graph, where compute_ao is a subgraph with a custom function node.



    Any help would be appriciated
     
  2. Sinterklaas

    Sinterklaas

    Joined:
    Jun 6, 2018
    Posts:
    93
    I don't think there's a way to pass custom data to vertex / fragment nodes. You're pretty much stuck with the vertex / fragment nodes provided by Shader Graph.

    That being said, have you tried to use the lit material option? With this option, the fragment node has inputs for albedo (base color), alpha, smoothness and metallic, which seems to be what you need in this case? You can change it under graph settings > universal > material. Don't forget to make sure that the workflow option below is set to metallic.
     
  3. X-BLaZeKiLL-X

    X-BLaZeKiLL-X

    Joined:
    Apr 9, 2013
    Posts:
    6
    I am using a lit shader I just removed the nodes I wasn't using.
    if there is no way to pass custom data then I guess I have only 2 options
    1. Do the vertex shader calculations (compute_ao) in C# while mesh generation and pass it directly to shader circumventing the need to pass custom v2f data (but this may slow down overall mesh generations as these calculations will happen much faster on the GPU)
    2. Write a URP shader, I have seen most people discourage this option and for good reason
    Anyway thanks for the help