为 Unity 编写着色器,我不明白这里发生了什么。我有一个附有材料的球体。材质附加了一个着色器。着色器非常简单,它会生成一些单纯形噪声,然后将其用作球体的颜色。
着色器代码:
Shader "SimplexTest"
{
Properties
{
_SimplexScale("Simplex Scale", Vector) = (4.0,4.0,4.0,1.0)
}
SubShader
{
Tags { "RenderType"="Opaque" "Queue"="Geometry" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "SimplexNoise3D.hlsl"
float3 _SimplexScale;
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 position : SV_POSITION;
float4 color : COLOR0;
};
v2f vert (appdata v)
{
v2f o;
float4 worldPosition = mul(unity_ObjectToWorld, v.vertex);
o.position = UnityObjectToClipPos(v.vertex);
float small = snoise(_SimplexScale * worldPosition);
// o.color = small * 10; // Non-Saturated (A) Version
o.color = saturate(small * 10); // Saturated (B) Version
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return i.color;
}
ENDCG
}
}
}
单纯形噪声函数来自Keijiro 的 Github。
非饱和版本(版本 A)正如预期的那样。通过将单纯形噪声乘以 10 倍,结果是一系列白色和黑色斑点。饱和(版本 B)理论上应该只是在 1 处切掉所有白色斑点,在 0 处切掉黑色斑点。但它似乎正在创建一个全新的渐变,我不知道为什么。
我假设我遗漏了一些关键假设,但我不清楚那会是什么,因为数学似乎是正确的。