Use lightmaps for surface lighting

This commit is contained in:
Holly Stubbs 2025-03-16 00:53:05 +00:00
parent 8ac40103ad
commit c0b59242a3
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
4 changed files with 54 additions and 16 deletions

View file

@ -0,0 +1,18 @@
// Shader to take a surface texture using UV1 and a lightmap texture
// using UV2 and a normal map and smash em together.
shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_back, diffuse_burley, specular_schlick_ggx;
uniform sampler2D texture_albedo : source_color, filter_linear_mipmap, repeat_enable;
uniform sampler2D texture_normal : hint_roughness_normal, filter_linear_mipmap, repeat_enable;
uniform sampler2D texture_lightmap : source_color, filter_linear_mipmap, repeat_enable;
void fragment() {
vec4 albedo_tex = texture(texture_albedo, UV);
vec4 lightmap_tex = texture(texture_lightmap, UV2);
ALBEDO = albedo_tex.rgb * lightmap_tex.rgb;
NORMAL_MAP = texture(texture_normal, UV).rgb;
NORMAL_MAP_DEPTH = 1.0;
}

View file

@ -0,0 +1 @@
uid://ccsn6p3j6cp0t

View file

@ -102,7 +102,9 @@ var cameraMoveByVector = Vector2()
func GetStepSound():
if floorPick.is_colliding():
return Global.StepSoundFromTexture(floorPick.get_collider().get_meta("texName"))
var a = floorPick.get_collider().get_meta("texName")
if a:
return Global.StepSoundFromTexture(a)
return 0
func _process(delta: float) -> void:

View file

@ -8,6 +8,8 @@ static var EMPTY_TEXTURE = Texture2D.new()
static var RMESH_LOAD_COUNT = 0
const surfaceShader = preload("res://shaders/LightmapSurface.gdshader")
static func LoadRMesh(file: String, rt: RoomTemplate):
var correctedPath = file.replace("\\", "/")
var fileName = correctedPath.split("/")[-1]
@ -73,7 +75,9 @@ static func LoadRMesh(file: String, rt: RoomTemplate):
count = reader.readInt()
var childMesh
var surf
var tex: Array = [0, 0]
var tex: Array = [null, null]
var texBump: Array = [null, null]
var texLightmap: Array = [null, null]
var brush
var isAlpha:float
@ -89,14 +93,15 @@ static func LoadRMesh(file: String, rt: RoomTemplate):
childMesh = Mesh.new()
var vertices = PackedVector3Array()
var uvs = PackedVector2Array()
var uv2s = PackedVector2Array()
var tris = PackedInt32Array()
#surf=CreateSurface(childMesh)
#brush=CreateBrush()
tex[0] = 0
tex[1] = 0
tex[0] = null
tex[1] = null
isAlpha = 0
for j2 in range(2):
@ -104,15 +109,20 @@ static func LoadRMesh(file: String, rt: RoomTemplate):
if blendType != 0:
textureName = ReadString(reader)
tex[j2] = Global.GetTextureFromCache(textureName)
if Global.materials.has(textureName):
texBump[j2] = Global.materials.get(textureName).bump.replace("\\", "/")
else:
texBump[j2] = Global.LoadTexture(str(file, textureName))
if tex[j2] == null: # texture is not in cache
if blendType < 3:
tex[j2] = Global.LoadTexture(str(file, textureName))
else:
tex[j2] = Global.LoadTexture(str(file, textureName)) # TODO: load with alpha
#if tex[j2] != 0:
#If blendType=1 Then TextureBlend tex[j2],5
#If Instr(Lower(textureName),"_lm")<>0 Then
#if tex[j2] != null:
##if blendType == 1:
##TextureBlend tex[j2],5
#if textureName.to_lower(),"_lm" != 0 Then
#TextureBlend tex[j2],3
#EndIf
#AddTextureToCache(tex[j2])
@ -131,7 +141,7 @@ static func LoadRMesh(file: String, rt: RoomTemplate):
if tex[1] != null:
#TextureBlend tex[1],2
#BrushTexture brush,tex[1],0,0
activeAlbedo = tex[1]
activeAlbedo = tex[1] # NOTE: THIS SHOULD BE 1
else:
#BrushTexture brush,blankTexture,0,0
activeAlbedo = EMPTY_TEXTURE
@ -190,14 +200,14 @@ static func LoadRMesh(file: String, rt: RoomTemplate):
#meshInstance.global_position.z = z
# texture coords
for k2 in range(0, 1):
for k2 in range(0, 2):
u = reader.readFloat()
v = reader.readFloat()
#VertexTexCoords surf,vertex,u,v,0.0,k
uvs.push_back(Vector2(u, v))
reader.readFloat()
reader.readFloat()
if k2 == 0:
uvs.push_back(Vector2(u, v))
else:
uv2s.push_back(Vector2(u, v))
# colors
temp1i = reader.readUByte()
@ -251,15 +261,22 @@ static func LoadRMesh(file: String, rt: RoomTemplate):
arr[Mesh.ARRAY_VERTEX]=vertices
arr[Mesh.ARRAY_TEX_UV]=uvs
arr[Mesh.ARRAY_TEX_UV2]=uv2s
arr[Mesh.ARRAY_INDEX]=tris
var meshInstance = MeshInstance3D.new()
arr_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
meshInstance.mesh = arr_mesh
var mat = StandardMaterial3D.new()
#var mat = StandardMaterial3D.new()
#mat.albedo_color = Color(randi() % 255 / 255.0, randi() % 255 / 255.0, randi() % 255 / 255.0)
mat.albedo_texture = activeAlbedo
mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA_DEPTH_PRE_PASS if activeAlbedoHasAlpha else BaseMaterial3D.TRANSPARENCY_DISABLED
#mat.albedo_texture = activeAlbedo
#mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA_DEPTH_PRE_PASS if activeAlbedoHasAlpha else BaseMaterial3D.TRANSPARENCY_DISABLED
var mat = ShaderMaterial.new()
mat.shader = surfaceShader
mat.set_shader_parameter("texture_albedo", activeAlbedo)
if texBump[1] != null:
mat.set_shader_parameter("texture_normal", texBump[1])
mat.set_shader_parameter("texture_lightmap", tex[0])
meshInstance.set_surface_override_material(0, mat)
meshInstance.create_trimesh_collision()
meshInstance.get_child(0).set_meta("texName", textureName)