You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
6.0 KiB
160 lines
6.0 KiB
class_name SyncController |
|
extends Node |
|
|
|
var module: copyMultiplayer |
|
var model_controller: ModelController |
|
var peer_id: int |
|
|
|
var nickname: String |
|
|
|
var version: int = -1 |
|
var bone_lookup: Array[String] |
|
var blendshape_lookup: Array[NodePath] |
|
|
|
var model_name : String |
|
var model : Node |
|
var skeleton : Skeleton3D |
|
var anim_player : AnimationPlayer |
|
var anim_root : Node |
|
|
|
# Reusable buffer to write data for synchronizing models. |
|
static var write_stream: StreamBuffer = StreamBuffer.with_capacity(2048) |
|
|
|
func _ready() -> void: |
|
module = get_parent().get_parent() |
|
model_controller = get_parent() |
|
peer_id = model_controller.name.to_int() |
|
|
|
func get_display_name() -> String: |
|
if nickname: return "Player '%s' (%d)" % [ nickname, peer_id ] |
|
else: return "Player (%d)" % peer_id |
|
|
|
func change_nickname(new_nickname: String) -> void: |
|
new_nickname = new_nickname.strip_edges() |
|
if new_nickname == "": return # Ignore empty nicknames. |
|
module.print_log("%s is now known as '%s'" % [ get_display_name(), new_nickname ]) |
|
nickname = new_nickname |
|
|
|
## Attempts to change the model of this player. |
|
func change_model( |
|
new_version: int, |
|
new_bone_lookup: Array[String], |
|
new_blendshape_lookup: Array[NodePath], |
|
filename: String, |
|
) -> void: |
|
# These should be safe to update even if the model doesn't load. |
|
# We just need to stay in sync with the lookup tables. |
|
version = new_version |
|
bone_lookup = new_bone_lookup |
|
blendshape_lookup = new_blendshape_lookup |
|
|
|
if not filename.is_valid_filename(): |
|
module.print_log("ERROR: '%s' is not a valid file name!" % filename) |
|
return |
|
var full_path := module.cache.path_join(filename) |
|
if not FileAccess.file_exists(full_path): |
|
module.print_log("%s wanted to switch to '%s', but it doesn't exist, skipping" % [ get_display_name(), filename ]) |
|
return |
|
if not model_controller.load_vrm(full_path): |
|
module.print_log("ERROR: Model '%s' could not be loaded!" % filename) |
|
return |
|
|
|
model_name = filename |
|
model = model_controller.get_node_or_null("Model") |
|
skeleton = model_controller._get_model_skeleton() |
|
anim_player = model.find_child("AnimationPlayer", false, false) |
|
anim_root = anim_player.get_node(anim_player.root_node) |
|
|
|
module.print_log("%s switched to '%s'" % [ get_display_name(), filename ]) |
|
|
|
func sync_model_animation( |
|
current_version: int, |
|
uncompressed_length: int, |
|
buffer: PackedByteArray, |
|
) -> void: |
|
if version != current_version: return |
|
if (not model) or (not skeleton) or (not anim_root): return |
|
|
|
var uncompressed_buffer := buffer.decompress(uncompressed_length, FileAccess.COMPRESSION_ZSTD); |
|
var stream := StreamBuffer.from_buffer(uncompressed_buffer) |
|
|
|
model.transform = stream.read_transform16() |
|
|
|
var all_bones: Array[Dictionary] = [] |
|
var all_blendshapes: Array[Dictionary] = [] |
|
|
|
# We filter out bones at rest and blendshapes at zero, so this |
|
# is initializing all_bones/blendshapes to the default values. |
|
for bone_name in module.bone_lookup: |
|
var bone_idx := skeleton.find_bone(bone_name) |
|
var bone_rest := skeleton.get_bone_rest(bone_idx) |
|
all_bones.append({ name = bone_name, idx = bone_idx, pose = bone_rest }) |
|
for anim_path in module.blendshape_lookup: |
|
all_blendshapes.append({ path = anim_path, value = 0.0 }) |
|
|
|
# 256 bones (and blendshapes) should be enough, right? |
|
for i in stream.read_uint8(): |
|
var lookup := stream.read_uint8() |
|
all_bones[lookup].pose = stream.read_bone_pose() |
|
for i in stream.read_uint8(): |
|
var lookup := stream.read_uint8() |
|
all_blendshapes[lookup].value = stream.read_range16() |
|
|
|
# Apply all the values to bones / blendshapes. |
|
for bone in all_bones: |
|
if bone.idx == -1: continue # Different model might not have this bone. |
|
skeleton.set_bone_pose(bone.idx, bone.pose) |
|
for blendshape in all_blendshapes: |
|
var anim_node := anim_root.get_node_or_null(blendshape.path) |
|
if not anim_node: continue # Different model might not have this node. |
|
anim_node.set("blend_shapes/" + blendshape.path.get_subname(0), blendshape.value) |
|
|
|
@warning_ignore("shadowed_variable") |
|
static func send_model_animation(module: copyMultiplayer) -> void: |
|
# Check if there's other players we're connected to. |
|
if module.multiplayer.get_peers().size() == 0: return |
|
|
|
var model := module.get_model() |
|
var skeleton := module.get_skeleton() |
|
var anim_player := model.find_child("AnimationPlayer", false, false) |
|
var anim_root := anim_player.get_node_or_null(anim_player.root_node) |
|
if (not model) or (not skeleton) or (not anim_root): return |
|
|
|
write_stream.write_transform16(model.transform) |
|
|
|
# Pre-filter any bones that are at rest / blendshapes that are at zero. |
|
# Unless most bones / blendshapes are active, this should reduce packet size. |
|
var restless_bones := [] |
|
var active_blendshapes := [] |
|
|
|
for i in module.bone_lookup.size(): |
|
var bone_name := module.bone_lookup[i] |
|
var bone_idx := skeleton.find_bone(bone_name) |
|
var bone_pose := skeleton.get_bone_pose(bone_idx) |
|
var bone_rest := skeleton.get_bone_rest(bone_idx) |
|
if not bone_pose.is_equal_approx(bone_rest): |
|
restless_bones.append({ lookup = i, pose = bone_pose }) |
|
|
|
for i in module.blendshape_lookup.size(): |
|
var anim_path := module.blendshape_lookup[i] |
|
var anim_node := anim_root.get_node_or_null(anim_path) |
|
var value: float = anim_node.get("blend_shapes/" + anim_path.get_subname(0)) |
|
if not is_zero_approx(value): |
|
active_blendshapes.append({ lookup = i, value = value }) |
|
|
|
write_stream.write_uint8(restless_bones.size()) |
|
for bone in restless_bones: |
|
write_stream.write_uint8(bone.lookup) |
|
write_stream.write_bone_pose(bone.pose) |
|
|
|
write_stream.write_uint8(active_blendshapes.size()) |
|
for blendshape in active_blendshapes: |
|
write_stream.write_uint8(blendshape.lookup) |
|
write_stream.write_range16(blendshape.value) |
|
|
|
# The compression still helps, so we'll keep it for now. |
|
var compressed_buffer := write_stream.slice().compress(FileAccess.COMPRESSION_ZSTD); |
|
# Uncomment this to see packet size. Can we hit < 256 bytes? |
|
# module.set_status("Packet size: %d bytes (%d uncompressed)" % [ compressed_buffer.size(), write_stream.size ]) |
|
module.sync_model_animation.rpc(module.version, write_stream.size, compressed_buffer) |
|
write_stream.clear()
|
|
|