Home>

In Unity3D,
In order to implement the shader (Unlit Shader) that we want to create from now on, "the coordinates of the point where the pixel draws" is required.
(Image to return coordinates for each pixel by flying a ray)

However, I investigated the method in my own way, but I did not know what would produce the expected result.
I would appreciate it if you could give me a professor.

Other (e.g. version)

Use Unity 2018.2.5f1
(Since my Unity history is shallow, the text may be quite strange.)

  • Answer # 1

    Do you want to do something in the Unlit shader using pixel world coordinates?

    [Introduction to Unity Shaders] introduced by sakura_hana-Representing polygons with points (points)-There are places in the toy lab that are colored according to the Y coordinate of the pixel. The same technique can be used.

    For example:

    Shader "Unlit/WorldPosition"
    {
        Properties
        {
            _MainTex ("Texture", 2D) = "white" {}
        }
        SubShader
        {
            Tags {"RenderType" = "Opaque"}
            LOD 100
            Pass
            {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                // make fog work
                #pragma multi_compile_fog
                #include "UnityCG.cginc"
                struct appdata
                {
                    float4 vertex: POSITION;
                    float2 uv: TEXCOORD0;
                };
                struct v2f
                {
                    float2 uv: TEXCOORD0;
                    UNITY_FOG_COORDS (1)
                    // Add float3 for world coordinate transmission to v2f
                    // Semantics should use the appropriate TEXCOORD
                    // (The name is "TEXCOORD", but you can use it freely, not limited to texture coordinates)
                    // TEXCOORD0 is already used for texture UV and TEXCOORD1 is used for fog effect
                    // Decided to use TEXCOORD2
                    // If texture coordinates and fog effects are not needed, delete them and use a free TEXCOORD
                    float3 worldPos: TEXCOORD2;
                    float4 vertex: SV_POSITION;
                };
                sampler2D _MainTex;
                float4 _MainTex_ST;
                v2f vert (appdata v)
                {
                    v2f o;
                    o.vertex = UnityObjectToClipPos (v.vertex);
                    o.uv = TRANSFORM_TEX (v.uv, _MainTex);
                    UNITY_TRANSFER_FOG (o, o.vertex);
                    // Convert the vertex coordinates of the model with the model conversion matrix (unity_ObjectToWorld)
                    // set to world coordinates and pass xyz to worldPos
                    o.worldPos = mul (unity_ObjectToWorld, v.vertex) .xyz;
                    return o;
                }
                fixed4 frag (v2f i): SV_Target
                {
                    // sample the texture
                    fixed4 col = tex2D (_MainTex, i.uv);
                    // apply fog
                    UNITY_APPLY_FOG (i.fogCoord, col);
                    // The previous o.worldPos (world coordinates of the vertices) is linearly interpolated between the vertices, and in the fragment shader
                    // The extracted i.worldPos is "pixel world coordinates"
                    // As a trial, add 2 to each xyz and divide by 4 so that -2 to 2 → 0 to 1,
                    // If you overwrite the col rgb with this, the pixels will be painted colorfully according to the world coordinates
                    // Note that the part beyond the range of -2 to 2 is out of the range of 0 to 1, so the color is saturated and there is no change
                    col.rgb = (i.worldPos + float3 (2.0, 2.0, 2.0))/4.0;
                    return col;
                }
                ENDCG
            }
        }
    }

    As shown in the figure below, we were able to visualize pixel world coordinates as colors.