PathControls.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.PathControls = function ( object, domElement ) {
  5. this.object = object;
  6. this.domElement = ( domElement !== undefined ) ? domElement : document;
  7. this.id = "PathControls" + THREE.PathControlsIdCounter ++;
  8. // API
  9. this.duration = 10 * 1000; // milliseconds
  10. this.waypoints = [];
  11. this.useConstantSpeed = true;
  12. this.resamplingCoef = 50;
  13. this.debugPath = new THREE.Object3D();
  14. this.debugDummy = new THREE.Object3D();
  15. this.animationParent = new THREE.Object3D();
  16. this.lookSpeed = 0.005;
  17. this.lookVertical = true;
  18. this.lookHorizontal = true;
  19. this.verticalAngleMap = { srcRange: [ 0, 2 * Math.PI ], dstRange: [ 0, 2 * Math.PI ] };
  20. this.horizontalAngleMap = { srcRange: [ 0, 2 * Math.PI ], dstRange: [ 0, 2 * Math.PI ] };
  21. // internals
  22. this.target = new THREE.Object3D();
  23. this.mouseX = 0;
  24. this.mouseY = 0;
  25. this.lat = 0;
  26. this.lon = 0;
  27. this.phi = 0;
  28. this.theta = 0;
  29. if ( this.domElement === document ) {
  30. this.viewHalfX = window.innerWidth / 2;
  31. this.viewHalfY = window.innerHeight / 2;
  32. } else {
  33. this.viewHalfX = this.domElement.offsetWidth / 2;
  34. this.viewHalfY = this.domElement.offsetHeight / 2;
  35. this.domElement.setAttribute( 'tabindex', -1 );
  36. }
  37. var PI2 = Math.PI * 2,
  38. PI180 = Math.PI / 180;
  39. // methods
  40. this.update = function ( delta ) {
  41. var srcRange, dstRange;
  42. if( this.lookHorizontal ) this.lon += this.mouseX * this.lookSpeed * delta;
  43. if( this.lookVertical ) this.lat -= this.mouseY * this.lookSpeed * delta;
  44. this.lon = Math.max( 0, Math.min( 360, this.lon ) );
  45. this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
  46. this.phi = ( 90 - this.lat ) * PI180;
  47. this.theta = this.lon * PI180;
  48. this.phi = normalize_angle_rad( this.phi );
  49. // constrain vertical look angle
  50. srcRange = this.verticalAngleMap.srcRange;
  51. dstRange = this.verticalAngleMap.dstRange;
  52. var tmpPhi = THREE.Math.mapLinear( this.phi, srcRange[ 0 ], srcRange[ 1 ], dstRange[ 0 ], dstRange[ 1 ] );
  53. var tmpPhiFullRange = dstRange[ 1 ] - dstRange[ 0 ];
  54. var tmpPhiNormalized = ( tmpPhi - dstRange[ 0 ] ) / tmpPhiFullRange;
  55. this.phi = QuadraticEaseInOut( tmpPhiNormalized ) * tmpPhiFullRange + dstRange[ 0 ];
  56. // constrain horizontal look angle
  57. srcRange = this.horizontalAngleMap.srcRange;
  58. dstRange = this.horizontalAngleMap.dstRange;
  59. var tmpTheta = THREE.Math.mapLinear( this.theta, srcRange[ 0 ], srcRange[ 1 ], dstRange[ 0 ], dstRange[ 1 ] );
  60. var tmpThetaFullRange = dstRange[ 1 ] - dstRange[ 0 ];
  61. var tmpThetaNormalized = ( tmpTheta - dstRange[ 0 ] ) / tmpThetaFullRange;
  62. this.theta = QuadraticEaseInOut( tmpThetaNormalized ) * tmpThetaFullRange + dstRange[ 0 ];
  63. var targetPosition = this.target.position,
  64. position = this.object.position;
  65. targetPosition.x = 100 * Math.sin( this.phi ) * Math.cos( this.theta );
  66. targetPosition.y = 100 * Math.cos( this.phi );
  67. targetPosition.z = 100 * Math.sin( this.phi ) * Math.sin( this.theta );
  68. this.object.lookAt( this.target.position );
  69. };
  70. this.onMouseMove = function ( event ) {
  71. if ( this.domElement === document ) {
  72. this.mouseX = event.pageX - this.viewHalfX;
  73. this.mouseY = event.pageY - this.viewHalfY;
  74. } else {
  75. this.mouseX = event.pageX - this.domElement.offsetLeft - this.viewHalfX;
  76. this.mouseY = event.pageY - this.domElement.offsetTop - this.viewHalfY;
  77. }
  78. };
  79. // utils
  80. function normalize_angle_rad( a ) {
  81. var b = a % PI2;
  82. return b >= 0 ? b : b + PI2;
  83. };
  84. function distance( a, b ) {
  85. var dx = a[ 0 ] - b[ 0 ],
  86. dy = a[ 1 ] - b[ 1 ],
  87. dz = a[ 2 ] - b[ 2 ];
  88. return Math.sqrt( dx * dx + dy * dy + dz * dz );
  89. };
  90. function QuadraticEaseInOut ( k ) {
  91. if ( ( k *= 2 ) < 1 ) return 0.5 * k * k;
  92. return - 0.5 * ( --k * ( k - 2 ) - 1 );
  93. };
  94. function bind( scope, fn ) {
  95. return function () {
  96. fn.apply( scope, arguments );
  97. };
  98. };
  99. function initAnimationPath( parent, spline, name, duration ) {
  100. var animationData = {
  101. name: name,
  102. fps: 0.6,
  103. length: duration,
  104. hierarchy: []
  105. };
  106. var i,
  107. parentAnimation, childAnimation,
  108. path = spline.getControlPointsArray(),
  109. sl = spline.getLength(),
  110. pl = path.length,
  111. t = 0,
  112. first = 0,
  113. last = pl - 1;
  114. parentAnimation = { parent: -1, keys: [] };
  115. parentAnimation.keys[ first ] = { time: 0, pos: path[ first ], rot: [ 0, 0, 0, 1 ], scl: [ 1, 1, 1 ] };
  116. parentAnimation.keys[ last ] = { time: duration, pos: path[ last ], rot: [ 0, 0, 0, 1 ], scl: [ 1, 1, 1 ] };
  117. for ( i = 1; i < pl - 1; i++ ) {
  118. // real distance (approximation via linear segments)
  119. t = duration * sl.chunks[ i ] / sl.total;
  120. // equal distance
  121. //t = duration * ( i / pl );
  122. // linear distance
  123. //t += duration * distance( path[ i ], path[ i - 1 ] ) / sl.total;
  124. parentAnimation.keys[ i ] = { time: t, pos: path[ i ] };
  125. }
  126. animationData.hierarchy[ 0 ] = parentAnimation;
  127. THREE.AnimationHandler.add( animationData );
  128. return new THREE.Animation( parent, name, THREE.AnimationHandler.CATMULLROM_FORWARD, false );
  129. };
  130. function createSplineGeometry( spline, n_sub ) {
  131. var i, index, position,
  132. geometry = new THREE.Geometry();
  133. for ( i = 0; i < spline.points.length * n_sub; i ++ ) {
  134. index = i / ( spline.points.length * n_sub );
  135. position = spline.getPoint( index );
  136. geometry.vertices[ i ] = new THREE.Vertex( new THREE.Vector3( position.x, position.y, position.z ) );
  137. }
  138. return geometry;
  139. };
  140. function createPath( parent, spline ) {
  141. var lineGeo = createSplineGeometry( spline, 10 ),
  142. particleGeo = createSplineGeometry( spline, 10 ),
  143. lineMat = new THREE.LineBasicMaterial( { color: 0xff0000, linewidth: 3 } ),
  144. lineObj = new THREE.Line( lineGeo, lineMat ),
  145. particleObj = new THREE.ParticleSystem( particleGeo, new THREE.ParticleBasicMaterial( { color: 0xffaa00, size: 3 } ) );
  146. lineObj.scale.set( 1, 1, 1 );
  147. parent.add( lineObj );
  148. particleObj.scale.set( 1, 1, 1 );
  149. parent.add( particleObj );
  150. var waypoint,
  151. geo = new THREE.SphereGeometry( 1, 16, 8 ),
  152. mat = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
  153. for ( var i = 0; i < spline.points.length; i ++ ) {
  154. waypoint = new THREE.Mesh( geo, mat );
  155. waypoint.position.copy( spline.points[ i ] );
  156. parent.add( waypoint );
  157. }
  158. };
  159. this.init = function ( ) {
  160. // constructor
  161. this.spline = new THREE.Spline();
  162. this.spline.initFromArray( this.waypoints );
  163. if ( this.useConstantSpeed ) {
  164. this.spline.reparametrizeByArcLength( this.resamplingCoef );
  165. }
  166. if ( this.createDebugDummy ) {
  167. var dummyParentMaterial = new THREE.MeshLambertMaterial( { color: 0x0077ff } ),
  168. dummyChildMaterial = new THREE.MeshLambertMaterial( { color: 0x00ff00 } ),
  169. dummyParentGeo = new THREE.CubeGeometry( 10, 10, 20 ),
  170. dummyChildGeo = new THREE.CubeGeometry( 2, 2, 10 );
  171. this.animationParent = new THREE.Mesh( dummyParentGeo, dummyParentMaterial );
  172. var dummyChild = new THREE.Mesh( dummyChildGeo, dummyChildMaterial );
  173. dummyChild.position.set( 0, 10, 0 );
  174. this.animation = initAnimationPath( this.animationParent, this.spline, this.id, this.duration );
  175. this.animationParent.add( this.object );
  176. this.animationParent.add( this.target );
  177. this.animationParent.add( dummyChild );
  178. } else {
  179. this.animation = initAnimationPath( this.animationParent, this.spline, this.id, this.duration );
  180. this.animationParent.add( this.target );
  181. this.animationParent.add( this.object );
  182. }
  183. if ( this.createDebugPath ) {
  184. createPath( this.debugPath, this.spline );
  185. }
  186. this.domElement.addEventListener( 'mousemove', bind( this, this.onMouseMove ), false );
  187. };
  188. };
  189. THREE.PathControlsIdCounter = 0;