參考來源:?
?天空盒教程第 1 部分 |開爾文·范·霍恩 (kelvinvanhoorn.com)
【程序化天空盒】過程記錄02:云擾動(dòng) 邊緣光 消散效果_九九345的博客-CSDN博客
程序化天空盒實(shí)現(xiàn)晝夜變換 - 知乎 (zhihu.com)
一、太陽(yáng)?
? ? ? ? 目標(biāo):改變光的方向,使天空球旋轉(zhuǎn)(日夜交替);光的正方向:太陽(yáng),光的反方向:月亮;
? ? ? ? 步驟一:計(jì)算天空球上的uv坐標(biāo)與光的方向的距離,越與光的方向重合,越接近0,貼圖越黑。(天空盒uv為xyz三個(gè)方向)
? ? ? ? 步驟二:除以半徑創(chuàng)建圓形,然后反相。
? ? ? ? smoothstep:可以用來生成0到1的平滑過渡值,它也叫平滑階梯函數(shù)。
代碼:
Shader "URP_Skybox"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_SunRadius ("太陽(yáng)半徑", float) = 1.0
}
SubShader
{
Tags { "RenderType"="Background" "RenderPipeline"="UniversalPipeline"}
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
CBUFFER_START(UnityPerMaterial)
float _SunRadius;
CBUFFER_END
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
struct appdata
{
float4 vertex : POSITION;
float3 uv : TEXCOORD0;
};
struct v2f
{
float3 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 posWS : TEXCOORD1;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = TransformObjectToHClip(v.vertex);
o.uv = v.uv;
o.posWS = mul(unity_ObjectToWorld, v.vertex);
return o;
}
float4 frag (v2f i) : SV_Target
{
Light light = GetMainLight(TransformWorldToShadowCoord(i.posWS));
half sunDist = distance(i.uv.xyz, light.direction);
//smoothstep:大于等于0.7 為0,小于等于1 為1
half sunArea = 1-smoothstep(0.7, 1, sunDist * _SunRadius);
return float4(sunArea,sunArea,sunArea,1);
}
ENDHLSL
}
}
}
二、月亮
? ? ? ? 目標(biāo):月亮定位、貼圖采樣等 月亮使用cubemap,可以旋轉(zhuǎn)。
? ? ? ? 步驟一:光線追蹤球體信息并從中創(chuàng)建模板(使用sphIntersect),如果沒有相交就創(chuàng)建遮罩。
? ? ? ? 步驟二:計(jì)算照明(NdotL),需要法線(視線方向-月亮頂點(diǎn)方向),光線方向?yàn)樘?yáng)光方向的反向。
文章來源:http://www.zghlxwxcb.cn/news/detail-555272.html
? ? ? ? ?步驟三:添加曝光度控制月亮的亮度,采樣月亮cubemap紋理,并使用矩陣在月亮變換的時(shí)候自旋轉(zhuǎn)。文章來源地址http://www.zghlxwxcb.cn/news/detail-555272.html
Shader "URP_Skybox"
{
Properties
{
[Header(Moon)]
[NoScaleOffset] _MoonCubeMap ("月亮貼圖", Cube) = "_Skybox" {}
_MoonRadius ("月亮半徑", Range(0, 1)) = 0.05
_MoonMaskRadius("月亮遮罩半徑", range(1, 25)) = 10
_MoonSmooth("月亮邊緣平滑度", Range(0, 1)) = 0.7
_MoonExposure("月亮曝光度", range(0, 1)) = 1
[Header(Sun)]
_SunRadius
到了這里,關(guān)于基于URP的程序化天空盒的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!