project-aster/samples/03_model_render/shader/model.ps.hlsl

116 lines
3.4 KiB
HLSL

#include "bindless_structs.hlsli"
struct FS_Input
{
float4 inPosition : POSITION;
float4 inNormal : NORMAL;
float4 inColor : COLOR0;
float2 inUV : TEXCOORD0;
};
struct FS_Output
{
float4 outColor : SV_Target0;
};
float4 GetAlbedo(uint materialBufferId, int materialId, float2 uv)
{
uint albedoTexId = materialsBuffer[materialBufferId][materialId].m_AlbedoTex;
if (albedoTexId == INVALID_HANDLE)
{
return (float4) materialsBuffer[materialBufferId][materialId].m_AlbedoFactor;
}
else
{
return textures[albedoTexId].Sample(immutableSamplers[albedoTexId], uv);
}
}
float3 GetDirectionalLightInfluence(float3 normal)
{
if (pcb.lightHandle == INVALID_HANDLE)
return float3(0.0f, 0.0f, 0.0f);
uint count = IndexerCount(pcb.directionLightIndexer);
float3 contrib = 0.0f;
for (uint i = 0; i < count; ++i)
{
Light light = lightBuffer[pcb.lightHandle][i];
float3 lightDir = - (float3) light.m_Position; // Position is actually direction for directionalLight; LightDir is Direction towards the light (-direction)
float diff = max(dot(normal, lightDir), 0.0f);
int ur = (light.m_Color & 0xFF000000) >> 24;
int ug = (light.m_Color & 0x00FF0000) >> 16;
int ub = (light.m_Color & 0x0000FF00) >> 8;
float r = ur;
float g = ug;
float b = ub;
float3 color = float3(r, g, b) * 0.00392156862f; // 0.00392156862 = 1/255
float3 diffuse = diff * color;
contrib += (light.m_Range < 0 ? float3(0.0f, 0.0f, 0.0f) : diffuse);
}
return contrib;
}
float3 GetPointLightInfluence(float3 position, float3 normal)
{
if (pcb.lightHandle == INVALID_HANDLE)
return float3(0.0f, 0.0f, 0.0f);
uint offset = IndexerOffset(pcb.pointLightIndexer);
uint count = IndexerCount(pcb.pointLightIndexer);
float3 contrib = 0.0f;
for (uint i = 0; i < count; ++i)
{
Light light = lightBuffer[pcb.lightHandle][i + offset];
float3 lightDir = normalize(((float3)light.m_Position) - position);
float diff = max(dot(normal, lightDir), 0.0f);
int ur = (light.m_Color & 0xFF000000) >> 24;
int ug = (light.m_Color & 0x00FF0000) >> 16;
int ub = (light.m_Color & 0x0000FF00) >> 8;
float r = ur;
float g = ug;
float b = ub;
float3 color = float3(r, g, b) * 0.00392156862f; // 0.00392156862 = 1/255
float3 diffuse = diff * color;
contrib += (light.m_Range < 0 ? float3(0.0f, 0.0f, 0.0f) : diffuse);
}
return contrib;
}
FS_Output
main(FS_Input stage_input)
{
//// Hereby assume that we always have a point light at
//float3 lightPos = float3(6.0f, 6.0f, 6.0f);
//// with
//float3 lightColor = float3(0.7f, 0.7f, 0.7f); //float3(0.7f, 0.4f, 0.1f);
//// and
float3 ambient = float3(0.02f, 0.02f, 0.02f);
float3 norm = normalize(stage_input.inNormal.xyz);
float3 pos = stage_input.inPosition.xyz;
float4 objColor = pcb.m_MaterialIdx < 0 ? stage_input.inColor : GetAlbedo(pcb.materialBufferHandle, pcb.m_MaterialIdx, stage_input.inUV);
float3 diffuse = GetDirectionalLightInfluence(norm) + GetPointLightInfluence(pos, norm);
FS_Output output;
output.outColor = float4(objColor.rgb * (diffuse + ambient), objColor.a);
return output;
}