SkeletonHelper.js 3.0 KB

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