forked from webcrumbs/threejs-crumbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example42.html
148 lines (120 loc) · 4.5 KB
/
example42.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
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
<!DOCTYPE html>
<html>
<head>
<title>Example 42 - Bump maps</title>
<style>
body{
margin: 0;
overflow: hidden;
}
#stats { /* Align stats top-left */
position: absolute;
left: 0px;
top: 0px;
}
</style>
</head>
<body>
<!-- JavaScript libraries -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r67/three.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/stats.js/r11/Stats.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
<!-- Javascript code that runs our Three.js examples -->
<script>
// once everything is loaded, we run our Three.js stuff.
$(function () {
var stats = initStats();
// create a scene, that will hold all our elements such as objects, cameras and lights.
var scene = new THREE.Scene();
// create a camera, which defines where we're looking at.
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
// create a render and set the size
var webGLRenderer = new THREE.WebGLRenderer();
webGLRenderer.setClearColor(0xEEEEEE, 1.0);
webGLRenderer.setSize(window.innerWidth, window.innerHeight);
var sphere1 = createMesh(new THREE.BocGeometry(15, 15, 2), "stone.jpg");
sphere1.rotation.y = -0.5;
sphere1.position.x = 12;
scene.add(sphere1);
var sphere2 = createMesh(new THREE.BoxGeometry(15, 15, 2), "stone.jpg", "stone-bump.jpg");
sphere2.rotation.y = 0.5;
sphere2.position.x = -12;
scene.add(sphere2);
var floorTex = THREE.ImageUtils.loadTexture("assets/textures/general/floor-wood.jpg")
var plane = new THREE.Mesh(
new THREE.CubeGeometry(200, 100, 0.1, 30),
new THREE.MeshPhongMaterial({color: 0x3c3c3c, map: floorTex})
);
plane.position.y = -7.5;
plane.rotation.x = -0.5 * Math.PI;
scene.add(plane);
// position and point the camera to the center of the scene
camera.position.x = 00;
camera.position.y = 12;
camera.position.z = 28;
camera.lookAt(new THREE.Vector3(0, 0, 0));
var ambiLight = new THREE.AmbientLight(0x242424)
scene.add(ambiLight);
var light = new THREE.SpotLight();
light.position.set(0, 30, 30);
light.intensity = 1.2;
scene.add(light);
// add the output of the renderer to the html element
$('body').append(webGLRenderer.domElement);
// call the render function
var step = 0;
// setup the control gui
var controls = new function () {
this.bumpScale = 0.2;
this.changeTexture = "weave";
this.rotate = false;
this.changeTexture = function (e) {
var texture = THREE.ImageUtils.loadTexture("assets/textures/general/" + e + ".jpg");
sphere2.material.map = texture;
sphere1.material.map = texture;
var bump = THREE.ImageUtils.loadTexture("assets/textures/general/" + e + "-bump.jpg");
sphere2.material.bumpMap = bump;
}
this.updateBump = function (e) {
sphere2.material.bumpScale = e;
}
};
var gui = new dat.GUI();
gui.add(controls, "bumpScale", -2, 2).onChange(controls.updateBump);
gui.add(controls, "changeTexture", ['stone', 'weave']).onChange(controls.changeTexture);
gui.add(controls, "rotate");
render();
function createMesh(geom, imageFile, bump) {
var texture = THREE.ImageUtils.loadTexture("assets/textures/general/" + imageFile)
geom.computeVertexNormals();
var mat = new THREE.MeshPhongMaterial();
mat.map = texture;
if (bump) {
var bump = THREE.ImageUtils.loadTexture("assets/textures/general/" + bump)
mat.bumpMap = bump;
mat.bumpScale = 0.2;
}
var mesh = new THREE.Mesh(geom, mat);
return mesh;
}
function render() {
stats.update();
if (controls.rotate) {
sphere1.rotation.y -= 0.01;
sphere2.rotation.y += 0.01;
}
// render using requestAnimationFrame
requestAnimationFrame(render);
webGLRenderer.render(scene, camera);
}
function initStats() {
stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
$('body').append(stats.domElement);
return stats;
}
});
</script>
</body>
</html>