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

unity, surface shader access world position and localposition的更多相关文章

  1. unity surface shader 1

    Unity ShaderLib :  CGPROGRAM  ENDCG之间是CG代码,之外的代码功能都由ShaderLib提供,CG中的一些方法比如tex2D(...)也是ShaderLib对CG进行 ...

  2. Unity surface shader 2

    UV滚动 Shader "Nafio/ScrollUV" { Properties { _Tex("T",2D) = "white" {} ...

  3. 【Unity Shaders】初探Surface Shader背后的机制

    转载请注明出处:http://blog.csdn.net/candycat1992/article/details/39994049 写在前面 一直以来,Unity Surface Shader背后的 ...

  4. Unity Shader (三)Surface Shader机制

    转自:http://blog.csdn.net/candycat1992/article/details/39994049 写在前面 一直以来,Unity Surface Shader背后的机制一直是 ...

  5. 【Unity Shaders】Diffuse Shading——在Surface Shader中使用properties

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  6. Unity Shader——Writing Surface Shaders(3)——Surface Shader Lighting Examples

    Surface Shader 光照例子 这里有一些自定义光照模型和Surface Shaders的例子.通常的Surface Shader例子在这里. 由于延迟光照在某些自定义的逐材质光照模型中表现得 ...

  7. 【Unity Shaders】Shader学习资源和Surface Shader概述

    写在前面 写这篇文章的时候,我断断续续学习Unity Shader半年了,其实还是个门外汉.我也能体会很多童鞋那种想要学好Shader却无从下手的感觉.在这个期间,我找到一些学习Shader的教程以及 ...

  8. Unity Shader——Writing Surface Shaders(1)——Surface Shader Examples

    这里有Surface Shader的一些例子.下面的这些例子关注使用内建的光照模型:关于如何使用自定义光照模型的例子参见Surface Shader Lighting Examples. 简单 我们将 ...

  9. unity, unlit surface shader (texColor only surface shader)

    要实现双面透明无光照只有纹理色的surface shader. 错误的写法:(导致带有曝光) Shader "Custom/doubleFaceTranspTexColor" { ...

随机推荐

  1. Linux中ping命令

    Linux系统的ping命令是常用的网络命令,它通常用来测试与目标主机的连通性,我们经常会说“ping一下某机器,看是不是开着”.不能打开网页时会说“你先ping网关地址192.168.1.1试试”. ...

  2. nodejs的require模块及路径

    在nodejs中,模块大概可以分为核心模块和文件模块. 核心模块是被编译成二进制代码,引用的时候只需require表示符即可,如(require('net')). 文件模块,则是指js文件.json文 ...

  3. java语言写文件内容

    import java.io.File;import java.io.FileWriter;import java.io.IOException; public static void main(St ...

  4. 【HDU 5030】Rabbit's String (二分+后缀数组)

    Rabbit's String Problem Description Long long ago, there lived a lot of rabbits in the forest. One d ...

  5. http://www.cnblogs.com/leiOOlei/p/5075402.html

    http://www.cnblogs.com/leiOOlei/p/5075402.html

  6. Android笔记——Drawerlayout创建侧滑出菜单

    1.首先务必导入support-v4包 2.布局文件主标签为<android.support.v4.widget.DrawerLayout>,并为其设置id         其子标签必须包 ...

  7. CodeForces485A——Factory(抽屉原理)

    Factory One industrial factory is reforming working plan. The director suggested to set a mythical d ...

  8. 统计MySQL数据表大小

    SELECT CONCAT(TRUNCATE(SUM(data_length)/1024/1024,2),'MB') AS data_size,CONCAT(TRUNCATE(SUM(max_data ...

  9. SSIS -->> Data Type

    SSIS和SQL SERVER, .NET数据类型的映射表

  10. openfire插件开发1

    http://www.cnblogs.com/hoojo/archive/2013/03/29/openfire_plugin_chatlogs_plugin_.html http://www.cnb ...