-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_texture-test.html
197 lines (146 loc) · 6.28 KB
/
_texture-test.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html>
<head>
<title>Texture By YD</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/three.js/69/three.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/stats.js/r16/Stats.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.5/dat.gui.min.js"></script>
<style>
body {
/* set margin to 0 and overflow to hidden, to go fullscreen */
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="Stats-output">
</div>
<!-- Div which will hold the Output -->
<div id="WebGL-output">
</div>
<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">
var camera;
var scene;
var renderer;
var cube;
// once everything is loaded, we run our Three.js stuff.
function init() {
var stats = initStats();
// create a scene, that will hold all our elements such as objects, cameras and lights.
scene = new THREE.Scene();
// create a camera, which defines where we're looking at.
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
// create a render and set the size
renderer = new THREE.WebGLRenderer();
renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapEnabled = true;
// create the ground plane
var planeGeometry = new THREE.PlaneGeometry(100, 100, 1, 1);
var planeMaterial = new THREE.MeshLambertMaterial({
color: 0xffffff
});
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.receiveShadow = true;
// rotate and position the plane
plane.rotation.x = -0.5 * Math.PI;
plane.position.x = 0;
plane.position.y = -30;
plane.position.z = 0;
// add the plane to the scene
scene.add(plane);
// create a cube
var cubeGeometry = new THREE.BoxGeometry(25, 25, 25);
var cubeMaterial = new THREE.MeshLambertMaterial({
color: 0xff0000
});
var texture = new THREE.ImageUtils.loadTexture("soil_diffuse.jpg");
var material = new THREE.MeshLambertMaterial({//将材质的map属性设置为texture:
map: texture
});
cube = new THREE.Mesh(cubeGeometry, material);
cube.castShadow = true;
// position the cube
cube.position.x = -20;
cube.position.y = 20;
cube.position.z = 0;
// add the cube to the scene
scene.add(cube);
camera.position.x = -100;
camera.position.y = 20;
camera.position.z = 45;
camera.lookAt(scene.position);
// add subtle ambient lighting
var ambientLight = new THREE.AmbientLight(0x0c0c0c);
scene.add(ambientLight);
// add spotlight for the shadows
var spotLight = new THREE.SpotLight(0xffffff);
spotLight.position.set(-100, 60, -10);
spotLight.castShadow = true;
scene.add(spotLight);
// add the output of the renderer to the html element
document.getElementById("WebGL-output").appendChild(renderer.domElement);
// call the render function
var step = 0;
var controls = new function () {
this.rotationSpeed = 0.02;
//this.bouncingSpeed = 0.03;
//this.r = 0.9;
//this.g = 0.2;
//this.b = 0.2;
this.texture = 'soil_diffuse.jpg';
};
var gui = new dat.GUI();
gui.add(controls, 'rotationSpeed', 0, 0.1);
//gui.add(controls, 'r', 0, 1);
//gui.add(controls, 'g', 0, 1);
//gui.add(controls, 'b', 0, 1);
gui.add(controls, 'texture', ['soil_diffuse.jpg', 'soil_normal.jpg', 'soil_specular.jpg']);
render();
var currentTexture = controls.texture;
function render() {
stats.update();
//cube.material.color.setRGB(controls.r, controls.g, controls.b);
if (currentTexture !== controls.texture) {
//cube
var texture = new THREE.ImageUtils.loadTexture(controls.texture);
cube.material.map = texture;
cube.material.needsUpdate = true;
currentTexture = controls.texture;
}
// rotate the cube around its axes
cube.rotation.x += controls.rotationSpeed;
cube.rotation.y += controls.rotationSpeed;
cube.rotation.z += controls.rotationSpeed;
// bounce the sphere up and down
//step += controls.bouncingSpeed;
//sphere.position.x = 20 + (10 * (Math.cos(step)));
//sphere.position.y = 2 + (10 * Math.abs(Math.sin(step)));
// render using requestAnimationFrame
requestAnimationFrame(render);
renderer.render(scene, camera);
}
function initStats() {
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
// Align top-left
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';
document.getElementById("Stats-output").appendChild(stats.domElement);
return stats;
}
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.onload = init;
// listen to the resize events
window.addEventListener('resize', onResize, false);
</script>
</body>
</html>