Skip to content

Commit b7c5cc9

Browse files
authored
SkeletonHelper: Add setColors() (#31484)
1 parent 51a119e commit b7c5cc9

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

docs/api/en/helpers/SkeletonHelper.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ <h3>[method:undefined dispose]()</h3>
5858
Frees the GPU-related resources allocated by this instance. Call this
5959
method whenever this instance is no longer used in your app.
6060
</p>
61+
62+
<h3>[method:this setColors]( [param:Color color1], [param:Color color2] )</h3>
63+
<p>Defines the colors of the helper.</p>
64+
6165
<h2>Source</h2>
6266

6367
<p>

examples/webgl_animation_walk.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
//
230230

231231
skeleton = new THREE.SkeletonHelper( model );
232+
skeleton.setColors( new THREE.Color( 0xe000ff ), new THREE.Color( 0x00e0ff ) );
232233
skeleton.visible = false;
233234
scene.add( skeleton );
234235

src/helpers/CameraHelper.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class CameraHelper extends LineSegments {
159159
* @param {Color} up - The up line color.
160160
* @param {Color} target - The target line color.
161161
* @param {Color} cross - The cross line color.
162+
* @return {CameraHelper} A reference to this helper.
162163
*/
163164
setColors( frustum, cone, up, target, cross ) {
164165

@@ -215,6 +216,8 @@ class CameraHelper extends LineSegments {
215216

216217
colorAttribute.needsUpdate = true;
217218

219+
return this;
220+
218221
}
219222

220223
/**

src/helpers/SkeletonHelper.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const _matrixWorldInv = /*@__PURE__*/ new Matrix4();
2323
class SkeletonHelper extends LineSegments {
2424

2525
/**
26-
* Constructs a new hemisphere light helper.
26+
* Constructs a new skeleton helper.
2727
*
2828
* @param {Object3D} object - Usually an instance of {@link SkinnedMesh}. However, any 3D object
2929
* can be used if it represents a hierarchy of bones (see {@link Bone}).
@@ -37,9 +37,6 @@ class SkeletonHelper extends LineSegments {
3737
const vertices = [];
3838
const colors = [];
3939

40-
const color1 = new Color( 0, 0, 1 );
41-
const color2 = new Color( 0, 1, 0 );
42-
4340
for ( let i = 0; i < bones.length; i ++ ) {
4441

4542
const bone = bones[ i ];
@@ -48,8 +45,8 @@ class SkeletonHelper extends LineSegments {
4845

4946
vertices.push( 0, 0, 0 );
5047
vertices.push( 0, 0, 0 );
51-
colors.push( color1.r, color1.g, color1.b );
52-
colors.push( color2.r, color2.g, color2.b );
48+
colors.push( 0, 0, 0 );
49+
colors.push( 0, 0, 0 );
5350

5451
}
5552

@@ -90,6 +87,13 @@ class SkeletonHelper extends LineSegments {
9087
this.matrix = object.matrixWorld;
9188
this.matrixAutoUpdate = false;
9289

90+
// colors
91+
92+
const color1 = new Color( 0x0000ff );
93+
const color2 = new Color( 0x00ff00 );
94+
95+
this.setColors( color1, color2 );
96+
9397
}
9498

9599
updateMatrixWorld( force ) {
@@ -127,6 +131,31 @@ class SkeletonHelper extends LineSegments {
127131

128132
}
129133

134+
/**
135+
* Defines the colors of the helper.
136+
*
137+
* @param {Color} color1 - The first line color for each bone.
138+
* @param {Color} color2 - The second line color for each bone.
139+
* @return {SkeletonHelper} A reference to this helper.
140+
*/
141+
setColors( color1, color2 ) {
142+
143+
const geometry = this.geometry;
144+
const colorAttribute = geometry.getAttribute( 'color' );
145+
146+
for ( let i = 0; i < colorAttribute.count; i += 2 ) {
147+
148+
colorAttribute.setXYZ( i, color1.r, color1.g, color1.b );
149+
colorAttribute.setXYZ( i + 1, color2.r, color2.g, color2.b );
150+
151+
}
152+
153+
colorAttribute.needsUpdate = true;
154+
155+
return this;
156+
157+
}
158+
130159
/**
131160
* Frees the GPU-related resources allocated by this instance. Call this
132161
* method whenever this instance is no longer used in your app.

0 commit comments

Comments
 (0)