SkeletonHelper.js 2.9 KB

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