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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值