-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.html
90 lines (71 loc) · 2.24 KB
/
cube.html
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
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>3D Cube</title>
<style>
div {
border : 1px solid;
}
</style>
</head>
<body>
<div id="webgl_view"></div>
<script type="text/javascript" src="three.js" charset="utf-8"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script>
var height = 400;
var width = 400;
var container = $('#webgl_view');
container.css({
height : height + "px",
width : width + "px"});
// (1) 立方体
// 形状
var cubeGeometry = new THREE.CubeGeometry(1,1,1);
// 色
var cubeMaterial = new THREE.MeshPhongMaterial({color : 0x00ff00});
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
// (2) ライト(平行光)
var light = new THREE.DirectionalLight(0xffffff);
light.position.x = 0;
light.position.y = 10;
light.position.z = 10;
// (3) 環境光
var alight = new THREE.AmbientLight(0x202020);
// (4) Scene
var scene = new THREE.Scene();
scene.add(cube);
scene.add(light);
scene.add(alight);
// (5) Camera (視野角, アスペクト比(縦横比), 描画対象(最小距離), 描画対象(最大距離)
var camera = new THREE.PerspectiveCamera(90, (height / width), 0.1, 10);
// カメラ位置
camera.position.x = 0;
camera.position.y = 2;
camera.position.z = 2;
// カメラ方向
camera.lookAt(new THREE.Vector3(0,-1,-1));
// (6) WebGL Renderer
var renderer = new THREE.WebGLRenderer();
renderer.setSize(height, width);
renderer.setClearColor(0x000000);
renderer.clear(true);
container.append(renderer.domElement);
// (7) Animation (Rotate)
function animation() {
var time = (new Date()).getTime();
cube.position.x = 2.0 * Math.sin(time / 1000);
cube.position.z = 2.0 * Math.cos(time / 1000);
cube.rotation.y = time / 300;
renderer.render(scene, camera);
// 次のアニメーション処理の呼び出し
// (実行環境によって、次回呼び出しまでの間隔が異なる
// 次回のオブジェクトやカメラの位置は、何回 animation()
// が呼び出されたかではなく、時刻から計算するようにする)
requestAnimationFrame(animation);
}
animation();
</script>
</body>
</html>