Cg Programming/Unity/Translucent Surfaces半透明表面

来源:互联网 发布:java绘制动态图 编辑:程序博客网 时间:2024/05/16 08:59

本教程涵盖了半透明表面。

这是几个关于光照教程中的其中之一,它超出了Phone反射模型的范围。但是,这是基于章节“光滑镜面高光”中描述的逐像素光照的Phone反射模型。如果你还没有阅读过那个教程,建议你先看一下。

Phone反射模型并没有把半透明考虑进来,即光照会穿透材质。本教程是关于半透明表面的,也就是说这个表面允许光从一个面传到另一个面,比如纸张、衣服、塑料薄膜或者树叶。

这里写图片描述

漫射半透明度

我们将要区分光传输的两种类型:漫射半透明度和向前发散的半透明度,它们各自对应于Phone反射模型中的漫射和高光项。漫射半透明度是类似于Phone反射模型中漫反射项的光的漫射传输:这只是取决于表面法向量和指向光源方向的点乘 — 除非光源在背面我们就使用负向表面法向量,于是漫射半透明光照的等式就是这样:
这里写图片描述
这是许多半透明表面最常见的光照,比如纸张和树叶。

向前散射的半透明

这里写图片描述

一些半透明表面(比如塑料薄膜)几乎是透明的并且允许光线直接透过表面但只有一些向前的散射;也就是说,我们可以透过表面看到光源但图像会有些模糊。这个跟Phone反射模型的镜面项相似(查看章节“镜面高光”中的等式),除了我们用负的光线方向-L替换光线方向R以及指数这里写图片描述现在对应于前向散射光的明锐度。
这里写图片描述

当然,这个前向散射半透明度的模型并不总是准确的,但是它允许我们可以伪造效果并调整参数。

下面的实现基于章节“光滑镜面高光”,它是用Phone反射模型的逐像素光照表示的。这个实现允许渲染背面,并且使用内置Cg函数faceforward(n, v, ng)来翻转表面法向量,如果dot(v, ng) < 0就返回n,否则返回-n。这个方法通常会在轮廓处失败,它会导致一些像素的照明不正确。一个改进的版本会为在章节“双面光滑表面”提到的正面和背面使用不同的通道和颜色。

除了Phone反射模型的高光项,我们也要用以下代码计算漫射半透明和前向散射半透明的光照:

 float3 diffuseTranslucency =                attenuation * _LightColor0.rgb                * _DiffuseTranslucentColor.rgb                * max(0.0, dot(lightDirection, -normalDirection));            float3 forwardTranslucency;            if (dot(normalDirection, lightDirection) > 0.0)                // light source on the wrong side?            {               forwardTranslucency = float3(0.0, 0.0, 0.0);                   // no forward-scattered translucency            }            else // light source on the right side            {               forwardTranslucency = attenuation * _LightColor0.rgb                  * _ForwardTranslucentColor.rgb * pow(max(0.0,                   dot(-lightDirection, viewDirection)), _Sharpness);            }

完整的着色器代码

完整的着色器代码为材质常量定义了着色器属性,并且为额外的光源添加了另一个有加性混合但没有环境光照的通道。

