Vertex shader does not receive vertex coordinates as I expected. Why?

Hi!

I’m making a vertex shader for transform the vertex coordinates. For some reason when I use cubes or plane gameobjects and I have several in the viewport with the same mesh and shader/material geometry vertices seems to arrive in v.vertex with the coordinates relative to the scene (0, 0, 0) instead of the gameobject center (which is the behaviour that I expected). When only one mesh is visible, then the coordinates comes relative to the gameobject center, or when different gameobjects have different shaders, or when mesh is more complex (like an sphere). It’s like Unity is grouping the geometry underneath somehow…

Is this behaviour configurable? There is something I’m doing wrong?

I attach the shader I used to finally realize that something wasn’t going like I expected:

Shader "Custom/BasicVertex" {
	Properties {
		_Color ("Main Color", Color) = (1,1,1,0.5)
		_MainTex ("Texture", 2D) = "white" { }
		_Mx ("Mx", Float) = 1
		_My ("My", Float) = 1
		_Mz ("Mz", Float) = 1
	}
	SubShader {
		Pass {
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			float4 _Color;
			sampler2D _MainTex;
			float _Mx, _My, _Mz;

			struct v2f {
				float4 pos: SV_POSITION;
				float2 uv: TEXCOORD0;
			};
			
			float4 _MainTex_ST;

			v2f vert(appdata_base v) {
				v2f o;
				float4 trvertex = float4(v.vertex.x * _Mx, v.vertex.y * _My, v.vertex.z * _Mz, v.vertex.w);
				o.pos = mul(UNITY_MATRIX_MVP, trvertex);
				o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
				return o;
			}

			half4 frag(v2f i): COLOR {
				half4 texcol = tex2D(_MainTex, i.uv);
				return texcol * _Color;
			}
			ENDCG
		}
	}
	Fallback "VertexLit"
}

It will certain ly not use the GameObjects center but the GameObjects pivot since all vertex coordinates are local coordinates. In a lot cases the pivot is the GameObjects center but they aren’t related in the first place. So make sure you set the pivot mode in Unity to pivot and not center.

And to answer your question: No, this behaviour is not configurable for Vertex shaders since that’s the point of a vertex shader :wink: Surface Shaders on the other hand work a little bit different, but since your shader is a “normal shader” the in coming vertex data stream is in local coordinates.

Hi Bunny83,

Thank you for trying to help :slight_smile: but probably the question can be answered only by an Unity Staff person.

I wasn’t talking about the editor (I believe the pivot’s mode is an editor-only tool and it didn’t affect runtime elements like scripts or shaders). Anyway the behaviour is fully independent from grouping performed at editor level, or from pivot-mode.

About the “behaviour”, there are other queue manager settings that can be changed, and I cannot see why “this is the point of a vertex shader” :slight_smile:

The mesh data grouping is made probably by the Unity Engine internals to improve performance (if I have made the engine probably I will try to make that kind of optimizations); I was asking if this behaviour (probably an optimization) can be disabled at material level or at queue mananger settings level somehow.