@nice2dev/ui-3d 1.3.15 → 1.3.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/cjs/collaborative/collaborativeScene.js +160 -206
  3. package/dist/cjs/core/editorAsk.js +51 -41
  4. package/dist/cjs/core/i18n.js +13 -14
  5. package/dist/cjs/core/threeCompat.js +29 -42
  6. package/dist/cjs/dance/DanceBridge.js +148 -177
  7. package/dist/cjs/dance/DanceScoreEngine.js +209 -221
  8. package/dist/cjs/dance/PoseDetector.js +302 -275
  9. package/dist/cjs/index.js +110 -113
  10. package/dist/cjs/material/MaterialEditor.module.css.js +242 -5
  11. package/dist/cjs/material/NiceMaterialEditor.js +1213 -757
  12. package/dist/cjs/material/materialEditorTypes.js +35 -49
  13. package/dist/cjs/material/materialEditorUtils.js +684 -747
  14. package/dist/cjs/material/materialNodeDefinitions.js +1411 -1459
  15. package/dist/cjs/model/ModelEditor.js +90 -40
  16. package/dist/cjs/model/ModelEditor.module.css.js +257 -5
  17. package/dist/cjs/model/ModelEditorLeftPanel.js +126 -47
  18. package/dist/cjs/model/ModelEditorMenuBar.js +359 -32
  19. package/dist/cjs/model/ModelEditorRightPanel.js +647 -131
  20. package/dist/cjs/model/ModelEditorSubComponents.js +277 -112
  21. package/dist/cjs/model/ModelEditorTimeline.js +232 -34
  22. package/dist/cjs/model/ModelEditorToolbar.js +170 -14
  23. package/dist/cjs/model/ModelEditorViewport.js +231 -39
  24. package/dist/cjs/model/ModelViewer.js +388 -63
  25. package/dist/cjs/model/ModelViewer.module.css.js +77 -5
  26. package/dist/cjs/model/NiceArmatureEditor.js +473 -274
  27. package/dist/cjs/model/NiceMorphTargetEditor.js +333 -227
  28. package/dist/cjs/model/NiceOctree.js +372 -320
  29. package/dist/cjs/model/NicePhysicsSimulation.js +528 -298
  30. package/dist/cjs/model/NiceProceduralGeometry.js +673 -300
  31. package/dist/cjs/model/NiceTerrainEditor.js +595 -385
  32. package/dist/cjs/model/NiceWeightPainter.js +384 -261
  33. package/dist/cjs/model/NiceXRPreview.js +385 -308
  34. package/dist/cjs/model/cadModeUtils.js +103 -106
  35. package/dist/cjs/model/editorShortcuts.js +171 -194
  36. package/dist/cjs/model/modelEditorTypes.js +100 -118
  37. package/dist/cjs/model/modelEditorUtils.js +1061 -1146
  38. package/dist/cjs/model/simsModeUtils.js +1023 -1053
  39. package/dist/cjs/model/useModelEditor.js +1977 -1933
  40. package/dist/cjs/model/useModelViewer.js +633 -665
  41. package/dist/cjs/nice2dev-ui-3d.css +1 -1
  42. package/dist/cjs/particle/NiceParticleEditor.js +1186 -571
  43. package/dist/cjs/particle/ParticleEditor.module.css.js +122 -5
  44. package/dist/cjs/particle/particleEditorTypes.js +62 -70
  45. package/dist/cjs/particle/particleEditorUtils.js +998 -1013
  46. package/dist/cjs/rendering/NiceCascadedShadows.js +381 -259
  47. package/dist/cjs/rendering/NiceRenderExport.js +424 -337
  48. package/dist/cjs/rendering/NiceSSAO.js +454 -337
  49. package/dist/cjs/rendering/NiceSSR.js +387 -261
  50. package/dist/cjs/rendering/NiceWebGPURenderer.js +359 -230
  51. package/dist/cjs/uv/NiceUVEditor.js +787 -550
  52. package/dist/cjs/uv/UVEditor.module.css.js +53 -5
  53. package/dist/cjs/uv/uvEditorTypes.js +59 -76
  54. package/dist/cjs/uv/uvEditorUtils.js +501 -575
  55. package/dist/esm/collaborative/collaborativeScene.js +163 -205
  56. package/dist/esm/core/editorAsk.js +53 -40
  57. package/dist/esm/core/i18n.js +15 -13
  58. package/dist/esm/core/threeCompat.js +18 -26
  59. package/dist/esm/dance/DanceBridge.js +151 -176
  60. package/dist/esm/dance/DanceScoreEngine.js +211 -220
  61. package/dist/esm/dance/PoseDetector.js +304 -274
  62. package/dist/esm/index.js +258 -36
  63. package/dist/esm/material/MaterialEditor.module.css.js +242 -3
  64. package/dist/esm/material/NiceMaterialEditor.js +1215 -756
  65. package/dist/esm/material/materialEditorTypes.js +45 -48
  66. package/dist/esm/material/materialEditorUtils.js +713 -746
  67. package/dist/esm/material/materialNodeDefinitions.js +1415 -1458
  68. package/dist/esm/model/ModelEditor.js +91 -39
  69. package/dist/esm/model/ModelEditor.module.css.js +257 -3
  70. package/dist/esm/model/ModelEditorLeftPanel.js +126 -45
  71. package/dist/esm/model/ModelEditorMenuBar.js +359 -30
  72. package/dist/esm/model/ModelEditorRightPanel.js +634 -114
  73. package/dist/esm/model/ModelEditorSubComponents.js +265 -94
  74. package/dist/esm/model/ModelEditorTimeline.js +232 -32
  75. package/dist/esm/model/ModelEditorToolbar.js +170 -12
  76. package/dist/esm/model/ModelEditorViewport.js +231 -37
  77. package/dist/esm/model/ModelViewer.js +389 -62
  78. package/dist/esm/model/ModelViewer.module.css.js +77 -3
  79. package/dist/esm/model/NiceArmatureEditor.js +462 -258
  80. package/dist/esm/model/NiceMorphTargetEditor.js +322 -211
  81. package/dist/esm/model/NiceOctree.js +361 -304
  82. package/dist/esm/model/NicePhysicsSimulation.js +517 -282
  83. package/dist/esm/model/NiceProceduralGeometry.js +667 -284
  84. package/dist/esm/model/NiceTerrainEditor.js +584 -369
  85. package/dist/esm/model/NiceWeightPainter.js +373 -245
  86. package/dist/esm/model/NiceXRPreview.js +374 -292
  87. package/dist/esm/model/cadModeUtils.js +95 -88
  88. package/dist/esm/model/editorShortcuts.js +172 -193
  89. package/dist/esm/model/modelEditorTypes.js +90 -100
  90. package/dist/esm/model/modelEditorUtils.js +1056 -1128
  91. package/dist/esm/model/simsModeUtils.js +1021 -1035
  92. package/dist/esm/model/useModelEditor.js +1946 -1920
  93. package/dist/esm/model/useModelViewer.js +619 -647
  94. package/dist/esm/nice2dev-ui-3d.css +1 -1
  95. package/dist/esm/particle/NiceParticleEditor.js +1188 -570
  96. package/dist/esm/particle/ParticleEditor.module.css.js +122 -3
  97. package/dist/esm/particle/particleEditorTypes.js +69 -69
  98. package/dist/esm/particle/particleEditorUtils.js +1027 -1012
  99. package/dist/esm/rendering/NiceCascadedShadows.js +370 -243
  100. package/dist/esm/rendering/NiceRenderExport.js +413 -321
  101. package/dist/esm/rendering/NiceSSAO.js +450 -328
  102. package/dist/esm/rendering/NiceSSR.js +383 -252
  103. package/dist/esm/rendering/NiceWebGPURenderer.js +326 -214
  104. package/dist/esm/uv/NiceUVEditor.js +788 -549
  105. package/dist/esm/uv/UVEditor.module.css.js +53 -3
  106. package/dist/esm/uv/uvEditorTypes.js +68 -75
  107. package/dist/esm/uv/uvEditorUtils.js +515 -557
  108. package/package.json +7 -12