Shader "Cg translucent surfaces" {   Properties {      _Color ("Diffuse Material Color", Color) = (1,1,1,1)       _SpecColor ("Specular Material Color", Color) = (1,1,1,1)       _Shininess ("Shininess", Float) = 10      _DiffuseTranslucentColor ("Diffuse Translucent Color", Color)          = (1,1,1,1)       _ForwardTranslucentColor ("Forward Translucent Color", Color)          = (1,1,1,1)       _Sharpness ("Sharpness", Float) = 10   }   SubShader {      Pass {               Tags { "LightMode" = "ForwardBase" }             // pass for ambient light and first light source         Cull Off // show frontfaces and backfaces         CGPROGRAM         #pragma vertex vert           #pragma fragment frag          #include "UnityCG.cginc"         uniform float4 _LightColor0;             // color of light source (from "Lighting.cginc")         // User-specified properties         uniform float4 _Color;          uniform float4 _SpecColor;          uniform float _Shininess;         uniform float4 _DiffuseTranslucentColor;          uniform float4 _ForwardTranslucentColor;          uniform float _Sharpness;         struct vertexInput {            float4 vertex : POSITION;            float3 normal : NORMAL;         };         struct vertexOutput {            float4 pos : SV_POSITION;            float4 posWorld : TEXCOORD0;            float3 normalDir : TEXCOORD1;         };         vertexOutput vert(vertexInput input)          {            vertexOutput output;            float4x4 modelMatrix = _Object2World;            float4x4 modelMatrixInverse = _World2Object;             output.posWorld = mul(modelMatrix, input.vertex);            output.normalDir = normalize(               mul(float4(input.normal, 0.0), modelMatrixInverse).xyz);            output.pos = mul(UNITY_MATRIX_MVP, input.vertex);            return output;         }         float4 frag(vertexOutput input) : COLOR         {            float3 normalDirection = normalize(input.normalDir);            float3 viewDirection = normalize(               _WorldSpaceCameraPos - input.posWorld.xyz);            normalDirection = faceforward(normalDirection,               -viewDirection, normalDirection);               // flip normal if dot(-viewDirection, normalDirection)>0            float3 lightDirection;            float attenuation;            if (0.0 == _WorldSpaceLightPos0.w) // directional light?            {               attenuation = 1.0; // no attenuation               lightDirection = normalize(_WorldSpaceLightPos0.xyz);            }             else // point or spot light            {               float3 vertexToLightSource =                   _WorldSpaceLightPos0.xyz - input.posWorld.xyz;               float distance = length(vertexToLightSource);               attenuation = 1.0 / distance; // linear attenuation                lightDirection = normalize(vertexToLightSource);            }            // Computation of the Phong reflection model:            float3 ambientLighting =                UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb;            float3 diffuseReflection =                attenuation * _LightColor0.rgb * _Color.rgb               * max(0.0, dot(normalDirection, lightDirection));            float3 specularReflection;            if (dot(normalDirection, lightDirection) < 0.0)                // light source on the wrong side?            {               specularReflection = float3(0.0, 0.0, 0.0);                   // no specular reflection            }            else // light source on the right side            {               specularReflection = attenuation * _LightColor0.rgb                   * _SpecColor.rgb * pow(max(0.0, dot(                  reflect(-lightDirection, normalDirection),                   viewDirection)), _Shininess);            }            // Computation of the translucent illumination:            float3 diffuseTranslucency =                attenuation * _LightColor0.rgb                * _DiffuseTranslucentColor.rgb                * max(0.0, dot(lightDirection, -normalDirection));            float3 forwardTranslucency;            if (dot(normalDirection, lightDirection) > 0.0)                // light source on the wrong side?            {               forwardTranslucency = float3(0.0, 0.0, 0.0);                   // no forward-scattered translucency            }            else // light source on the right side            {               forwardTranslucency = attenuation * _LightColor0.rgb                  * _ForwardTranslucentColor.rgb * pow(max(0.0,                   dot(-lightDirection, viewDirection)), _Sharpness);            }            // Computation of the complete illumination:            return float4(ambientLighting                + diffuseReflection + specularReflection                + diffuseTranslucency + forwardTranslucency, 1.0);         }         ENDCG      }      Pass {               Tags { "LightMode" = "ForwardAdd" }             // pass for additional light sources         Cull Off         Blend One One // additive blending          CGPROGRAM         #pragma vertex vert           #pragma fragment frag          #include "UnityCG.cginc"         uniform float4 _LightColor0;             // color of light source (from "Lighting.cginc")         // User-specified properties         uniform float4 _Color;          uniform float4 _SpecColor;          uniform float _Shininess;         uniform float4 _DiffuseTranslucentColor;          uniform float4 _ForwardTranslucentColor;          uniform float _Sharpness;         struct vertexInput {            float4 vertex : POSITION;            float3 normal : NORMAL;         };         struct vertexOutput {            float4 pos : SV_POSITION;            float4 posWorld : TEXCOORD0;            float3 normalDir : TEXCOORD1;         };         vertexOutput vert(vertexInput input)          {            vertexOutput output;            float4x4 modelMatrix = _Object2World;            float4x4 modelMatrixInverse = _World2Object;            output.posWorld = mul(modelMatrix, input.vertex);            output.normalDir = normalize(               mul(float4(input.normal, 0.0), modelMatrixInverse).xyz);            output.pos = mul(UNITY_MATRIX_MVP, input.vertex);            return output;         }         float4 frag(vertexOutput input) : COLOR         {            float3 normalDirection = normalize(input.normalDir);            float3 viewDirection = normalize(               _WorldSpaceCameraPos - input.posWorld.xyz);            normalDirection = faceforward(normalDirection,               -viewDirection, normalDirection);               // flip normal if dot(-viewDirection, normalDirection)>0            float3 lightDirection;            float attenuation;            if (0.0 == _WorldSpaceLightPos0.w) // directional light?            {               attenuation = 1.0; // no attenuation               lightDirection = normalize(_WorldSpaceLightPos0.xyz);            }             else // point or spot light            {               float3 vertexToLightSource =                   _WorldSpaceLightPos0.xyz - input.posWorld.xyz;               float distance = length(vertexToLightSource);               attenuation = 1.0 / distance; // linear attenuation                lightDirection = normalize(vertexToLightSource);            }            // Computation of the Phong reflection model:            float3 diffuseReflection =                attenuation * _LightColor0.rgb * _Color.rgb               * max(0.0, dot(normalDirection, lightDirection));            float3 specularReflection;            if (dot(normalDirection, lightDirection) < 0.0)                // light source on the wrong side?            {               specularReflection = float3(0.0, 0.0, 0.0);                   // no specular reflection            }            else // light source on the right side            {               specularReflection = attenuation * _LightColor0.rgb                   * _SpecColor.rgb * pow(max(0.0, dot(                  reflect(-lightDirection, normalDirection),                   viewDirection)), _Shininess);            }            // Computation of the translucent illumination:            float3 diffuseTranslucency =                attenuation * _LightColor0.rgb                * _DiffuseTranslucentColor.rgb                * max(0.0, dot(lightDirection, -normalDirection));            float3 forwardTranslucency;            if (dot(normalDirection, lightDirection) > 0.0)                // light source on the wrong side?            {               forwardTranslucency = float3(0.0, 0.0, 0.0);                   // no forward-scattered translucency            }            else // light source on the right side            {               forwardTranslucency = attenuation * _LightColor0.rgb                  * _ForwardTranslucentColor.rgb * pow(max(0.0,                   dot(-lightDirection, viewDirection)), _Sharpness);            }            // Computation of the complete illumination:            return float4(diffuseReflection + specularReflection                + diffuseTranslucency + forwardTranslucency, 1.0);         }         ENDCG      }   }}

总结

恭喜!你完成了半透明表面的教程,它非常常见但又不能用Phone反射模型来建模。我们学到了:

  • 什么是半透明表面。
  • 哪种半透明是最常见的(漫射半透明和前向散射半透明)。
  • 如何实现漫射半透明和前向散射半透明。