- Home /
How can I get a texture to render in object space?
I want to make a shader that is 1) completely emissive with no albedo component, 2) uses a UV-mapped texture with transparency based on the texture's alpha, and 3) uses a second texture in object space without regard to UV coordinates for additional transparency.
So far I have got everything working, except for the texture in part 3), I got it working in both screen space and in world space, but I don't know how to get it into object space.
Here's my shader so far.
Shader "Custom/ExtraTransp" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_ScreenAlpha ("Base (RGB) Trans (A)", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 200
CGPROGRAM
#pragma surface surf Lambert alpha
sampler2D _MainTex;
fixed4 _Color;
sampler2D _ScreenAlpha;
struct Input {
float2 uv_MainTex;
float2 worldPos;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
fixed4 sa = tex2D(_ScreenAlpha, IN.worldPos);
o.Emission = c.rgb;
o.Alpha = c.a * sa.a;
}
ENDCG
}
Fallback "Transparent/Diffuse"
}
What do I need to change to get this texture to move with the object's position?
Answer by Pangamini · Jan 10, 2014 at 05:00 PM
You should use vertex position that enters the vertex shader as UV. This is normally consumed by fragment shader and inaccessible.
Surface shaders can have vertex shaders too, but i believe verts are somehow pre-transformed there (so if your vertex shader does nothing, projection is still correct). Since you only use emissive, i suggest you switch to vertex / fragment shader, surf is not necessary if you are not using lighting. Basically your fragment color output will be equal to float4(Emission.rgb,Alpha);
Now you need to output original POSITION value as TEXCOORD so you can access it in fragment shader.