From c93b0a00ba6ea6cc8a0b4685fb5de6c11e5aa4ad Mon Sep 17 00:00:00 2001 From: raccoon Date: Thu, 7 Nov 2024 21:25:07 +0500 Subject: [PATCH] Better solution --- .../shaders/InteriorReflectionShader.shader | 50 +++++-------------- 1 file changed, 13 insertions(+), 37 deletions(-) diff --git a/Assets/measuregeneric/shaders/InteriorReflectionShader.shader b/Assets/measuregeneric/shaders/InteriorReflectionShader.shader index 490ff85..c1b7487 100644 --- a/Assets/measuregeneric/shaders/InteriorReflectionShader.shader +++ b/Assets/measuregeneric/shaders/InteriorReflectionShader.shader @@ -1,15 +1,14 @@ +// Original code from https://gist.github.com/josephbk117/a43f5335cea9b3e12a44f196dc81f30f Shader "Custom/InteriorReflectionShader" { Properties { - _ReflectionIntensity ("Reflection Intensity", Range(0, 1)) = 1.0 + // TODO: Implement me! + _Opacity("Opacity", Range(0.0, 1.0)) = 1.0 } - + SubShader { - Tags { "RenderType"="Opaque" } - LOD 300 - Pass { CGPROGRAM @@ -17,10 +16,7 @@ Shader "Custom/InteriorReflectionShader" #pragma fragment frag #include "UnityCG.cginc" - float _ReflectionIntensity; - - // Use built-in cubemap sampler (unity_SpecCube0) for reflection probe - // UNITY_DECLARE_TEXCUBE(unity_SpecCube0); + float _Opacity; // TODO: Implement me! struct appdata { @@ -28,11 +24,11 @@ Shader "Custom/InteriorReflectionShader" float3 normal : NORMAL; }; - struct v2f + struct v2f { - float4 pos : SV_POSITION; float3 worldPos : TEXCOORD0; float3 worldNormal : TEXCOORD1; + float4 pos : SV_POSITION; }; v2f vert (appdata v) @@ -40,38 +36,18 @@ Shader "Custom/InteriorReflectionShader" v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; - o.worldNormal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal)); + o.worldNormal = UnityObjectToWorldNormal(v.normal); return o; } - half3 ComputeBoxProjectedReflection(float3 worldPos, float3 worldNormal) + fixed4 frag (v2f i) : SV_Target { - // Calculate direction from the object to the reflection probe position - float3 probeToPixel = worldPos - unity_SpecCube0_ProbePosition; - - // Calculate inverse box scale manually - float3 boxSize = unity_SpecCube0_BoxMax - unity_SpecCube0_BoxMin; - float3 invBoxSize = 1.0 / boxSize; - - // Transform the position into the box projection space - float3 localPos = probeToPixel * invBoxSize + 0.5; - - // Calculate reflection direction - float3 reflectionDir = normalize(worldPos - _WorldSpaceCameraPos); - - // Sample the cubemap using the adjusted reflection direction - return UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, reflectionDir).rgb; - } - - half4 frag (v2f i) : SV_Target - { - // Calculate reflection using box projection - half3 reflectionColor = ComputeBoxProjectedReflection(i.worldPos, i.worldNormal); - return half4(reflectionColor, 1.0); + 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 skyColor = DecodeHDR(skyData, unity_SpecCube0_HDR); // This is done becasue the cubemap is stored HDR + return half4(skyColor, 1.0); } ENDCG } } - - FallBack "Diffuse" }