Hey everyone. I recently noticed that in my shader, when I don’t mention a “float4 position : SV_POSITION” parameter in the fragment function, the texture being supplied to the material doesn’t show. Instead what shows up is just one color from the texture.
Here’s my code with the “float4 position : SV_POSITION” parameter in the Fragment function:
Shader "Custom/MyShader08"
{
Properties
{
_MainTex01 ("Texture01", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex01;
float4 _MainTex01_ST;
void vert(
float4 position : POSITION,
float2 texCoord0 : TEXCOORD0,
out float4 oposition : SV_POSITION,
out float2 otexCoord0 : TEXCOORD0
)
{
oposition = UnityObjectToClipPos(position);
otexCoord0 = TRANSFORM_TEX(texCoord0, _MainTex01);
}
float4 frag(
float4 position : SV_POSITION,
float2 texCoord0 : TEXCOORD0) : SV_Target
{
float4 color = tex2D(_MainTex01, texCoord0);
return color;
}
ENDCG
}
}
}
Here’s the result. Texture shows up as it should.
Here’s my code without “float4 position : SV_POSITION” in the Fragment shader:
Shader "Custom/MyShader08"
{
Properties
{
_MainTex01 ("Texture01", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex01;
float4 _MainTex01_ST;
void vert(
float4 position : POSITION,
float2 texCoord0 : TEXCOORD0,
out float4 oposition : SV_POSITION,
out float2 otexCoord0 : TEXCOORD0
)
{
oposition = UnityObjectToClipPos(position);
otexCoord0 = TRANSFORM_TEX(texCoord0, _MainTex01);
}
float4 frag(
float2 texCoord0 : TEXCOORD0) : SV_Target
{
float4 color = tex2D(_MainTex01, texCoord0);
return color;
}
ENDCG
}
}
}
This is the result:
I’m very new to writing shaders so there’s definitely something I’m missing here. I’m not able to understand why an SV_POSITION parameter is required in the Fragment function when I’m not using it.
I’ve tried looking online for an answer and couldn’t find one. The Shader Semantics page doesn’t mention it either.