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

Decal Shader issue

Discussion in 'VR' started by wightwhale, Nov 12, 2018.

  1. wightwhale

    wightwhale

    Joined:
    Jul 28, 2011
    Posts:
    397
    I'm having trouble getting this shader code to run on the Hololens. I can't seem to find any error messages or warnings as to why it's not working. Does anyone know what has to be converted to fun a shader on the hololens?

    Code (CSharp):
    1.  
    2. // MIT License - Chris Wade, 2017
    3.  
    4. Shader "Custom/ForwardDecal"
    5. {
    6.     Properties
    7.     {
    8.         _Color ("Tint", Color) = (1, 1, 1, 1)
    9.         _MainTex ("Diffuse", 2D) = "white" {}
    10.         //_BumpMap ("Bump Map", 2D) = "bump" {}
    11.     }
    12.     SubShader
    13.     {
    14.         Tags{"RenderType" = "Transparent" "Queue" = "Transparent" "DisableBatching"="True"}
    15.         LOD 200
    16.  
    17.         ZWrite Off
    18.         Blend SrcAlpha OneMinusSrcAlpha
    19.  
    20.         // Some things you won't want to project on. We need to use the Stencil buffer
    21.         // The Stencil Buffer associates each drawn fragment with a number, then lets us reference that in later renders of the same fragment
    22.        
    23.         // First, add this to the shaders of the things you don't want to project onto:
    24.         /*
    25.         //Stencil
    26.         //{
    27.             //Ref 1
    28.             //Comp Always
    29. //            Pass Replace
    30.         //}
    31.         */
    32.  
    33.         // Then uncomment this here so this shader knows to reject things
    34.         /*
    35.         //Stencil
    36.         //{
    37.             //Ref 0
    38. //            Comp Equal
    39.         //}
    40.         */
    41.  
    42.         Pass
    43.         {
    44.             CGPROGRAM
    45.             #pragma vertex vert
    46.             #pragma fragment frag
    47.             #pragma multi_compile_instancing
    48.            
    49.             #pragma target 5.0
    50.             #pragma only_renderers d3d11
    51.  
    52.             //#include "HoloToolkitCommon.cginc"
    53.             #include "UnityCG.cginc"
    54.             #include "UnityStandardUtils.cginc"
    55.  
    56.             struct appdata
    57.             {
    58.                 float4 vertex : POSITION;
    59.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    60.             };
    61.  
    62.             struct v2f
    63.             {
    64.                 float4 pos : POSITION;
    65.                 float4 localPos : TEXCOORD0;
    66.                 float4 screenUV : TEXCOORD1;
    67.                 UNITY_VERTEX_OUTPUT_STEREO
    68.             };
    69.  
    70.             v2f vert(appdata v)
    71.             {
    72.                 v2f o;
    73.                 UNITY_SETUP_INSTANCE_ID(v);
    74.                 UNITY_TRANSFER_INSTANCE_ID(v, o);
    75.  
    76.                 o.pos = UnityObjectToClipPos(v.vertex);
    77.                 o.localPos = v.vertex;
    78.  
    79.                 o.screenUV = ComputeScreenPos(o.pos);
    80.                 //TRANSFER_VERTEX_TO_FRAGMENT(o);
    81.                 return o;
    82.             }
    83.  
    84.             sampler2D _MainTex;
    85.             sampler2D _MaskTex;
    86.             //sampler2D _BumpMap;
    87.  
    88.             sampler2D_float _CameraDepthTexture;
    89.             sampler2D_float _CameraDepthNormalsTexture;
    90.             sampler2D _NormalsCopy;
    91.  
    92.             fixed4 _Color;
    93.             float4 _LightColor0;
    94.  
    95.             fixed4 frag(v2f i) : SV_Target
    96.             {
    97.                 UNITY_SETUP_INSTANCE_ID(i);
    98.  
    99.                 // Get view space vector toward this fragment
    100.                 //float3 ray = mul(UNITY_MATRIX_MV, i.localPos).xyz * float3(-1, -1, 1);
    101.                 float3 ray = mul(UNITY_MATRIX_MV, i.localPos).xyz * float3(-1, -1, 1);
    102.                 ray *= (_ProjectionParams.z / ray.z); // Far clip dist/viewspace distance
    103.  
    104.                 float2 screenUV = i.screenUV.xy / i.screenUV.w;
    105.                 float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, screenUV);
    106.                 depth = Linear01Depth(depth);
    107.  
    108.                 float4 vpos = float4(ray * depth, 1); // View space projection onto surface in depth buffer
    109.                 float3 wpos = mul(unity_CameraToWorld, vpos).xyz; // Transform to world space
    110.                 float3 opos = mul(unity_WorldToObject, float4(wpos, 1)).xyz; // Transform to object space
    111.  
    112.                 clip(float3(0.5, 0.5, 0.5) - abs(opos.xyz)); // Clip any fragments outside our box
    113.  
    114.                 // Next we want to clip any surfaces not facing us
    115.                 float4 dn = tex2D(_CameraDepthNormalsTexture, screenUV);
    116.                 float3 normal = DecodeViewNormalStereo(dn) * float3(1, 1, -1);
    117.                 float3 worldN = mul((half3x3)unity_CameraToWorld, normal); // Transform normals to world space
    118.  
    119.                 float3 orientation = mul(unity_ObjectToWorld, float3(0, 1, 0)); // Transform objects up to world space
    120.                 clip(dot(worldN, orientation) + 0.2); // Clip anything that's not facing mostly up
    121.  
    122.                 float2 texUV = opos.xz + 0.5; // Treat surface projection's object space xz position as UVs (use z instead of y because we're using object's y as the projection direction)
    123.                 fixed4 col = tex2D(_MainTex, texUV) * _Color;
    124.  
    125.                 float3 orientationX = mul((float3x3)unity_ObjectToWorld, float3(1, 0, 0));
    126.                 float3 orientationZ = mul((float3x3)unity_ObjectToWorld, float3(0, 0, 1));
    127.                 half3x3 norMat = half3x3(orientationX, orientationZ, orientation);
    128.                 //normal = UnpackNormal(tex2D(_BumpMap, texUV));
    129.                 //normal = normal * 0.5 + 0.5;
    130.                
    131.                 float3 viewDir = normalize(_WorldSpaceCameraPos - wpos);
    132.  
    133.                 float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
    134.                 float3 diffuse = _LightColor0.rgb * col.rgb
    135.                     * saturate(dot((worldN + normal) * 0.5, lightDir) * 2) + 0.1;
    136.  
    137.                 return float4(diffuse, col.a);
    138.             }
    139.             ENDCG
    140.         }
    141.     }
    142.     FallBack "Diffuse"
    143. }
    144.  
     
  2. wightwhale

    wightwhale

    Joined:
    Jul 28, 2011
    Posts:
    397
    It would appear
    clip(float3(0.5, 0.5, 0.5) - abs(opos.xyz)); // Clip any fragments outside our box
    isn't working on the hololens.