- Home /
Projected Texture Shader Issues
So I've written a shader that allows me to project a texture onto a model
Shader "Custom/FlatStaticTexture" {
Properties {
_Texture("Texture", 2D) = "white" {}
}
SubShader {
Pass {
CGPROGRAM
#pragma fragment frag
#pragma vertex vert
#include "UnityCG.cginc"
float4 _Color;
sampler2D _Texture;
struct v2f {
float4 pos : SV_POSITION;
float4 ref : TEXCOORD0;
};
float4 _Texture_ST;
v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.ref = ComputeScreenPos(o.pos);
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed4 texcol = tex2Dproj(_Texture, i.ref);
return texcol;
}
ENDCG
}
}
}
the only issue is that it is distorted because it's stretching it across the entire screen. Would there be any way to tile the image based on screen width so that it doesn't get distorted. The tile and offset parameters in the inspector do not do anything.
Answer by helarts · Nov 04, 2015 at 09:50 PM
I think this works:
float2 coords = i.ref;
coords.x *= _ScreenParams.x / _ScreenParams.y;
fixed4 texcol = tex2Dproj(_Texture, coords);
"_ScreenParams" is a builtin variable, more infos can be found here: http://docs.unity3d.com/Manual/SL-UnityShaderVariables.html
Your answer
Follow this Question
Related Questions
Blend textures in shader 0 Answers
shader cg blending two textures 1 Answer
Projector + custom fragment shader = endless texture draw 2 Answers
Stop shader values from being clamped 1 Answer
_LightMatrix0 and Directional Light 1 Answer