zoukankan      html  css  js  c++  java
  • unity, surface shader access world position and localposition

    一,surface shader中访问worldposition

    在surface shader中访问世界坐标,只需在Input结构体中声明float3 worldPos即可,如下:

         struct Input {
                float2 uv_MainTex;
                float3 worldPos;
            };
            void surf (Input IN, inout SurfaceOutputStandard o) {

        //access worldposition by using IN.worldPos

        ...
            }

    参考:http://wiki.unity3d.com/index.php?title=Shader_Code

    二,surface shader中访问localposition

    没有直接的内置变量可用,需要把vert中的v.vertex引进surf中使用,如下:

            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
               ...
            }

    需要注意的是应该加"DisableBatching"="True"标签,原因是:Some shaders (mostly ones that do object-space vertex deformations) do not work when Draw Call Batching is used – that’s because batching transforms all geometry into world space, so “object space” is lost.引自:http://docs.unity3d.com/Manual/SL-SubShaderTags.html

    就是说,如果开启了自动网格合并,则vert中的v.vertex将直接就是世界坐标而不是局部坐标,可我们现在想用的是局部坐标,所以必须禁用自动网格合并。

  • 相关阅读:
    四种会话跟踪技术的对比
    【转载】.NET中使用Redis
    【转载】Windows平台下利用APM来做负载均衡方案
    【转载】Windows平台分布式架构实践
    MVC插件式开发平台
    如何用JS和HTML 做一个桌面炒股小插件【原创】
    如果用HTML5做一个在线视频聊天【原创】
    BraveOS正式版发布,希望大家下载使用
    短期将不再更新更多内容,见谅!
    打造自己的移动绿色版 Python 环境
  • 原文地址:https://www.cnblogs.com/wantnon/p/4689304.html
Copyright © 2011-2022 走看看