中級Shader教程19 動態霧

2021-08-19 11:04:24 字數 2303 閱讀 7422

layout: post

title: 「中級shader教程19 動態霧」

date: 2018-04-23 16:09:03

author: jiepeng tan

categories:

1.noise和fbm請參考這篇文章中給出的鏈結

2.ranymarching 框架

1.利用fbm來模擬空間霧的密度分布

2.通過raymarch 距離根據密度從前到後多層顏色混合

這裡的noise 使用trinoise,其實perlinnoise 也行,不過perlinnoise,需要的指令較多

1.noise

float

_tri

(in float x)

float2 _tri2

(in float2 p)

float3 _tri3

(in float3 p)

// float

tnoise

(in float3 p,

float time,

float spd)

return rz;

}

整個場景中noise 分布 效果如下

通過fbm 給整個場景中的noise 新增更多的變化,然後通過raymarch 從前王rayhit 的方向進行顏色混合來模擬fog 對於最終場景的影響。

注意這裡因為速度問題,我們取樣的量不會太多,為了是霧氣效果平滑的過渡,我們需要對取樣點之間進行插值過渡

fixed3 fog

(in fixed3 bgcol, in fixed3 ro, in fixed3 rd, in fixed maxt,

float3 fogcol,float3 spd,float2 heightrange)

return col;

}

// create by jiepengtan 2018-04-13  

// email: [email protected]

shader "fishmanshadertutorial/fog"

_loopnum (

"_loopnum"

, vector)=(

40.,

128.,1

,1) _fogspd (

"_fogspd"

, vector)=(

1.,0.,

0.,0.5)

_foghighrange (

"_foghighrange"

, vector)=(

-5,10

,0.,0.5

) _fogcol (

"_fogcol"

, color)=(

.025,.2

,.125,0.

)}subshader

fixed raycast

(in fixed3 ro, in fixed3 rd)

float d =

-(ro.y -0.)

/rd.y;

d =min(

100000.0

, d)

;return d;

} float4 processraymarch

(float2 uv,float3 ro,float3 rd,inout float scenedep,float4 scenecol)

mergeraymarchingintounity

(rz,col,scenedep,scenecol)

;

col =

lerp

(col, fogb,

smoothstep

(far-

7.,far,rz));

//then volumetric fog

col =

fog(col, ro, rd, rz,_fogcol,_fogspd,_foghighrange)

;//post

col =

pow(col,

float3

(0.8

,0.8

,0.8))

; scenecol.xyz = col;

return scenecol;

} endcg

}//end pass

}//end subshader

fallback off

}

中級Shader教程16 水渲染

layout post title 中級shader教程16 水渲染 date 2018 04 23 16 09 03 author jiepeng tan categories 1.noise和fbm請參考這篇文章中給出的鏈結 2.ranymarching 框架 1.基本形狀構造 這裡利用了noi...

中級Shader教程05 2D雪花

layout post title 中級shader教程05 2d雪花 date 2018 03 27 16 09 03 author jiepeng tan categories 本篇主要技術點有 grid 空間劃分 2d模擬3d中 分層 的概念 透視模擬 在前面中介紹了2d模擬3d中的一些小技巧...

中級Shader教程06 2D火焰粒子

layout post title 中級shader教程06 2d火焰粒子 date 2018 03 27 16 09 03 author jiepeng tan categories grid 空間劃分 2d模擬3d中 分層 的概念 透視模擬 基於grid的隨機變化 旋轉,位移,閃爍 sin 週期...