PathControls.js 7.6 KB

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