Skip to content

Commit f5c8400

Browse files
committed
✨ feat: 支持触摸响应
1 parent 6b9f20f commit f5c8400

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/libs/vrmViewer/model.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ import { LoopOnce } from 'three/src/constants';
66

77
import { loadMixamoAnimation } from '@/libs/FBXAnimation/loadMixamoAnimation';
88
import { loadVMDAnimation } from '@/libs/VMDAnimation/loadVMDAnimation';
9-
import { convert } from '@/libs/VMDAnimation/vmd2vrmanim';
10-
import { bindToVRM, toOffset } from '@/libs/VMDAnimation/vmd2vrmanim.binding';
119
import IKHandler from '@/libs/VMDAnimation/vrm-ik-handler';
1210
import { VRMAnimation } from '@/libs/VRMAnimation/VRMAnimation';
1311
import { loadVRMAnimation } from '@/libs/VRMAnimation/loadVRMAnimation';
1412
import { VRMLookAtSmootherLoaderPlugin } from '@/libs/VRMLookAtSmootherLoaderPlugin/VRMLookAtSmootherLoaderPlugin';
15-
import { AudioPlayer } from '@/libs/audioPlayer/audioPlayer';
1613
import { EmoteController } from '@/libs/emoteController/emoteController';
1714
import { LipSync } from '@/libs/lipSync/lipSync';
1815
import { Screenplay } from '@/types/touch';

src/libs/vrmViewer/viewer.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1+
import { VRMHumanBoneName } from '@pixiv/three-vrm';
12
import { Parser } from 'mmd-parser';
23
import * as THREE from 'three';
34
import { Audio, GridHelper, Mesh, MeshLambertMaterial, PlaneGeometry } from 'three';
45
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
5-
import { LoopOnce } from 'three/src/constants';
6-
7-
import { loadVMDAnimation } from '@/libs/VMDAnimation/loadVMDAnimation';
8-
import { AudioPlayer } from '@/libs/audioPlayer/audioPlayer';
96

107
import { Model } from './model';
118

@@ -159,6 +156,49 @@ export class Viewer {
159156

160157
resizeObserver.observe(parentElement!);
161158

159+
// 假设你已经有一个场景和 VRM 模型
160+
const raycaster = new THREE.Raycaster();
161+
const mouse = new THREE.Vector2();
162+
163+
// 添加触摸事件
164+
canvas.addEventListener(
165+
'click',
166+
(event) => {
167+
if (!this.model) {
168+
return;
169+
}
170+
const rect = canvas.getBoundingClientRect();
171+
const canvasWidth = rect.width;
172+
const canvasHeight = rect.height;
173+
174+
// 将鼠标坐标转换到[-1, 1]范围
175+
mouse.x = ((event.clientX - rect.left) / canvasWidth) * 2 - 1; // 水平坐标
176+
mouse.y = -((event.clientY - rect.top) / canvasHeight) * 2 + 1; // 垂直坐标
177+
178+
// 更新射线
179+
raycaster.setFromCamera(mouse, this._camera!);
180+
181+
// 检测与 VRM 模型的交互
182+
const intersects = raycaster.intersectObjects(this._scene.children, true);
183+
if (intersects.length > 0) {
184+
// 触摸到模型,执行反馈效果
185+
const touchedObject = intersects[0].object;
186+
const vrmNodeName = this.model.vrm?.humanoid?.getNormalizedBoneNode(
187+
VRMHumanBoneName.Chest,
188+
)?.name; // 'Normalized_J_Bip_C_Chest'
189+
190+
// TODO: 如何对应
191+
192+
// console.log(touchedObject, vrmNodeName);
193+
// console.log(intersects);
194+
// console.log(this.model.vrm?.scene.children);
195+
// touchedObject.material.color.set(0xff0000); // 改变颜色为红色
196+
// 你可以在这里添加更多的反馈效果
197+
}
198+
},
199+
false,
200+
);
201+
162202
this.isReady = true;
163203
this.update();
164204
}

0 commit comments

Comments
 (0)