72 lines
2.1 KiB
GDScript

extends Spatial
# Saves every frame as a PNG in the project directory. Use for debugging (with caution)
export var output_debug_textures := false
export var first_output_frame := 0
export var plane_size := 2.0
var _frame_number := 0
var _positions_to_set = []
func set_depth_at_position(pos: Vector3, depth: float):
# Transform the global position into 2D points on the water surface with values between 0 and 1
pos -= translation
var position_2d = Vector2(pos.x, pos.z)
position_2d += Vector2(plane_size, plane_size) / 2.0
position_2d /= plane_size
if position_2d.x < 1.0 and position_2d.y < 1.0 \
and position_2d.x >= 0.0 and position_2d.y >= 0.0:
_positions_to_set.append([position_2d, depth])
func _ready():
$WaterMesh.mesh.size = Vector2(plane_size, plane_size)
# Apply the scale and positioning to the collider
$WaterMesh/StaticBody.scale.x = plane_size / 2.0
$WaterMesh/StaticBody.scale.y = plane_size / 2.0
$WaterMesh/StaticBody.scale.z = plane_size / 2.0
$WaterMesh/StaticBody.translation.y = 0.6 * 40.0
func _physics_process(delta):
# Get result of previous frame
var result = $WaterHeights.get_texture()
# Set it as the data of the water mesh
$WaterMesh.material_override.set_shader_param("water_heights", result)
# Calculate a new frame. First, get the data of the last frame and modify it accordingly
var image_data = result.get_data()
# Set outstanding pixels
for position_and_depth in _positions_to_set:
var pos = position_and_depth[0]
var depth = position_and_depth[1]
image_data.lock()
image_data.set_pixel(floor(pos.x * 512), floor(pos.y * 512), Color(depth, 0.0, 0.0, 0.0))
image_data.unlock()
_positions_to_set.clear()
# Create an ImageTexture for this new frame
var previous_frame = ImageTexture.new()
previous_frame.create_from_image(image_data)
# Set the previous texture in the shader so that a new texture will be available next frame
$WaterHeights.set_previous_texture(previous_frame)
# Debug output if needed
if output_debug_textures and _frame_number > first_output_frame:
image_data.save_png("res://debugframes/frame%s.png" % [_frame_number])
_frame_number += 1