树叶边缘渲染

来源:互联网 发布:macbook卸载软件失败 编辑:程序博客网 时间:2024/04/27 21:53

树叶边缘渲染一般的做法就是使用alpha test,但这样做在边缘上会出现一些很“碎”的效果,特别是离摄像机远的时候。

如果要边缘效果好,就要采用alpha blend的方式,但这种方法有排序的问题,会造成前后错乱。

之前看斗战神的草,发现边缘是用blend的效果,但没有排序问题。当时有美术同事问如何做到的,一时我也答不出来。

后来翻译unity3d官方文档的时候,发现有个例子提供了解决方案:只在边缘进行alpha blend,其它部位仍然使用alpha test来渲染。这样既保证了边缘的柔和,也没有渲染顺序的问题,因为边缘只是很小的部分,边缘本身的排序问题几乎不会被发现。

以下是原文:


When rendering plants and trees, many games have the hard edges typical of alpha testing. A way around that is to render the object twice. In the first pass, we use alpha testing to only render pixels that are more than 50% opaque. In the second pass, we alpha-blend the graphic in the parts that were cut away, without recording the depth of the pixel. We might get a bit of confusion as further away branches overwrite the nearby ones, but in practice, that is hard to see as leaves have a lot of visual detail in them.

当渲染植物和树木的时候,许多游戏都由于alpha测试的问题产生了硬边。解决这个问题的一个方法是将对象渲染两遍。第一遍,我们使用alpha测试的方式来渲染,但只是渲染那些不透明度大于50%的像素。第二遍,我们反过来将一遍没有渲染的像素进行alpha混合方式的渲染,并不进行深度操作。理论上,我们也许会看到远距离的像素覆盖近距离的像素,但实际上,我们很难在大量叶子的细节上发现这个问题。

Shader "Vegetation" {    Properties {        _Color ("Main Color", Color) = (.5, .5, .5, .5)        _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}        _Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5    }    SubShader {        // Set up basic lighting        Material {            Diffuse [_Color]            Ambient [_Color]        }        Lighting On        // Render both front and back facing polygons.        Cull Off        // first pass:        // render any pixels that are more than [_Cutoff] opaque        Pass {            AlphaTest Greater [_Cutoff]            SetTexture [_MainTex] {                combine texture * primary, texture            }        }        // Second pass:        // render in the semitransparent details.        Pass {            // Dont write to the depth buffer            ZWrite off            // Don't write pixels we have already written.            ZTest Less            // Only render pixels less or equal to the value            AlphaTest LEqual [_Cutoff]            // Set up alpha blending            Blend SrcAlpha OneMinusSrcAlpha            SetTexture [_MainTex] {                combine texture * primary, texture            }        }    }}

0 0
原创粉丝点击