[Three.js] 平面ジオメトリに画像をテクスチャとして貼り付ける(複数版)
前回のものを複数ジオメトリにしただけ。
次への途中段階に過ぎませんが・・・
common.js
x
89
89
1
import * as THREE from "three";
2
3
import { PhotoPlane } from "./Mesh/PhotoPlane";
4
5
6
(function(window, document) {
7
8
class Common {
9
10
/**
11
* メイン
12
* @constructor
13
*/
14
constructor() {
15
16
this.scene = null;
17
this.camera = null;
18
this.renderer = null;
19
20
this.planes = [];
21
22
this.textures = [
23
'./images/texture01.jpg',
24
'./images/texture02.jpg',
25
'./images/texture03.jpg'
26
];
27
28
this.init();
29
30
}
31
32
33
34
/**
35
* 初期化
36
*/
37
init = () => {
38
39
// シーンの生成
40
this.scene = new THREE.Scene();
41
42
// カメラ
43
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
44
45
// レンダラーの生成と追加(要するにcanvas要素である)
46
this.renderer = new THREE.WebGLRenderer();
47
this.renderer.setSize( window.innerWidth, window.innerHeight );
48
document.body.appendChild( this.renderer.domElement );
49
50
// ライトの追加(環境光)
51
const light = new THREE.AmbientLight(0xFFFFFF, 1.0);
52
this.scene.add(light);
53
54
// ライトの追加(特定方向に照射される光源)
55
const directLight = new THREE.DirectionalLight(0xFFFFFF, 1);
56
this.scene.add(directLight);
57
58
for(let i = 0; i < 3; i++) {
59
const plane = new PhotoPlane(this.textures[i]);
60
this.scene.add(plane);
61
plane.position.y = i * 9 - 9;
62
63
this.planes.push(plane);
64
}
65
66
this.camera.position.z = 20;
67
68
this.animate();
69
70
}
71
72
73
animate = () => {
74
75
requestAnimationFrame( this.animate );
76
77
this.renderer.render( this.scene, this.camera );
78
79
}
80
81
}
82
83
84
85
86
new Common();
87
88
})(window, document);
89
次回からのことも考慮して、使いやすくするためにTHREE.Meshを継承したPhotoPlaneクラスを用意。
PhotoPlane.js
1
32
32
1
import * as THREE from "three";
2
3
4
5
/**
6
* 画像テクスチャ平面メッシュ
7
* @class
8
*/
9
10
export class PhotoPlane extends THREE.Mesh {
11
12
/**
13
*
14
* @constructor
15
*/
16
constructor(url) {
17
18
const geometry = new THREE.PlaneGeometry(16, 9);
19
20
const loader = new THREE.TextureLoader();
21
const texture = loader.load(url);
22
23
const material = new THREE.MeshBasicMaterial( { map:texture } );
24
25
super(geometry, material);
26
27
// this.textures = url;
28
29
}
30
31
}
32