Is it possible that the vertex position given in the struct appdata_base are already the transformed? I need to get the local positions of my vertices in the vertex shader but after 30 min of confusion and testing I noticed that the vertex position are already in the world space and all the matrices are changed to compensate this. e.g. _Object2World seems to be a normal identity matrix. I can’t find any documentation about this.
How do I get the actual local vertex positions and the real world/model matrix? Is there any documentation?
Example shader:
Shader "own/markercell"
{
Properties
{
_MaskTex ("_MaskTex", 2D) = "white" {}
_MainColor ("_MainColor", Color) = (1,1,1,1)
_NeighbourPos("_NeighbourPos", Vector) = (1,1,1,1)
}
SubShader
{
Lighting Off
Fog { Mode Off }
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MaskTex;
uniform fixed4 _MainColor;
uniform float _Scale;
uniform float4 _NeighbourPos;
struct vertOut
{
float4 pos : SV_POSITION;
float2 tex : TEXCOORD0;
float3 lpos : TEXCOORD2;
};
vertOut vert(appdata_full input)
{
vertOut output;
fixed4 pos = input.vertex;
output.lpos = input.vertex;
output.pos = mul (UNITY_MATRIX_MVP, pos);
output.tex = input.texcoord;
return output;
}
fixed4 frag(vertOut input) : COLOR0
{
fixed4 output = fixed4(0,0,0,0);
output.a = 1;
output.rgb = input.lpos;
return output;
}
ENDCG
}
}
}
The shader should simply show the local position as color but it shows the world position.
getting more and more the feeling this could be a bug in unity. If I change the scale of the object it works ... sometimes.
– devluzUsing this shader on a default cube primitive, I get local shading exactly as I would expect. I admit I do get some slightly odd effects if I change to use non-uniform scaling of that cube - it doesn't become worldspace, but the lighting seems to go awry. Left pic has scale of 2,2,2 - Right pic shows effect of changing scale slightly to 2,2.001,2 [34449-untitled.jpg|34449]. Don't know if this is related or not though.
– tanoshimiI have a theory (though not a solution).... when you say that you move your camera around and sometimes they work, sometimes they don't. Would it by any chance work with local shading when there's only one object in view, but turns to "global" when there's two or more?
– tanoshimiHere's what I'm getting: 9 cubes all using the same shader = "global" vertex position. [34462-global.jpg|34462] Disable the mesh renderer on (any) 8 of the cubes, and the remaining cube becomes "local" vertex position shaded. [34463-local.jpg|34463]
– tanoshimi