Blaze/Assets/Shaders/Material.slang

63 lines
1.9 KiB
Plaintext

import Bindless;
public struct Material {
float4 baseColor;
float4 emissiveColor;
Sampler2D.RID albedoTextureID;
Sampler2D.RID normalTextureID;
Sampler2D.RID metalRoughTextureID;
Sampler2D.RID emissiveTextureID;
float metallic;
float roughness;
public float4 getAlbedo(float2 uv, float4 inColor) {
if (let albedoTex = albedoTextureID) {
return baseColor * albedoTex.Sample(uv).rgba;
}
return baseColor;
}
public float3 getEmissive(float2 uv) {
if (let emissionTex = emissiveTextureID) {
return emissionTex.Sample(uv).rgb * emissiveColor.rgb * emissiveColor.a;
}
return emissiveColor.rgb * emissiveColor.a;
}
public float3 getNormal(float3 position, float3 normal, float4 tangent, float2 uv) {
float3 N = normalize(normal.xyz);
if (let normalTex = normalTextureID) {
let vNt = normalize(2.0f * normalTex.Sample(uv).rgb - 1.0f);
float3 T;
float3 B;
if (tangent.w == 0.0f) {
float3 q1 = ddx(position);
float3 q2 = ddy(position);
float2 st1 = ddx(uv);
float2 st2 = ddy(uv);
float det = (st1.x * st2.y - st2.x * st1.y);
T = -(q1 * st2.y - q2 * st1.y) / det;
T = T - N * dot(N, T);
B = normalize(cross(N, T));
} else {
T = normalize(tangent.xyz);
B = tangent.w * cross(N, T);
}
N = normalize(T * vNt.x + B * vNt.y + N * vNt.z);
}
return N;
}
public float2 getMetalRough(float2 uv) {
if (let metalRoughTex = metalRoughTextureID) {
return metalRoughTex.Sample(uv).bg * float2(metallic, roughness);
}
return float2(metallic, roughness);
}
}