RollerCoaster.js 13 KB

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