SkeletonHelper.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { LineSegments } from '../objects/LineSegments.js';
  2. import { Matrix4 } from '../math/Matrix4.js';
  3. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  4. import { Color } from '../math/Color.js';
  5. import { Vector3 } from '../math/Vector3.js';
  6. import { BufferGeometry } from '../core/BufferGeometry.js';
  7. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  8. const _vector = /*@__PURE__*/ new Vector3();
  9. const _boneMatrix = /*@__PURE__*/ new Matrix4();
  10. const _matrixWorldInv = /*@__PURE__*/ new Matrix4();
  11. class SkeletonHelper extends LineSegments {
  12. constructor( object ) {
  13. const bones = getBoneList( object );
  14. const geometry = new BufferGeometry();
  15. const vertices = [];
  16. const colors = [];
  17. const color1 = new Color( 0, 0, 1 );
  18. const color2 = new Color( 0, 1, 0 );
  19. for ( let i = 0; i < bones.length; i ++ ) {
  20. const bone = bones[ i ];
  21. if ( bone.parent && bone.parent.isBone ) {
  22. vertices.push( 0, 0, 0 );
  23. vertices.push( 0, 0, 0 );
  24. colors.push( color1.r, color1.g, color1.b );
  25. colors.push( color2.r, color2.g, color2.b );
  26. }
  27. }
  28. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  29. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  30. const material = new LineBasicMaterial( { vertexColors: true, depthTest: false, depthWrite: false, toneMapped: false, transparent: true } );
  31. super( geometry, material );
  32. this.type = 'SkeletonHelper';
  33. this.isSkeletonHelper = true;
  34. this.root = object;
  35. this.bones = bones;
  36. this.matrix = object.matrixWorld;
  37. this.matrixAutoUpdate = false;
  38. }
  39. updateMatrixWorld( force ) {
  40. const bones = this.bones;
  41. const geometry = this.geometry;
  42. const position = geometry.getAttribute( 'position' );
  43. _matrixWorldInv.getInverse( this.root.matrixWorld );
  44. for ( let i = 0, j = 0; i < bones.length; i ++ ) {
  45. const bone = bones[ i ];
  46. if ( bone.parent && bone.parent.isBone ) {
  47. _boneMatrix.multiplyMatrices( _matrixWorldInv, bone.matrixWorld );
  48. _vector.setFromMatrixPosition( _boneMatrix );
  49. position.setXYZ( j, _vector.x, _vector.y, _vector.z );
  50. _boneMatrix.multiplyMatrices( _matrixWorldInv, bone.parent.matrixWorld );
  51. _vector.setFromMatrixPosition( _boneMatrix );
  52. position.setXYZ( j + 1, _vector.x, _vector.y, _vector.z );
  53. j += 2;
  54. }
  55. }
  56. geometry.getAttribute( 'position' ).needsUpdate = true;
  57. super.updateMatrixWorld( force );
  58. }
  59. }
  60. function getBoneList( object ) {
  61. const boneList = [];
  62. if ( object && object.isBone ) {
  63. boneList.push( object );
  64. }
  65. for ( let i = 0; i < object.children.length; i ++ ) {
  66. boneList.push.apply( boneList, getBoneList( object.children[ i ] ) );
  67. }
  68. return boneList;
  69. }
  70. export { SkeletonHelper };