diff --git a/Assets/measuregeneric/shaders/InteriorReflectionShader.shader b/Assets/measuregeneric/shaders/InteriorReflectionShader.shader index c1b7487..840f72f 100644 --- a/Assets/measuregeneric/shaders/InteriorReflectionShader.shader +++ b/Assets/measuregeneric/shaders/InteriorReflectionShader.shader @@ -40,10 +40,30 @@ Shader "Custom/InteriorReflectionShader" return o; } + // ChatGPT snippet + float3 BoxProjectReflectionDir(float3 worldPos, float3 worldRefl) + { + // Calculate relative position within the box + float3 boxMin = unity_SpecCube0_BoxMin; + float3 boxMax = unity_SpecCube0_BoxMax; + float3 boxCenter = 0.5 * (boxMin + boxMax); + float3 boxSize = boxMax - boxMin; + + float3 localPos = worldPos - boxCenter; + float3 absLocalRefl = abs(worldRefl); + + // Scale the reflection vector based on which side of the box it hits + float3 scale = boxSize / absLocalRefl; + float scaleMin = min(min(scale.x, scale.y), scale.z); + return localPos + worldRefl * scaleMin; + } + fixed4 frag (v2f i) : SV_Target { - half3 worldViewDir = -normalize(UnityWorldSpaceViewDir(i.worldPos)); // Direction of ray from the camera towards the object surface - half4 skyData = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, worldViewDir); + half3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos)); // Direction of ray from the camera towards the object surface + half3 reflection = reflect(-worldViewDir, i.worldNormal); // Direction of ray after hitting the surface of object + reflection = BoxProjectReflectionDir(i.worldPos, reflection); + half4 skyData = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, reflection); half3 skyColor = DecodeHDR(skyData, unity_SpecCube0_HDR); // This is done becasue the cubemap is stored HDR return half4(skyColor, 1.0); }