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.

Question Box projection data wiped when selecting refleciton probe

Discussion in 'Shaders' started by djancool, Jul 12, 2021.

  1. djancool

    djancool

    Joined:
    May 1, 2016
    Posts:
    6
    I am trying to make a shader that gets box projected reflection from a reflection probe in URP.

    if i compile my shader it works but as soon as i select the reflection probe it looks like the BoxMin, BoxMax and the ProbePosition just get wiped. it stays like this until i recompile my shader

    Any help would be greatly apriciated!

    upload_2021-7-12_16-44-58.png
    upload_2021-7-12_16-45-11.png

    Code (CSharp):
    1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    2.  
    3. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    4.  
    5. Shader "Unlit/Djanco/BoxProjection"
    6. {
    7.     Properties
    8.     {
    9.         //_MainTex ("Texture", 2D) = "white" {}
    10.         //_ReflPos ("Probe Position", float) = (0,0,0,0)
    11.     }
    12.     SubShader
    13.     {
    14.         Tags { "RenderType"="Opaque" }
    15.         LOD 100
    16.  
    17.         Pass
    18.         {
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma target 4.0
    22.             #pragma fragment frag
    23.             // make fog work
    24.             #pragma multi_compile_fog
    25.  
    26.             #define UNITY_SPECCUBE_BLENDING
    27.             #define UNITY_SPECCUBE_BOX_PROJECTION
    28.             #define UNITY_ENABLE_REFLECTION_BUFFERS
    29.             #define UNITY_SPECCUBE_BOX_PROJECTION
    30.  
    31.             #include "UnityCG.cginc"
    32.  
    33.             struct appdata
    34.             {
    35.                 float4 vertex : POSITION;
    36.                 float2 uv : TEXCOORD0;
    37.                 float3 normal : NORMAL;
    38.             };
    39.  
    40.             struct v2f
    41.             {
    42.                 float2 uv : TEXCOORD0;
    43.                 float4 vertex : SV_POSITION;
    44.                 float3 viewDir : TEXCOORD3;
    45.                 float4 worldPos : TEXCOORD4;
    46.                 float3 normal : TEXCOORD5;
    47.             };
    48.  
    49.             sampler2D _MainTex;
    50.             float4 _MainTex_ST;
    51.             float3 _ReflPos;
    52.  
    53.             v2f vert (appdata v)
    54.             {
    55.                 v2f o;
    56.                 o.worldPos = mul (unity_ObjectToWorld, v.vertex);
    57.                 o.vertex = UnityObjectToClipPos(v.vertex);
    58.                 o.viewDir = WorldSpaceViewDir(o.worldPos);
    59.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    60.                 o.normal = v.normal;
    61.                 return o;
    62.             }
    63.  
    64.  
    65.  
    66.             float3 BoxProjection (float3 direction, float3 position, float3 cubemapPosition, float3 boxMin, float3 boxMax)
    67.             {
    68.                 float3 factors = ((direction > 0 ? boxMax : boxMin) - position) / direction;
    69.                 float scalar = min(min(factors.x, factors.y), factors.z);
    70.                 return direction * scalar + (position - cubemapPosition);
    71.             }
    72.  
    73.  
    74.             fixed4 frag (v2f i) : SV_Target
    75.             {  
    76.                 float3 worldNormal = mul( unity_ObjectToWorld, float4( i.normal, 0.0 ) ).xyz;
    77.  
    78.                 float4 worldPos = i.worldPos;
    79.                 float3 WorldSpaceViewDirection = _WorldSpaceCameraPos.xyz - worldPos;
    80.                 WorldSpaceViewDirection *= abs(worldNormal) * 2 - 1;
    81.  
    82.                 float3 boxMin = unity_SpecCube0_BoxMin;
    83.                 float3 boxMax = unity_SpecCube0_BoxMax;
    84.                 float3 probePos = unity_SpecCube0_ProbePosition;
    85.  
    86.                 float3 projection = BoxProjection(WorldSpaceViewDirection, worldPos, probePos, boxMin, boxMax);
    87.  
    88.                 half4 skyData = UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, projection, float4(0,0,0,0));
    89.  
    90.                 return skyData;
    91.             }
    92.             ENDCG
    93.         }
    94.     }
    95. }
    96.  
     
  2. djancool

    djancool

    Joined:
    May 1, 2016
    Posts:
    6
    Updating unity to LTS 2020 fixes this problem