Skip to content

Commit

Permalink
Merge pull request #452 from snipercup/shape-materials
Browse files Browse the repository at this point in the history
Limit Shape materials
  • Loading branch information
snipercup authored Nov 22, 2024
2 parents a14b3d0 + 4faa207 commit f539f4d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
12 changes: 1 addition & 11 deletions Scripts/FurnitureStaticSrv.gd
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,7 @@ func create_box_shape():
# Function to create a visual instance with a mesh to represent the shape
# Apply the hide_above_player_shader to the MeshInstance
func create_visual_instance(shape_type: String):
var color = Color.html(dfurniture.support_shape.color)
var material: ShaderMaterial = ShaderMaterial.new()

if dfurniture.support_shape.transparent:
material.shader = Gamedata.hide_above_player_shader # Assign the shader to the material
material.set_shader_parameter("object_color", color)
material.set_shader_parameter("alpha", 0.5)
else: # Use a shader with shadow
material.shader = Gamedata.hide_above_player_shadow # Assign the shader to the material
material.set_shader_parameter("object_color", color)
material.set_shader_parameter("alpha", 1.0)
var material: ShaderMaterial = Gamedata.furnitures.get_shape_material_by_id(dfurniture.id)

if shape_type == "Box":
support_mesh = BoxMesh.new()
Expand Down
35 changes: 35 additions & 0 deletions Scripts/Gamedata/DFurnitures.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var spritePath: String = "./Mods/Core/Furniture/"
var furnituredict: Dictionary = {}
var sprites: Dictionary = {}
var shader_materials: Dictionary = {} # Cache for shader materials by furniture ID
var shape_materials: Dictionary = {} # Cache for shape materials by furniture ID

func _init():
load_sprites()
Expand Down Expand Up @@ -137,6 +138,40 @@ func create_furniture_shader_material(furniture_id: String) -> ShaderMaterial:
return shader_material


# New function to get or create a visual instance material for a furniture ID
func get_shape_material_by_id(furniture_id: String) -> ShaderMaterial:
# Check if the material already exists
if shape_materials.has(furniture_id):
return shape_materials[furniture_id]
else:
# Create a new ShaderMaterial
var material: ShaderMaterial = create_shape_material(furniture_id)
# Store it in the dictionary
shape_materials[furniture_id] = material
return material


# Helper function to create a ShaderMaterial for the support shape
func create_shape_material(furniture_id: String) -> ShaderMaterial:
var dfurniture: DFurniture = by_id(furniture_id)
if dfurniture.moveable: # Only static furniture has a support shape
return null
var color = Color.html(dfurniture.support_shape.color)
var material: ShaderMaterial = ShaderMaterial.new()

# Determine the shader and parameters based on transparency
if dfurniture.support_shape.transparent:
material.shader = Gamedata.hide_above_player_shader # Assign the shader
material.set_shader_parameter("object_color", color)
material.set_shader_parameter("alpha", 0.5)
else:
material.shader = Gamedata.hide_above_player_shadow # Assign the shadow shader
material.set_shader_parameter("object_color", color)
material.set_shader_parameter("alpha", 1.0)

return material


# Handle the game ended signal. We need to clear the shader materials because they
# need to be re-created on game start since some of them may have changed in between.
func _on_game_ended():
Expand Down

0 comments on commit f539f4d

Please sign in to comment.