webgl_geometry_cube.html 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - cube</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <script type="module">
  11. import * as THREE from '../build/three.module.js';
  12. var camera, scene, renderer;
  13. var mesh;
  14. init();
  15. animate();
  16. function init() {
  17. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  18. camera.position.z = 400;
  19. scene = new THREE.Scene();
  20. var texture = new THREE.TextureLoader().load( 'textures/crate.gif' );
  21. var geometry = new THREE.BoxBufferGeometry( 200, 200, 200 );
  22. var material = new THREE.MeshBasicMaterial( { map: texture } );
  23. mesh = new THREE.Mesh( geometry, material );
  24. scene.add( mesh );
  25. renderer = new THREE.WebGLRenderer( { antialias: true } );
  26. renderer.setPixelRatio( window.devicePixelRatio );
  27. renderer.setSize( window.innerWidth, window.innerHeight );
  28. document.body.appendChild( renderer.domElement );
  29. //
  30. window.addEventListener( 'resize', onWindowResize, false );
  31. }
  32. function onWindowResize() {
  33. camera.aspect = window.innerWidth / window.innerHeight;
  34. camera.updateProjectionMatrix();
  35. renderer.setSize( window.innerWidth, window.innerHeight );
  36. }
  37. function animate() {
  38. requestAnimationFrame( animate );
  39. mesh.rotation.x += 0.005;
  40. mesh.rotation.y += 0.01;
  41. renderer.render( scene, camera );
  42. }
  43. </script>
  44. </body>
  45. </html>