Skip to content

Commit

Permalink
Merge pull request #97 from MizunagiKB/update_v0_6_1
Browse files Browse the repository at this point in the history
Update v0 6 1
  • Loading branch information
MizunagiKB authored Aug 14, 2024
2 parents 508f09f + 83c117b commit 2fbc409
Show file tree
Hide file tree
Showing 20 changed files with 728 additions and 178 deletions.
39 changes: 0 additions & 39 deletions demo/addons/gd_cubism/example/custom_efx_01.gd

This file was deleted.

105 changes: 29 additions & 76 deletions demo/addons/gd_cubism/example/custom_efx_02.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,37 @@
extends GDCubismEffectCustom


@export var param_mouth_name: String = "ParamMouthA"
@export var min_db: float = 60.0
@export var min_voice_freq: int = 0
@export var max_voice_freq: int = 3200
@export var history_size: int = 6

@export_category("Audio")
@export var audio_bus_name: String = "Voice"
@export var audio_efx_index: int = 0

var spectrum: AudioEffectSpectrumAnalyzerInstance
var param_mouth: GDCubismParameter
var ary_volume_history: Array
var history_position: int = 0
var lipsync_ready: bool = false


func array_rebuild():
if history_size != ary_volume_history.size():
ary_volume_history.resize(history_size)
ary_volume_history.fill(0.0)


func array_avg() -> float:
var sum_v: float = 0.0
for v in ary_volume_history:
sum_v += v
return sum_v / ary_volume_history.size()


func _on_cubism_init(model: GDCubismUserModel):
param_mouth = null
ary_volume_history.clear()
history_position = 0

var ary_param = model.get_parameters()

for param in ary_param:
if param.id == param_mouth_name:
param_mouth = param

var bus_index = AudioServer.get_bus_index(audio_bus_name)
spectrum = AudioServer.get_bus_effect_instance(bus_index, audio_efx_index)

if spectrum == null:
lipsync_ready = false
else:
lipsync_ready = true


func _on_cubism_term(model: GDCubismUserModel):
lipsync_ready = false
param_mouth = null
ary_volume_history.clear()
history_position = 0
var first_time: bool = true
var counter: float = 0.0
var base_vct: Vector2
var base_scale: Vector2
@export var art_mesh_name: String = "ArtMesh278"
@export var vector_index_0: int = 0
@export var vector_index_1: int = 1


func _on_cubism_process(model: GDCubismUserModel, delta: float):
if lipsync_ready == false:
return
var dict_mesh: Dictionary = model.get_meshes()

array_rebuild()

var m: float = spectrum.get_magnitude_for_frequency_range(
min_voice_freq,
max_voice_freq
).length()
var v = clamp((min_db + linear_to_db(m)) / min_db, 0.0, 1.0)

ary_volume_history[history_position] = v
history_position += 1

if history_position >= ary_volume_history.size():
history_position = 0

var avg_v: float = array_avg()
if dict_mesh.has(art_mesh_name) == false:
return

if param_mouth != null:
param_mouth.value = max(param_mouth.value - 0.2, 0.0)
if avg_v > 0.1:
param_mouth.value = 1.0
var ary_mesh: ArrayMesh = dict_mesh[art_mesh_name]
var ary_surface: Array = ary_mesh.surface_get_arrays(0)
var mesh_vertex: PackedVector2Array = ary_surface[ArrayMesh.ARRAY_VERTEX]

if first_time == true:
var fr = ary_surface[ArrayMesh.ARRAY_VERTEX][vector_index_0]
var to = ary_surface[ArrayMesh.ARRAY_VERTEX][vector_index_1]
base_vct = (to - fr)
base_scale = $Sprite2D.scale
first_time = false

var fr = ary_surface[ArrayMesh.ARRAY_VERTEX][vector_index_0]
var to = ary_surface[ArrayMesh.ARRAY_VERTEX][vector_index_1]
var calc_vct = (to - fr)
var scale: float = calc_vct.length() / base_vct.length()

$Sprite2D.position = fr
$Sprite2D.rotation = base_vct.normalized().cross(calc_vct.normalized())
$Sprite2D.scale = base_scale * scale
86 changes: 86 additions & 0 deletions demo/addons/gd_cubism/example/custom_efx_03.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2023 MizunagiKB <[email protected]>
extends GDCubismEffectCustom


