One, surface shader access Worldposition
To access world coordinates in surface shader, simply declare FLOAT3 worldpos in the input struct, as follows:
struct Input {
FLOAT2 Uv_maintex;
FLOAT3 Worldpos;
};
void Surf (Input in, InOut surfaceoutputstandard o) {
Access Worldposition by using In.worldpos
...
}
Reference: Http://wiki.unity3d.com/index.php?title=Shader_Code
Second, surface shader access Localposition
There is no direct built-in variable available, you need to use the V.vertex introduced in the Vert surf, as follows:
Tags {"Rendertype" = "Opaque" "disablebatching" = "True"}//disablebatching tag,ref:http://docs.unity3d.com/manual/ Sl-subshadertags.html
#pragma surface surf standard fullforwardshadows Vertex:vert
struct Input {
FLOAT2 Uv_maintex;
FLOAT3 My_vertpos;
};
void Vert (InOut appdata_full V, out Input o) {
Unity_initialize_output (input,o);//ref:http://forum.unity3d.com/threads/what-does-unity_initialize_output-do.186109/
O.my_vertpos=v.vertex;
}
void Surf (Input in, InOut surfaceoutputstandard o) {
Access Localposition by using In.my_vertpos
...
}
It should be noted that the "disablebatching" = "True" label should be added because: Some shaders (mostly ones, do object-space vertex deformations) does not work WH En Draw call batching are used–that ' s because batching transforms all geometry to world space, so "object space" is Los T. Citation: http://docs.unity3d.com/Manual/SL-SubShaderTags.html
That is, if automatic mesh merging is turned on, the V.vertex in vert will be the world coordinates instead of the local coordinates, but we now want to use local coordinates, so we must disable automatic mesh merging.
Unity, surface shader access world position and localposition