unity3d Crease效果分析
生活随笔
收集整理的這篇文章主要介紹了
unity3d Crease效果分析
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
unity3d的Crease描邊效果非常不錯,所以分析了一下這個效果。
用到3個shader:
ConvertDepth //渲染depthmap
CreaseApply //描邊
SeparableBlur//模糊
這個效果的重點是 1.怎么查找邊緣 2.怎么模糊邊緣
unity3d的方法是:
采用2張depthmap,一張清晰,一張模糊,模糊的depthmap上可以看到邊緣都模糊了,兩種圖做差,就知道邊緣在哪里了。
看代碼:return color * (1.0-abs(hrDepth.a-lrDepth.a)*intensity);
假如abs(hrDepth.a-lrDepth.a)即深度差越大,值越大,不是邊緣的部分模糊之后值是不會有變化的,原理很簡單。而且模糊之后這個差也能有漸變的效果,所以最后的描邊可以比較虛幻。但是消耗是很大的,會增加drawcall,overdraw比較高,模糊的shader用到了很多次的圖片采樣,而且是在frag shader里面采樣的,手機上的顯卡填充率本來就不高,根本就用不了。
下面是深度渲染的shader:
Shader "Hidden/ConvertDepth" {Properties {_MainTex ("Base (RGB)", 2D) = "" {}}CGINCLUDE#include "UnityCG.cginc"struct v2f {float4 pos : POSITION;float2 uv : TEXCOORD0;};sampler2D _MainTex;sampler2D _CameraDepthTexture;v2f vert( appdata_img v ) {v2f o;o.pos = mul(UNITY_MATRIX_MVP, v.vertex);o.uv = v.texcoord.xy;return o;}half4 frag(v2f i) : COLOR {float d = UNITY_SAMPLE_DEPTH( tex2D(_CameraDepthTexture, i.uv.xy) );d = Linear01Depth(d);if(d>0.99999)return half4(1,1,1,1);elsereturn half4(d,d,d,1);//灰度顯示的深度圖//return EncodeFloatRGBA(d); }ENDCGSubshader {Pass {ZTest Always Cull Off ZWrite OffFog { Mode off } CGPROGRAM#pragma fragmentoption ARB_precision_hint_fastest#pragma vertex vert#pragma fragment fragENDCG}}Fallback off } 其中的EncodeFloatRGBA函數(shù)是為了能夠更精確的存儲深度值,具體算法如下: // Encoding/decoding [0..1) floats into 8 bit/channel RGBA. Note that 1.0 will not be encoded properly. inline float4 EncodeFloatRGBA( float v ) {float4 kEncodeMul = float4(1.0, 255.0, 65025.0, 160581375.0);float kEncodeBit = 1.0/255.0;float4 enc = kEncodeMul * v;enc = frac (enc);enc -= enc.yzww * kEncodeBit;return enc; } inline float DecodeFloatRGBA( float4 enc ) {float4 kDecodeDot = float4(1.0, 1/255.0, 1/65025.0, 1/160581375.0);return dot( enc, kDecodeDot ); }
總結(jié)
以上是生活随笔為你收集整理的unity3d Crease效果分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux c++ 编译 库,LINUX
- 下一篇: FPGA 设计算法篇 —— 格雷码编解码