@@ -1,262 +1,390 @@
1
- import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
2
- import { NiceIcon } from '@nice2dev/ui';
3
- import { useEffect, useState, useRef, useCallback } from 'react';
4
- import * as THREE from 'three';
5
- import { useNiceTranslation } from '../core/i18n.js';
6
-
7
- /* ── Weight painting utilities ── */
1
+ import { jsxs, jsx, Fragment } from "react/jsx-runtime";
2
+ import { NiceIcon } from "@nice2dev/ui";
3
+ import { useEffect, useState, useRef, useCallback } from "react";
4
+ import * as THREE from "three";
5
+ import { useNiceTranslation } from "../core/i18n.js";
8
6
  function getFalloff(distance, radius, type) {
9
- const t = Math.min(distance / radius, 1);
10
- switch (type) {
11
- case 'constant':
12
- return 1;
13
- case 'linear':
14
- return 1 - t;
15
- case 'smooth':
16
- return 1 - t * t * (3 - 2 * t); // smoothstep
17
- case 'sharp':
18
- return Math.pow(1 - t, 3);
19
- default:
20
- return 1 - t;
21
- }
7
+ const t = Math.min(distance / radius, 1);
8
+ switch (type) {
9
+ case "constant":
10
+ return 1;
11
+ case "linear":
12
+ return 1 - t;
13
+ case "smooth":
14
+ return 1 - t * t * (3 - 2 * t);
15
+ // smoothstep
16
+ case "sharp":
17
+ return Math.pow(1 - t, 3);
18
+ default:
19
+ return 1 - t;
20
+ }
22
21
  }
