- Home /
Shader to make a quad work like a canvas
I am trying to learn a bit about shaders.
What I am trying to do right now, is to set up a Vertex Shader in such a way that I can use a Quad as a canvas to draw animations on using only the fragment shader, which is for instance what is used on https://www.shadertoy.com
Right now my Shader looks like the following:
Shader "Custom/FragmentShaderTest" {
Properties {
_Color("Color", Color) = (1.0,1.0,1.0,1.0)
}
SubShader{
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
//user defined variables
uniform float4 _Color;
struct vertexInput{
float4 vertex: POSITION;
};
struct vertexOutput{
float4 pos: SV_POSITION;
float4 posWorld: TEXCOORD0;
};
vertexOutput vert(vertexInput v){
vertexOutput o;
o.posWorld=mul(UNITY_MATRIX_MVP, v.vertex);
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
return o;
}
float4 frag(vertexOutput i):COLOR
{
float2 uv = i.posWorld.xy;
uv= 2.*uv-1.;
float dist = distance(uv, float2(0.5,0.5));
float4 col;
col = float4(uv.x,uv.y,1.0,0.5);
col *=dist;
return col;
}
ENDCG
}
}
//Fallback "Diffuse"
}
However, the position that is given to the fragment shader from the vertex shader is not the local position on the plane; it changes when I move my camera or rotate the object.
How do I pass the position along in the correct way?
Your answer
Follow this Question
Related Questions
How to realize Unity Editor Scene Filter View Effect in my game. 0 Answers
GPU performance with tris and verts 1 Answer
My mesh didn’t appear in Unity 0 Answers
Creating a point cloud engine using shaders/meshes 0 Answers
Cancel out mesh alpha 1 Answer