-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironmentmapping.html
219 lines (173 loc) · 5.74 KB
/
environmentmapping.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="libs/three.js"></script>
<script src="libs/OrbitControls.js"></script>
<script src="libs/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
<script src="libs/shaderloader/ShaderLoader.min.js" type="text/javascript" charset="utf-8"></script>
<script data-src="shader/env.vert" type="x-shader/x-vertex" data-name="env"></script>
<script data-src="shader/env.frag" type="x-shader/x-fragment" data-name="env"></script>
<style>
html{
height: 100%;
}
body{
height: 100%;
margin: 0px;
}
#viewer{
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="viewer"></div>
<script type="text/javascript">
var scene, camera, renderer, controls, clock;
var rotatingSphere;
var env_vs, env_fs;
var textureCam;
var viewerWidth, viewerHeight;
$(window).load(function(){
SHADER_LOADER.load(
function (data)
{
env_vs = data.env.vertex;
env_fs = data.env.fragment;
viewerWidth = $("#viewer").innerWidth();
viewerHeight = $("#viewer").innerHeight();
initViewer();
animate();
}
);
});
$( window ).resize(function() {
camera.aspect = viewerWidth / viewerHeight;
camera.updateProjectionMatrix();
renderer.setSize( viewerWidth, viewerHeight );
});
function initViewer() {
var viewerWidth = $("#viewer").innerWidth();
var viewerHeight = $("#viewer").innerHeight();
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 75, viewerWidth / viewerHeight, 0.1, 1000 );
renderer = new THREE.WebGLRenderer();
clock = new THREE.Clock;
controls = new THREE.OrbitControls( camera, renderer.domElement );
renderer.setClearColor(0xEEEEEE, 1.0);
renderer.clear();
renderer.setSize( viewerWidth, viewerHeight );
camera.position.z = 30;
camera.position.y = 20;
camera.position.x = -10;
camera.lookAt(new THREE.Vector3(0,0,0));
$("#viewer").append( renderer.domElement );
addHelpers();
addLights();
drawScene();
}
function addHelpers(){
var axes = new THREE.AxisHelper(50);
axes.position.set(0,0,0);
scene.add(axes);
var gridXZ = new THREE.GridHelper(100, 10);
gridXZ.setColors( new THREE.Color(0xFFC0CB), new THREE.Color(0x8f8f8f) );
gridXZ.position.set(0,0,0 );
scene.add(gridXZ);
}
function addLights(){
var light = new THREE.AmbientLight( 0x111111 ); // soft white light
scene.add( light );
var directionalLight = new THREE.DirectionalLight( 0xFFFFFF );
directionalLight.position.set( 1, -0.5, 1).normalize();
scene.add( directionalLight );
}
function drawScene(){
textureCam = new THREE.CubeCamera( 0.1, 1000, 512);
textureCam.position.set(10, 5, 10);
scene.add(textureCam);
var settings = {
ambientColor: { x: .4, y: .3, z: .2 },
diffuseColor: { x: .1, y: .1, z: .1 },
specularColor: { x: .6, y: .6, z: .6 },
shininess: 50
};
var lightPosition = new THREE.Vector3(1.0, -0.5, 1.0);
var uniforms = {
textureCam: {
type: "t",
value: textureCam.renderTarget
},
lightPosition: {
type: "v3",
value: lightPosition
},
diffuseColor: {
type: "v3",
value: new THREE.Vector3(0.5, 0.5, 0.5)
},
mixValue: {
type: "f",
value: 0.1
},
};
var shaderMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: env_vs,
fragmentShader: env_fs
});
object = new THREE.Mesh(new THREE.SphereGeometry( 5, 64, 64 ), shaderMaterial);
object.scale.set(1,1,1);
object.position.set(10, 5, 10);
scene.add( object );
drawReflectedObjects();
drawSkybox();
}
function drawReflectedObjects(){
var donut = new THREE.Mesh(new THREE.TorusKnotGeometry( 10, 3, 100, 16 ), new THREE.MeshPhongMaterial({color : 0xFF0000}));
donut.position.set(15, 0, 0);
scene.add(donut);
var donut2 = new THREE.Mesh(new THREE.CylinderGeometry( 5, 5, 20, 32 ), new THREE.MeshPhongMaterial({color : 0x00FF00}));
donut2.position.set(-15, -5, 0);
scene.add(donut2);
}
function drawSkybox() {
var urls = [ "textures/dunes_right.jpg", "textures/dunes_left.jpg",
"textures/dunes_top.jpg", "textures/dunes_bottom.jpg",
"textures/dunes_front.jpg", "textures/dunes_back.jpg" ];
var textureCube = THREE.ImageUtils.loadTextureCube( urls );
var shader = THREE.ShaderLib[ "cube" ];
shader.uniforms[ "tCube" ].value = textureCube;
var material = new THREE.ShaderMaterial( {
fragmentShader: shader.fragmentShader,
vertexShader: shader.vertexShader,
uniforms: shader.uniforms,
depthWrite: false,
side: THREE.BackSide
} ),
mesh = new THREE.Mesh( new THREE.BoxGeometry( 1000, 1000, 1000 ), material );
scene.add( mesh );
}
function animate(){
requestAnimationFrame(animate);
render();
}
function render() {
var elapsed = clock.getElapsedTime() / 2.0;
object.position.z = Math.cos(elapsed) * 25;
object.position.x = Math.sin(elapsed) * 25;
// TODO: update Position of textureCam
textureCam.position.z = object.position.z;
textureCam.position.x = object.position.x;
// TODO: update CubeMap of textureCam
// don't forget to make the object invisible before
object.visible = false;
textureCam.updateCubeMap(renderer, scene);
object.visible = true;
renderer.render(scene, camera);
}
</script>
</body>
</html>