Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Trouble with URP projector component not appearing on objects with custom shader

Discussion in 'Universal Render Pipeline' started by benritchie, May 11, 2022.

  1. benritchie

    benritchie

    Joined:
    Feb 5, 2021
    Posts:
    8
    I am wanting to use the unity URP projector component with objects that have a custom shader applied.

    I'm not really sure what to do, ive been trying to find a solution for a few days now.
    any help is appreciated :)
     
  2. AdamLacko

    AdamLacko

    Joined:
    Oct 10, 2017
    Posts:
    41
    Does your shader write into depth buffer? In what render queue is it rendered?
     
  3. benritchie

    benritchie

    Joined:
    Feb 5, 2021
    Posts:
    8
    yes it does write to the z buffer and the render is the Tag - "RenderType" = "Opaque"
     
  4. AdamLacko

    AdamLacko

    Joined:
    Oct 10, 2017
    Posts:
    41
    Alright, just to make things clear - you want your custom shader to be influenced by the decal, right? So you have objects in a scene with materials with custom shader and you want the projector to work on those surfaces, is that correct?

    Or is it that you want to use custom shader in the projector itself?
     
  5. benritchie

    benritchie

    Joined:
    Feb 5, 2021
    Posts:
    8
    I want the projector to work on the surfaces of the objects with custom shaders
     
  6. AdamLacko

    AdamLacko

    Joined:
    Oct 10, 2017
    Posts:
    41
    Well from my experience, it usually boils down to render queue. I use projector (as fog of war) on many custom shaders (even got it to work with transparents, which isn't supported natively), so it definitely does work.

    However, you will have to provide a bit more details about what you're doing for me to be able to help you. For starters, post your shader here (at least the tag section of it). And perhaps a screenshot of what your setup looks like.
     
  7. benritchie

    benritchie

    Joined:
    Feb 5, 2021
    Posts:
    8
    Code (CSharp):
    1. Shader "Custom/Texture/Affine/snap"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.         _Albeado("Albeado", color) = (1,1,1,1)
    7.         _Accuracy("Vertex Accuracy",Range(0,1000)) = 100
    8.         _AmbientColour("Ambient colour", color) = (0,0,0,0)
    9.         [MaterialToggle]_GlossUse("Use Gloss", Float) = 1
    10.         _Gloss("Gloss", Float) = 1
    11.     }
    12.         SubShader
    13.         {
    14.             Tags { "RenderType" = "Opaque"}
    15.  
    16.             Zwrite On
    17.             Cull Back
    18.  
    19.             Pass
    20.             {
    21.                 CGPROGRAM
    22.                 #pragma vertex vert
    23.                 #pragma fragment frag
    24.  
    25.                 #include "UnityCG.cginc"
    26.                 #include "Lighting.cginc"
    27.  
    28.                 struct appdata
    29.                 {
    30.                     float4 vertex : POSITION;
    31.                     float2 uv : TEXCOORD0;
    32.                     half4 Vcolour : COLOR0;
    33.                     float3 normal : NORMAL;
    34.                 };
    35.  
    36.                 struct v2f
    37.                 {
    38.                     noperspective float2 uv : TEXCOORD0; //KeyWord: noperspective - ignore perspective
    39.                     float4 vertex : SV_POSITION;
    40.                     half4 Vertcolour : COLOR0;
    41.                     float3 normal : TEXCOORD1;
    42.                     float3 worldPos : TEXCOORD2;
    43.                 };
    44.  
    45.                 sampler2D _MainTex;
    46.                 float4 _MainTex_ST;
    47.                 float4 _Albeado;
    48.                 float _Accuracy;
    49.                 float4 _AmbientColour;
    50.                 float _LightColourUse;
    51.                 float _Gloss;
    52.                 float _GlossUse;
    53.  
    54.                 float2 Posterize(float steps, float value)
    55.                 {
    56.                     value = floor(value * steps) / steps;
    57.                     return value;
    58.                 }
    59.  
    60.                 v2f vert(appdata v)
    61.                 {
    62.                     v2f o;
    63.                     o.vertex = UnityObjectToClipPos(v.vertex);
    64.  
    65.                     float4 vertexPos = o.vertex;
    66.                     vertexPos.xyz = o.vertex.xyz / o.vertex.w;
    67.                     vertexPos.x = Posterize(_Accuracy, vertexPos.x);
    68.                     vertexPos.y = Posterize(_Accuracy, vertexPos.y);
    69.                     vertexPos.xyz *= o.vertex.w;
    70.                     o.vertex = vertexPos;
    71.                  
    72.                     o.worldPos = mul(unity_ObjectToWorld, v.vertex);
    73.  
    74.                     o.Vertcolour = v.Vcolour;
    75.  
    76.                     o.normal = v.normal;
    77.                     o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    78.                     return o;
    79.                 }
    80.  
    81.                 float4 frag(v2f i) : SV_Target
    82.                 {
    83.                     //decleration of uv and normal(world space).
    84.                     float2 uv = i.uv;
    85.                     float3 normal = normalize(i.normal);
    86.                     float3 worldSpaceNormal = UnityObjectToWorldNormal(normal.xyz);
    87.                     normal = normalize(worldSpaceNormal);
    88.  
    89.                     //LightLevel
    90.                     float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
    91.                     float lightFalloff = max(0, dot(lightDir, normal));
    92.  
    93.                     //Light Colour
    94.                     float3 lightColour = lerp(float3(1, 1, 1), _LightColor0.rgb, _LightColourUse);
    95.                     float3 directDiffuse = lightFalloff * lightColour;
    96.                     float3 diffuse = _AmbientColour.rgb + directDiffuse;
    97.  
    98.                     //Specular
    99.                     float3 fragToCam = _WorldSpaceCameraPos - i.worldPos;
    100.                     float3 viewDir = normalize(fragToCam);
    101.                     float3 reflectNormal = reflect(-viewDir, normal);
    102.                     float specularFalloff = max(0, dot(lightDir, reflectNormal));
    103.                     specularFalloff = pow(specularFalloff, _Gloss);
    104.                     float3 specular = specularFalloff * lightColour;
    105.                     specular = specular * _GlossUse;
    106.  
    107.                     //finalise and output
    108.                     float3 UnderColour = tex2D(_MainTex, i.uv) * _Albeado * i.Vertcolour;
    109.                     float3 FinalColour = diffuse * UnderColour + specular;
    110.                     return float4(FinalColour, 0);
    111.                 }
    112.                 ENDCG
    113.             }
    114.         }
    115. }
     
  8. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,133
    Custom shaders need to explicitly incorporate decals into their shading through the
    ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData); function. https://github.com/Unity-Technologi...nes.universal/ShaderLibrary/DBuffer.hlsl#L191

    The shader itself also needs to have #pragma multi_compile_fragment _ _DBUFFER_MRT1 _DBUFFER_MRT2 _DBUFFER_MRT3 added to the ForwardLit and GBuffer passes

    But the shader you posted isn't written for URP, so it won't render to begin with :p
     
  9. benritchie

    benritchie

    Joined:
    Feb 5, 2021
    Posts:
    8
    Im a bit confused:(, the shader does render with urp and im not sure how the apply decal function works.
    Screenshot 2022-05-12 095306.png
     

    Attached Files:

  10. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,133
    Interesting, it shouldn't o_O

    You won't be able to use the function that applies decals without first converting the shader to URP entirely, which will probably be more work than it's worth.

    The snapping of the vertices (PSX-style) can most likely be done in Shader Graph instead. That's worth trying out first. If so, you won't have to deal the other 95% that goes into writing a fully functional URP shader.
     
  11. benritchie

    benritchie

    Joined:
    Feb 5, 2021
    Posts:
    8
    Hmm, Thanks for the help, i will give shader graph a try and maybe look into making it actually urp code. :)