Three.js で作る 3D 年賀状

cx20が2015年1月3日14:53:59に投稿しました。

JavaScript

5

お気に入り

準備中

Views

このレシピで作るもの

Three.js を使って、手軽に立体ドット絵を表示させてみます。


目次

準備するもの

今回必要なものは以下のようになります。

  • three.js r69
  • 立体ドット絵

つくっていくもの

今回のレシピで作っていくファイルは以下のようになります。

  • index.html
  • index.js

index.html を作る

index.html を以下のように記述してください。

<!DOCTYPE html>
<meta charset="utf-8">
<title>Happy New Year 2015</title>
<!-- three.min.js -->
<script src="http://threejs.org/build/three.min.js"></script>
<script src="index.js"></script>

index.js を書いていく

雛形コピーした後、dataSet ~ の箇所に、ドット絵を描いていきます。
"×" は色無し、"赤" は赤色といった感じです。

// Jumping Sheep by cx20
// http://kyucon.com/qblock/#/63138
// 
var dataSet = [
    [
    ...
    ],
    [
    "×","×","×","×","×","×","×","×","×","×","×","×","×","×","×","×",
    "×","×","×","×","×","×","×","×","×","×","×","×","×","×","×","×",
    "×","×","×","×","×","×","×","×","×","×","×","×","×","×","×","×",
    "×","×","×","×","×","×","×","×","×","×","×","×","×","×","×","×",
    "×","×","×","×","×","×","×","×","×","×","×","×","×","×","×","×",
    "×","×","×","×","×","×","×","×","×","×","×","×","×","×","×","×",
    "×","×","×","×","×","×","×","×","×","×","×","×","×","×","×","×",
    "×","×","×","×","×","×","×","×","×","×","×","×","×","×","×","×",
    "×","×","×","×","×","×","×","×","×","×","×","×","×","×","×","×",
    "×","赤","赤","赤","×","赤","赤","赤","×","赤","×","赤","赤","赤","×","×",
    "×","赤","×","赤","×","赤","×","赤","×","赤","×","赤","×","×","×","×",
    "×","×","×","赤","×","赤","×","赤","×","赤","×","赤","赤","赤","×","×",
    "×","×","赤","×","×","赤","×","赤","×","赤","×","×","×","赤","×","×",
    "×","赤","赤","赤","×","赤","赤","赤","×","赤","×","赤","赤","赤","×","×",
    "×","×","×","×","×","×","×","×","×","×","×","×","×","×","×","×",
    "×","×","×","×","×","×","×","×","×","×","×","×","×","×","×","×",
    "×","×","×","×","×","×","×","×","×","×","×","×","×","×","×","×"
    ]
];

ドット絵を自作する自信が無い場合は、以下の投稿作品が参考になるかもしれません。
[Edit] - [Show Canvas] を選択することで、立体ドット絵の各レイヤーを表示することが出来ます。

■ Q-BLOCK : Create 3D Pixel Art Online
http://kyucon.com/qblock/

表示までの流れ

全体的な流れは以下のようになります。

  1. 初期化処理
  2. 立体ドット絵の作成
  3. 立体ドット絵の回転

初期化処理

function init() {
    container = document.createElement('div');
    document.body.appendChild(container);

    // カメラを作成する
    camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
    camera.position.x = 0;
    camera.position.y = 0;
    camera.position.z = 500;

    // シーンを作成する
    scene = new THREE.Scene();

    // 立体ドット絵を作成する
    group = createSheep();

    // シーンに立体ドット絵を追加する
    scene.add(group);

    // ライトを作成する
    var light = new THREE.DirectionalLight(0xffffff);
    light.position.set(0, 0, 10).normalize();
    scene.add(light);

    // レンダラーを作成する
    renderer = new THREE.WebGLRenderer();

    // 背景色を指定する(空色に変更する)
    renderer.setClearColor(0x4584b4);

    // レンダラーのサイズを指定する
    renderer.setSize(window.innerWidth, window.innerHeight);

    // HTML との紐づけを行う
    container.appendChild(renderer.domElement);
}

立体ドット絵の作成

function createSheep() {
    // 立体ドット絵をグループ化して返却する
    var group = new THREE.Object3D();

    // 箱を用意する
    var geometry = new THREE.BoxGeometry(DOT_SIZE * 0.9, DOT_SIZE * 0.9, DOT_SIZE * 0.9);

    // ドット絵の枚数ぶんループする
    for (var j = 0; j < dataSet.length; j++) {

        // ドット絵1枚のドット数ぶんループする
        for (var i = 0; i < dataSet[j].length; i++) {

            // ドット絵の表示座標を計算する
            var x = (i % 16) * DOT_SIZE + X_START_POS;
            var y = (16 - Math.floor(i / 16)) * DOT_SIZE + Y_START_POS;
            var z = j * DOT_SIZE + Z_START_POS;

            // ドット絵の色を取得する("赤"→"#ff0000"に変換する)
            var color = getRgbColor(dataSet[j][i]);

            // ドットが "×" でなければ
            if (dataSet[j][i] != "×") {
                // 色を指定する
                var material = new THREE.MeshLambertMaterial({color: color});

                // 箱に色をつける
                var mesh = new THREE.Mesh(geometry, material);

                // 箱の位置を指定する
                mesh.position.x = x;
                mesh.position.y = y;
                mesh.position.z = z;

                // グループ化する
                group.add(mesh);
            }
        }
    }

    // 立体ドット絵を返却する
    return group;
}

立体ドット絵の回転

function render() {
    // 立体ドット絵をY軸に回転させる
    group.rotation.y += Math.PI / 180;

    // 表示を更新する
    renderer.render(scene, camera);
}

おわりに

以上で回転する立体ドット絵の完成です。

three_js_cloud_20150101_002.png

お疲れ様でした。

このレシピを書いたシェフ

cx20

@cx20

プログラマ(マイクロソフト系の言語を使用することが多いです。)


このシェフが書いたレシピ