From d04a1afb664a2c0a050bd2a97e1e3b79e379aab9 Mon Sep 17 00:00:00 2001 From: Lightling Date: Tue, 19 Apr 2022 21:32:18 -0400 Subject: [PATCH] implement toon shading basics --- Game.cpp | 6 ++++-- SimplePixelShader.hlsl | 2 +- ToonShader.hlsl | 34 ++++++++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/Game.cpp b/Game.cpp index 8833fd6..76ea700 100644 --- a/Game.cpp +++ b/Game.cpp @@ -190,8 +190,10 @@ void Game::LoadTextures() materials[8]->LoadTexture(L"Assets/Textures/HQGame/structure-endgame-floor_specular.png", TEXTYPE_SPECULAR, device.Get(), context.Get()); materials[9]->PushSampler("BasicSampler", sampler); - materials[9]->LoadTexture(L"Assets/Textures/HQGame/structure-endgame-deepfloor_albedo.png", TEXTYPE_ALBEDO, device.Get(), context.Get()); - materials[9]->LoadTexture(L"Assets/Textures/HQGame/structure-endgame-deepfloor_specular.png", TEXTYPE_SPECULAR, device.Get(), context.Get()); + materials[9]->SetRoughness(1); + materials[9]->LoadTexture(L"Assets/Textures/WithNormals/cushion.png", TEXTYPE_ALBEDO, device.Get(), context.Get()); + materials[9]->LoadTexture(L"Assets/Textures/WithNormals/cushion_normals.png", TEXTYPE_NORMAL, device.Get(), context.Get()); + materials[9]->LoadTexture(L"Assets/Textures/WithNormals/cushion_specular.png", TEXTYPE_SPECULAR, device.Get(), context.Get()); } // -------------------------------------------------------- diff --git a/SimplePixelShader.hlsl b/SimplePixelShader.hlsl index 7427528..642b8e2 100644 --- a/SimplePixelShader.hlsl +++ b/SimplePixelShader.hlsl @@ -66,7 +66,7 @@ float4 main(VertexToPixel input) : SV_TARGET switch (lights[i].Type) { case LIGHT_TYPE_DIRECTIONAL: - light += calculateDirectionalLight(lights[i], input.normal, view, roughness, surface, specular); + light += calculateDirectionalLight(lights[i], -input.normal, view, roughness, surface, specular); break; case LIGHT_TYPE_POINT: light += calculatePointLight(lights[i], input.normal, view, input.worldPosition, roughness, surface, specular); diff --git a/ToonShader.hlsl b/ToonShader.hlsl index f6482f1..0530c5a 100644 --- a/ToonShader.hlsl +++ b/ToonShader.hlsl @@ -16,6 +16,8 @@ cbuffer ExternalData : register(b0) float lightCount; float3 tint; + + float roughness; int hasSpecularMap; Light lights[MAX_LIGHTS]; @@ -26,6 +28,22 @@ Texture2D Specular : register(t1); Texture2D Normal : register(t2); SamplerState BasicSampler : register(s0); +float RampDiffuse(float original) +{ + if (original < 0.25f) return 0.0f; + //if (original < 0.5f) return 0.5f; + if (original < 0.75f) return 0.75f; + + return 1; +} + +float RampSpecular(float original) +{ + if (original < 0.95f) return 0.0f; + + return 1.0f; +} + float4 main(VertexToPixel input) : SV_TARGET { input.normal = normalize(input.normal); @@ -42,10 +60,10 @@ float4 main(VertexToPixel input) : SV_TARGET input.normal = mul(unpackedNormal, TBN); } - float specular = 1; + float specularValue = 1; if (hasSpecularMap > 0) { - specular = Specular.Sample(BasicSampler, input.uv).r; + specularValue = Specular.Sample(BasicSampler, input.uv).r; } float4 albedo = pow(Albedo.Sample(BasicSampler, input.uv).rgba, 2.2f); @@ -53,15 +71,23 @@ float4 main(VertexToPixel input) : SV_TARGET float3 light = ambient * surface; for (int i = 0; i < lightCount; i++) { + float3 toLight = float3(0, 0, 0); + float attenuate = 1; switch (lights[i].Type) { case LIGHT_TYPE_DIRECTIONAL: - light += calculateDirectionalLight(lights[i], -input.normal, view, 1 /*roughness*/, surface, specular); + toLight = normalize(lights[i].Direction); break; case LIGHT_TYPE_POINT: - light += calculatePointLight(lights[i], input.normal, view, input.worldPosition, 1 /*roughness*/, surface, specular); + toLight = normalize(lights[i].Position - input.worldPosition); + attenuate = getAttenuation(lights[i].Position, input.worldPosition, lights[i].Range); break; } + + float diffuse = RampDiffuse(getDiffuse(input.normal, toLight)); + float specular = RampSpecular(calculateSpecular(input.normal, toLight, view, specularValue, diffuse) * roughness); + + light += (diffuse * surface.rgb + specular) * attenuate * lights[i].Intensity * lights[i].Color; } float3 final = float3(light/* + (emit * emitAmount)*/);