Follow us

Blog

  1. ホーム
  2. >
  3. ブログ一覧
  4. >
  5. 【three.js】3Dデータ読み込み・背景色の変更・canvas外のスクロール制御(r171対応ver)

広告

【three.js】3Dデータ読み込み・背景色の変更・canvas外のスクロール制御(r171対応ver)

みなさま、ご無沙汰しております。

ほぼ1年ぶりの更新になってしまいました。。。
書こう書こうと思っていたんですが、日々の忙しさに追われ気づけば1年経っておりました。
僕は生きております!!

さて、今回3Dイメージを使用したウェブサイト制作の依頼があり、three.jsを使用し自分がつまずいた点をまとめましたので、同じく悩んでいる方のお力になれればと思っています!

three.jsの読み込み

Three.js公式サイまたはGitHubリポジトリから最新リリース(r171)をダウンロードします。
リポジトリ内のbuild/three.module.jsがES Modules用のメインファイルとなります。また、examples/jsmディレクトリ以下に各種コントローラーやローダーなどのモジュールが格納されています。

project-root/
├─ public/
│  ├─ index.html
│  ├─ three/
│  │  ├─ build/three.module.js
│  │  ├─ examples/jsm/controls/OrbitControls.js
│  │  └─ examples/jsm/loaders/GLTFLoader.js
│  └─ ...
  • three.module.js
  • OrbitControls.js
  • GLTFLoader.js

上記のファイルをサーバーサーバーにアップしてください。

3Dデータの読み込み

<script type="module">
    import * as THREE from './three/build/three.module.js';
    import { OrbitControls } from './three/examples/jsm/controls/OrbitControls.js';
    import { ColladaLoader } from './three/examples/jsm/loaders/ColladaLoader.js';

    // ページの読み込み完了時に初期化
    window.addEventListener('load', init);

    function init() {
      // サイズを指定
      const width = 960;
      const height = 540;

      // レンダラーを作成
      const renderer = new THREE.WebGLRenderer({
        canvas: document.querySelector('#myCanvas'),
        antialias: true
      });
      renderer.setPixelRatio(window.devicePixelRatio);
      renderer.setSize(width, height);

      // シーンを作成
      const scene = new THREE.Scene();
      scene.background = new THREE.Color(0x404040);

      // カメラを作成
      const camera = new THREE.PerspectiveCamera(
        45,
        width / height,
        0.1,
        10000
      );
      // カメラの初期座標を設定
      camera.position.set(60, 0, 100);

      // カメラコントローラーを作成 (renderer.domElementを渡す)
      const controls = new OrbitControls(camera, renderer.domElement);
      controls.target.set(0, -5, 0);
      controls.update();

      // 平行光源を作成
      const directionalLight = new THREE.DirectionalLight(0xffffff);
      directionalLight.position.set(1, 1, 1);
      scene.add(directionalLight);

      // 環境光を追加
      const ambientLight = new THREE.AmbientLight(0x333333);
      scene.add(ambientLight);

      // Colladaモデルデータを読み込む
      const loader = new ColladaLoader();
      // Colladaファイルのパスを指定(相対パスまたは絶対パス)
      loader.load('path/to/model.dae', collada => {
        // 読み込み後に3D空間に追加
        const model = collada.scene;
        scene.add(model);
      });

      // アニメーションループ
      function tick() {
        renderer.render(scene, camera);
        requestAnimationFrame(tick);
      }
      tick();
    }
  </script>

53行目の「path/to/model.dae」を.daeデータまでのパスに変更してください。

背景色の変更方法

19行目にある

scene.background = new THREE.Color( 0x404040 );

404040 部分を任意の色に変更する。

canvas外のスクロール制御

37行目にある

const controls = new OrbitControls(camera);

の部分を

const controls = new OrbitControls(camera, renderer.domElement);

に変更する。

まとめ

なかなか使う機会がないと思いますが、忘れないようにメモとして見ていただければと思います。

参考にさせていただいた記事

Three.jsでモデルデータを読み込む – ICS MEDIA

広告

Area

best-dayは、群馬県吉岡町・前橋市・渋川市・高崎市を中心に、ホームページ制作、運用・保守、SNS運用、SEO対策などを行なっております。

訪問可能エリア

前橋市、高崎市、桐生市、伊勢崎市、太田市、沼田市、館林市、渋川市、藤岡市、富岡市、安中市、榛東村、吉岡町、上野村、神流町、下仁田町、南牧村、甘楽町、中之条町、長野原町、嬬恋村、草津町、高山村、東吾妻町、片品村、川場村、昭和村、みなかみ町、板倉町、明和町、千代田町、大泉町、邑楽町

遠方のお客様

Google MeetやZOOMにてお打ち合わせを行います。

ホームページ制作対応エリア

CONTACT

ホームページ制作・WEBサイト制作に関する
ご依頼・ご相談・ご質問などお気軽にお問い合わせください。
外注・業務委託・WEBサイト制作パートナーを
お探しの制作会社様もお気軽にお問い合わせください。