Unity, surface shader access world position and localposition

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.