Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trying to construct world position in image shader

Discussion in 'Shaders' started by cybie, Oct 12, 2010.

  1. cybie

    cybie

    Joined:
    Sep 17, 2010
    Posts:
    91
    I am trying to reconstruct the pixels world position inside the fragment shader. I have read the suggestion of using Internal-PrepassLighting.shader as a reference. I am still no able to achieve what I want. I pretty much copy the code out of Internal-PrepassLighting and output the world position as color. I have attached the output image of my shader. The first one shows the scene without the shader, the second with the shader, and the third is with the shader, with the camera tilted.

    Here is the C# script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. [RequireComponent (typeof(Camera))]
    5. [AddComponentMenu("")]
    6. public class postfx : MonoBehaviour {
    7.     /// Provides a shader property that is set in the inspector
    8.     /// and a material instantiated from the shader
    9.     public Shader   shader;
    10.     private Material m_Material;
    11.  
    12.     protected void Start ()
    13.     {
    14.         // Disable if we don't support image effects
    15.         if (!SystemInfo.supportsImageEffects) {
    16.             enabled = false;
    17.             return;
    18.         }
    19.        
    20.         // Disable the image effect if the shader can't
    21.         // run on the users graphics card
    22.         if (!shader || !shader.isSupported)
    23.             enabled = false;
    24.     }
    25.  
    26.     protected Material material {
    27.         get {
    28.             if (m_Material == null) {
    29.                 m_Material = new Material (shader);
    30.                 m_Material.hideFlags = HideFlags.HideAndDontSave;
    31.             }
    32.             return m_Material;
    33.         }
    34.     }
    35.    
    36.     protected void OnDisable() {
    37.         if( m_Material ) {
    38.             DestroyImmediate( m_Material );
    39.         }
    40.     }
    41.    
    42.     void OnRenderImage(RenderTexture source, RenderTexture destination)
    43.     {
    44.         Graphics.Blit (source, destination, material);
    45.     }
    46. }
    47.  
    And here is the shader code

    Code (csharp):
    1.  
    2. Shader "Test Post Effect" {
    3. Properties {
    4.     _MainTex ("Base (RGB)", 2D) = "white" {}
    5. }
    6.  
    7. SubShader {
    8.     Pass {
    9.         ZTest Always
    10.         Cull Off
    11.         ZWrite Off
    12.         Fog { Mode off }
    13.                
    14. CGPROGRAM
    15. #pragma vertex vert
    16. #pragma fragment frag
    17. #pragma multi_compile_lightpass
    18. #include "UnityCG.cginc"
    19.  
    20. uniform sampler2D _MainTex;
    21.  
    22. struct appdata {
    23.     float4 vertex : POSITION;
    24.     float3 texcoord : TEXCOORD0;
    25. };
    26.  
    27. struct v2f {
    28.     float4 pos : SV_POSITION;
    29.     float4 uv : TEXCOORD0;
    30.     float3 ray : TEXCOORD1;
    31. };
    32.  
    33. v2f vert (appdata v)
    34. {
    35.     v2f o;
    36.     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    37.     o.uv = ComputeScreenPos (o.pos);
    38.     o.ray = mul (UNITY_MATRIX_MV, v.vertex).xyz * float3(-1,-1,1);
    39.    
    40.     // v.texcoord is equal to 0 when we are drawing 3D light shapes and
    41.     // contains a ray pointing from the camera to one of near plane's
    42.     // corners in camera space when we are drawing a full screen quad.
    43.     o.ray = lerp(o.ray, v.texcoord, v.texcoord.z != 0);
    44.    
    45.     return o;
    46. }
    47.  
    48. sampler2D _CameraDepthTexture;
    49. float4x4 _CameraToWorld;
    50.  
    51. half4 frag (v2f i) : COLOR
    52. {
    53.     i.ray = i.ray * (_ProjectionParams.z / i.ray.z);
    54.     float2 uv = i.uv.xy / i.uv.w;
    55.    
    56.     float depth = tex2D (_CameraDepthTexture, uv).r;
    57.     depth = Linear01Depth (depth);
    58.     float4 vpos = float4(i.ray * depth,1);
    59.     float3 wpos = mul (_CameraToWorld, vpos).xyz;
    60.    
    61.     return half4(wpos,1.0f);
    62. }
    63. ENDCG
    64.  
    65.     }
    66. }
    67.  
    68. Fallback off
    69.  
    70. }
    71.  
    Can you spot the problem with my code?

    Thanks
     

    Attached Files:

  2. cybie

    cybie

    Joined:
    Sep 17, 2010
    Posts:
    91
    Ok I managed to solve my own problem.


    Thanks