From 93488ffc65d78a42af38ef64b902f4090a0b31b8 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Tue, 13 Aug 2024 21:38:00 +0100 Subject: [PATCH] Add proper overbrightened 32bit2x lightmap rendering --- project/code/TMV/Mission.cs | 12 +++++++----- project/materials/base.tres | 9 ++++++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/project/code/TMV/Mission.cs b/project/code/TMV/Mission.cs index 41d84a7..9dd4005 100644 --- a/project/code/TMV/Mission.cs +++ b/project/code/TMV/Mission.cs @@ -99,12 +99,13 @@ public partial class Mission : Node3D if (Dump) DumpTextureList(textureList); var wr = (WorldRep)_file.Chunks["WREXT"]; - - BuildMeshes(wr.Cells); + BuildMeshes(wr.Cells, wr.DataHeader.LightmapFormat == 2); } - private void BuildMeshes(WorldRep.Cell[] cells) + private void BuildMeshes(WorldRep.Cell[] cells, bool lmHdr) { + GD.Print($"HDR: {lmHdr}"); + var packingRects = new List(); var surfaceDataMap = new Dictionary(); var rectDataMap = new Dictionary(); @@ -151,7 +152,7 @@ public partial class Mission : Node3D var mesh = new ArrayMesh(); mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, surface.BuildSurfaceArray()); - mesh.SurfaceSetMaterial(0, BuildMaterial(albedoTexture, lightmapTexture)); + mesh.SurfaceSetMaterial(0, BuildMaterial(albedoTexture, lightmapTexture, lmHdr)); var meshInstance = new MeshInstance3D { Mesh = mesh }; AddChild(meshInstance); @@ -362,11 +363,12 @@ public partial class Mission : Node3D } const string MATERIAL_PATH = "res://project/materials/base.tres"; - private static Material BuildMaterial(Texture albedoTexture, Texture lightmapTexture) + private static Material BuildMaterial(Texture albedoTexture, Texture lightmapTexture, bool lmHdr) { var material = ResourceLoader.Load(MATERIAL_PATH).Duplicate() as ShaderMaterial; material.SetShaderParameter("texture_albedo", albedoTexture); material.SetShaderParameter("lightmap_albedo", lightmapTexture); + material.SetShaderParameter("lightmap_2x", lmHdr); return material; } } \ No newline at end of file diff --git a/project/materials/base.tres b/project/materials/base.tres index 3d9f105..ccfb39d 100644 --- a/project/materials/base.tres +++ b/project/materials/base.tres @@ -5,7 +5,7 @@ code = "shader_type spatial; render_mode blend_mix,depth_draw_opaque,cull_back,unshaded; uniform sampler2D texture_albedo : filter_linear_mipmap_anisotropic,repeat_enable; uniform sampler2D lightmap_albedo : filter_linear_mipmap_anisotropic,repeat_enable; -uniform float lightmap_modulation; +uniform bool lightmap_2x; float srgb_to_linear_e(float input) { float output; @@ -28,7 +28,10 @@ vec3 srgb_to_linear(vec3 input) { void fragment() { vec4 albedo_tex = texture(texture_albedo,UV); - vec4 lightmap_tex = texture(lightmap_albedo,UV2) * lightmap_modulation; + vec4 lightmap_tex = texture(lightmap_albedo,UV2); + if (lightmap_2x) { + lightmap_tex *= 2.0f; + } ALBEDO = srgb_to_linear((albedo_tex * lightmap_tex).rgb); } " @@ -36,4 +39,4 @@ void fragment() { [resource] render_priority = 0 shader = SubResource("Shader_eumy4") -shader_parameter/lightmap_modulation = 1.0 +shader_parameter/lightmap_2x = null