RollerCoaster.js 13 KB

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