From b2fc7897348d9a70de33d5653a4d834b3ee98991 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Mon, 5 Aug 2024 21:46:03 +0100 Subject: [PATCH] Render textures and lightmaps together --- project/code/Mission.cs | 13 ++++++------- project/materials/base.tres | 20 ++++++++++++++++++++ project/scenes/main.tscn | 7 +++++-- 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 project/materials/base.tres diff --git a/project/code/Mission.cs b/project/code/Mission.cs index b14e358..91ad40c 100644 --- a/project/code/Mission.cs +++ b/project/code/Mission.cs @@ -108,7 +108,7 @@ public partial class Mission : Node3D Image lightmap = BuildLightmap(cell, packingRects, surfaceArrays); // !Hack: This should be somewhere else? - var materials = new List(); + var materials = new List(); for (var i = 0; i < maxPolyIdx; i++) { var textureId = cell.RenderPolys[i].TextureId; @@ -118,11 +118,9 @@ public partial class Mission : Node3D textureId = 0; } - var material = new StandardMaterial3D - { - AlbedoTexture = _textures[textureId], - TextureFilter = BaseMaterial3D.TextureFilterEnum.Nearest, - }; + var material = ResourceLoader.Load("res://project/materials/base.tres").Duplicate() as ShaderMaterial; + material.SetShaderParameter("texture_albedo", _textures[textureId]); + material.SetShaderParameter("lightmap_albedo", ImageTexture.CreateFromImage(lightmap)); materials.Add(material); } @@ -244,6 +242,7 @@ public partial class Mission : Node3D return surfacesArrays; } + // TODO: This is broke? private static OccluderInstance3D GenerateOccluder(List surfaceArrays) { var vertices = new List(); @@ -269,7 +268,7 @@ public partial class Mission : Node3D return occluderInstance; } - private static MeshInstance3D GenerateMesh(List surfaceArrays, List materials) + private static MeshInstance3D GenerateMesh(List surfaceArrays, List materials) { var arrMesh = new ArrayMesh(); for (var i = 0; i < surfaceArrays.Count; i++) diff --git a/project/materials/base.tres b/project/materials/base.tres new file mode 100644 index 0000000..5eb9e9b --- /dev/null +++ b/project/materials/base.tres @@ -0,0 +1,20 @@ +[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://ck8h6dvojuegj"] + +[sub_resource type="Shader" id="Shader_eumy4"] +code = "shader_type spatial; +render_mode blend_mix,depth_draw_opaque,cull_back,unshaded; +uniform sampler2D texture_albedo : source_color,filter_linear_mipmap,repeat_enable; +uniform sampler2D lightmap_albedo : source_color,filter_linear_mipmap,repeat_enable; +uniform float lightmap_modulation; + +void fragment() { + vec4 albedo_tex = texture(texture_albedo,UV); + vec4 lightmap_tex = texture(lightmap_albedo,UV2) * lightmap_modulation; + ALBEDO = albedo_tex.rgb * lightmap_tex.rgb; +} +" + +[resource] +render_priority = 0 +shader = SubResource("Shader_eumy4") +shader_parameter/lightmap_modulation = 1.0 diff --git a/project/scenes/main.tscn b/project/scenes/main.tscn index a7e066d..9405fd5 100644 --- a/project/scenes/main.tscn +++ b/project/scenes/main.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=5 format=3 uid="uid://boxi211q3kx6c"] +[gd_scene load_steps=6 format=3 uid="uid://boxi211q3kx6c"] [ext_resource type="Script" path="res://project/code/Mission.cs" id="1_xhqt7"] [ext_resource type="Script" path="res://project/code/camera.gd" id="2_w5otl"] [ext_resource type="PackedScene" uid="uid://cekg1xb5f0ux1" path="res://project/scenes/ui/mission_selector.tscn" id="3_hwfcj"] +[ext_resource type="Material" uid="uid://ck8h6dvojuegj" path="res://project/materials/base.tres" id="4_vxwmj"] [sub_resource type="Environment" id="Environment_oxkvl"] ambient_light_source = 2 @@ -15,7 +16,6 @@ ssao_enabled = true transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.110309, 0.187101, -0.461656) script = ExtResource("1_xhqt7") FileName = "/home/jarrod/Dev/thief/de-specs/test_data/rose-garden.mis" -Dump = true [node name="Camera3D" type="Camera3D" parent="."] script = ExtResource("2_w5otl") @@ -27,3 +27,6 @@ environment = SubResource("Environment_oxkvl") [node name="MissionSelector" parent="UI" instance=ExtResource("3_hwfcj")] unique_name_in_owner = true + +[node name="MeshInstance3D" type="MeshInstance3D" parent="."] +material_overlay = ExtResource("4_vxwmj")