Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Godot 4 port fixes #17

Open
wants to merge 6 commits into
base: godot-4-port
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 41 additions & 28 deletions game/BeepCube/BeepCube.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ class CutPiece extends RigidBody3D:
var coll := CollisionShape3D.new()
var lifetime: float = 0.0

func _init() -> void:
func _init(mesh_in: Mesh, mat_in: ShaderMaterial) -> void:
add_to_group(&"cutted_cube")
mat_in.set_shader_parameter(&"cutted", true)
collision_layer = 0
collision_mask = CollisionLayerConstants.Floor_mask
gravity_scale = 1
# set a phyiscs material for some more bouncy behaviour
physics_material_override = cube_phys_mat

mesh.mesh = mesh_in
mesh.layers = 3 # visible to both spectator and player
mesh.material_override = mat_in

var shape := BoxShape3D.new()
shape.size = Vector3(0.25, 0.25, 0.125)
Expand All @@ -41,13 +44,26 @@ class CutPiece extends RigidBody3D:
add_child(coll)
add_child(mesh)

func setup_cut(dist_from_center, angle) -> void:
lifetime = 0.0
angular_velocity = Vector3()
linear_velocity = Vector3()
mesh.material_override.set_shader_parameter(&"cut_dist_from_center", dist_from_center)
mesh.material_override.set_shader_parameter(&"cut_angle", angle)

func set_color(new_color: Color) -> void:
mesh.material_override.set_shader_parameter(&"color", new_color)

func set_chain_head(is_chain_head: bool) -> void:
mesh.material_override.set_shader_parameter(&"is_chain_head", is_chain_head)

func _physics_process(delta: float) -> void:
lifetime += delta
if lifetime > 0.3:
queue_free()
get_parent().remove_child(self)
else:
var f := lifetime*(1.0/0.3)
(mesh.material_override as ShaderMaterial).set_shader_parameter(&"cut_vanish",ease(f,2)*0.5)
mesh.material_override.set_shader_parameter(&"cut_vanish",ease(f,2)*0.5)


# we store the mesh here as part of the BeepCube for easier access because we will
Expand All @@ -56,11 +72,18 @@ var _mesh: Mesh
var _mat: ShaderMaterial
@export var min_speed := 0.5

var piece_left : CutPiece = null
var piece_right : CutPiece = null

func _ready() -> void:
var mi := $BeepCubeMesh as MeshInstance3D
_mat = mi.material_override as ShaderMaterial
_mesh = mi.mesh


# init our cut pieces with unique copies of our own material for reference
piece_left = CutPiece.new(_mesh, _mat.duplicate(true) as ShaderMaterial)
piece_right = CutPiece.new(_mesh, _mat.duplicate(true) as ShaderMaterial)

func spawn(note_info: ColorNoteInfo, current_beat: float, color: Color) -> void:
speed = Constants.BEAT_DISTANCE * Map.current_info.beats_per_minute * 0.016666666666666667
beat = note_info.beat
Expand All @@ -78,11 +101,15 @@ func spawn(note_info: ColorNoteInfo, current_beat: float, color: Color) -> void:

rotation.z = Constants.CUBE_ROTATIONS[note_info.cut_direction] + deg_to_rad(note_info.angle_offset)

piece_left.set_color(color)
piece_right.set_color(color)
_mat.set_shader_parameter(&"color", color)
_mat.set_shader_parameter(&"is_dot", is_dot)
# since cube instances get recycled, we gotta reset cubes that were chain
# heads in a past life
_mat.set_shader_parameter(&"is_chain_head", false)
piece_left.set_chain_head(false)
piece_right.set_chain_head(false)

# separate cube collision layers to allow a diferent collider on right/wrong cuts.
# opposing collision layers (ie. right note & left saber) will be placed on the
Expand Down Expand Up @@ -113,6 +140,8 @@ func release() -> void:

func make_chain_head() -> void:
_mat.set_shader_parameter(&"is_chain_head", true)
piece_left.set_chain_head(true)
piece_right.set_chain_head(true)

