scenegraph-tank.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Scenegraph - Tank</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. #info {
  19. position: absolute;
  20. left: 1em;
  21. top: 1em;
  22. background: rgba(0,0,0,.8);
  23. padding: .5em;
  24. color: white;
  25. font-family: monospace;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <canvas id="c"></canvas>
  31. <div id="info"></div>
  32. </body>
  33. <script type="importmap">
  34. {
  35. "imports": {
  36. "three": "../../build/three.module.js"
  37. }
  38. }
  39. </script>
  40. <script type="module">
  41. import * as THREE from 'three';
  42. function main() {
  43. const canvas = document.querySelector( '#c' );
  44. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas: canvas } );
  45. renderer.setClearColor( 0xAAAAAA );
  46. renderer.shadowMap.enabled = true;
  47. function makeCamera( fov = 40 ) {
  48. const aspect = 2; // the canvas default
  49. const zNear = 0.1;
  50. const zFar = 1000;
  51. return new THREE.PerspectiveCamera( fov, aspect, zNear, zFar );
  52. }
  53. const camera = makeCamera();
  54. camera.position.set( 8, 4, 10 ).multiplyScalar( 3 );
  55. camera.lookAt( 0, 0, 0 );
  56. const scene = new THREE.Scene();
  57. {
  58. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  59. light.position.set( 0, 20, 0 );
  60. scene.add( light );
  61. light.castShadow = true;
  62. light.shadow.mapSize.width = 2048;
  63. light.shadow.mapSize.height = 2048;
  64. const d = 50;
  65. light.shadow.camera.left = - d;
  66. light.shadow.camera.right = d;
  67. light.shadow.camera.top = d;
  68. light.shadow.camera.bottom = - d;
  69. light.shadow.camera.near = 1;
  70. light.shadow.camera.far = 50;
  71. light.shadow.bias = 0.001;
  72. }
  73. {
  74. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  75. light.position.set( 1, 2, 4 );
  76. scene.add( light );
  77. }
  78. const groundGeometry = new THREE.PlaneGeometry( 50, 50 );
  79. const groundMaterial = new THREE.MeshPhongMaterial( { color: 0xCC8866 } );
  80. const groundMesh = new THREE.Mesh( groundGeometry, groundMaterial );
  81. groundMesh.rotation.x = Math.PI * - .5;
  82. groundMesh.receiveShadow = true;
  83. scene.add( groundMesh );
  84. const carWidth = 4;
  85. const carHeight = 1;
  86. const carLength = 8;
  87. const tank = new THREE.Object3D();
  88. scene.add( tank );
  89. const bodyGeometry = new THREE.BoxGeometry( carWidth, carHeight, carLength );
  90. const bodyMaterial = new THREE.MeshPhongMaterial( { color: 0x6688AA } );
  91. const bodyMesh = new THREE.Mesh( bodyGeometry, bodyMaterial );
  92. bodyMesh.position.y = 1.4;
  93. bodyMesh.castShadow = true;
  94. tank.add( bodyMesh );
  95. const tankCameraFov = 75;
  96. const tankCamera = makeCamera( tankCameraFov );
  97. tankCamera.position.y = 3;
  98. tankCamera.position.z = - 6;
  99. tankCamera.rotation.y = Math.PI;
  100. bodyMesh.add( tankCamera );
  101. const wheelRadius = 1;
  102. const wheelThickness = .5;
  103. const wheelSegments = 6;
  104. const wheelGeometry = new THREE.CylinderGeometry(
  105. wheelRadius, // top radius
  106. wheelRadius, // bottom radius
  107. wheelThickness, // height of cylinder
  108. wheelSegments );
  109. const wheelMaterial = new THREE.MeshPhongMaterial( { color: 0x888888 } );
  110. const wheelPositions = [
  111. [ - carWidth / 2 - wheelThickness / 2, - carHeight / 2, carLength / 3 ],
  112. [ carWidth / 2 + wheelThickness / 2, - carHeight / 2, carLength / 3 ],
  113. [ - carWidth / 2 - wheelThickness / 2, - carHeight / 2, 0 ],
  114. [ carWidth / 2 + wheelThickness / 2, - carHeight / 2, 0 ],
  115. [ - carWidth / 2 - wheelThickness / 2, - carHeight / 2, - carLength / 3 ],
  116. [ carWidth / 2 + wheelThickness / 2, - carHeight / 2, - carLength / 3 ],
  117. ];
  118. const wheelMeshes = wheelPositions.map( ( position ) => {
  119. const mesh = new THREE.Mesh( wheelGeometry, wheelMaterial );
  120. mesh.position.set( ...position );
  121. mesh.rotation.z = Math.PI * .5;
  122. mesh.castShadow = true;
  123. bodyMesh.add( mesh );
  124. return mesh;
  125. } );
  126. const domeRadius = 2;
  127. const domeWidthSubdivisions = 12;
  128. const domeHeightSubdivisions = 12;
  129. const domePhiStart = 0;
  130. const domePhiEnd = Math.PI * 2;
  131. const domeThetaStart = 0;
  132. const domeThetaEnd = Math.PI * .5;
  133. const domeGeometry = new THREE.SphereGeometry(
  134. domeRadius, domeWidthSubdivisions, domeHeightSubdivisions,
  135. domePhiStart, domePhiEnd, domeThetaStart, domeThetaEnd );
  136. const domeMesh = new THREE.Mesh( domeGeometry, bodyMaterial );
  137. domeMesh.castShadow = true;
  138. bodyMesh.add( domeMesh );
  139. domeMesh.position.y = .5;
  140. const turretWidth = .1;
  141. const turretHeight = .1;
  142. const turretLength = carLength * .75 * .2;
  143. const turretGeometry = new THREE.BoxGeometry(
  144. turretWidth, turretHeight, turretLength );
  145. const turretMesh = new THREE.Mesh( turretGeometry, bodyMaterial );
  146. const turretPivot = new THREE.Object3D();
  147. turretMesh.castShadow = true;
  148. turretPivot.scale.set( 5, 5, 5 );
  149. turretPivot.position.y = .5;
  150. turretMesh.position.z = turretLength * .5;
  151. turretPivot.add( turretMesh );
  152. bodyMesh.add( turretPivot );
  153. const turretCamera = makeCamera();
  154. turretCamera.position.y = .75 * .2;
  155. turretMesh.add( turretCamera );
  156. const targetGeometry = new THREE.SphereGeometry( .5, 6, 3 );
  157. const targetMaterial = new THREE.MeshPhongMaterial( { color: 0x00FF00, flatShading: true } );
  158. const targetMesh = new THREE.Mesh( targetGeometry, targetMaterial );
  159. const targetOrbit = new THREE.Object3D();
  160. const targetElevation = new THREE.Object3D();
  161. const targetBob = new THREE.Object3D();
  162. targetMesh.castShadow = true;
  163. scene.add( targetOrbit );
  164. targetOrbit.add( targetElevation );
  165. targetElevation.position.z = carLength * 2;
  166. targetElevation.position.y = 8;
  167. targetElevation.add( targetBob );
  168. targetBob.add( targetMesh );
  169. const targetCamera = makeCamera();
  170. const targetCameraPivot = new THREE.Object3D();
  171. targetCamera.position.y = 1;
  172. targetCamera.position.z = - 2;
  173. targetCamera.rotation.y = Math.PI;
  174. targetBob.add( targetCameraPivot );
  175. targetCameraPivot.add( targetCamera );
  176. // Create a sine-like wave
  177. const curve = new THREE.SplineCurve( [
  178. new THREE.Vector2( - 10, 0 ),
  179. new THREE.Vector2( - 5, 5 ),
  180. new THREE.Vector2( 0, 0 ),
  181. new THREE.Vector2( 5, - 5 ),
  182. new THREE.Vector2( 10, 0 ),
  183. new THREE.Vector2( 5, 10 ),
  184. new THREE.Vector2( - 5, 10 ),
  185. new THREE.Vector2( - 10, - 10 ),
  186. new THREE.Vector2( - 15, - 8 ),
  187. new THREE.Vector2( - 10, 0 ),
  188. ] );
  189. const points = curve.getPoints( 50 );
  190. const geometry = new THREE.BufferGeometry().setFromPoints( points );
  191. const material = new THREE.LineBasicMaterial( { color: 0xff0000 } );
  192. const splineObject = new THREE.Line( geometry, material );
  193. splineObject.rotation.x = Math.PI * .5;
  194. splineObject.position.y = 0.05;
  195. scene.add( splineObject );
  196. function resizeRendererToDisplaySize( renderer ) {
  197. const canvas = renderer.domElement;
  198. const width = canvas.clientWidth;
  199. const height = canvas.clientHeight;
  200. const needResize = canvas.width !== width || canvas.height !== height;
  201. if ( needResize ) {
  202. renderer.setSize( width, height, false );
  203. }
  204. return needResize;
  205. }
  206. const targetPosition = new THREE.Vector3();
  207. const tankPosition = new THREE.Vector2();
  208. const tankTarget = new THREE.Vector2();
  209. const cameras = [
  210. { cam: camera, desc: 'detached camera', },
  211. { cam: turretCamera, desc: 'on turret looking at target', },
  212. { cam: targetCamera, desc: 'near target looking at tank', },
  213. { cam: tankCamera, desc: 'above back of tank', },
  214. ];
  215. const infoElem = document.querySelector( '#info' );
  216. function render( time ) {
  217. time *= 0.001;
  218. if ( resizeRendererToDisplaySize( renderer ) ) {
  219. const canvas = renderer.domElement;
  220. cameras.forEach( ( cameraInfo ) => {
  221. const camera = cameraInfo.cam;
  222. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  223. camera.updateProjectionMatrix();
  224. } );
  225. }
  226. // move target
  227. targetOrbit.rotation.y = time * .27;
  228. targetBob.position.y = Math.sin( time * 2 ) * 4;
  229. targetMesh.rotation.x = time * 7;
  230. targetMesh.rotation.y = time * 13;
  231. targetMaterial.emissive.setHSL( time * 10 % 1, 1, .25 );
  232. targetMaterial.color.setHSL( time * 10 % 1, 1, .25 );
  233. // move tank
  234. const tankTime = time * .05;
  235. curve.getPointAt( tankTime % 1, tankPosition );
  236. curve.getPointAt( ( tankTime + 0.01 ) % 1, tankTarget );
  237. tank.position.set( tankPosition.x, 0, tankPosition.y );
  238. tank.lookAt( tankTarget.x, 0, tankTarget.y );
  239. // face turret at target
  240. targetMesh.getWorldPosition( targetPosition );
  241. turretPivot.lookAt( targetPosition );
  242. // make the turretCamera look at target
  243. turretCamera.lookAt( targetPosition );
  244. // make the targetCameraPivot look at the at the tank
  245. tank.getWorldPosition( targetPosition );
  246. targetCameraPivot.lookAt( targetPosition );
  247. wheelMeshes.forEach( ( obj ) => {
  248. obj.rotation.x = time * 3;
  249. } );
  250. const camera = cameras[ time * .25 % cameras.length | 0 ];
  251. infoElem.textContent = camera.desc;
  252. renderer.render( scene, camera.cam );
  253. requestAnimationFrame( render );
  254. }
  255. requestAnimationFrame( render );
  256. }
  257. main();
  258. </script>
  259. </html>