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

  • 相关阅读:
    将绿色版Tomcat服务添加到系统服务并设为开机运行
    简单的递归遍历树
    js浏览器中的alert死浏览器
    Crontab文件的参数【转载】
    修改tomcat项目的图标
    最后两个and半月
    没有信的信乐团,依然让我动情
    The Network Adapter could not establish the connec
    MySql数据库的备份和恢复
    extjs
  • 原文地址:https://www.cnblogs.com/wantnon/p/4689304.html
Copyright © 2011-2022 走看看