func on_miss() -> void:
Scoreboard.reset_combo()
Expand All @@ -130,7 +159,8 @@ func cut(saber_type: int, cut_speed: Vector3, cut_plane: Plane, controller: Beep
var base_cut_angle_accuracy := global_transform.basis.y.dot(cut_direction_xy)
var cut_distance := cut_plane.distance_to(global_transform.origin)

_create_cut_rigid_body(cut_plane)
if Settings.cube_cuts_falloff:
_create_cut_rigid_body(cut_plane)

if saber_type == which_saber:
var cut_angle_accuracy := clampf((base_cut_angle_accuracy-0.7)/0.3, 0.0, 1.0)
Expand All @@ -155,32 +185,16 @@ func cut(saber_type: int, cut_speed: Vector3, cut_plane: Plane, controller: Beep
# cut the cube by creating two rigid bodies and using a CSGBox to create
# the cut plane
func _create_cut_rigid_body(cutplane: Plane) -> void:
var piece_left := CutPiece.new()
var piece_right := CutPiece.new()
piece_left.global_transform = global_transform
piece_right.global_transform = global_transform

# the original cube mesh
piece_left.mesh.mesh = _mesh
piece_right.mesh.mesh = _mesh

var left_mat := _mat.duplicate(true) as ShaderMaterial
var right_mat := _mat.duplicate(true) as ShaderMaterial
left_mat.set_shader_parameter(&"cutted", true)
right_mat.set_shader_parameter(&"cutted", true)

# calculate angle and position of the cut
var cut_angle_abs := Vector2(cutplane.normal.x, cutplane.normal.y).angle()
# multiply by 1.25 to make up for the mesh being at 0.8 scale
var cut_dist_from_center := cutplane.distance_to(global_transform.origin)# * 1.25
var cut_dist_from_center := cutplane.distance_to(global_transform.origin)
var cut_angle_rel := cut_angle_abs - global_rotation.z

left_mat.set_shader_parameter(&"cut_dist_from_center", -cut_dist_from_center)
right_mat.set_shader_parameter(&"cut_dist_from_center", cut_dist_from_center)
left_mat.set_shader_parameter(&"cut_angle", cut_angle_rel + PI)
right_mat.set_shader_parameter(&"cut_angle", cut_angle_rel)
piece_left.mesh.material_override = left_mat
piece_right.mesh.material_override = right_mat
piece_left.setup_cut(-cut_dist_from_center, cut_angle_rel + PI)
piece_right.setup_cut(cut_dist_from_center, cut_angle_rel)

# transform the normal into the orientation of the actual cube mesh
var normal := piece_left.mesh.transform.basis.inverse() * cutplane.normal
Expand All @@ -192,10 +206,9 @@ func _create_cut_rigid_body(cutplane: Plane) -> void:
piece_right.coll.look_at_from_position(-cutplane.normal*0.125, cutplane.normal, Vector3(0,1,0))

# some impulse so the cube half moves
var cutplane_2d := Vector3(cutplane.x * 2.0,cutplane.y * 2.0,0.0)
var splitplane_2d := cutplane_2d.cross(piece_left.transform.basis.z)
piece_left.apply_central_impulse(-splitplane_2d)
piece_right.apply_central_impulse(splitplane_2d)
var split_vector := cutplane.normal * 2.0
piece_left.apply_central_impulse(-split_vector)
piece_right.apply_central_impulse(split_vector)

get_parent().add_child(piece_left)
get_parent().add_child(piece_right)
Expand Down
4 changes: 2 additions & 2 deletions game/scripts/BackgroundImgLoader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func load_texture(filepath: String, callback_func: Callable, is_main_cover: bool
_start_next_img_load()

func _start_next_img_load() -> void:
_img_load_mutex.lock()
if _img_load_request_queue.size() > 0 and _running_img_load_threads < _max_img_load_threads:
var next_req := _img_load_request_queue.pop_front() as ImgLoadRequest

_img_load_mutex.lock()
if next_req and next_req.thread.start(_load_img_threaded.bind(next_req)) == OK:
_running_img_load_threads += 1
_img_load_mutex.unlock()
_img_load_mutex.unlock()

func _load_img_threaded(req: ImgLoadRequest) -> void:
# read cover image data from file into a buffer
Expand Down
21 changes: 10 additions & 11 deletions game/scripts/BeepSaberController.gd
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,23 @@ func _update_movement_aabb() -> void:
func reset_movement_aabb() -> void:
movement_aabb = AABB(global_transform.origin, Vector3.ZERO)

var _rumble_intensity := 0.0
var _rumble_duration := -128.0 #-1 means deactivated so applications can also set their own rumble
var _is_simple_rumbling := false
var _rumble_duration_remaining := 0.0

func simple_rumble(intensity: float, duration: float) -> void:
_rumble_intensity = intensity;
_rumble_duration = duration;
_rumble_duration_remaining = duration;
_is_simple_rumbling = true
trigger_haptic_pulse("haptic", 20, intensity, duration, 0)

func is_simple_rumbling() -> bool:
return _rumble_duration > 0.0
return _is_simple_rumbling

func _update_rumble(dt: float) -> void:
if _rumble_duration < -100: return
simple_rumble(_rumble_intensity, _rumble_duration)
_rumble_duration -= dt
if _rumble_duration <= 0.0:
_rumble_duration = -128.0
simple_rumble(0.0, _rumble_duration)
if _rumble_duration_remaining > 0.0:
_rumble_duration_remaining -= dt
if _rumble_duration_remaining <= 0.0:
_rumble_duration_remaining = 0.0
_is_simple_rumbling = false

var first_time := true

Expand Down
3 changes: 1 addition & 2 deletions game/scripts/ScenePool/ScenePool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func _ready() -> void:
print("cubes in pool: ",_free_list.size())

func acquire() -> BeepCube:
var cube := _free_list.pop_back() as BeepCube
var cube := _free_list.pop_front() as BeepCube
if not cube:
var new_cube := scene.instantiate() as BeepCube
new_cube.scene_released.connect(_on_scene_released)
Expand All @@ -34,5 +34,4 @@ func acquire() -> BeepCube:
return cube

func _on_scene_released(cube: BeepCube) -> void:
cube.scene_released.disconnect(_on_scene_released)
Copy link
Author

@hankedan000 hankedan000 Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leandrodreamer haha i see you've been making attempts at improving cube cutting performance recently too, and we have some conflicts.

I think the major issue with cubes leaking from the resource pool still exists on main (should probably retest though). I believe the big issue was that the scene_released signal was being disconnected here for no good reason, resulting in object not getting re-added to _free_list list after released(). This basically results in a pool object only ever getting used twice (i think) before the pool will become exhausted and start dynamically instancing entire new cube each time acquire() is called.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm i see, i haven't noticed any performance issue on new cubes, even on web, but there may still be some issue if they stay in memory forever

_free_list.push_back(cube)
2 changes: 1 addition & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ common/physics_fps=72
[rendering]

renderer/rendering_method="mobile"
renderer/rendering_method.mobile="gl_compatibility"
textures/vram_compression/import_etc2_astc=true
shading/overrides/force_vertex_shading.mobile=false
shading/overrides/force_lambert_over_burley=true
Expand Down Expand Up @@ -170,5 +169,6 @@ right_saber={
[xr]

openxr/enabled=true
openxr/reference_space=2
openxr/startup_alert=false
shaders/enabled=true