0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Three.js の導入 ~プロジェクト立ち上げてからThree.jsで描画するまで~

Last updated at Posted at 2025-08-02

この手順をすると以下のものができます。

threejs_first_demo.gif

手順:

  • three.jsを導入する任意の repository を準備する (説明省略)

  • node をPCに install (説明省略)

  • npm の準備

    • npm init -y
  • buildツール: vite の準備

    • npm i vite
  • package.json にvite コマンドのaliasを登録

    package.json
    ...省略
      "scripts": {
        "dev": "vite",
        "build": "vite build"
      },
    ...省略
    
  • index.html の作成

    • projectフォルダのルートに index.html を作成する
    • index.html をvscodeで開き、 ! を打ち込む
    • すると、scaffold 機能でindex.htmlのテンプレートを自動生成できる

現在のリポジトリ状態

* (root)
    * node_modules
+   * index.html
    * pacakage-lock.json
    * package.json
    * README.md
  • three.jsを使えるようにするために script.js を作成する
script.js
console.log("Hello, World!");

現在のリポジトリ状態

* (root)
    * node_modules
    * index.html
    * pacakage-lock.json
    * package.json
    * README.md
+   * script.js 
  • html にjavascriptを読み込ませるために以下のようにする
index.html
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
+    <script type="module" src="./script.js"></script>
  </body>
</html>
  • three.js の準備
    • npm i three
  • script.js にてthree.jsを読み込み、ログを出して確認する
script.js
+ import * as THREE from "three";

+ console.log(THREE);
- console.log("Hello, World!");
  • index.html にて描画用のcanvasを準備する
index.html
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
+   <canvas class="webgl"></canvas>
    <script type="module" src="./script.js"></script>
  </body>
</html>
  • script.js にて描画内容を作成する
script.js
import * as THREE from "three";

// Createb a canvas element
const canvas = document.querySelector("canvas.webgl");

// Create a scene
const scene = new THREE.Scene();

// Create a camera
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);
camera.position.z = 5;

// Create a renderer
const renderer = new THREE.WebGLRenderer({ canvas });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// Create a cube
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);

// Animation loop
function animate() {
  requestAnimationFrame(animate);
  cube.rotation.x += 0.01;
  cube.rotation.y += 0.01;
  renderer.render(scene, camera);
}
animate();

console.log(THREE);

参考

https://threejs-journey.com/lessons/what-is-webgl-and-why-use-three-js
https://threejs-journey.com/lessons/first-threejs-project

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?