This repository has no description
2.3 kB
70 lines
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025 MizunagiKB <mizukb@live.jp>
3using Godot;
4using System;
5
6
7#pragma warning disable IDE1006
8
9
10public partial class demo_effect_target_point : Node2D
11{
12 const System.String DEFAULT_ASSET = "res://addons/gd_cubism/example/res/live2d/mao_pro_jp/runtime/mao_pro.model3.json";
13
14 private GDCubismUserModelCS cubism_model;
15 private GDCubismEffectTargetPointCS cubism_efx;
16
17 private bool pressed = false;
18
19
20 private void recalc_model_position(GDCubismUserModelCS model)
21 {
22 if(model.Assets == "") return;
23
24 Godot.Collections.Dictionary canvas_info = model.GetCanvasInfo();
25
26 if(canvas_info.Count > 0)
27 {
28 Vector2 vct_viewport_size = new(GetViewportRect().Size.X, GetViewportRect().Size.Y);
29 Vector2 size_in_pixels = (Vector2)canvas_info["size_in_pixels"];
30 float scale = vct_viewport_size.Y / Math.Max(size_in_pixels.X, size_in_pixels.Y);
31 model.GetInternalObject().Position = new(vct_viewport_size.X * 0.5f, vct_viewport_size.Y * 0.5f);
32 model.GetInternalObject().Scale = new(scale, scale);
33 }
34 }
35
36 public override void _Ready()
37 {
38 this.cubism_model = new(GetNode<Node2D>("GDCubismUserModel"));
39 if (this.cubism_model.Assets == "") this.cubism_model.Assets = DEFAULT_ASSET;
40
41 this.cubism_efx = new(GetNode<Node>("GDCubismUserModel/GDCubismEffectTargetPoint"));
42 }
43
44 public override void _Process(double delta)
45 {
46 this.recalc_model_position(this.cubism_model);
47 base._Process(delta);
48 }
49
50 public override void _Input(InputEvent @event)
51 {
52 if (@event is InputEventMouseButton evt_mouse_button)
53 {
54 this.pressed = evt_mouse_button.IsPressed();
55 }
56
57 if (@event is InputEventMouseMotion evt_mouse_motion)
58 {
59 if (this.pressed == true)
60 {
61 Vector2 calc_pos = this.cubism_model.GetInternalObject().ToLocal(evt_mouse_motion.Position) * new Vector2(1.0f, -1.0f);
62 this.cubism_efx.SetTarget(calc_pos.Normalized());
63 } else {
64 this.cubism_efx.SetTarget(Vector2.Zero);
65 }
66 }
67
68 base._Input(@event);
69 }
70}