Search Unity

How Can I Get UV(TEXCOORD) by Mirrored Camera in Reflection Effect( inverted image) on Water?

Discussion in 'Shaders' started by Dr-Game, Sep 16, 2019.

  1. Dr-Game

    Dr-Game

    Joined:
    Mar 12, 2015
    Posts:
    161
    How Can I Get UV(TEXCOORD) by Mirrored Camera in Reflection Effect( inverted image) on Water?
    Here is the Script to Mirror Camera by Water Plane

    Code (CSharp):
    1. public class MirrorCamera : MonoBehaviour
    2. {
    3.     public Camera original_camera;
    4.     public Camera target_camera;
    5.     Vector3 eye;
    6.     Vector3 up;
    7.     Vector3 right;
    8.     Vector3 lookat;
    9.     Vector3 m;
    10.     Matrix4x4 matrix = new Matrix4x4();
    11.     void Start()
    12.     {
    13.      
    14.     }
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         print(target_camera.transform.forward);
    19.         //RaycastHit hitpoint;
    20.         //Physics.Raycast(original_camera.transform.position, original_camera.transform.forward, out hitpoint);
    21.         eye = GetMirrorPoint(this.gameObject.transform.up, original_camera.transform.position, this.gameObject.transform.position);
    22.         up = GetMirrorPoint(this.gameObject.transform.up, original_camera.transform.up, this.gameObject.transform.position);
    23.         lookat = GetMirrorPoint(this.gameObject.transform.up, original_camera.transform.forward, this.gameObject.transform.position);
    24.         Vector3 z  = lookat.normalized;
    25.         Vector3 x = Vector3.Cross(up.normalized, z);
    26.         Vector3 y = Vector3.Cross(z, x);
    27.  
    28.         matrix.SetColumn(0, x);
    29.         matrix.SetColumn(1, y);
    30.         matrix.SetColumn(2, z);
    31.         matrix.SetColumn(3, new Vector4(eye.x, eye.y, eye.z, 1));
    32.         print(matrix);
    33.         target_camera.transform.position = eye;
    34.         target_camera.transform.rotation = matrix.rotation;
    35.         Shader.SetGlobalMatrix("_MirrorCameraMatrix", matrix.inverse);
    36.     }
    37.     Vector3 GetMirrorPoint(Vector3 pn, Vector3 cp, Vector3 pp)
    38.     {
    39.         Vector3 d1 = cp - Vector3.ProjectOnPlane(cp, pn);
    40.         Vector3 d2 = pp - Vector3.ProjectOnPlane(pp,pn);
    41.         float direct=  Vector3.Dot(d1, pn) * Vector3.Dot(d2, pn);
    42.         Vector3 d0 ;
    43.  
    44.         if (Vector3.Dot(pn, d1) > 0)
    45.         {
    46.             if (d1.magnitude - d2.magnitude > 0)
    47.             {
    48.                 d0 = d1 - d2;
    49.                 m = cp - 2 * d0.magnitude * pn;
    50.             }
    51.             else
    52.             {
    53.                 d0 = d2 - d1;
    54.                 m = cp + 2 * d0.magnitude * pn;
    55.             }
    56.         }
    57.         else
    58.         {
    59.             if (d1.magnitude - d2.magnitude > 0)
    60.             {
    61.                 d0 = d1 - d2;
    62.                 m = cp + 2 * d0.magnitude * pn;
    63.             }
    64.             else
    65.             {
    66.                 d0 = d2 - d1;
    67.                 m = cp - 2 * d0.magnitude * pn;
    68.             }
    69.         }
    70.         return m;
    71.     }
    72. }
    the reflection Water Shader is below:

    Code (CSharp):
    1. Properties
    2.     {
    3.         _ReflectMap("Reflect Map", 2D) = "white" {}
    4.         [Header(BaseColor)]
    5.         _DepthGradientShallow("Depth Gradient Shallow", Color) = (0.325, 0.807, 0.971, 0.725)
    6.         _DepthGradientDeep("Depth Gradient Deep", Color) = (0.086, 0.407, 1, 0.749)
    7.         _DepthMaxDistance("Depth Maximum Distance", Float) = 1
    8.  
    9.         [Header(Wave)]
    10.         _WaveLen("WaveLen", range(0.0,3.2)) = 1.0
    11.         _Speed("Speed", range(0.0,10.0)) = 1.0
    12.         _Height("Height", range(0.0,1.0)) = 0.2
    13.         _deepcolor("deepcolor", range(0.0,1.0)) = 0.47
    14.         _horizontal_thrill("horizontal_thrill", range(0.0,1.0)) = 0.3
    15.         _Z_Offset("Z-Offset", range(-1.0,1.0)) = 0.4
    16.  
    17.         [Header(Caustics)]
    18.         _CausticsColor("Caustics Color", Color) = (1,1,1,1)
    19.         _CausticsMap("Caustics Map", 2D) = "white" {}
    20.         _CausticsCutoff("Caustics Cutoff", Range(0, 1)) = 0.777
    21.         _CausticsScroll_X("Caustics Scroll X",Float) = 0.03
    22.         _CausticsScroll_Y("Caustics Scroll Y",Float) = 0.03
    23.         _CausticsDistortion("Caustics Distortion", 2D) = "white" {}
    24.         _CausticsDistortionAmount("Caustics Distortion Amount", Range(0, 1)) = 0.27
    25.  
    26.         [Header(Foam)]
    27.         _FoamColor("Foam Color", Color) = (1,1,1,1)
    28.         _SurfaceNoise("Surface Noise", 2D) = "white" {}
    29.         _SurfaceNoiseCutoff("Surface Noise Cutoff", Range(0, 1)) = 0.777
    30.         _FoamMaxDistance("Foam Maximum Distance", Float) = 0.4
    31.      
    32.         [MaterialToggle] _hasSurfaceFoam("SurfaceFoam", Float) = 0
    33.         _SurfaceFoamDenSity("SurfaceFoamDenSity", Range(0, 2.0)) = 1.0
    34.         _SurfaceNoiseScroll_X("Surface Noise Scroll X",Float) = 0.03
    35.         _SurfaceNoiseScroll_Y("Surface Noise Scroll Y",Float) = 0.03
    36.         _SurfaceDistortion("Surface Distortion", 2D) = "white" {}
    37.         _SurfaceDistortionAmount("Surface Distortion Amount", Range(0, 1)) = 0.27
    38.     }
    39.     SubShader
    40.     {
    41.         Tags
    42.         {
    43.             "Queue" = "Transparent"
    44.         }
    45.         Pass
    46.         {
    47.             Blend SrcAlpha OneMinusSrcAlpha
    48.             ZWrite Off
    49.             CGPROGRAM
    50.             #define SMOOTHSTEP_AA 0.02
    51.             #pragma vertex vert
    52.             #pragma fragment frag
    53.  
    54.             #include "UnityCG.cginc"
    55.  
    56.             sampler2D _CameraDepthTexture;
    57.  
    58.  
    59.             float _Height;
    60.             float _horizontal_thrill;
    61.             float _Z_Offset;
    62.             float _Speed;
    63.             float _WaveLen;
    64.             float _deepcolor;
    65.             sampler2D _ReflectMap;
    66.             float4 _ReflectMap_ST;
    67.  
    68.             float4 _CausticsColor;
    69.             sampler2D _CausticsMap;
    70.             float4 _CausticsMap_ST;
    71.             float _CausticsCutoff;
    72.  
    73.             float _CausticsScroll_X;
    74.             float _CausticsScroll_Y;
    75.             sampler2D _CausticsDistortion;
    76.             float4 _CausticsDistortion_ST;
    77.             float _CausticsDistortionAmount;
    78.  
    79.  
    80.             float4 _FoamColor;
    81.             float4 _DepthGradientShallow;
    82.             float4 _DepthGradientDeep;
    83.             float _DepthMaxDistance;
    84.             sampler2D _SurfaceNoise;
    85.             float4 _SurfaceNoise_ST;
    86.             float _SurfaceNoiseCutoff;
    87.  
    88.             float _SurfaceNoiseScroll_X;
    89.             float _SurfaceNoiseScroll_Y;
    90.             float _hasSurfaceFoam;
    91.             sampler2D _SurfaceDistortion;
    92.             float4 _SurfaceDistortion_ST;
    93.  
    94.             float _SurfaceDistortionAmount;
    95.             float _FoamMaxDistance;
    96.  
    97.             float _SurfaceFoamDenSity;
    98.             fixed4 _UnifyMapColor;
    99.  
    100.  
    101.             float4x4  _MirrorCameraMatrix;
    102.  
    103.             float4 alphaBlend(float4 top, float4 bottom)
    104.             {
    105.                 float3 color = (top.rgb * top.a) + (bottom.rgb * (1 - top.a));
    106.                 float alpha = top.a + bottom.a * (1 - top.a);
    107.                 return float4(color, alpha);
    108.             }
    109.  
    110.  
    111.  
    112.             struct appdata
    113.             {
    114.                 float4 vertex : POSITION;
    115.                 float4 uv : TEXCOORD0;
    116.                 float3 normal : NORMAL;
    117.             };
    118.  
    119.             struct v2f
    120.             {
    121.                 float4 vertex : SV_POSITION;
    122.                 float4 vertex_mirror : TEXCOORD5;
    123.  
    124.                 float4 screenPosition : TEXCOORD2;
    125.                 float2 noiseUV : TEXCOORD0;
    126.                 float2 distortUV : TEXCOORD1;
    127.                 float2 CausticsUV : TEXCOORD3;
    128.                 float2 CausticsdistortUV :TEXCOORD4;
    129.                 float3 viewNormal : NORMAL;
    130.                 float4 cc:COLOR;
    131.             };
    132.  
    133.  
    134.             inline fixed4 PointToPlaneDistance(fixed4 c_point,fixed n_plane)
    135.             {
    136.                 return dot(c_point, n_plane);
    137.             }
    138.             inline fixed4 MirrorPoint(fixed4 c_point, fixed n_plane)
    139.             {
    140.                 fixed4 m_point;
    141.                 fixed d = PointToPlaneDistance(c_point, n_plane)[0];
    142.                 m_point = c_point - (2 * d)*n_plane;
    143.  
    144.                 return m_point;
    145.             }
    146.             v2f vert (appdata v)
    147.             {
    148.                 v2f o;
    149.                 float new_x = (v.vertex.x *_WaveLen + _Time.z*_Speed);
    150.                 float hh = sin(new_x + _Z_Offset * v.vertex.z)*_Height*v.vertex.x;
    151.                 float dd = sin(new_x)*_Height*v.vertex.x;
    152.                 float3 pp = v.vertex.xyz;
    153.                 pp.y += hh;
    154.                 pp.z += _horizontal_thrill * dd;
    155.  
    156.                 o.vertex = UnityObjectToClipPos(float4(pp, 1.0));
    157.                 o.cc = (_deepcolor*pp.y) + 1.0;
    158.  
    159.                 o.screenPosition = ComputeScreenPos(o.vertex);
    160.                 o.noiseUV = TRANSFORM_TEX(v.uv, _SurfaceNoise);
    161.                 o.distortUV = TRANSFORM_TEX(v.uv, _SurfaceDistortion);
    162.                 o.CausticsUV = TRANSFORM_TEX(v.uv, _CausticsMap);
    163.                 o.CausticsdistortUV = TRANSFORM_TEX(v.uv,_CausticsDistortion);
    164.                 o.viewNormal = COMPUTE_VIEW_NORMAL;
    165.  
    166.                 float4x4 mirror_m = unity_ObjectToWorld * _MirrorCameraMatrix * UNITY_MATRIX_P;
    167.                 o.vertex_mirror = mul(mirror_m, float4(pp, 1.0));
    168.                 return o;
    169.             }
    170.  
    171.             float4 frag (v2f i) : SV_Target
    172.             {
    173.                 float foamDistance = _FoamMaxDistance;
    174.                 float existingDepth01 = tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPosition)).r;
    175.                 float existingDepthLinear = LinearEyeDepth(existingDepth01);
    176.                 float depthDifference = existingDepthLinear - i.screenPosition.w;
    177.  
    178.                 float2 CausticsdistortSample = (tex2D(_CausticsDistortion, i.CausticsdistortUV).xy /* 2 - 1*/) * _CausticsDistortionAmount;
    179.                 float2 myCausticsUV = float2(i.CausticsUV.x + _Time.y *_CausticsScroll_X + CausticsdistortSample.x, i.CausticsUV.y + _Time.y *_CausticsScroll_Y + CausticsdistortSample.y);
    180.  
    181.                 float4 CausticsMap = tex2D(_CausticsMap,myCausticsUV);
    182.                 CausticsMap.rgb *= _CausticsColor.rgb;
    183.  
    184.                 //case1
    185.                 // fixed4 r = tex2D(_ReflectMap, float2(1-((i.vertex.x)/ _ScreenParams.x), (i.vertex.y )/ _ScreenParams.y));
    186.                 //case2
    187.         fixed4 r = tex2D(_ReflectMap, float2(((i.vertex_mirror.x / i.vertex_mirror.w)*0.5 + 0.5),  ((i.vertex_mirror.y / i.vertex_mirror.w)*0.5 + 0.5)));
    188.  
    189.                 float waterDepthDifference01 = saturate(depthDifference / _DepthMaxDistance);
    190.                 float4 waterColor = (lerp(_DepthGradientShallow, _DepthGradientDeep, waterDepthDifference01))*i.cc*r;
    191.                 waterColor.rgb = lerp(waterColor.rgb, CausticsMap.rgb, (CausticsMap.a > _CausticsCutoff ? 1 : 0)*_CausticsColor.a);
    192.  
    193.                 float2 distortSample = (tex2D(_SurfaceDistortion, i.distortUV).xy /* 2 - 1*/) * _SurfaceDistortionAmount;
    194.  
    195.                 float2 noiseUV = float2(i.noiseUV.x + _Time.y *_SurfaceNoiseScroll_X  +distortSample.x, i.noiseUV.y + _Time.y *_SurfaceNoiseScroll_Y  +distortSample.y);
    196.                 float surfaceNoiseSample = tex2D(_SurfaceNoise, noiseUV).r;
    197.  
    198.              
    199.  
    200.                 float foamDepthDifference01 = saturate(depthDifference / foamDistance);
    201.                 float surfaceNoiseCutoff = foamDepthDifference01 /* _SurfaceNoiseCutoff*/;
    202.                 if (_hasSurfaceFoam > 0)surfaceNoiseCutoff *= (_SurfaceNoiseCutoff *_SurfaceFoamDenSity);
    203.                 float surfaceNoise = smoothstep(surfaceNoiseCutoff - SMOOTHSTEP_AA, surfaceNoiseCutoff + SMOOTHSTEP_AA, surfaceNoiseSample);
    204.                 float4 surfaceNoiseColor = _FoamColor;
    205.                 surfaceNoiseColor.a *= surfaceNoise;
    206.                 //return  float4 (existingNormal,1.0);
    207.                 return  alphaBlend(surfaceNoiseColor, waterColor)*_UnifyMapColor;
    208.             }
    209.             ENDCG
    210.         }
    211.     }
    How can I get Correct UV to assign to _ReflectMap?

    //case1 is near but not correct
    fixed4 r = tex2D(_ReflectMap, float2(1-((i.vertex.x)/ _ScreenParams.x), (i.vertex.y )/ _ScreenParams.y));
    //case2 is totally not correct
    fixed4 r = tex2D(_ReflectMap, float2(1 - ((i.vertex_mirror.x / i.vertex_mirror.w)*0.5 + 0.5), 1 - ((i.vertex_mirror.y / i.vertex_mirror.w)*0.5 + 0.5)));
     
    Last edited: Sep 16, 2019
  2. Dr-Game

    Dr-Game

    Joined:
    Mar 12, 2015
    Posts:
    161