Search Unity

Matt Stark's Wile E. Coyote Effect - How do I code this shader?

Discussion in 'Scripting' started by unity_CmT6_OMupL-Hjw, Jun 24, 2020.

  1. unity_CmT6_OMupL-Hjw

    unity_CmT6_OMupL-Hjw

    Joined:
    Nov 13, 2019
    Posts:
    3
    Hi everyone! I am a Digital Media student working on optical illusions in virtual space, but unfortunately, Shader coding is a very new thing to me.

    I tried following Matt Stark's documentation-

    https://matt.stark.scot/2019/11/06/wile-e-coyote-effect.html

    up until the "Applying the texture to the object" section and I am lost. I don't even know if I'm asking the right questions but here goes;

    • How do I get the "projection Matrix" which is data outside of the shader INTO the shader? Matrix4x4 cannot be announced in the property section of the shader code. He talked about _WorldToCam but I don't know how he wrote the code.

    • How do I announce the clipPos in the v2f struct?
    Here is my current, unworkable shader code;

    https://drive.google.com/file/d/1lJxWDtb-OsFI60rutVV8Ld7KWcBUJxBa/view?usp=sharing

    Thank you for your time. If I'm asking this in the wrong place please point me where if possible.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    You don't need to write your own shader for this - all you need to do is apply the texture to a quad with any unlit shader that alrady exists ("unlit shader", so there are no shadows on your texture, or use a surface shader that will have a shadow cast upon it).

    You real problem is getting the texture itself. That, fortunately, is relatively simple - use a render texture and have a camera draw onto the render texture. Use that render texture as the texture to display in the quad.

    No shader programming required :)

    (note: using this you are also 90% down the road to create a functioning mirror)
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    OTOH, if you want the full effect, as described in the article, the projection matrix and world conversion matrix are defined here

    Getting your data passed from vert to frag is usually done by defining the output structure from vert, and using that as input for frag, but make sure you apply the correct semantics (use position semantics). Use an unlit shader since surface shaders are exceedingly tricky when it comes to passing variables from vert to surf.
     
  4. unity_CmT6_OMupL-Hjw

    unity_CmT6_OMupL-Hjw

    Joined:
    Nov 13, 2019
    Posts:
    3
    I'm very sorry, I have never done this before and I did not understand your suggestion... I decided to follow into the shader more and wrote this. Is it possible for you to see something wrong here? My object (with this shader) became invisible because of something in the code.

    Code (CSharp):
    1. Shader "Unlit/myInvisibleWallShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Main Texture", 2D) = "white" {}
    6.         _CamTex ("Cam Texture", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 100
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.             // make fog work
    19.             //#pragma multi_compile_fog
    20.  
    21.             #include "UnityCG.cginc"
    22.  
    23.             struct appdata
    24.             {
    25.                 float4 vertex : POSITION;
    26.                 float2 uv : TEXCOORD0;
    27.             };
    28.  
    29.             struct v2f
    30.             {
    31.                 float2 uv : TEXCOORD0;
    32.                 //UNITY_FOG_COORDS(1)
    33.                // float4 vertex : SV_POSITION;
    34.                 float4 clipPos : SV_POSITION;
    35.             };
    36.  
    37.             sampler2D _MainTex;
    38.             float4 _MainTex_ST;
    39.             sampler2D _CamTex;
    40.             float4 _CamTex_ST;
    41.             float4x4 _WorldToCam;
    42.  
    43.             v2f vert (appdata v)
    44.             {
    45.                 v2f o;
    46.                 //o.vertex = UnityObjectToClipPos(v.vertex);
    47.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    48.                 //UNITY_TRANSFER_FOG(o,o.vertex);
    49.  
    50.                 // Project the vertex into world space
    51.                 float3 worldPos = mul(unity_ObjectToWorld, v.vertex);
    52.                 // Project the world space position into the temporary camera's clip space
    53.                 o.clipPos = mul(_WorldToCam, float4(worldPos, 1));
    54.  
    55.                 return o;
    56.             }
    57.  
    58.             fixed4 frag (v2f i) : SV_Target
    59.             {
    60.                 // sample the texture
    61.                 //fixed4 col = tex2D(_MainTex, i.uv);
    62.                 // apply fog
    63.                 //UNITY_APPLY_FOG(i.fogCoord, col);
    64.                 // Get the UV coordinate
    65.                 float2 uv = i.clipPos.xy / i.clipPos.w;
    66.                 uv = (uv + float2(1, 1)) / 2; // Convert it from the range -1 to 1 to the range 0 to 1
    67.                 // Sample the texture
    68.                 fixed4 col = tex2D(_CamTex, uv);
    69.                 return col;
    70.                
    71.             }
    72.             ENDCG
    73.         }
    74.     }
    75. }
    76.  
    Thank you in advance.