@export var param_mouth_name: String = "ParamMouthA"
@export var min_db: float = 60.0
@export var min_voice_freq: int = 0
@export var max_voice_freq: int = 3200
@export var history_size: int = 6

@export_category("Audio")
@export var audio_bus_name: String = "Voice"
@export var audio_efx_index: int = 0

var spectrum: AudioEffectSpectrumAnalyzerInstance
var param_mouth: GDCubismParameter
var ary_volume_history: Array
var history_position: int = 0
var lipsync_ready: bool = false


func array_rebuild():
if history_size != ary_volume_history.size():
ary_volume_history.resize(history_size)
ary_volume_history.fill(0.0)


func array_avg() -> float:
var sum_v: float = 0.0
for v in ary_volume_history:
sum_v += v
return sum_v / ary_volume_history.size()


func _on_cubism_init(model: GDCubismUserModel):
param_mouth = null
ary_volume_history.clear()
history_position = 0

var ary_param = model.get_parameters()

for param in ary_param:
if param.id == param_mouth_name:
param_mouth = param

var bus_index = AudioServer.get_bus_index(audio_bus_name)
spectrum = AudioServer.get_bus_effect_instance(bus_index, audio_efx_index)

if spectrum == null:
lipsync_ready = false
else:
lipsync_ready = true


func _on_cubism_term(model: GDCubismUserModel):
lipsync_ready = false
param_mouth = null
ary_volume_history.clear()
history_position = 0


func _on_cubism_process(model: GDCubismUserModel, delta: float):
if lipsync_ready == false:
return

array_rebuild()

var m: float = spectrum.get_magnitude_for_frequency_range(
min_voice_freq,
max_voice_freq
).length()
var v = clamp((min_db + linear_to_db(m)) / min_db, 0.0, 1.0)

ary_volume_history[history_position] = v
history_position += 1

if history_position >= ary_volume_history.size():
history_position = 0

var avg_v: float = array_avg()

if param_mouth != null:
param_mouth.value = max(param_mouth.value - 0.2, 0.0)
if avg_v > 0.1:
param_mouth.value = 1.0
59 changes: 59 additions & 0 deletions demo/addons/gd_cubism/example/demo_effect_custom_01.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
extends Node2D


enum E_PARAM_MODE {
PROCESS,
SIGNAL
}

var param_valid: bool = false
var param_mode: E_PARAM_MODE = E_PARAM_MODE.PROCESS
var l_eye: GDCubismParameter
var r_eye: GDCubismParameter


func _ready():
pass


func _process(delta):
if param_mode == E_PARAM_MODE.PROCESS:
var model: GDCubismUserModel = $Sprite2D/GDCubismUserModel

for _o in model.get_parameters():
var o: GDCubismParameter = _o
if o.id == "ParamEyeLOpen":
o.value = 0.0
if o.id == "ParamEyeROpen":
o.value = 0.0


func _on_gd_cubism_effect_custom_cubism_init(model: GDCubismUserModel):
if param_mode == E_PARAM_MODE.SIGNAL:
for _o in model.get_parameters():
var o: GDCubismParameter = _o
if o.id == "ParamEyeLOpen":
l_eye = o
if o.id == "ParamEyeROpen":
r_eye = o
param_valid = true


func _on_gd_cubism_effect_custom_cubism_term(model):
if param_mode == E_PARAM_MODE.SIGNAL:
param_valid = false


func _on_gd_cubism_effect_custom_cubism_prologue(model, delta):
pass # Replace with function body.


func _on_gd_cubism_effect_custom_cubism_process(model: GDCubismUserModel, delta: float):
if param_mode == E_PARAM_MODE.SIGNAL:
if param_valid == true:
l_eye.value = 0.0
r_eye.value = 0.0


func _on_gd_cubism_effect_custom_cubism_epilogue(model, delta):
pass # Replace with function body.
18 changes: 8 additions & 10 deletions demo/addons/gd_cubism/example/demo_effect_custom_01.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[gd_scene load_steps=3 format=3 uid="uid://drt0184vymus2"]
[gd_scene load_steps=2 format=3 uid="uid://0sa003phknvv"]

[ext_resource type="Script" path="res://addons/gd_cubism/example/custom_efx_01.gd" id="1_lkrys"]
[ext_resource type="Texture2D" uid="uid://c8o4yd28cp3jy" path="res://icon.svg" id="2_ur0ha"]
[ext_resource type="Script" path="res://addons/gd_cubism/example/demo_effect_custom_01.gd" id="1_2sdfp"]

