- Home /
Get mesh size in frag/vert shader
I have a shader generating the following effect:
It's a simple frag/vert shader which changes the alpha value from 0 - {user defined} based on the position from the center of the mesh.
My pass block looks like this:
struct appdata
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
float distance : float2;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _MainTex_TexelSize;
fixed4 _Color;
fixed _Scale;
fixed _Radius;
fixed _Iris;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.distance = distance(float3(0, 0, 0), v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float4 c = _Color;
float alpha = i.distance / 2;
c.a = _Color.a * alpha; // / _MainTex_TexelSize.x;
return c;
}
You can see in the frag function I have a hard coded '2'. This is supposed to be the radius of the mesh and I would like to make it not hard coded so the mesh can be scaled and the alpha blend scales with the size of the mesh. Currently if I scale the mesh to radius 4 it will look like this:
What value could I use in my shader to get a normalized value representing this size? Is there a {0 - 1} or {-1 - 1} value I could use somehow?
New to shaders so apologies if the question is not clear..
Answer by tanoshimi · Mar 03, 2017 at 07:40 PM
The shader doesn't know the size of the mesh it's rendering - it works with vertices and pixels. But you can provide this information via script.
Declare a new float variable in the Properties block at the top of your shader:
_MeshRadius ("Mesh Radius", Float) = 1
And declare a matching variable within the CG code of your subshader.
uniform float _MeshRadius;
Now you can replace your hardcoded value with a reference to the _MeshRadius variable.
To change the parameter, you can either explicity set a value in the material inspector, or through code:
material.SetFloat("_MeshRadius", 3f);
Right, I'm just wondering because he already has a "_Scale", "_Radius" and "_Iris" variable but none of them is actually used.
True. And we don't know if those are exposed in the properties block or set anywhere else. I'm guessing this is some Frankenstein code that has been cobbled together from various places!
This is what I ended up doing. You can see in the code that I had a radius property setup (I should have tidied my code up..) which I set on awake of the gameobject. I now understand shaders work on pixels and vertices as you say so the concept of mesh size is not available.
Your answer
![](https://koobas.hobune.stream/wayback/20220612112230im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Create a hole in a mesh using a shader 0 Answers
How do I debug the following error DirectX 11 shader input signature is null ? 1 Answer
How do I only show parts of objects that overlap [2D] 1 Answer
How do I properly apply a transparent texture to a mesh? 2 Answers
Apply new complex fbx to saved prefab 0 Answers