Search Unity

Converting volumeric shader to LWRP

Discussion in 'Shaders' started by lowbl_dan, Apr 23, 2019.

  1. lowbl_dan

    lowbl_dan

    Joined:
    Jan 22, 2019
    Posts:
    34
    Hi all, I'm currently facing some problems trying to convert my volumeric shader to LWRP. My shader reads in a 3D texture as a .asset file that is pre-generated by reading in pixels from a series of dicom files and uses raymarching to sample and render what I need.

    Below is an image of the supposed effect after moving out from UnityCG.cginc into Core.hlsl
    The left side of the picture shows the volume without the custom render pipeline applied.
    The volume is totally not rendered in the LWRP.

    Note: I'm currently using 2019.1.0f2
    raymarching.png

    Some snippets of the code
    Code (csharp):
    1.  
    2. SubShader{
    3.             Tags { "RenderType" = "Transparent" }
    4.             LOD 200
    5.             Blend One OneMinusSrcAlpha // Premultiplied transparency
    6.             ZWrite Off
    7.             Pass
    8.             {
    9.                 HLSLPROGRAM
    10.                 #pragma prefer_hlslcc gles
    11.                 #pragma vertex vert
    12.                 #pragma fragment frag
    13.            
    14.                  #include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Core.hlsl"
    15.  
    16.                 v2f vert(appdata v)
    17.                 {
    18.                     v2f o;
    19.                     o.local = v.vertex;
    20.                     o.vertex = mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, v.vertex));
    21.                     //o.vertex = UnityObjectToClipPos(v.vertex);
    22.                     o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    23.                     o.screenPos = ComputeGrabScreenPos(o.vertex);
    24.                     return o;
    25.                 }
    26.  
    27.     half4 frag(v2f i) : SV_Target
    28.      {    
    29.          float4 outColor = float4(0.0,0.0,0.0,1.0);
    30.           float eyeDepth = LinearEyeDepth(tex2Dproj(_CameraDepthTexture, i.screenPos), _ZBufferParams);
    31.           //other variables  here. Most of the functions used here are not from UnityCG.cginc
    32.  
    33.            [loop]
    34.            for (int i = 0; i < iterations; i++)
    35.            {
    36.                //raymarching operations are done here to modify outColor ...
    37.  
    38.             }
    39.  
    40.          
    41.            return outColor;
    42.      }
    43. ENDHLSL
    44. }
    45.  
    Most of the functions were substituted with the ones from common.HLSL with the exception of the following functions from UnityCG.cginc where i redefined them because I couldnt find the appropriate substitute.

    Code (csharp):
    1.  
    2.                 inline float4 ComputeGrabScreenPos(float4 pos)
    3.                 {
    4.                     #if UNITY_UV_STARTS_AT_TOP
    5.                     float scale = -1.0;
    6.                     #else
    7.                     float scale = 1.0;
    8.                     #endif
    9.                     float4 o = pos * 0.5f;
    10.                     o.xy = float2(o.x, o.y*scale) + o.w;
    11.                     #ifdef UNITY_SINGLE_PASS_STEREO
    12.                     o.xy = TransformStereoScreenSpaceTex(o.xy, pos.w);
    13.                     #endif
    14.                     o.zw = pos.zw;
    15.                     return o;
    16.                 }
    17.                 // Computes object space view direction
    18.                 inline float3 ObjSpaceViewDir(in float4 v)
    19.                 {
    20.                     float3 objSpaceCameraPos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos.xyz, 1)).xyz;
    21.                     return objSpaceCameraPos - v.xyz;
    22.                 }
    23.                 // Tranforms position from object to camera space
    24.                 inline float3 UnityObjectToViewPos(in float3 pos)
    25.                 {
    26.                     return mul(UNITY_MATRIX_V, mul(unity_ObjectToWorld, float4(pos, 1.0))).xyz;
    27.                 }
    28.  
    29.  
    30.  
    Some of the things I've done to attempt to convert the shader.
    1. I've tested the rendering of a simple 3D texture without raymarching and it renders fine in LWRP.
    2. All CGProgram and functions replaced with HLSLProgram
    3. I also tried to do this in shadergraph but I'm not sure how to go about implementing a custom node for n iteration raymarching,

    I would appreciate it if some of the shader experts would enlighten me on this subject as I'm still new to LWRP. Thanks!
     
    Last edited: Apr 23, 2019
  2. lowbl_dan

    lowbl_dan

    Joined:
    Jan 22, 2019
    Posts:
    34
    I forgot to include

    Code (csharp):
    1.  
    2.  Tags{ "RenderPipeline" = "LightweightPipeline" }
    3.  
    now its working! Thanks for viewing.