- Home /
Question by
Def · Aug 27, 2011 at 11:28 AM ·
vertexshader
Sampling Texture in Vertex Shader?
Hi, I'm trying to sample a texture in a vertex shader, but for some reason I'm always getting 0,0,0,0 back from tex2D.
For debug purposes I'm just forwarding the sampled data through to the pixel shader for display. The input texture is solid light green and can be sampled fine in the pixel shader. For some reason it's not working from the vertex shader.
My graphics card is an nVidia Quadro FX1500, which claims to support shader model 3.0 (which, I believe, is the minimum version for texture sampling from a vertex shader).
Shader "VSTex"
{
Properties
{
_Tex( "Tex", 2D ) = "white" {}
}
SubShader
{
Tags
{
"Queue"="Transparent"
"RenderType"="Transparent"
}
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma target 3.0
#pragma glsl
#pragma vertex VS
#pragma fragment PS
sampler2D _Tex;
struct SVSInput
{
float4 vertex : POSITION;
};
struct SVSToPS
{
float4 pos : POSITION;
float4 color : COLOR;
};
SVSToPS VS( SVSInput input )
{
SVSToPS output;
output.pos = mul( UNITY_MATRIX_MVP, float4( input.vertex.xyz, 1.0f ));
output.color = tex2D( _Tex, float2( 0.0f, 0.0f ));
return output;
}
float4 PS( SVSToPS input ) : COLOR
{
return float4( input.color.xyz, 0.5f );
}
ENDCG
}
}
Fallback Off
}
Comment
$$anonymous$$inor update: tex2D is never supported in vertex shaders. The correct function to use is tex2Dlod. So I'm not sure why Unity allows the former to compile. It's still not working though.