[node name="demo_effect_custom_01" type="Node2D"]
script = ExtResource("1_2sdfp")

[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(640, 512)
Expand All @@ -16,11 +16,9 @@ size = Vector2i(1024, 1024)
render_target_update_mode = 4

[node name="GDCubismEffectCustom" type="GDCubismEffectCustom" parent="Sprite2D/GDCubismUserModel"]
script = ExtResource("1_lkrys")

[node name="Sprite2D" type="Sprite2D" parent="Sprite2D/GDCubismUserModel/GDCubismEffectCustom"]
z_index = 1024
scale = Vector2(0.5, 0.5)
texture = ExtResource("2_ur0ha")

[connection signal="cubism_process" from="Sprite2D/GDCubismUserModel/GDCubismEffectCustom" to="Sprite2D/GDCubismUserModel/GDCubismEffectCustom" method="_on_cubism_process"]
[connection signal="cubism_epilogue" from="Sprite2D/GDCubismUserModel/GDCubismEffectCustom" to="." method="_on_gd_cubism_effect_custom_cubism_epilogue"]
[connection signal="cubism_init" from="Sprite2D/GDCubismUserModel/GDCubismEffectCustom" to="." method="_on_gd_cubism_effect_custom_cubism_init"]
[connection signal="cubism_process" from="Sprite2D/GDCubismUserModel/GDCubismEffectCustom" to="." method="_on_gd_cubism_effect_custom_cubism_process"]
[connection signal="cubism_prologue" from="Sprite2D/GDCubismUserModel/GDCubismEffectCustom" to="." method="_on_gd_cubism_effect_custom_cubism_prologue"]
[connection signal="cubism_term" from="Sprite2D/GDCubismUserModel/GDCubismEffectCustom" to="." method="_on_gd_cubism_effect_custom_cubism_term"]
22 changes: 9 additions & 13 deletions demo/addons/gd_cubism/example/demo_effect_custom_02.tscn
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
[gd_scene load_steps=3 format=3 uid="uid://xk7rqvf532rj"]
[gd_scene load_steps=3 format=3 uid="uid://drt0184vymus2"]

[ext_resource type="Script" path="res://addons/gd_cubism/example/custom_efx_02.gd" id="1_ittjg"]
[ext_resource type="Texture2D" uid="uid://c8o4yd28cp3jy" path="res://icon.svg" id="2_0d7x8"]

[sub_resource type="CanvasItemMaterial" id="CanvasItemMaterial_w6cby"]
blend_mode = 4
light_mode = 1

[node name="demo_effect_custom_02" type="Node2D"]
[node name="demo_effect_custom_01" type="Node2D"]

[node name="Sprite2D" type="Sprite2D" parent="."]
material = SubResource("CanvasItemMaterial_w6cby")
position = Vector2(256, 256)
position = Vector2(640, 512)

[node name="GDCubismUserModel" type="GDCubismUserModel" parent="Sprite2D"]
disable_3d = true
transparent_bg = true
gui_disable_input = true
size = Vector2i(1024, 1024)
render_target_update_mode = 4

[node name="GDCubismEffectCustom" type="GDCubismEffectCustom" parent="Sprite2D/GDCubismUserModel"]
script = ExtResource("1_ittjg")

[node name="AudioStreamPlayer2D" type="AudioStreamPlayer2D" parent="."]
autoplay = true
bus = &"Voice"
[node name="Sprite2D" type="Sprite2D" parent="Sprite2D/GDCubismUserModel/GDCubismEffectCustom"]
z_index = 1024
scale = Vector2(0.5, 0.5)
texture = ExtResource("2_0d7x8")

[connection signal="cubism_init" from="Sprite2D/GDCubismUserModel/GDCubismEffectCustom" to="Sprite2D/GDCubismUserModel/GDCubismEffectCustom" method="_on_cubism_init"]
[connection signal="cubism_process" from="Sprite2D/GDCubismUserModel/GDCubismEffectCustom" to="Sprite2D/GDCubismUserModel/GDCubismEffectCustom" method="_on_cubism_process"]
[connection signal="cubism_term" from="Sprite2D/GDCubismUserModel/GDCubismEffectCustom" to="Sprite2D/GDCubismUserModel/GDCubismEffectCustom" method="_on_cubism_term"]
Loading

0 comments on commit 2fbc409

Please sign in to comment.