23
- /* ── Hook ── */
24
22
  function useWeightPainter(skinnedMesh, scene, camera, canvas, onWeightsChange) {
25
- var _a, _b;
26
- const [brush, setBrushState] = useState({
27
- radius: 0.5,
28
- strength: 0.5,
29
- falloff: 'smooth',
30
- mode: 'add',
31
- });
32
- const [isPainting, setIsPainting] = useState(false);
33
- const [activeBoneIndex, setActiveBoneIndex] = useState(0);
34
- const [showHeatMap, setShowHeatMap] = useState(true);
35
- const raycaster = useRef(new THREE.Raycaster());
36
- const heatMapMaterial = useRef(null);
37
- const originalMaterial = useRef(null);
38
- // Get bone names from skeleton
39
- const boneNames = (_b = (_a = skinnedMesh === null || skinnedMesh === void 0 ? void 0 : skinnedMesh.skeleton) === null || _a === void 0 ? void 0 : _a.bones.map((b, i) => b.name || `Bone ${i}`)) !== null && _b !== void 0 ? _b : [];
40
- // Create heat map overlay material
41
- useEffect(() => {
42
- if (!skinnedMesh || !showHeatMap) {
43
- return;
23
+ const [brush, setBrushState] = useState({
24
+ radius: 0.5,
25
+ strength: 0.5,
26
+ falloff: "smooth",
27
+ mode: "add"
28
+ });
29
+ const [isPainting, setIsPainting] = useState(false);
30
+ const [activeBoneIndex, setActiveBoneIndex] = useState(0);
31
+ const [showHeatMap, setShowHeatMap] = useState(true);
32
+ const raycaster = useRef(new THREE.Raycaster());
33
+ const heatMapMaterial = useRef(null);
34
+ const originalMaterial = useRef(null);
35
+ const boneNames = skinnedMesh?.skeleton?.bones.map((b, i) => b.name || `Bone ${i}`) ?? [];
36
+ useEffect(() => {
37
+ if (!skinnedMesh || !showHeatMap) {
38
+ return;
39
+ }
40
+ originalMaterial.current = skinnedMesh.material;
41
+ const weightColors = new Float32Array(skinnedMesh.geometry.attributes.position.count * 3);
42
+ updateHeatMapColors(skinnedMesh, activeBoneIndex, weightColors);
43
+ skinnedMesh.geometry.setAttribute("weightColor", new THREE.BufferAttribute(weightColors, 3));
44
+ const mat = new THREE.ShaderMaterial({
45
+ vertexShader: `
46
+ attribute vec3 weightColor;
47
+ varying vec3 vColor;
48
+ void main() {
49
+ vColor = weightColor;
50
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
44
51
  }
45
- originalMaterial.current = skinnedMesh.material;
46
- const weightColors = new Float32Array(skinnedMesh.geometry.attributes.position.count * 3);
47
- updateHeatMapColors(skinnedMesh, activeBoneIndex, weightColors);
48
- skinnedMesh.geometry.setAttribute('weightColor', new THREE.BufferAttribute(weightColors, 3));
49
- const mat = new THREE.ShaderMaterial({
50
- vertexShader: `
51
- attribute vec3 weightColor;
52
- varying vec3 vColor;
53
- void main() {
54
- vColor = weightColor;
55
- gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
56
- }
57
52
  `,
58
- fragmentShader: `
59
- varying vec3 vColor;
60
- void main() {
61
- gl_FragColor = vec4(vColor, 1.0);
62
- }
63
- `,
64
- side: THREE.DoubleSide,
65
- });
66
- heatMapMaterial.current = mat;
67
- skinnedMesh.material = mat;
68
- return () => {
69
- if (originalMaterial.current) {
70
- skinnedMesh.material = originalMaterial.current;
71
- }
72
- skinnedMesh.geometry.deleteAttribute('weightColor');
73
- mat.dispose();
74
- };
75
- }, [skinnedMesh, showHeatMap, activeBoneIndex]);
76
- const setBrush = useCallback((partial) => {
77
- setBrushState((prev) => ({ ...prev, ...partial }));
78
- }, []);
79
- const paintAtPoint = useCallback((point) => {
80
- if (!skinnedMesh) {
81
- return;
82
- }
83
- const geometry = skinnedMesh.geometry;
84
- const posAttr = geometry.attributes.position;
85
- const skinWeightAttr = geometry.attributes.skinWeight;
86
- const skinIndexAttr = geometry.attributes.skinIndex;
87
- if (!skinWeightAttr || !skinIndexAttr) {
88
- return;
89
- }
90
- const vertex = new THREE.Vector3();
91
- for (let i = 0; i < posAttr.count; i++) {
92
- vertex.fromBufferAttribute(posAttr, i);
93
- skinnedMesh.localToWorld(vertex);
94
- const dist = vertex.distanceTo(point);
95
- if (dist > brush.radius) {
96
- continue;
97
- }
98
- const influence = getFalloff(dist, brush.radius, brush.falloff) * brush.strength;
99
- // Get current skin weights for this vertex
100
- const weights = new THREE.Vector4().fromBufferAttribute(skinWeightAttr, i);
101
- const indices = new THREE.Vector4().fromBufferAttribute(skinIndexAttr, i);
102
- // Find which component (x/y/z/w) has our bone index
103
- const components = [indices.x, indices.y, indices.z, indices.w];
104
- let slot = components.indexOf(activeBoneIndex);
105
- // If bone not assigned to this vertex, find an empty or lowest-weight slot
106
- if (slot === -1) {
107
- const weightArr = [weights.x, weights.y, weights.z, weights.w];
108
- slot = weightArr.indexOf(Math.min(...weightArr));
109
- // Set the bone index in that slot
110
- const compKey = ['x', 'y', 'z', 'w'][slot];
111
- indices[compKey] = activeBoneIndex;
112
- skinIndexAttr.setXYZW(i, indices.x, indices.y, indices.z, indices.w);
113
- }
114
- const compKey = ['x', 'y', 'z', 'w'][slot];
115
- let currentWeight = weights[compKey];
116
- switch (brush.mode) {
117
- case 'add':
118
- currentWeight = Math.min(1, currentWeight + influence);
119
- break;
120
- case 'subtract':
121
- currentWeight = Math.max(0, currentWeight - influence);
122
- break;
123
- case 'smooth': {
124
- // Average with neighbors — simplified: move toward 0.5
125
- currentWeight = currentWeight + (0.5 - currentWeight) * influence;
126
- break;
127
- }
128
- case 'blur': {
129
- currentWeight = currentWeight * (1 - influence * 0.5);
130
- break;
131
- }
132
- }
133
- weights[compKey] = currentWeight;
134
- // Normalize weights so they sum to 1
135
- const total = weights.x + weights.y + weights.z + weights.w;
136
- if (total > 0) {
137
- weights.divideScalar(total);
138
- }
139
- skinWeightAttr.setXYZW(i, weights.x, weights.y, weights.z, weights.w);
53
+ fragmentShader: `
54
+ varying vec3 vColor;
55
+ void main() {
56
+ gl_FragColor = vec4(vColor, 1.0);
140
57
  }
141
- skinWeightAttr.needsUpdate = true;
142
- skinIndexAttr.needsUpdate = true;
143
- // Update heat map if shown
144
- if (showHeatMap && heatMapMaterial.current) {
145
- const colorAttr = geometry.attributes.weightColor;
146
- if (colorAttr) {
147
- const colors = new Float32Array(posAttr.count * 3);
148
- updateHeatMapColors(skinnedMesh, activeBoneIndex, colors);
149
- colorAttr.set(colors);
150
- colorAttr.needsUpdate = true;
151
- }
58
+ `,
59
+ side: THREE.DoubleSide
60
+ });
61
+ heatMapMaterial.current = mat;
62
+ skinnedMesh.material = mat;
63
+ return () => {
64
+ if (originalMaterial.current) {
65
+ skinnedMesh.material = originalMaterial.current;
66
+ }
67
+ skinnedMesh.geometry.deleteAttribute("weightColor");
68
+ mat.dispose();
69
+ };
70
+ }, [skinnedMesh, showHeatMap, activeBoneIndex]);
71
+ const setBrush = useCallback((partial) => {
72
+ setBrushState((prev) => ({ ...prev, ...partial }));
73
+ }, []);
74
+ const paintAtPoint = useCallback(
75
+ (point) => {
76
+ if (!skinnedMesh) {
77
+ return;
78
+ }
79
+ const geometry = skinnedMesh.geometry;
80
+ const posAttr = geometry.attributes.position;
81
+ const skinWeightAttr = geometry.attributes.skinWeight;
82
+ const skinIndexAttr = geometry.attributes.skinIndex;
83
+ if (!skinWeightAttr || !skinIndexAttr) {
84
+ return;
85
+ }
86
+ const vertex = new THREE.Vector3();
87
+ for (let i = 0; i < posAttr.count; i++) {
88
+ vertex.fromBufferAttribute(posAttr, i);
89
+ skinnedMesh.localToWorld(vertex);
90
+ const dist = vertex.distanceTo(point);
91
+ if (dist > brush.radius) {
92
+ continue;
152
93
  }
153
- onWeightsChange === null || onWeightsChange === void 0 ? void 0 : onWeightsChange(skinnedMesh);
154
- }, [skinnedMesh, brush, activeBoneIndex, showHeatMap, onWeightsChange]);
155
- // Mouse event handlers
156
- useEffect(() => {
157
- if (!canvas || !camera || !isPainting || !skinnedMesh) {
158
- return;
94
+ const influence = getFalloff(dist, brush.radius, brush.falloff) * brush.strength;
95
+ const weights = new THREE.Vector4().fromBufferAttribute(skinWeightAttr, i);
96
+ const indices = new THREE.Vector4().fromBufferAttribute(skinIndexAttr, i);
97
+ const components = [indices.x, indices.y, indices.z, indices.w];
98
+ let slot = components.indexOf(activeBoneIndex);
99
+ if (slot === -1) {
100
+ const weightArr = [weights.x, weights.y, weights.z, weights.w];
101
+ slot = weightArr.indexOf(Math.min(...weightArr));
102
+ const compKey2 = ["x", "y", "z", "w"][slot];
103
+ indices[compKey2] = activeBoneIndex;
104
+ skinIndexAttr.setXYZW(i, indices.x, indices.y, indices.z, indices.w);
159
105
  }
160
- const onMouseMove = (e) => {
161
- const rect = canvas.getBoundingClientRect();
162
- const mouse = new THREE.Vector2(((e.clientX - rect.left) / rect.width) * 2 - 1, -((e.clientY - rect.top) / rect.height) * 2 + 1);
163
- raycaster.current.setFromCamera(mouse, camera);
164
- const intersects = raycaster.current.intersectObject(skinnedMesh);
165
- if (intersects.length > 0) {
166
- paintAtPoint(intersects[0].point);
167
- }
168
- };
169
- canvas.addEventListener('mousemove', onMouseMove);
170
- return () => canvas.removeEventListener('mousemove', onMouseMove);
171
- }, [canvas, camera, isPainting, skinnedMesh, paintAtPoint]);
172
- const normalizeWeights = useCallback(() => {
173
- if (!skinnedMesh) {
174
- return;
106
+ const compKey = ["x", "y", "z", "w"][slot];
107
+ let currentWeight = weights[compKey];
108
+ switch (brush.mode) {
109
+ case "add":
110
+ currentWeight = Math.min(1, currentWeight + influence);
111
+ break;
112
+ case "subtract":
113
+ currentWeight = Math.max(0, currentWeight - influence);
114
+ break;
115
+ case "smooth": {
116
+ currentWeight = currentWeight + (0.5 - currentWeight) * influence;
117
+ break;
118
+ }
119
+ case "blur": {
120
+ currentWeight = currentWeight * (1 - influence * 0.5);
121
+ break;
122
+ }
175
123
  }
176
- const skinWeightAttr = skinnedMesh.geometry.attributes.skinWeight;
177
- if (!skinWeightAttr) {
178
- return;
124
+ weights[compKey] = currentWeight;
125
+ const total = weights.x + weights.y + weights.z + weights.w;
126
+ if (total > 0) {
127
+ weights.divideScalar(total);
179
128
  }
180
- for (let i = 0; i < skinWeightAttr.count; i++) {
181
- const w = new THREE.Vector4().fromBufferAttribute(skinWeightAttr, i);
182
- const total = w.x + w.y + w.z + w.w;
183
- if (total > 0) {
184
- w.divideScalar(total);
185
- skinWeightAttr.setXYZW(i, w.x, w.y, w.z, w.w);
186
- }
129
+ skinWeightAttr.setXYZW(i, weights.x, weights.y, weights.z, weights.w);
130
+ }
131
+ skinWeightAttr.needsUpdate = true;
132
+ skinIndexAttr.needsUpdate = true;
133
+ if (showHeatMap && heatMapMaterial.current) {
134
+ const colorAttr = geometry.attributes.weightColor;
135
+ if (colorAttr) {
136
+ const colors = new Float32Array(posAttr.count * 3);
137
+ updateHeatMapColors(skinnedMesh, activeBoneIndex, colors);
138
+ colorAttr.set(colors);
139
+ colorAttr.needsUpdate = true;
187
140
  }
188
- skinWeightAttr.needsUpdate = true;
189
- }, [skinnedMesh]);
190
- return {
191
- brush,
192
- setBrush,
193
- isPainting,
194
- activeBoneIndex,
195
- setActiveBoneIndex,
196
- boneNames,
197
- startPainting: () => setIsPainting(true),
198
- stopPainting: () => setIsPainting(false),
199
- normalizeWeights,
200
- showHeatMap,
201
- setShowHeatMap,
141
+ }
142
+ onWeightsChange?.(skinnedMesh);
143
+ },
144
+ [skinnedMesh, brush, activeBoneIndex, showHeatMap, onWeightsChange]
145
+ );
146
+ useEffect(() => {
147
+ if (!canvas || !camera || !isPainting || !skinnedMesh) {
148
+ return;
149
+ }
150
+ const onMouseMove = (e) => {
151
+ const rect = canvas.getBoundingClientRect();
152
+ const mouse = new THREE.Vector2(
153
+ (e.clientX - rect.left) / rect.width * 2 - 1,
154
+ -((e.clientY - rect.top) / rect.height) * 2 + 1
155
+ );
156
+ raycaster.current.setFromCamera(mouse, camera);
157
+ const intersects = raycaster.current.intersectObject(skinnedMesh);
158
+ if (intersects.length > 0) {
159
+ paintAtPoint(intersects[0].point);
160
+ }
202
161
  };
203
- }
204
- /* ── Helpers ── */
205
- function updateHeatMapColors(mesh, boneIndex, colors) {
206
- const skinWeightAttr = mesh.geometry.attributes.skinWeight;
207
- const skinIndexAttr = mesh.geometry.attributes.skinIndex;
208
- if (!skinWeightAttr || !skinIndexAttr) {
209
- return;
162
+ canvas.addEventListener("mousemove", onMouseMove);
163
+ return () => canvas.removeEventListener("mousemove", onMouseMove);
164
+ }, [canvas, camera, isPainting, skinnedMesh, paintAtPoint]);
165
+ const normalizeWeights = useCallback(() => {
166
+ if (!skinnedMesh) {
167
+ return;
168
+ }
169
+ const skinWeightAttr = skinnedMesh.geometry.attributes.skinWeight;
170
+ if (!skinWeightAttr) {
171
+ return;
210
172
  }
211
173
  for (let i = 0; i < skinWeightAttr.count; i++) {
212
- const indices = new THREE.Vector4().fromBufferAttribute(skinIndexAttr, i);
213
- const weights = new THREE.Vector4().fromBufferAttribute(skinWeightAttr, i);
214
- const components = [indices.x, indices.y, indices.z, indices.w];
215
- const weightArr = [weights.x, weights.y, weights.z, weights.w];
216
- const slot = components.indexOf(boneIndex);
217
- const weight = slot >= 0 ? weightArr[slot] : 0;
218
- // Blue → Green → Red heat map
219
- const r = Math.min(1, weight * 2);
220
- const g = weight < 0.5 ? weight * 2 : 2 - weight * 2;
221
- const b = Math.max(0, 1 - weight * 2);
222
- colors[i * 3] = r;
223
- colors[i * 3 + 1] = g;
224
- colors[i * 3 + 2] = b;
174
+ const w = new THREE.Vector4().fromBufferAttribute(skinWeightAttr, i);
175
+ const total = w.x + w.y + w.z + w.w;
176
+ if (total > 0) {
177
+ w.divideScalar(total);
178
+ skinWeightAttr.setXYZW(i, w.x, w.y, w.z, w.w);
179
+ }
225
180
  }
181
+ skinWeightAttr.needsUpdate = true;
182
+ }, [skinnedMesh]);
183
+ return {
184
+ brush,
185
+ setBrush,
186
+ isPainting,
187
+ activeBoneIndex,
188
+ setActiveBoneIndex,
189
+ boneNames,
190
+ startPainting: () => setIsPainting(true),
191
+ stopPainting: () => setIsPainting(false),
192
+ normalizeWeights,
193
+ showHeatMap,
194
+ setShowHeatMap
195
+ };
226
196
  }
227
- /* ── Component ── */
228
- const NiceWeightPainter = ({ skinnedMesh, activeBoneIndex: initialBoneIndex, scene, camera, canvas, onWeightsChange, className = '', }) => {
229
- const { t } = useNiceTranslation();
230
- const { brush, setBrush, isPainting, activeBoneIndex, setActiveBoneIndex, boneNames, startPainting, stopPainting, normalizeWeights, showHeatMap, setShowHeatMap, } = useWeightPainter(skinnedMesh, scene, camera, canvas, onWeightsChange);
231
- useEffect(() => {
232
- setActiveBoneIndex(initialBoneIndex);
233
- }, [initialBoneIndex, setActiveBoneIndex]);
234
- return (jsxs("div", { className: `nice-weight-painter ${className}`, style: { display: 'grid', gap: 8, fontSize: 13 }, children: [jsxs("label", { children: [t('weightPainter.bone', 'Bone:'), jsx("select", { value: activeBoneIndex, onChange: (e) => setActiveBoneIndex(parseInt(e.target.value, 10)), style: { marginLeft: 8, width: '100%' }, children: boneNames.map((name, i) => (jsx("option", { value: i, children: name }, i))) })] }), jsxs("fieldset", { style: {
235
- display: 'flex',
236
- gap: 4,
237
- border: '1px solid var(--nice-border, #e2e8f0)',
238
- borderRadius: 6,
239
- padding: 4,
240
- }, children: [jsx("legend", { style: { fontSize: 11 }, children: t('weightPainter.mode', 'Mode') }), ['add', 'subtract', 'smooth', 'blur'].map((mode) => (jsx("button", { type: "button", onClick: () => setBrush({ mode }), style: {
241
- flex: 1,
242
- padding: '4px 6px',
243
- background: brush.mode === mode ? 'var(--nice-color-accent, #6366f1)' : 'transparent',
244
- color: brush.mode === mode ? 'var(--nice-color-white, #fff)' : 'inherit',
245
- border: '1px solid var(--nice-border, #e2e8f0)',
246
- borderRadius: 4,
247
- cursor: 'pointer',
248
- fontSize: 11,
249
- }, children: mode }, mode)))] }), jsxs("label", { children: [t('weightPainter.radius', 'Radius: {radius}').replace('{radius}', String(brush.radius.toFixed(2))), jsx("input", { type: "range", min: 0.05, max: 2, step: 0.05, value: brush.radius, onChange: (e) => setBrush({ radius: parseFloat(e.target.value) }), style: { width: '100%' } })] }), jsxs("label", { children: [t('weightPainter.strength', 'Strength: {strength}').replace('{strength}', String(brush.strength.toFixed(2))), jsx("input", { type: "range", min: 0.01, max: 1, step: 0.01, value: brush.strength, onChange: (e) => setBrush({ strength: parseFloat(e.target.value) }), style: { width: '100%' } })] }), jsxs("div", { style: { display: 'flex', gap: 4 }, children: [jsx("button", { type: "button", onClick: isPainting ? stopPainting : startPainting, style: {
250
- flex: 1,
251
- background: isPainting
252
- ? 'var(--nice-danger, #ef4444)'
253
- : 'var(--nice-color-accent, #6366f1)',
254
- color: 'var(--nice-color-white, #fff)',
255
- border: 'none',
256
- borderRadius: 4,
257
- padding: '6px 12px',
258
- cursor: 'pointer',
259
- }, children: isPainting ? (jsxs(Fragment, { children: [jsx(NiceIcon, { name: "stop", size: 14 }), " ", t('weightPainter.stopPainting', 'Stop Painting')] })) : (jsxs(Fragment, { children: [jsx(NiceIcon, { name: "brush", size: 14 }), ' ', t('weightPainter.startPainting', 'Start Painting')] })) }), jsxs("button", { type: "button", onClick: normalizeWeights, title: t('weightPainter.normalizeAllWeights', 'Normalize all weights'), children: [jsx(NiceIcon, { name: "scale", size: 14 }), " ", t('weightPainter.normalize', 'Normalize')] })] }), jsxs("label", { style: { display: 'flex', alignItems: 'center', gap: 6 }, children: [jsx("input", { type: "checkbox", checked: showHeatMap, onChange: (e) => setShowHeatMap(e.target.checked) }), t('weightPainter.showWeightHeatMap', 'Show weight heat map')] })] }));
197
+ function updateHeatMapColors(mesh, boneIndex, colors) {
198
+ const skinWeightAttr = mesh.geometry.attributes.skinWeight;
199
+ const skinIndexAttr = mesh.geometry.attributes.skinIndex;
200
+ if (!skinWeightAttr || !skinIndexAttr) {
201
+ return;
202
+ }
203
+ for (let i = 0; i < skinWeightAttr.count; i++) {
204
+ const indices = new THREE.Vector4().fromBufferAttribute(skinIndexAttr, i);
205
+ const weights = new THREE.Vector4().fromBufferAttribute(skinWeightAttr, i);
206
+ const components = [indices.x, indices.y, indices.z, indices.w];
207
+ const weightArr = [weights.x, weights.y, weights.z, weights.w];
208
+ const slot = components.indexOf(boneIndex);
209
+ const weight = slot >= 0 ? weightArr[slot] : 0;
210
+ const r = Math.min(1, weight * 2);
211
+ const g = weight < 0.5 ? weight * 2 : 2 - weight * 2;
212
+ const b = Math.max(0, 1 - weight * 2);
213
+ colors[i * 3] = r;
214
+ colors[i * 3 + 1] = g;
215
+ colors[i * 3 + 2] = b;
216
+ }
217
+ }
218
+ const NiceWeightPainter = ({
219
+ skinnedMesh,
220
+ activeBoneIndex: initialBoneIndex,
221
+ scene,
222
+ camera,
223
+ canvas,
224
+ onWeightsChange,
225
+ className = ""
226
+ }) => {
227
+ const { t } = useNiceTranslation();
228
+ const {
229
+ brush,
230
+ setBrush,
231
+ isPainting,
232
+ activeBoneIndex,
233
+ setActiveBoneIndex,
234
+ boneNames,
235
+ startPainting,
236
+ stopPainting,
237
+ normalizeWeights,
238
+ showHeatMap,
239
+ setShowHeatMap
240
+ } = useWeightPainter(skinnedMesh, scene, camera, canvas, onWeightsChange);
241
+ useEffect(() => {
242
+ setActiveBoneIndex(initialBoneIndex);
243
+ }, [initialBoneIndex, setActiveBoneIndex]);
244
+ return /* @__PURE__ */ jsxs(
245
+ "div",
246
+ {
247
+ className: `nice-weight-painter ${className}`,
248
+ style: { display: "grid", gap: 8, fontSize: 13 },
249
+ children: [
250
+ /* @__PURE__ */ jsxs("label", { children: [
251
+ t("weightPainter.bone", "Bone:"),
252
+ /* @__PURE__ */ jsx(
253
+ "select",
254
+ {
255
+ value: activeBoneIndex,
256
+ onChange: (e) => setActiveBoneIndex(parseInt(e.target.value, 10)),
257
+ style: { marginLeft: 8, width: "100%" },
258
+ children: boneNames.map((name, i) => /* @__PURE__ */ jsx("option", { value: i, children: name }, i))
259
+ }
260
+ )
261
+ ] }),
262
+ /* @__PURE__ */ jsxs(
263
+ "fieldset",
264
+ {
265
+ style: {
266
+ display: "flex",
267
+ gap: 4,
268
+ border: "1px solid var(--nice-border, #e2e8f0)",
269
+ borderRadius: 6,
270
+ padding: 4
271
+ },
272
+ children: [
273
+ /* @__PURE__ */ jsx("legend", { style: { fontSize: 11 }, children: t("weightPainter.mode", "Mode") }),
274
+ ["add", "subtract", "smooth", "blur"].map((mode) => /* @__PURE__ */ jsx(
275
+ "button",
276
+ {
277
+ type: "button",
278
+ onClick: () => setBrush({ mode }),
279
+ style: {
280
+ flex: 1,
281
+ padding: "4px 6px",
282
+ background: brush.mode === mode ? "var(--nice-color-accent, #6366f1)" : "transparent",
283
+ color: brush.mode === mode ? "var(--nice-color-white, #fff)" : "inherit",
284
+ border: "1px solid var(--nice-border, #e2e8f0)",
285
+ borderRadius: 4,
286
+ cursor: "pointer",
287
+ fontSize: 11
288
+ },
289
+ children: mode
290
+ },
291
+ mode
292
+ ))
293
+ ]
294
+ }
295
+ ),
296
+ /* @__PURE__ */ jsxs("label", { children: [
297
+ t("weightPainter.radius", "Radius: {radius}").replace(
298
+ "{radius}",
299
+ String(brush.radius.toFixed(2))
300
+ ),
301
+ /* @__PURE__ */ jsx(
302
+ "input",
303
+ {
304
+ type: "range",
305
+ min: 0.05,
306
+ max: 2,
307
+ step: 0.05,
308
+ value: brush.radius,
309
+ onChange: (e) => setBrush({ radius: parseFloat(e.target.value) }),
310
+ style: { width: "100%" }
311
+ }
312
+ )
313
+ ] }),
314
+ /* @__PURE__ */ jsxs("label", { children: [
315
+ t("weightPainter.strength", "Strength: {strength}").replace(
316
+ "{strength}",
317
+ String(brush.strength.toFixed(2))
318
+ ),
319
+ /* @__PURE__ */ jsx(
320
+ "input",
321
+ {
322
+ type: "range",
323
+ min: 0.01,
324
+ max: 1,
325
+ step: 0.01,
326
+ value: brush.strength,
327
+ onChange: (e) => setBrush({ strength: parseFloat(e.target.value) }),
328
+ style: { width: "100%" }
329
+ }
330
+ )
331
+ ] }),
332
+ /* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: 4 }, children: [
333
+ /* @__PURE__ */ jsx(
334
+ "button",
335
+ {
336
+ type: "button",
337
+ onClick: isPainting ? stopPainting : startPainting,
338
+ style: {
339
+ flex: 1,
340
+ background: isPainting ? "var(--nice-danger, #ef4444)" : "var(--nice-color-accent, #6366f1)",
341
+ color: "var(--nice-color-white, #fff)",
342
+ border: "none",
343
+ borderRadius: 4,
344
+ padding: "6px 12px",
345
+ cursor: "pointer"
346
+ },
347
+ children: isPainting ? /* @__PURE__ */ jsxs(Fragment, { children: [
348
+ /* @__PURE__ */ jsx(NiceIcon, { name: "stop", size: 14 }),
349
+ " ",
350
+ t("weightPainter.stopPainting", "Stop Painting")
351
+ ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
352
+ /* @__PURE__ */ jsx(NiceIcon, { name: "brush", size: 14 }),
353
+ " ",
354
+ t("weightPainter.startPainting", "Start Painting")
355
+ ] })
356
+ }
357
+ ),
358
+ /* @__PURE__ */ jsxs(
359
+ "button",
360
+ {
361
+ type: "button",
362
+ onClick: normalizeWeights,
363
+ title: t("weightPainter.normalizeAllWeights", "Normalize all weights"),
364
+ children: [
365
+ /* @__PURE__ */ jsx(NiceIcon, { name: "scale", size: 14 }),
366
+ " ",
367
+ t("weightPainter.normalize", "Normalize")
368
+ ]
369
+ }
370
+ )
371
+ ] }),
372
+ /* @__PURE__ */ jsxs("label", { style: { display: "flex", alignItems: "center", gap: 6 }, children: [
373
+ /* @__PURE__ */ jsx(
374
+ "input",
375
+ {
376
+ type: "checkbox",
377
+ checked: showHeatMap,
378
+ onChange: (e) => setShowHeatMap(e.target.checked)
379
+ }
380
+ ),
381
+ t("weightPainter.showWeightHeatMap", "Show weight heat map")
382
+ ] })
383
+ ]
384
+ }
385
+ );
386
+ };
387
+ export {
388
+ NiceWeightPainter,
389
+ useWeightPainter
260
390
  };
261
-
262
- export { NiceWeightPainter, useWeightPainter };