-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathstackchan_robot_box.dart
More file actions
502 lines (425 loc) · 14.1 KB
/
Copy pathstackchan_robot_box.dart
File metadata and controls
502 lines (425 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
import 'dart:math';
import 'dart:ui' as ui;
import 'package:flutter/cupertino.dart';
import 'package:three_js/three_js.dart' as three;
import '../../model/dance_list.dart';
import '../../model/expression_data.dart';
class StackChanRobotBox extends StatelessWidget {
final DanceData data;
final double width;
final double height;
final bool topLook;
final bool allowsCameraControl;
final bool mirrorFace;
const StackChanRobotBox({
super.key,
required this.width,
required this.height,
required this.data,
this.topLook = false,
this.allowsCameraControl = false,
this.mirrorFace = false,
});
@override
Widget build(BuildContext context) {
return SizedBox(
width: width,
height: height,
child: LayoutBuilder(
builder: (context, constraints) {
return StackchanRobotJs(
width: constraints.maxWidth,
height: constraints.maxHeight,
data: data,
topLook: topLook,
allowsCameraControl: allowsCameraControl,
mirrorFace: mirrorFace,
);
},
),
);
}
}
class StackchanRobotJs extends StatefulWidget {
const StackchanRobotJs({
super.key,
required this.data,
required this.width,
required this.height,
required this.topLook,
required this.allowsCameraControl,
required this.mirrorFace,
});
final DanceData data;
final double width;
final double height;
final bool topLook;
final bool allowsCameraControl;
final bool mirrorFace;
@override
State<StatefulWidget> createState() => _StackchanRobotThreeState();
}
class _StackchanRobotThreeState extends State<StackchanRobotJs> {
late three.ThreeJS threeJs;
@override
void initState() {
super.initState();
threeJs = three.ThreeJS(
settings: three.Settings(
alpha: true,
clearAlpha: 0.0,
clearColor: 0x000000,
antialias: true,
toneMapping: three.ReinhardToneMapping,
toneMappingExposure: 1,
),
onSetupComplete: () {
setState(() {});
},
setup: setup,
);
}
@override
void dispose() {
threeJs.dispose();
super.dispose();
}
@override
void didUpdateWidget(covariant StackchanRobotJs oldWidget) {
super.didUpdateWidget(oldWidget);
applyDanceData();
if (oldWidget.topLook != widget.topLook) {
setupCamera();
}
}
Future<void> setup() async {
threeJs.scene = three.Scene();
//translated comment
final hemiLight = three.HemisphereLight(0xffffff, 0x444444, 1);
hemiLight.position.setValues(0, 100, 0);
threeJs.scene.add(hemiLight);
//translated comment
final dirLight = three.DirectionalLight(0xffffff, 1);
dirLight.position.setValues(50, 50, 70);
threeJs.scene.add(dirLight);
//andset
threeJs.camera = three.PerspectiveCamera(
60,
widget.width / widget.height,
1,
300,
);
threeJs.camera.position.setValues(0, -100, 0);
//loadmodel
three.GLTFLoader loader = three.GLTFLoader(flipY: true).setPath('assets/');
final sky = await loader.fromAsset('stack_chan_model.glb');
if (sky == null || !mounted) return;
final model = sky.scene;
threeJs.scene.add(model);
setupCamera();
setupRobotHierarchy();
applyDanceData();
}
//setAngle
void setupCamera() {
if (widget.topLook) {
threeJs.camera.position.setValues(0, -100, 70);
} else {
threeJs.camera.position.setValues(0, -100, 0);
}
threeJs.camera.lookAt(threeJs.scene.position);
}
three.Object3D yawAxis = three.Object3D();
three.Object3D pitchAxis = three.Object3D();
three.Mesh? expressionPlaneMesh; //faceshow
three.CanvasTexture? expressionTexture; //facecanvastexture
final double canvasWidth = 210; //canvas(correspondingiOS 42*5)
final double canvasHeight = 160; //canvas(correspondingiOS 32*5)
final String expressionPlaneName = "expressionPlane"; //name(foriOS)
Function(double)? currentRotationEvent;
//translated comment
void setupRobotHierarchy() {
final model = threeJs.scene.children.firstWhere(
(element) => element.type == "Group",
);
final foundation = model.getObjectByName('_00_stackchan450_3');
final centralComponent = model.getObjectByName('_00_stackchan450_2');
final head = model.getObjectByName('_00_stackchan450_1');
if (foundation == null || centralComponent == null || head == null) return;
//========== LeftRightto(yaw axis)logic(originalhaslogicCankeep,) ==========
final centralWorldPos = centralComponent.worldPosition();
centralWorldPos.y -= 20;
yawAxis.setWorldPosition(centralWorldPos);
foundation.add(yawAxis);
final centralWorldTransform = centralComponent.worldTransform();
final centralWorldPosition = centralComponent.worldPosition();
yawAxis.add(centralComponent);
centralComponent.setWorldTransform(centralWorldTransform);
centralComponent.setWorldPosition(centralWorldPosition);
//========== UpDown(pitch axis)logic(corefixPart) ==========
final headWorldPosition = head.worldPosition();
final headWorldTransform = head.worldTransform();
final pitchAxisWorldPosition = pitchAxis.worldPosition();
pitchAxisWorldPosition.y -= 25;
pitchAxis.setWorldPosition(pitchAxisWorldPosition);
centralComponent.add(pitchAxis);
pitchAxis.add(head);
head.setWorldTransform(headWorldTransform);
head.setWorldPosition(headWorldPosition);
addExpressionPlane();
}
void addExpressionPlane() {
final model = threeJs.scene.children.firstWhere(
(element) => element.type == "Group",
);
final head = model.getObjectByName('_00_stackchan450_1');
if (head == null) return;
final geometry = three.PlaneGeometry(42, 32);
expressionTexture = three.CanvasTexture();
final material = three.MeshBasicMaterial({
three.MaterialProperty.map: expressionTexture,
three.MaterialProperty.transparent: false,
three.MaterialProperty.side: three.DoubleSide,
});
expressionPlaneMesh = three.Mesh(geometry, material);
expressionPlaneMesh!.name = "expressionPlane";
expressionPlaneMesh!.position.setValues(0, 15.8, 0);
expressionPlaneMesh!.rotation.x = -90 * pi / 180.0;
expressionPlaneMesh!.rotation.z = pi;
head.add(expressionPlaneMesh);
material.needsUpdate = true;
}
//writedata
void applyDanceData() {
updateServos();
updateExpression();
updateRGBColor();
setupContinuousRotation();
}
void updateServos() async {
final data = widget.data;
if (data.yawServo.rotate == 0) {
double clampedYaw = data.yawServo.angle / 10.0;
if (clampedYaw < -128) clampedYaw = -128;
if (clampedYaw > 128) clampedYaw = 128;
yawAxis.rotation.z = clampedYaw * pi / 180.0;
}
double clampedPitch = data.pitchServo.angle / 10.0;
if (clampedPitch < 0) clampedPitch = 0;
if (clampedPitch > 90) clampedPitch = 90;
pitchAxis.rotation.x = -clampedPitch * pi / 180.0;
}
void setupContinuousRotation() {
final data = widget.data;
if (currentRotationEvent != null) {
threeJs.events.remove(currentRotationEvent);
currentRotationEvent = null;
}
if (data.yawServo.rotate != 0) {
double rotateSpeed = data.yawServo.rotate / 10.0;
double radiansPerSecond = rotateSpeed * pi / 180.0;
currentRotationEvent = (double dt) {
yawAxis.rotation.z -= radiansPerSecond * dt;
};
threeJs.addAnimationEvent(currentRotationEvent!);
}
}
void updateRGBColor() {
final threeColor = toThreeColor(widget.data.leftRgbColor);
for (var node in threeJs.scene.children) {
if (node is three.Mesh) {
if (node.material != null) {
if (node.material!.name == "MTL12") {
if (node.material! is three.MeshStandardMaterial) {
node.material!.emissive = threeColor;
} else {
node.material!.color = threeColor;
}
}
}
}
}
}
Future<void> updateExpression() async {
final data = widget.data;
if (expressionPlaneMesh == null || expressionTexture == null) {
return;
}
//1. createdrawExpressioncanvas
final recorder = ui.PictureRecorder();
final canvas = ui.Canvas(recorder);
final paint = ui.Paint();
//fliphandle(Yaxisflip)
if (widget.mirrorFace) {
canvas.save();
canvas.translate(canvasWidth, 0);
canvas.scale(-1, 1);
}
//background:With / Carry 70% transparency (0xB3 = 179/255)
paint.color = const ui.Color(0xB3000000);
canvas.drawRect(ui.Rect.fromLTWH(0, 0, canvasWidth, canvasHeight), paint);
final eyeSize = canvasWidth / 10;
//draweyefunction
void drawEye(ExpressionItem item, ui.Offset centerOffset) {
canvas.save();
//calculatesizescale
final clampedSize = item.size.clamp(-100, 100);
final sizeScale = clampedSize >= 0
? 1.0 + clampedSize / 100.0
: 1.0 + clampedSize / 200.0;
final scaledEyeSize = eyeSize * sizeScale;
final visibleHeight = scaledEyeSize * (item.weight / 100);
//positionoffset
final centerX = centerOffset.dx + item.x / 10 + eyeSize / 2;
final centerY = centerOffset.dy + item.y / 10 + eyeSize / 2;
final eyeRect = ui.Rect.fromCenter(
center: ui.Offset(centerX, centerY),
width: scaledEyeSize,
height: scaledEyeSize,
);
//rotatehandle
final rotationDegrees = item.rotation / 10.0;
canvas.translate(centerX, centerY);
canvas.rotate(rotationDegrees * pi / 180);
canvas.translate(-centerX, -centerY);
//createcropeye
final clipRect = ui.Rect.fromLTRB(
eyeRect.left,
eyeRect.bottom - visibleHeight,
eyeRect.right,
eyeRect.bottom,
);
canvas.clipRect(clipRect);
//draweye
paint.color = const ui.Color(0xFFFFFFFF);
canvas.drawOval(eyeRect, paint);
canvas.restore();
}
//calculateeyeposition
final eyeY = (canvasHeight * 0.4) - (eyeSize / 2);
final leftEyePoint = ui.Offset((canvasWidth / 4) - (eyeSize / 2), eyeY);
final rightEyePoint = ui.Offset(
(canvasWidth / 4 * 3) - (eyeSize / 2),
eyeY,
);
drawEye(data.leftEye, leftEyePoint);
drawEye(data.rightEye, rightEyePoint);
//2. drawmouth
canvas.save();
final mouthWidth = (canvasWidth * 0.3 - data.mouth.weight / 10).toDouble();
final mouthHeight = (3 + data.mouth.weight * 0.2).toDouble();
final mouthX = ((canvasWidth - mouthWidth) / 2) + data.mouth.x / 10;
final mouthY = (canvasHeight * 0.65) + data.mouth.y / 10;
final mouthCenter = ui.Offset(
mouthX + mouthWidth / 2,
mouthY + mouthHeight / 2,
);
final mRotation = data.mouth.rotation / 10.0;
canvas.translate(mouthCenter.dx, mouthCenter.dy);
canvas.rotate(mRotation * pi / 180);
canvas.translate(-mouthCenter.dx, -mouthCenter.dy);
final mouthRect = ui.Rect.fromLTWH(mouthX, mouthY, mouthWidth, mouthHeight);
paint.color = const ui.Color(0xFFFFFFFF);
canvas.drawRRect(
ui.RRect.fromRectAndRadius(
mouthRect,
ui.Radius.circular(mouthHeight / 2),
),
paint,
);
canvas.restore();
//resumecanvasstate(ifperformflip)
if (widget.mirrorFace) {
canvas.restore();
}
//3. will Canvas convertastexturedata
final picture = recorder.endRecording();
final image = await picture.toImage(
canvasWidth.toInt(),
canvasHeight.toInt(),
);
if (!mounted) {
image.dispose();
return;
}
//[Core Fix]:use rawRgba And / WhileNotis png
final byteData = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
if (byteData != null) {
//convertas three_js Uint8Array
final uint8List = byteData.buffer.asUint8List();
final nativeArray = uint8List;
//updatetexture
expressionTexture!.image = three.ImageElement(
data: nativeArray,
width: canvasWidth.toInt(),
height: canvasHeight.toInt(),
);
//marktextureNeedupdate
expressionTexture!.needsUpdate = true;
//ifuse MeshBasicMaterial,EnsureWillrereadtexture
if (expressionPlaneMesh!.material is three.Material) {
(expressionPlaneMesh!.material as three.Material).needsUpdate = true;
}
}
image.dispose();
}
@override
Widget build(BuildContext context) {
return threeJs.build();
}
three.Color toThreeColor(String rgbString) {
String hex = rgbString.replaceFirst('#', '');
if (hex.length == 6) {
hex = 'FF$hex';
} else if (hex.length != 8) {
return three.Color(1, 1, 1);
}
final intValue = int.parse(hex, radix: 16);
final int a = (intValue >> 24) & 0xFF;
final int r = (intValue >> 16) & 0xFF;
final int g = (intValue >> 8) & 0xFF;
final int b = intValue & 0xFF;
return three.Color(r / 255.0, g / 255.0, b / 255.0);
}
}
extension Object3DUtil on three.Object3D {
three.Vector3 worldPosition() {
final position = three.Vector3.zero();
getWorldPosition(position);
return position;
}
void setWorldPosition(three.Vector3 worldPosition) {
if (parent != null) {
parent!.updateWorldMatrix(true, false);
final inverseParentMatrix = three.Matrix4()
.setFrom(parent!.matrixWorld)
.invert();
final localPosition = worldPosition.clone().applyMatrix4(
inverseParentMatrix,
);
position.setFrom(localPosition);
} else {
position.setFrom(worldPosition);
}
}
three.Quaternion worldTransform() {
final worldQuaternion = three.Quaternion();
getWorldQuaternion(worldQuaternion);
return worldQuaternion;
}
void setWorldTransform(three.Quaternion worldTransform) {
if (parent != null) {
parent!.updateWorldMatrix(true, false);
final parentWorldQuaternion = three.Quaternion();
parent!.getWorldQuaternion(parentWorldQuaternion);
final inverseParentQuaternion = parentWorldQuaternion.clone().conjugate();
quaternion.setFrom(inverseParentQuaternion.multiply(worldTransform));
} else {
quaternion.setFrom(worldTransform);
}
}
}