Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Cg Programming/Unity/Translucent Surfaces

From Wikibooks, open books for an open world
<Cg Programming |Unity
Leaves lit from both sides: note that the missing specular reflection results in a more saturated green of the backlit leaves.

This tutorial coverstranslucent surfaces.

It is one of several tutorials about lighting that go beyond the Phong reflection model. However, it is based on per-pixel lighting with the Phong reflection model as described inSection “Smooth Specular Highlights”. If you haven't read that tutorial yet, you should read it first.

The Phong reflection model doesn't take translucency into account, i.e. the possibility that light is transmitted through a material. This tutorial is about translucent surfaces, i.e. surfaces that allow light to transmit from one face to the other, e.g. paper, clothes, plastic films, or leaves.

For translucent illumination, the vector V to the viewer and the vector L to the light source are on opposite sides.

Diffuse Translucency

[edit |edit source]

We will distinguish between two kinds of light transmission: diffuse translucency and forward-scattered translucency, which correspond to the diffuse and specular terms in the Phong reflection model. Diffuse translucency is a diffuse transmission of light analogously to the diffuse reflection term in the Phong reflection model (seeSection “Diffuse Reflection”): it only depends on the dot product of the surface normal vector and the direction to the light source — except that we use the negative surface normal vector since the light source is on the backside, thus the equation for the diffuse translucent illumination is:

Idiffuse trans.=Iincomingkdiffuse trans.max(0,L(N)){\displaystyle I_{\text{diffuse trans.}}=I_{\text{incoming}}\,k_{\text{diffuse trans.}}\max(0,\mathbf {L} \cdot (-\mathbf {N} ))}

This is the most common illumination for many translucent surfaces, e.g. paper and leaves.

Forward-Scattered Translucency

[edit |edit source]

Some translucent surfaces (e.g. plastic films) are almost transparent and allow light to shine through the surface almost directly but with some forward scattering; i.e., one can see light sources through the surface but the image is somewhat blurred. This is similar to the specular term of the Phong reflection model (seeSection “Specular Highlights” for the equation) except that we replace the reflected light directionR by the negative light direction -L and the exponentnshininess{\displaystyle n_{\text{shininess}}} corresponds now to the sharpness of the forward-scattered light:

Iforward trans.=Iincomingkforward trans.max(0,LV)nsharpness{\displaystyle I_{\text{forward trans.}}=I_{\text{incoming}}\,k_{\text{forward trans.}}\max(0,\mathbf {-L} \cdot \mathbf {V} )^{n_{\text{sharpness}}}}

Of course, this model of forward-scattered translucency is not accurate at all but it allows us to fake the effect and tweak the parameters.

Implementation

[edit |edit source]

The following implementation is based onSection “Smooth Specular Highlights”, which presents per-pixel lighting with the Phong reflection model. The implementation allows for rendering backfaces and flips the surface normal vector in this case using the built-in Cg functionfaceforward(n, v, ng) which returnsn ifdot(v,ng)<0 and-n otherwise. This method often fails at silhouettes, which results in incorrect lighting for some pixels. An improved version would use different passes and colors for the frontfaces and the backfaces as inSection “Two-Sided Smooth Surfaces”.

In addition to the terms of the Phong reflection model, we also compute illumination by diffuse translucency and forward-scattered translucency with this code:

float3diffuseTranslucency=attenuation*_LightColor0.rgb*_DiffuseTranslucentColor.rgb*max(0.0,dot(lightDirection,-normalDirection));float3forwardTranslucency;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);}

Complete Shader Code

[edit |edit source]

