-
Notifications
You must be signed in to change notification settings - Fork 0
/
6_threejs_all.js
131 lines (131 loc) · 4.57 KB
/
6_threejs_all.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
window.addEventListener("load", preload);
/** シーンオブジェクトです。 */
let scene;
/** カメラオブジェクトです。(PerspectiveCamera のみ) */
let camera;
/** レンダラーオブジェクトです。(WebGL のみ) */
let renderer;
/** 3D のコンテナーオブジェクトの配列です。 */
const wraps = [];
/** 3D の文字メッシュオブジェクトの配列です。 */
const words = [];
const ROUND = 2000;
const CAMERA_POSITION = 2500;
const COLOR_LIST = [0x003399, 0x0066cc, 0x0099ff, 0x33ccff];
/**
* 初期化前に必要な素材を読み込みます。
*/
function preload() {
// JSライブラリ「WebFont」を使って、フォントの読み込み完了を検知
WebFont.load({
custom: {
// フォントファミリーを指定
families: ["FontAwesome"],
// CSS の URL を指定
urls: [
"https://netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css"
],
// 他のフォントでは不要だが、FontAwesome のフォントを利用するには必要な指定
testStrings: {
// FontAwesome の検証用テキストを指定
FontAwesome: "\uf001" // グラスアイコンを指定 (任意で支障ない)
}
},
// Web Fontが使用可能になったとき
active: init
});
}
/**
* コンテンツを初期化します。
*/
function init() {
// --------------------------------
// 3D の初期化
// --------------------------------
// 3D空間の作成
scene = new THREE.Scene();
// カメラの作成
camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
1,
200000
);
// レンダラーの作成
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setClearColor(0x000000);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// --------------------------------
// コンテンツの作成
// --------------------------------
// FontAwesome 4.4 が取り得る文字コードは 61440〜62080 の間
for (let i = 0; i < 641; i++) {
// Unicode から文字コードに変換
const iconInt = 61440 + i;
// 文字コードから文字列に変換する
const iconStr = String.fromCharCode(iconInt);
// CreateJS のテキストを作成
const iconText = new createjs.Text(iconStr, "200px FontAwesome", "#FFF");
// Canvas 要素としてレンダリングさせる
iconText.cache(0, 0, 256, 256);
// Three.js のテクスチャに展開する(GPU にアップロードする)
const texture = new THREE.Texture(iconText.cacheCanvas);
texture.needsUpdate = true;
// 平面を作成する
const geometry = new THREE.PlaneBufferGeometry(200, 200);
const material = new THREE.MeshBasicMaterial({
color: COLOR_LIST[(COLOR_LIST.length * Math.random()) >> 0],
map: texture,
transparent: true,
side: THREE.DoubleSide
});
// メッシュオブジェクトを作成し、3D空間に追加する
const mesh = new THREE.Mesh(geometry, material);
mesh.position.z = 1000 * Math.random() + 500;
mesh.rotation.y = 360 * Math.random();
mesh.scale.multiplyScalar(0.5 * Math.random() + 0.5);
const wrap = new THREE.Object3D();
wrap.position.y = ROUND * Math.random();
wraps.push(wrap);
scene.add(wrap);
wrap.add(mesh);
// 配列にも保存しておく
words.push(mesh);
}
// 地面
const grid = new THREE.GridHelper(4000, 20);
grid.position.y = -30;
scene.add(grid);
// 画面のアップデート
render();
window.addEventListener("resize", handleResize);
}
/**
* エンターフレームイベントです。
*/
function render() {
requestAnimationFrame(render);
// アイコンの回転
let i = wraps.length;
while (i--) wraps[i].rotation.y += ((i / wraps.length) * Math.PI) / 180;
i = words.length;
while (i--) words[i].rotation.y += (0.5 * Math.PI) / 180;
// カメラは円周上を移動させる
camera.position.x = CAMERA_POSITION * Math.sin(Date.now() / 1000);
camera.position.z = CAMERA_POSITION * Math.cos(Date.now() / 1000);
camera.position.y = 100 + 50 * Math.cos(Date.now() / 2000);
camera.lookAt(new THREE.Vector3(0, 200, 0));
// Three.js のレンダリング
renderer.render(scene, camera);
}
/**
* リサイズイベント発生時の処理です。
* @param event
*/
function handleResize(event) {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}