-
Notifications
You must be signed in to change notification settings - Fork 8
/
webgl.html
113 lines (97 loc) · 3.29 KB
/
webgl.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebGL Logo</title>
<link rel="stylesheet" href="all.css">
<style>
#world {
position: absolute;
width:100%;
height: 100%;
overflow: hidden;
background: -webkit-linear-gradient(#2a3340, #172533);
background: -o-linear-gradient(#2a3340, #172533);
background: linear-gradient(#2a3340, #172533);
}
</style>
</head>
<body>
<div id="world"></div>
<!-- include scripts at the bottom of the body -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js"></script>
<script src="vendor/OrbitControls.js"></script>
<script>
// Values to play with:
var color1 = "rgb(255, 255, 255)";
var color2 = "rgb(255, 255, 255)";
var rotateXSpeed = 1;
var rotateYSpeed = 0;
var rotateZSpeed = 0;
// Other variable initialization.
var scene, renderer, camera, light, sblogo, controls, loader, material;
var WIDTH = window.innerWidth,
HEIGHT = window.innerHeight;
function initWorld(){
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(30, WIDTH/HEIGHT, 0.1, 2000);
camera.position.z = 30;
renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true
});
renderer.setSize(WIDTH, HEIGHT);
renderer.shadowMapEnabled = true;
container = document.getElementById('world');
container.appendChild(renderer.domElement);
light = new THREE.DirectionalLight(color1, 1);
light.position.set(0,100,50);
light.castShadow = true;
light.shadowDarkness = .2;
scene.add(light);
light2 = new THREE.DirectionalLight(color2, 1);
light2.position.set(-100,0,0);
light2.castShadow = true;
light2.shadowDarkness = .2;
scene.add(light2);
controls = new THREE.OrbitControls(camera, renderer.domElement);
// HANDLE SCREEN RESIZE
window.addEventListener('resize', handleWindowResize, false);
// Sparbox Logo Mesh
loader = new THREE.JSONLoader();
loader.load('webgl/data.json', function(geometry) {
material = new THREE.MeshLambertMaterial({
color:0xCCCCCC,
shininess:0,
specular:0x000000,
shading:THREE.FlatShading
});
sblogo = new THREE.Mesh(geometry, material);
sblogo.rotation.x = Math.PI / 5; // initial position
scene.add(sblogo);
});
loop();
}
function loop(){
// Rotation values
if (sblogo) {
sblogo.rotation.x += (0.01 * rotateXSpeed);
sblogo.rotation.y += (0.01 * rotateYSpeed);
sblogo.rotation.z += (0.01 * rotateZSpeed);
}
renderer.render(scene, camera);
requestAnimationFrame(loop);
}
function handleWindowResize() {
// Recalculate Width and Height as they had changed
HEIGHT = window.innerHeight;
WIDTH = window.innerWidth;
// Update the renderer and the camera
renderer.setSize(WIDTH, HEIGHT);
camera.aspect = WIDTH / HEIGHT;
camera.updateProjectionMatrix();
}
initWorld();
</script>
</body>
</html>