美文网首页
Shader入门笔记--Unity内置文件和变量

Shader入门笔记--Unity内置文件和变量

作者: lonecolonel | 来源:发表于2018-08-29 13:07 被阅读0次
    1535519251(1).jpg
    image.png
    Unity中CGIncludes文件夹:Mac 下 image.png
    Windows: image.png

    // Computes world space view direction, from object space position
    inline float3 UnityWorldSpaceViewDir( in float3 worldPos )
    {
    return _WorldSpaceCameraPos.xyz - worldPos;
    }

    // Computes object space view direction
    inline float3 ObjSpaceViewDir( in float4 v )
    {
    float3 objSpaceCameraPos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos.xyz, 1)).xyz;
    return objSpaceCameraPos - v.xyz;
    }

    // Computes world space light direction, from object space position
    // Legacy Please use UnityWorldSpaceLightDir instead
    inline float3 WorldSpaceLightDir( in float4 localPos )
    {
    float3 worldPos = mul(unity_ObjectToWorld, localPos).xyz;
    return UnityWorldSpaceLightDir(worldPos);
    }

    // Transforms normal from object to world space
    inline float3 UnityObjectToWorldNormal( in float3 norm )
    {

    ifdef UNITY_ASSUME_UNIFORM_SCALING

    return UnityObjectToWorldDir(norm);
    

    else

    // mul(IT_M, norm) => mul(norm, I_M) => {dot(norm, I_M.col0), dot(norm, I_M.col1), dot(norm, I_M.col2)}
    return normalize(mul(norm, (float3x3)unity_WorldToObject));
    

    endif

    }
    // Transforms direction from object to world space
    inline float3 UnityObjectToWorldDir( in float3 dir )
    {
    return normalize(mul((float3x3)unity_ObjectToWorld, dir));
    }

    // Transforms direction from world to object space
    inline float3 UnityWorldToObjectDir( in float3 dir )
    {
    return normalize(mul((float3x3)unity_WorldToObject, dir));
    }

    相关文章

      网友评论

          本文标题:Shader入门笔记--Unity内置文件和变量

          本文链接:https://www.haomeiwen.com/subject/ufwhwftx.html