-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11-user.html
More file actions
167 lines (139 loc) · 5.39 KB
/
11-user.html
File metadata and controls
167 lines (139 loc) · 5.39 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<html>
<head>
<meta charset="utf-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/110/three.min.js"></script>
<script src="orbitcontrols.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
<script>
// ページの読み込みを待つ
window.addEventListener('load', init);
function init() {
// レンダラーを作成
const renderer = new THREE.WebGLRenderer({
canvas: document.querySelector('#myCanvas'),
antialias: true
});
// シーンを作成
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x000000);
// カメラを作成
const camera = new THREE.PerspectiveCamera(60); // 45,1.0
camera.position.y = 300;
// カメラコントローラーを作成
const controls = new THREE.OrbitControls(camera);
// 滑らかにカメラコントローラーを制御する
controls.enableDamping = true;
controls.dampingFactor = 0.2;
/*
// 地面と軸の生成
scene.add(new THREE.GridHelper(600));
scene.add(new THREE.AxesHelper(300));
*/
const geometry = new THREE.BoxGeometry(100, 100, 100);
const material = new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load('imgs/risu3.png'),
side: THREE.DoubleSide,
});
const earthMesh = new THREE.Mesh(geometry, material);
scene.add(earthMesh);
earthMesh.position.set(
0, 0, 0
);
// 光源
scene.add(new THREE.DirectionalLight(0xff0000, 2)); // 平行光源
scene.add(new THREE.AmbientLight(0x00ffff)); // 環境光源
// 星屑を作成します (カメラの動きをわかりやすくするため)
createStarField();
function createStarField() {
// 形状データを作成
const geometry = new THREE.Geometry();
for (let i = 0; i < 1000; i++) {
geometry.vertices.push(
new THREE.Vector3(
3000 * (Math.random() - 0.5),
3000 * (Math.random() - 0.5),
3000 * (Math.random() - 0.5)
)
);
}
// マテリアルを作成
const material = new THREE.PointsMaterial({
size: 10,
color: 0xffffff
});
// 物体を作成
const mesh = new THREE.Points(geometry, material);
scene.add(mesh);
}
let delta = 0.01;
let rot = 0;
let dist = 400;
tick();
// 毎フレーム時に実行されるループイベントです
function tick() {
rot -= 0.5; // 毎フレーム角度を0.5度ずつ足していく
// ラジアンに変換する
const radian = (rot * Math.PI) / 180;
camera.position.x = dist * Math.sin(radian);
camera.position.z = dist * Math.cos(radian);
earthMesh.rotation.y += delta;
if (delta == 0.01) {
dist += 7;
}
else if (delta < 0.01) {
delta = 0.01;
dist = 200;
}
else {
delta -= 0.003;
dist -= 25;
camera.position.y += 4;
if (camera.position.y > 200) camera.position.y = 200;
}
if (dist > 400) {
document.onclick = function () {
delta = 0.5;
camera.position.y = -300;
rot += 180;
flag=1;
};
dist = 400;
camera.position.y = 200;
}
if (dist < 200) {
dist = 200;
}
// カメラコントローラーを更新
controls.update();
// レンダリング
renderer.render(scene, camera);
requestAnimationFrame(tick);
}
// 初期化のために実行
onResize();
// リサイズイベント発生時に実行
window.addEventListener('resize', onResize);
function onResize() {
// サイズを取得
const width = window.innerWidth;
const height = window.innerHeight;
// レンダラーのサイズを調整する
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(width, height);
// カメラのアスペクト比を正す
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
}
</script>
</head>
<body>
<canvas id="myCanvas"></canvas>
</body>
</html>