Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with true mirror like reflections with a cubemap

Discussion in 'Shaders' started by ryand-unity, Mar 11, 2022.

  1. ryand-unity

    ryand-unity

    Joined:
    Jan 21, 2011
    Posts:
    547
    Im trying to make a true mirror with a cubemap. is there a way to make a shader that will give me a flat mirror like reflection with a cubemap. i need the cubemap to zoom out as you back away from the mirror in stead of zooming in.
     
  2. ryand-unity

    ryand-unity

    Joined:
    Jan 21, 2011
    Posts:
    547
    this is what i have to start with simple reflect shader
    Code (CSharp):
    1. Shader "Mirror"
    2. {
    3.     Properties{
    4.     _Color("Main Color", Color) = (1,1,1,1)
    5.     _ReflectColor("Reflection Color", Color) = (1,1,1,0.5)
    6.     _MainTex("Base (RGB) RefStrength (A)", 2D) = "white" {}
    7.     _Cube("Reflection Cubemap", Cube) = "_Skybox" { }
    8.     _BumpMap("Normalmap", 2D) = "bump" {}
    9.     }
    10.  
    11.         SubShader{
    12.             Tags { "RenderType" = "Opaque" }
    13.             LOD 300
    14.  
    15.         CGPROGRAM
    16.         #pragma surface surf Lambert
    17.  
    18.         sampler2D _MainTex;
    19.         sampler2D _BumpMap;
    20.         samplerCUBE _Cube;
    21.  
    22.         fixed4 _Color;
    23.         fixed4 _ReflectColor;
    24.  
    25.         struct Input {
    26.             float2 uv_MainTex;
    27.             float2 uv_BumpMap;
    28.             float3 worldRefl;
    29.             INTERNAL_DATA
    30.         };
    31.  
    32.         void surf(Input IN, inout SurfaceOutput o) {
    33.             fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    34.             fixed4 c = tex * _Color;
    35.             o.Albedo = c.rgb;
    36.  
    37.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    38.  
    39.             float3 worldRefl = WorldReflectionVector(IN, o.Normal);
    40.             fixed4 reflcol = texCUBE(_Cube, worldRefl);
    41.             reflcol *= tex.a;
    42.             o.Emission = reflcol.rgb * _ReflectColor.rgb;
    43.             o.Alpha = reflcol.a * _ReflectColor.a;
    44.         }
    45.         ENDCG
    46.     }
    47.  
    48. }
     
  3. mabulous

    mabulous

    Joined:
    Jan 4, 2013
    Posts:
    198
    If you want to get a nice reflection on a flat surface using a cubemap, you must create a reflection probe, enable https://docs.unity3d.com/ScriptReference/ReflectionProbe-boxProjection.html and adjust the Box Size and Box Offset to create a rough approximation of the scene depicted by your cubemap and then sample this reflection probe in your custom shader (because otherwise the sides of the cube are infinitely far away and the flat surface will reflect only a tiny part of the cubemap). How to do so you can learn here: https://www.bitshiftprogrammer.com/2018/11/reflection-probe-custom-shader-tutorial.html
     
    Last edited: Mar 14, 2022
    ryand-unity likes this.