zoukankan      html  css  js  c++  java
  • Unity可编程管线的顶点光照Shader

    UnityCG.cginc有一个叫ShadeVertexLightsFull的函数可以用来计算顶点光照。

    源码如下:

    // Used in Vertex pass: Calculates diffuse lighting from lightCount lights. Specifying true to spotLight is more expensive
    // to calculate but lights are treated as spot lights otherwise they are treated as point lights.
    float3 ShadeVertexLightsFull (float4 vertex, float3 normal, int lightCount, bool spotLight)
    {
    float3 viewpos = UnityObjectToViewPos (vertex);
    float3 viewN = normalize (mul ((float3x3)UNITY_MATRIX_IT_MV, normal));

    float3 lightColor = UNITY_LIGHTMODEL_AMBIENT.xyz;
    for (int i = 0; i < lightCount; i++) {
    float3 toLight = unity_LightPosition[i].xyz - viewpos.xyz * unity_LightPosition[i].w;
    float lengthSq = dot(toLight, toLight);

    // don't produce NaNs if some vertex position overlaps with the light
    lengthSq = max(lengthSq, 0.000001);

    toLight *= rsqrt(lengthSq);

    float atten = 1.0 / (1.0 + lengthSq * unity_LightAtten[i].z);
    if (spotLight)
    {
    float rho = max (0, dot(toLight, unity_SpotDirection[i].xyz));
    float spotAtt = (rho - unity_LightAtten[i].x) * unity_LightAtten[i].y;
    atten *= saturate(spotAtt);
    }

    float diff = max (0, dot (viewN, toLight));
    lightColor += unity_LightColor[i].rgb * (diff * atten);
    }
    return lightColor;
    }
     
    代码中使用了unity_LightPosition等Unity的预制变量,这些变量只在Vertex Pass中可以使用。在Pass的Tags中设置"LightMode" = "Vertex"来激活Vertex Pass,否则这些预制变量不生效。

    参考文献

    https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html

    https://gist.github.com/aras-p/4175727

  • 相关阅读:
    Android SD卡读写文件
    Android 是什么
    Canvas 类
    Java IO流之字节流 FileInputStream
    Android中asset文件夹和raw文件夹区别
    随手收藏
    Java IO流
    Android私有文件资源文件的存取
    ubuntu 下的jdk安装
    Paint类
  • 原文地址:https://www.cnblogs.com/lilei9110/p/9669805.html
Copyright © 2011-2022 走看看