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将直接就是世界坐标而不是局部坐标,可我们现在想用的是局部坐标,所以必须禁用自动网格合并。

  • 相关阅读:
    [转] 计算机网络中的服务原语
    VMWare的三种网络连接方式
    Vim常用操作
    [转] 图解单片机下载程序电路原理之USB转串口线、CH340、PL2303、MAX232芯片的使用
    [转] MMU 配置
    [转] C++项目中的extern "C" {}
    数据结构62:表插入排序算法
    数据结构61:2-路插入排序算法
    数据结构60:折半插入排序算法(折半排序算法)
    数据结构59:插入排序算法
  • 原文地址:https://www.cnblogs.com/wantnon/p/4689304.html
Copyright © 2011-2022 走看看