Use Custom Shader to Determine Texture Coordinates

I’m currently writing a custom shader to determine the texture coordinates by calculating the final coordinates in vert method and use the results to fetch colors from the main texture in frag methods.

But the thing is that I can’t fetch the color by calling tex2D method which uses ‘_MainTex’ and a float4 variable. The error says that ‘Undeclared identifier ‘_MainTex’’. My shader code is as follows:

Shader "Custom/Point" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" { }
    }
    SubShader {
        Pass{
            //ZTest Always Cull Off ZWrite Off
            //Fog { Mode off }
            Tags { "RenderType"="Opaque" } 
            CGPROGRAM
            #include "UnityCG.cginc"
            #pragma target 5.0
            #pragma vertex vert
            #pragma fragment frag
            #pragma debug
            struct Vert
            {
                float3 position : POSITION;
                float3 normal : NORMAL;
                float4 texCoord : TEXCOORD0;
            };

            uniform StructuredBuffer<Vert> m_buffer;
            uniform float4 m_yawPitch;
            uniform float4 m_worldPosition;
            uniform float4 m_index;
            uniform float4 m_color1;
            uniform float4 m_color2;

            struct v2f
            {
                float4 position : SV_POSITION;
                float3 normal : TEXCOORD0;
                float4 texCoord : TEXCOORD1;
                float3 color : COLOR;
            };

            v2f vert(uint id : SV_VertexID)
            {
                Vert vert = m_buffer[id];
                v2f OUT;
                float4x4 world = {
                m_yawPitch.x, -m_yawPitch.y, 0, 0,
                m_yawPitch.z * m_yawPitch.y, m_yawPitch.z * m_yawPitch.x, m_yawPitch.w, 0,
                -m_yawPitch.w * m_yawPitch.y, -m_yawPitch.w * m_yawPitch.x, m_yawPitch.z, 0,
                m_worldPosition };
                OUT.position = mul(vert.position, world);
                OUT.position = mul(OUT.position, UNITY_MATRIX_V);
                OUT.position = mul(OUT.position, UNITY_MATRIX_P);
                OUT.normal = mul(vert.normal, world);
                OUT.color = lerp(m_color1, m_color2, vert.texCoord.w);
                float texIndex = lerp(lerp(m_index.x, m_index.y, vert.texCoord.w), 15, vert.texCoord.z);
                OUT.texCoord = float4((texIndex % 4 + vert.texCoord.x) / 4,(((int)texIndex / 4) + vert.texCoord.y) / 4,0,0);
                return OUT;
            }

            float4 frag(v2f IN) : COLOR
            {
                return float4((.5f * (1 - dot(normalize(_WorldSpaceLightPos0.xyz),float3(IN.normal.x,-IN.normal.y,IN.normal.z)) * (1 - tex2D(_MainTex, IN.texCoord).a) * IN.color.rgb), 1);
            }
            ENDCG
        }
    } 
    Fallback "Diffuse"
}

Thanks for reading, any help would be appreciated.

you need to declare:

uniform sampler2D _MainTex;

with the rest of your uniforms.

Also as an optimization you can just do one matrix multiplication in your vertex shader:

OUT.position = mul(OUT.position,UNITY_MATRIX_VP);

Thanks for your answer and suggestions. Actually I got it compiled yesterday but it is not working properly as the material transparent now. Do you have any ideas?

And there are a lot of errors saying that ‘DestroyBuffer can only be called from the main thread.’.

Your shader is set to Opaque, you want to change this line:

Tags {"RenderType"="Opaque"}

to

Tags {"RenderType"="Transparent" Queue ="Transparent"}

Also add:

Blend SrcAlpha OneMinusSrcAlpha

Also your fragment shader is always writing 1 for alpha. you’ll want to adjust that value to the correct alpha.
I’m unsure what would be causing those DestroyBuffer errors you’re receiving

Thanks a lot. Actually I want my shader to be opaque not transparent, so the render type should be fine.