RollerCoaster.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var RollerCoasterGeometry = function ( curve, size ) {
  5. THREE.BufferGeometry.call( this );
  6. var vertices = [];
  7. var normals = [];
  8. var colors = [];
  9. var color1 = [ 1, 1, 1 ];
  10. var color2 = [ 1, 1, 0 ];
  11. var up = new THREE.Vector3( 0, 1, 0 );
  12. var forward = new THREE.Vector3();
  13. var right = new THREE.Vector3();
  14. var quaternion = new THREE.Quaternion();
  15. var prevQuaternion = new THREE.Quaternion();
  16. prevQuaternion.setFromAxisAngle( up , Math.PI / 2 );
  17. var point = new THREE.Vector3();
  18. var prevPoint = new THREE.Vector3();
  19. prevPoint.copy( curve.getPointAt( 0 ) );
  20. // shapes
  21. var step = [
  22. new THREE.Vector3( -2.25, 0, 0 ),
  23. new THREE.Vector3( 0, -0.5, 0 ),
  24. new THREE.Vector3( 0, -1.75, 0 ),
  25. new THREE.Vector3( 0, -0.5, 0 ),
  26. new THREE.Vector3( 2.25, 0, 0 ),
  27. new THREE.Vector3( 0, -1.75, 0 )
  28. ];
  29. var PI2 = Math.PI * 2;
  30. var sides = 5;
  31. var tube1 = [];
  32. for ( var i = 0; i < sides; i ++ ) {
  33. var angle = ( i / sides ) * PI2;
  34. tube1.push( new THREE.Vector3( Math.sin( angle ) * 0.6, Math.cos( angle ) * 0.6, 0 ) );
  35. }
  36. var sides = 6;
  37. var tube2 = [];
  38. for ( var i = 0; i < sides; i ++ ) {
  39. var angle = ( i / sides ) * PI2;
  40. tube2.push( new THREE.Vector3( Math.sin( angle ) * 0.25, Math.cos( angle ) * 0.25, 0 ) );
  41. }
  42. var vector = new THREE.Vector3();
  43. var normal = new THREE.Vector3();
  44. var drawShape = function ( shape, color ) {
  45. normal.set( 0, 0, -1 ).applyQuaternion( quaternion );
  46. for ( var j = 0; j < shape.length; j ++ ) {
  47. vector.copy( shape[ j ] );
  48. vector.applyQuaternion( quaternion );
  49. vector.add( point );
  50. vertices.push( vector.x, vector.y, vector.z );
  51. normals.push( normal.x, normal.y, normal.z );
  52. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  53. }
  54. normal.set( 0, 0, 1 ).applyQuaternion( quaternion );
  55. for ( var j = shape.length - 1; j >= 0; j -- ) {
  56. vector.copy( shape[ j ] );
  57. vector.applyQuaternion( quaternion );
  58. vector.add( point );
  59. vertices.push( vector.x, vector.y, vector.z );
  60. normals.push( normal.x, normal.y, normal.z );
  61. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  62. }
  63. };
  64. var vector1 = new THREE.Vector3();
  65. var vector2 = new THREE.Vector3();
  66. var vector3 = new THREE.Vector3();
  67. var vector4 = new THREE.Vector3();
  68. var normal1 = new THREE.Vector3();
  69. var normal2 = new THREE.Vector3();
  70. var normal3 = new THREE.Vector3();
  71. var normal4 = new THREE.Vector3();
  72. var extrudeShape = function ( shape, offset, color ) {
  73. for ( var j = 0, jl = shape.length; j < jl; j ++ ) {
  74. var point1 = shape[ j ];
  75. var point2 = shape[ ( j + 1 ) % jl ];
  76. vector1.copy( point1 ).add( offset );
  77. vector1.applyQuaternion( quaternion );
  78. vector1.add( point );
  79. vector2.copy( point2 ).add( offset );
  80. vector2.applyQuaternion( quaternion );
  81. vector2.add( point );
  82. vector3.copy( point2 ).add( offset );
  83. vector3.applyQuaternion( prevQuaternion );
  84. vector3.add( prevPoint );
  85. vector4.copy( point1 ).add( offset );
  86. vector4.applyQuaternion( prevQuaternion );
  87. vector4.add( prevPoint );
  88. vertices.push( vector1.x, vector1.y, vector1.z );
  89. vertices.push( vector2.x, vector2.y, vector2.z );
  90. vertices.push( vector4.x, vector4.y, vector4.z );
  91. vertices.push( vector2.x, vector2.y, vector2.z );
  92. vertices.push( vector3.x, vector3.y, vector3.z );
  93. vertices.push( vector4.x, vector4.y, vector4.z );
  94. //
  95. normal1.copy( point1 );
  96. normal1.applyQuaternion( quaternion );
  97. normal1.normalize();
  98. normal2.copy( point2 );
  99. normal2.applyQuaternion( quaternion );
  100. normal2.normalize();
  101. normal3.copy( point2 );
  102. normal3.applyQuaternion( prevQuaternion );
  103. normal3.normalize();
  104. normal4.copy( point1 );
  105. normal4.applyQuaternion( prevQuaternion );
  106. normal4.normalize();
  107. normals.push( normal1.x, normal1.y, normal1.z );
  108. normals.push( normal2.x, normal2.y, normal2.z );
  109. normals.push( normal4.x, normal4.y, normal4.z );
  110. normals.push( normal2.x, normal2.y, normal2.z );
  111. normals.push( normal3.x, normal3.y, normal3.z );
  112. normals.push( normal4.x, normal4.y, normal4.z );
  113. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  114. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  115. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  116. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  117. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  118. colors.push( color[ 0 ], color[ 1 ], color[ 2 ] );
  119. }
  120. };
  121. var offset = new THREE.Vector3();
  122. for ( var i = 1; i <= size; i ++ ) {
  123. point.copy( curve.getPointAt( i / size ) );
  124. up.set( 0, 1, 0 );
  125. forward.subVectors( point, prevPoint ).normalize();
  126. right.crossVectors( up, forward ).normalize();
  127. up.crossVectors( forward, right );
  128. var angle = Math.atan2( forward.x, forward.z );
  129. quaternion.setFromAxisAngle( up, angle );
  130. if ( i % 2 === 0 ) {
  131. drawShape( step, color2 );
  132. }
  133. extrudeShape( tube1, offset.set( 0, -1.25, 0 ), color2 );
  134. extrudeShape( tube2, offset.set( 2, 0, 0 ), color1 );
  135. extrudeShape( tube2, offset.set( -2, 0, 0 ), color1 );
  136. prevPoint.copy( point );
  137. prevQuaternion.copy( quaternion );
  138. }
  139. // console.log( vertices.length );
  140. this.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
  141. this.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
  142. this.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( colors ), 3 ) );
  143. };
  144. RollerCoasterGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
  145. var RollerCoasterLiftersGeometry = function ( curve, size ) {
  146. THREE.BufferGeometry.call( this );
  147. var vertices = [];
  148. var normals = [];
  149. var quaternion = new THREE.Quaternion();
  150. var up = new THREE.Vector3( 0, 1, 0 );
  151. var point = new THREE.Vector3();
  152. var tangent = new THREE.Vector3();
  153. // shapes
  154. var tube1 = [
  155. new THREE.Vector3( 0, 0.5, -0.5 ),
  156. new THREE.Vector3( 0, 0.5, 0.5 ),
  157. new THREE.Vector3( 0, -0.5, 0 )
  158. ];
  159. var tube2 = [
  160. new THREE.Vector3( -0.5, 0, 0.5 ),
  161. new THREE.Vector3( -0.5, 0, -0.5 ),
  162. new THREE.Vector3( 0.5, 0, 0 )
  163. ];
  164. var tube3 = [
  165. new THREE.Vector3( 0.5, 0, -0.5 ),
  166. new THREE.Vector3( 0.5, 0, 0.5 ),
  167. new THREE.Vector3( -0.5, 0, 0 )
  168. ];
  169. var vector1 = new THREE.Vector3();
  170. var vector2 = new THREE.Vector3();
  171. var vector3 = new THREE.Vector3();
  172. var vector4 = new THREE.Vector3();
  173. var normal1 = new THREE.Vector3();
  174. var normal2 = new THREE.Vector3();
  175. var normal3 = new THREE.Vector3();
  176. var normal4 = new THREE.Vector3();
  177. var extrudeShape = function ( shape, fromPoint, toPoint ) {
  178. for ( var j = 0, jl = shape.length; j < jl; j ++ ) {
  179. var point1 = shape[ j ];
  180. var point2 = shape[ ( j + 1 ) % jl ];
  181. vector1.copy( point1 );
  182. vector1.applyQuaternion( quaternion );
  183. vector1.add( fromPoint );
  184. vector2.copy( point2 );
  185. vector2.applyQuaternion( quaternion );
  186. vector2.add( fromPoint );
  187. vector3.copy( point2 );
  188. vector3.applyQuaternion( quaternion );
  189. vector3.add( toPoint );
  190. vector4.copy( point1 );
  191. vector4.applyQuaternion( quaternion );
  192. vector4.add( toPoint );
  193. vertices.push( vector1.x, vector1.y, vector1.z );
  194. vertices.push( vector2.x, vector2.y, vector2.z );
  195. vertices.push( vector4.x, vector4.y, vector4.z );
  196. vertices.push( vector2.x, vector2.y, vector2.z );
  197. vertices.push( vector3.x, vector3.y, vector3.z );
  198. vertices.push( vector4.x, vector4.y, vector4.z );
  199. //
  200. normal1.copy( point1 );
  201. normal1.applyQuaternion( quaternion );
  202. normal1.normalize();
  203. normal2.copy( point2 );
  204. normal2.applyQuaternion( quaternion );
  205. normal2.normalize();
  206. normal3.copy( point2 );
  207. normal3.applyQuaternion( quaternion );
  208. normal3.normalize();
  209. normal4.copy( point1 );
  210. normal4.applyQuaternion( quaternion );
  211. normal4.normalize();
  212. normals.push( normal1.x, normal1.y, normal1.z );
  213. normals.push( normal2.x, normal2.y, normal2.z );
  214. normals.push( normal4.x, normal4.y, normal4.z );
  215. normals.push( normal2.x, normal2.y, normal2.z );
  216. normals.push( normal3.x, normal3.y, normal3.z );
  217. normals.push( normal4.x, normal4.y, normal4.z );
  218. }
  219. };
  220. var fromPoint = new THREE.Vector3();
  221. var toPoint = new THREE.Vector3();
  222. for ( var i = 1; i <= size; i ++ ) {
  223. point.copy( curve.getPointAt( i / size ) );
  224. tangent.copy( curve.getTangentAt( i / size ) );
  225. var angle = Math.atan2( tangent.x, tangent.z );
  226. quaternion.setFromAxisAngle( up, angle );
  227. //
  228. if ( point.y > 100 ) {
  229. fromPoint.set( -7.5, -3.5, 0 );
  230. fromPoint.applyQuaternion( quaternion );
  231. fromPoint.add( point );
  232. toPoint.set( 7.5, -3.5, 0 );
  233. toPoint.applyQuaternion( quaternion );
  234. toPoint.add( point );
  235. extrudeShape( tube1, fromPoint, toPoint );
  236. fromPoint.set( -7, -3, 0 );
  237. fromPoint.applyQuaternion( quaternion );
  238. fromPoint.add( point );
  239. toPoint.set( -7, -point.y, 0 );
  240. toPoint.applyQuaternion( quaternion );
  241. toPoint.add( point );
  242. extrudeShape( tube2, fromPoint, toPoint );
  243. fromPoint.set( 7, -3, 0 );
  244. fromPoint.applyQuaternion( quaternion );
  245. fromPoint.add( point );
  246. toPoint.set( 7, -point.y, 0 );
  247. toPoint.applyQuaternion( quaternion );
  248. toPoint.add( point );
  249. extrudeShape( tube3, fromPoint, toPoint );
  250. } else {
  251. fromPoint.set( 0, -2, 0 );
  252. fromPoint.applyQuaternion( quaternion );
  253. fromPoint.add( point );
  254. toPoint.set( 0, -point.y, 0 );
  255. toPoint.applyQuaternion( quaternion );
  256. toPoint.add( point );
  257. extrudeShape( tube3, fromPoint, toPoint );
  258. }
  259. }
  260. this.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
  261. this.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
  262. };
  263. RollerCoasterLiftersGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
  264. var RollerCoasterShadowGeometry = function ( curve, size ) {
  265. THREE.BufferGeometry.call( this );
  266. var vertices = [];
  267. var up = new THREE.Vector3( 0, 1, 0 );
  268. var forward = new THREE.Vector3();
  269. var quaternion = new THREE.Quaternion();
  270. var prevQuaternion = new THREE.Quaternion();
  271. prevQuaternion.setFromAxisAngle( up , Math.PI / 2 );
  272. var point = new THREE.Vector3();
  273. var prevPoint = new THREE.Vector3();
  274. prevPoint.copy( curve.getPointAt( 0 ) );
  275. prevPoint.y = 0;
  276. var vector1 = new THREE.Vector3();
  277. var vector2 = new THREE.Vector3();
  278. var vector3 = new THREE.Vector3();
  279. var vector4 = new THREE.Vector3();
  280. for ( var i = 1; i <= size; i ++ ) {
  281. point.copy( curve.getPointAt( i / size ) );
  282. point.y = 0;
  283. forward.subVectors( point, prevPoint );
  284. var angle = Math.atan2( forward.x, forward.z );
  285. quaternion.setFromAxisAngle( up, angle );
  286. vector1.set( -3, 0, 0 );
  287. vector1.applyQuaternion( quaternion );
  288. vector1.add( point );
  289. vector2.set( 3, 0, 0 );
  290. vector2.applyQuaternion( quaternion );
  291. vector2.add( point );
  292. vector3.set( 3, 0, 0 );
  293. vector3.applyQuaternion( prevQuaternion );
  294. vector3.add( prevPoint );
  295. vector4.set( -3, 0, 0 );
  296. vector4.applyQuaternion( prevQuaternion );
  297. vector4.add( prevPoint );
  298. vertices.push( vector1.x, vector1.y, vector1.z );
  299. vertices.push( vector2.x, vector2.y, vector2.z );
  300. vertices.push( vector4.x, vector4.y, vector4.z );
  301. vertices.push( vector2.x, vector2.y, vector2.z );
  302. vertices.push( vector3.x, vector3.y, vector3.z );
  303. vertices.push( vector4.x, vector4.y, vector4.z );
  304. prevPoint.copy( point );
  305. prevQuaternion.copy( quaternion );
  306. }
  307. this.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
  308. };
  309. RollerCoasterShadowGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
  310. var SkyGeometry = function () {
  311. THREE.BufferGeometry.call( this );
  312. var vertices = [];
  313. for ( var i = 0; i < 100; i ++ ) {
  314. var x = Math.random() * 8000 - 4000;
  315. var y = Math.random() * 500 + 500;
  316. var z = Math.random() * 8000 - 4000;
  317. var size = Math.random() * 400 + 200;
  318. vertices.push( x - size, y, z - size );
  319. vertices.push( x + size, y, z - size );
  320. vertices.push( x - size, y, z + size );
  321. vertices.push( x + size, y, z - size );
  322. vertices.push( x + size, y, z + size );
  323. vertices.push( x - size, y, z + size );
  324. }
  325. this.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
  326. };
  327. SkyGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
  328. var TreesGeometry = function ( landscape ) {
  329. THREE.BufferGeometry.call( this );
  330. var vertices = [];
  331. var colors = [];
  332. var raycaster = new THREE.Raycaster();
  333. raycaster.ray.direction.set( 0, -1, 0 );
  334. for ( var i = 0; i < 2000; i ++ ) {
  335. var x = Math.random() * 5000 - 2500;
  336. var z = Math.random() * 5000 - 2500;
  337. raycaster.ray.origin.set( x, 500, z );
  338. var intersections = raycaster.intersectObject( landscape );
  339. if ( intersections.length === 0 ) continue;
  340. var y = intersections[ 0 ].point.y;
  341. var height = Math.random() * 50 + 5;
  342. var angle = Math.random() * Math.PI * 2;
  343. vertices.push( x + Math.sin( angle ) * 10, y, z + Math.cos( angle ) * 10 );
  344. vertices.push( x, y + height, z );
  345. vertices.push( x + Math.sin( angle + Math.PI ) * 10, y, z + Math.cos( angle + Math.PI ) * 10 );
  346. angle += Math.PI / 2;
  347. vertices.push( x + Math.sin( angle ) * 10, y, z + Math.cos( angle ) * 10 );
  348. vertices.push( x, y + height, z );
  349. vertices.push( x + Math.sin( angle + Math.PI ) * 10, y, z + Math.cos( angle + Math.PI ) * 10 );
  350. var random = Math.random() * 0.1;
  351. for ( var j = 0; j < 6; j ++ ) {
  352. colors.push( 0.2 + random, 0.4 + random, 0 );
  353. }
  354. }
  355. this.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
  356. this.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( colors ), 3 ) );
  357. };
  358. TreesGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );