🗂

Three.js Hello World

2023/04/13に公開

概要

Three.jsの最小限サンプル

コード

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Three.js Sample</title>
  </head>
  <body>
    <script type="module">
      import * as THREE from 'https://cdn.skypack.dev/three@0.151.3';
      // シーン、カメラ、レンダラーを作成
      const scene = new THREE.Scene();
      const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
      const renderer = new THREE.WebGLRenderer();
      renderer.setSize(window.innerWidth, window.innerHeight);
      renderer.shadowMap.enabled = true;
      document.body.appendChild(renderer.domElement);
      
      //ライトの生成
      const directionalLight = new THREE.DirectionalLight( 0xffffff, 1.0 );
      directionalLight.position.set(5, 10, 5)
      directionalLight.castShadow = true;
      scene.add( directionalLight );

      // 立方体を作成
      const geometry = new THREE.BoxGeometry();
      const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
      const cube = new THREE.Mesh(geometry, material);
      cube.castShadow = true;
      cube.position.set(0,2,0);
      scene.add(cube);

      // 床の生成
      const planeGeometry = new THREE.PlaneGeometry(2000, 2000)
      planeGeometry.rotateX(-Math.PI / 2)
      const planeMaterial = new THREE.MeshStandardMaterial()
      const plane = new THREE.Mesh(planeGeometry, planeMaterial)
      plane.receiveShadow = true
      scene.add(plane)

      // カメラを設定
      camera.position.set(0,2,5);
      // アニメーションループを作成
      function animate() {
        requestAnimationFrame(animate);
        cube.rotation.x += 0.01;
        cube.rotation.y += 0.01;
        renderer.render(scene, camera);
      }
      animate();
    </script>
  </body>
</html>

import mapを使った方法

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Three.js Sample</title>
    <script async src="https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js"></script>
    <script type="importmap">
      {
        "imports": {
          "three": "https://unpkg.com/three@0.152.2/build/three.module.js",
          "three/addons/": "https://unpkg.com/three@0.152.2/examples/jsm/"
        }
      }
    </script>
  </head>
  <body>
    <script type="module">
      import * as THREE from 'three';
      // シーン、カメラ、レンダラーを作成
      const scene = new THREE.Scene();
      const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
      const renderer = new THREE.WebGLRenderer();
      renderer.setSize(window.innerWidth, window.innerHeight);
      renderer.shadowMap.enabled = true;
      document.body.appendChild(renderer.domElement);
      
      //ライトの生成
      const directionalLight = new THREE.DirectionalLight( 0xffffff, 1.0 );
      directionalLight.position.set(5, 10, 5)
      directionalLight.castShadow = true;
      scene.add( directionalLight );

      // 立方体を作成
      const geometry = new THREE.BoxGeometry();
      const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
      const cube = new THREE.Mesh(geometry, material);
      cube.castShadow = true;
      cube.position.set(0,2,0);
      scene.add(cube);

      // 床の生成
      const planeGeometry = new THREE.PlaneGeometry(2000, 2000)
      planeGeometry.rotateX(-Math.PI / 2)
      const planeMaterial = new THREE.MeshStandardMaterial()
      const plane = new THREE.Mesh(planeGeometry, planeMaterial)
      plane.receiveShadow = true
      scene.add(plane)

      // カメラを設定
      camera.position.set(0,2,5);
      // アニメーションループを作成
      function animate() {
        requestAnimationFrame(animate);
        cube.rotation.x += 0.01;
        cube.rotation.y += 0.01;
        renderer.render(scene, camera);
      }
      animate();
    </script>
  </body>
</html>

Discussion