BoneHelper.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @author Sean Griffin / http://twitter.com/sgrif
  3. * @author Michael Guerrero / http://realitymeltdown.com
  4. */
  5. THREE.BoneAxisHelper = function ( bone, baseBoxSize, scaleRatio ) {
  6. THREE.Object3D.call( this );
  7. this.scaleRatio = ( scaleRatio !== undefined ) ? scaleRatio : 1;
  8. this.bone = bone;
  9. var baseBoxSize = ( baseBoxSize !== undefined ) ? baseBoxSize : 1;
  10. var boxSize = baseBoxSize * this.scaleRatio;
  11. var boxGeometry = new THREE.BoxGeometry(boxSize, boxSize, boxSize);
  12. var boxMaterial = new THREE.MeshBasicMaterial();
  13. this.cube = new THREE.Mesh(boxGeometry, boxMaterial);
  14. this.add(this.cube);
  15. this.axes = new THREE.AxisHelper( baseBoxSize * 3 );
  16. this.add( this.axes );
  17. if ( this.bone.parent instanceof THREE.Bone ) {
  18. var lineMaterial = new THREE.LineBasicMaterial();
  19. var lineGeometry = new THREE.Geometry();
  20. lineGeometry.vertices.push( new THREE.Vector3() );
  21. lineGeometry.vertices.push( new THREE.Vector3() );
  22. this.line = new THREE.Line( lineGeometry, lineMaterial );
  23. this.add(this.line);
  24. }
  25. this.update();
  26. };
  27. THREE.BoneAxisHelper.prototype = Object.create( THREE.Object3D.prototype );
  28. THREE.BoneAxisHelper.prototype.update = function () {
  29. if ( this.visible && this.bone.parent instanceof THREE.Bone ) {
  30. this.bone.skinMatrix.decompose( this.cube.position, this.cube.quaternion, this.cube.scale );
  31. this.cube.position.multiplyScalar( this.scaleRatio );
  32. this.axes.quaternion = this.cube.quaternion;
  33. this.axes.position = this.cube.position;
  34. this.axes.scale = this.cube.scale;
  35. this.line.geometry.vertices[0].setFromMatrixPosition( this.bone.skinMatrix );
  36. this.line.geometry.vertices[0].multiplyScalar( this.scaleRatio );
  37. this.line.geometry.vertices[1].setFromMatrixPosition( this.bone.parent.skinMatrix );
  38. this.line.geometry.vertices[1].multiplyScalar( this.scaleRatio );
  39. this.line.geometry.verticesNeedUpdate = true;
  40. }
  41. };