From 8a33809c73e79aacdcc9a8a9030a5594cde5ff9c Mon Sep 17 00:00:00 2001 From: Lightling Date: Tue, 19 Apr 2022 21:03:55 -0400 Subject: [PATCH] general copy of standard shader to toon shader --- Game.cpp | 18 ++++++++++------- ToonShader.hlsl | 51 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/Game.cpp b/Game.cpp index c7e789b..8833fd6 100644 --- a/Game.cpp +++ b/Game.cpp @@ -188,6 +188,10 @@ void Game::LoadTextures() materials[8]->PushSampler("BasicSampler", sampler); materials[8]->LoadTexture(L"Assets/Textures/HQGame/structure-endgame-floor_albedo.png", TEXTYPE_ALBEDO, device.Get(), context.Get()); 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()); } // -------------------------------------------------------- @@ -195,16 +199,16 @@ void Game::LoadTextures() // -------------------------------------------------------- void Game::LoadLighting() { - ambient = XMFLOAT3(0.1f, 0.1f, 0.15f); + ambient = XMFLOAT3(0.01f, 0.01f, 0.015f); lights = { Light::Directional(XMFLOAT3(1, 0.5f, -0.5f), XMFLOAT3(1, 1, 1), 1.0f), - Light::Directional(XMFLOAT3(-0.25f, -1, 0.75f), XMFLOAT3(1, 1, 1), 0.25f), - Light::Directional(XMFLOAT3(-1, 1, -0.5f), XMFLOAT3(1, 1, 1), 0.25f), - Light::Point(XMFLOAT3(-1.5f, 0, 0), XMFLOAT3(1, 1, 1), 0.35f, 10), - Light::Point(XMFLOAT3(1.5f, 0, 0), XMFLOAT3(1, 1, 1), 0.35f, 10), - Light::Point(XMFLOAT3(0, 2, 0), XMFLOAT3(1, 0, 0), 0.35f, 10), - Light::Point(XMFLOAT3(-27.5f, 0, 0), XMFLOAT3(1, 1, 0.5f), 0.35f, 20), + //Light::Directional(XMFLOAT3(-0.25f, -1, 0.75f), XMFLOAT3(1, 1, 1), 0.25f), + //Light::Directional(XMFLOAT3(-1, 1, -0.5f), XMFLOAT3(1, 1, 1), 0.25f), + //Light::Point(XMFLOAT3(-1.5f, 0, 0), XMFLOAT3(1, 1, 1), 0.35f, 10), + //Light::Point(XMFLOAT3(1.5f, 0, 0), XMFLOAT3(1, 1, 1), 0.35f, 10), + //Light::Point(XMFLOAT3(0, 2, 0), XMFLOAT3(1, 0, 0), 0.35f, 10), + //Light::Point(XMFLOAT3(-27.5f, 0, 0), XMFLOAT3(1, 1, 0.5f), 0.35f, 20), }; } diff --git a/ToonShader.hlsl b/ToonShader.hlsl index c22ed58..f6482f1 100644 --- a/ToonShader.hlsl +++ b/ToonShader.hlsl @@ -7,31 +7,64 @@ cbuffer ExternalData : register(b0) { float3 cameraPosition; - float roughness; + int hasNormalMap; float2 offset; float2 scale; float3 ambient; - float emitAmount; - - float3 tint; float lightCount; - int hasEmissiveMap; + float3 tint; int hasSpecularMap; - int hasNormalMap; Light lights[MAX_LIGHTS]; } Texture2D Albedo : register(t0); Texture2D Specular : register(t1); -Texture2D Emissive : register(t2); -Texture2D Normal : register(t3); +Texture2D Normal : register(t2); SamplerState BasicSampler : register(s0); float4 main(VertexToPixel input) : SV_TARGET { - return float4(1.0f, 1.0f, 1.0f, 1.0f); + input.normal = normalize(input.normal); + input.tangent = normalize(input.tangent); + input.uv = input.uv * scale + offset; + float3 view = getView(cameraPosition, input.worldPosition); + + if (hasNormalMap > 0) + { + float3 unpackedNormal = Normal.Sample(BasicSampler, input.uv).rgb * 2 - 1; + float3 T = normalize(input.tangent - input.normal * dot(input.tangent, input.normal)); + float3 B = cross(T, input.normal); + float3x3 TBN = float3x3(T, B, input.normal); + input.normal = mul(unpackedNormal, TBN); + } + + float specular = 1; + if (hasSpecularMap > 0) + { + specular = Specular.Sample(BasicSampler, input.uv).r; + } + + float4 albedo = pow(Albedo.Sample(BasicSampler, input.uv).rgba, 2.2f); + float3 surface = albedo.rgb * tint; + float3 light = ambient * surface; + for (int i = 0; i < lightCount; i++) + { + switch (lights[i].Type) + { + case LIGHT_TYPE_DIRECTIONAL: + light += calculateDirectionalLight(lights[i], -input.normal, view, 1 /*roughness*/, surface, specular); + break; + case LIGHT_TYPE_POINT: + light += calculatePointLight(lights[i], input.normal, view, input.worldPosition, 1 /*roughness*/, surface, specular); + break; + } + } + + float3 final = float3(light/* + (emit * emitAmount)*/); + + return float4(pow(final, 1.0f / 2.2f), albedo.a); } \ No newline at end of file