110 lines
3.3 KiB
GDScript
110 lines
3.3 KiB
GDScript
@tool
|
|
extends MeshInstance3D
|
|
|
|
enum DisplayType{
|
|
Fluid,
|
|
Solid,
|
|
}
|
|
|
|
@export var display_type: DisplayType
|
|
@export_tool_button("Wiggle") var wiggle = func() -> void:
|
|
var tween = create_tween()
|
|
material_override.set_shader_parameter("wave_intensity",1.)
|
|
tween.tween_property(material_override,"shader_parameter/wave_intensity",0.0,2.0)
|
|
@export var particle_collision: GPUParticlesCollisionBox3D
|
|
|
|
func update_fluid_material(substances: Array[RuntimeSubstanceData],capacity: float) -> void:
|
|
var mat: ShaderMaterial = material_override
|
|
if len(substances) == 0:
|
|
visible = false
|
|
return
|
|
else:
|
|
visible = true
|
|
|
|
var total_weight = 0
|
|
var total_color = 0
|
|
|
|
for sub in substances:
|
|
total_weight += sub.amount
|
|
total_color += sub.substance.color_influence
|
|
|
|
var pH: float = 0
|
|
var color: Color = Color(0,0,0,0)
|
|
|
|
for sub: RuntimeSubstanceData in substances:
|
|
var weight: float = sub.amount/total_weight
|
|
pH += sub.substance.get_ph_runtime(substances) * weight
|
|
|
|
for sub: RuntimeSubstanceData in substances:
|
|
var color_to_add: Color
|
|
if sub.substance.pHColor != null:
|
|
color_to_add = sub.substance.pHColor.sample(pH/14.0)
|
|
else:
|
|
color_to_add = sub.substance.color
|
|
|
|
if display_type == DisplayType.Fluid and sub.is_solution:
|
|
color_to_add.a = 0.5
|
|
|
|
#if color == Color(0,0,0,0):
|
|
# color = color_to_add
|
|
#else:
|
|
# color = color.lerp(color_to_add,sub.substance.color_influence)
|
|
color += color_to_add * sub.substance.color_influence
|
|
|
|
|
|
mat.set_shader_parameter("albedo_color", color / total_color)
|
|
mat.set_shader_parameter("fill",total_weight/capacity)
|
|
particle_collision.position.y = + particle_collision.size.y/2 + position.y - 4 + 8 * total_weight/capacity
|
|
|
|
func update_solid_material(substances: Array[RuntimeSubstanceData]) -> void:
|
|
var mat: StandardMaterial3D = material_override
|
|
if len(substances) == 0:
|
|
visible = false
|
|
return
|
|
else:
|
|
visible = true
|
|
|
|
var total_weight = 0
|
|
var total_color = 0
|
|
|
|
for sub in substances:
|
|
total_weight += sub.amount
|
|
total_color += sub.substance.color_influence
|
|
|
|
var pH: float = 0
|
|
var color: Color = Color(0,0,0,0)
|
|
|
|
for sub: RuntimeSubstanceData in substances:
|
|
var weight: float = sub.amount/total_weight
|
|
pH += sub.substance.get_ph_runtime(substances) * weight
|
|
|
|
for sub: RuntimeSubstanceData in substances:
|
|
var color_to_add: Color
|
|
if sub.substance.pHColor != null:
|
|
color_to_add = sub.substance.pHColor.sample(pH/14.0)
|
|
else:
|
|
color_to_add = sub.substance.color
|
|
|
|
if display_type == DisplayType.Fluid and sub.is_solution:
|
|
color_to_add.a = 0.5
|
|
|
|
#if color == Color(0,0,0,0):
|
|
# color = color_to_add
|
|
#else:
|
|
# color = color.lerp(color_to_add,sub.substance.color_influence)
|
|
color += color_to_add * sub.substance.color_influence
|
|
|
|
|
|
mat.albedo_color = color / total_color
|
|
|
|
func update_material_unfiltered(substances: Array[RuntimeSubstanceData],capacity: float):
|
|
match display_type:
|
|
DisplayType.Fluid:
|
|
var fluids = substances.filter(filter_liquids)
|
|
update_fluid_material(fluids,capacity)
|
|
DisplayType.Solid:
|
|
var solids = substances.filter(filter_solids)
|
|
update_solid_material(solids)
|
|
|
|
func filter_liquids(sub: RuntimeSubstanceData): return sub.substance.melting_point < sub.temperature and sub.substance.boiling_point > sub.temperature or sub.is_solution
|
|
func filter_solids(sub: RuntimeSubstanceData): return sub.substance.melting_point > sub.temperature and not sub.is_solution
|