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.