The complete shader code defines the shader properties for the material constants and adds another pass for additional light sources with additive blending but without the ambient lighting:

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 sourceCullOff// show frontfaces and backfacesCGPROGRAM#pragma vertex vert#pragma fragment frag#include"UnityCG.cginc"uniformfloat4_LightColor0;// color of light source (from "Lighting.cginc")// User-specified propertiesuniformfloat4_Color;uniformfloat4_SpecColor;uniformfloat_Shininess;uniformfloat4_DiffuseTranslucentColor;uniformfloat4_ForwardTranslucentColor;uniformfloat_Sharpness;structvertexInput{float4vertex:POSITION;float3normal:NORMAL;};structvertexOutput{float4pos:SV_POSITION;float4posWorld:TEXCOORD0;float3normalDir:TEXCOORD1;};vertexOutputvert(vertexInputinput){vertexOutputoutput;float4x4modelMatrix=unity_ObjectToWorld;float4x4modelMatrixInverse=unity_WorldToObject;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);returnoutput;}float4frag(vertexOutputinput):COLOR{float3normalDirection=normalize(input.normalDir);float3viewDirection=normalize(_WorldSpaceCameraPos-input.posWorld.xyz);normalDirection=faceforward(normalDirection,-viewDirection,normalDirection);// flip normal if dot(-viewDirection, normalDirection)>0float3lightDirection;floatattenuation;if(0.0==_WorldSpaceLightPos0.w)// directional light?{attenuation=1.0;// no attenuationlightDirection=normalize(_WorldSpaceLightPos0.xyz);}else// point or spot light{float3vertexToLightSource=_WorldSpaceLightPos0.xyz-input.posWorld.xyz;floatdistance=length(vertexToLightSource);attenuation=1.0/distance;// linear attenuationlightDirection=normalize(vertexToLightSource);}// Computation of the Phong reflection model:float3ambientLighting=UNITY_LIGHTMODEL_AMBIENT.rgb*_Color.rgb;float3diffuseReflection=attenuation*_LightColor0.rgb*_Color.rgb*max(0.0,dot(normalDirection,lightDirection));float3specularReflection;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:float3diffuseTranslucency=attenuation*_LightColor0.rgb*_DiffuseTranslucentColor.rgb*max(0.0,dot(lightDirection,-normalDirection));float3forwardTranslucency;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:returnfloat4(ambientLighting+diffuseReflection+specularReflection+diffuseTranslucency+forwardTranslucency,1.0);}ENDCG}Pass{Tags{"LightMode"="ForwardAdd"}// pass for additional light sourcesCullOffBlendOneOne// additive blendingCGPROGRAM#pragma vertex vert#pragma fragment frag#include"UnityCG.cginc"uniformfloat4_LightColor0;// color of light source (from "Lighting.cginc")// User-specified propertiesuniformfloat4_Color;uniformfloat4_SpecColor;uniformfloat_Shininess;uniformfloat4_DiffuseTranslucentColor;uniformfloat4_ForwardTranslucentColor;uniformfloat_Sharpness;structvertexInput{float4vertex:POSITION;float3normal:NORMAL;};structvertexOutput{float4pos:SV_POSITION;float4posWorld:TEXCOORD0;float3normalDir:TEXCOORD1;};vertexOutputvert(vertexInputinput){vertexOutputoutput;float4x4modelMatrix=unity_ObjectToWorld;float4x4modelMatrixInverse=unity_WorldToObject;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);returnoutput;}float4frag(vertexOutputinput):COLOR{float3normalDirection=normalize(input.normalDir);float3viewDirection=normalize(_WorldSpaceCameraPos-input.posWorld.xyz);normalDirection=faceforward(normalDirection,-viewDirection,normalDirection);// flip normal if dot(-viewDirection, normalDirection)>0float3lightDirection;floatattenuation;if(0.0==_WorldSpaceLightPos0.w)// directional light?{attenuation=1.0;// no attenuationlightDirection=normalize(_WorldSpaceLightPos0.xyz);}else// point or spot light{float3vertexToLightSource=_WorldSpaceLightPos0.xyz-input.posWorld.xyz;floatdistance=length(vertexToLightSource);attenuation=1.0/distance;// linear attenuationlightDirection=normalize(vertexToLightSource);}// Computation of the Phong reflection model:float3diffuseReflection=attenuation*_LightColor0.rgb*_Color.rgb*max(0.0,dot(normalDirection,lightDirection));float3specularReflection;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:float3diffuseTranslucency=attenuation*_LightColor0.rgb*_DiffuseTranslucentColor.rgb*max(0.0,dot(lightDirection,-normalDirection));float3forwardTranslucency;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:returnfloat4(diffuseReflection+specularReflection+diffuseTranslucency+forwardTranslucency,1.0);}ENDCG}}}

Summary

[edit |edit source]

Congratulations! You finished this tutorial on translucent surfaces, which are very common but cannot be modeled by the Phong reflection model. We have covered:

  • What translucent surfaces are.
  • Which forms of translucency are most common (diffuse translucency and forward-scattered translucency).
  • How to implement diffuse and forward-scattered translucency.

Further reading

[edit |edit source]

If you still want to know more

<Cg Programming/Unity

Unless stated otherwise, all example source code on this page is granted to the public domain.
Retrieved from "https://en.wikibooks.org/w/index.php?title=Cg_Programming/Unity/Translucent_Surfaces&oldid=3677720"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp