-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathno-box-skybox.html
224 lines (175 loc) · 6.67 KB
/
no-box-skybox.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
220
221
222
223
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - materials - no box skybox</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
html, body {
background: #000;
color: #fff;
margin: 0;
overflow: hidden;
font-family: monospace;
text-align: center;
width: 100%;
height: 100%;
}
a {
color: #ff0080;
text-decoration: none;
}
a:hover {
color: #0080ff;
}
canvas {
pointer-events: none;
width: 100%;
height: 100%;
}
#d {
text-align: center;
margin: 1em 0 -7.5em 0;
z-index: 1000;
position: absolute;
top: 0;
display:block;
width: 100%;
}
.button { background:orange; color:#fff; padding:0.2em 0.5em; cursor:pointer }
.inactive { background:#999; color:#eee }
</style>
</head>
<body>
<div id="d">
<p><a href="http://threejs.org">Three.js</a> no box sky box (from <a href="http://twgljs.org/examples/no-box-skybox.html">twgl.js example</a>)</p>
<p>Texture by <a href="http://www.humus.name/index.php?page=Textures" target="_blank">Humus</a></p>
</div>
<script src="js/three.75.min.js"></script>
<script id="vs" type="notjs">
varying vec4 v_position;
void main() {
v_position = vec4(position, 1);
gl_Position = vec4(position, 1);
}
</script>
<script id="fs" type="notjs">
precision mediump float;
uniform samplerCube skybox;
uniform mat4 viewDirectionProjectionInverse;
varying vec4 v_position;
void main() {
vec4 t = viewDirectionProjectionInverse * v_position;
gl_FragColor = textureCube(skybox, normalize(t.xyz / t.w));
}
</script>
<script>
var mouseX = 0;
var mouseY = 0;
var camera = new THREE.PerspectiveCamera( 50, 1, 1, 5000 );
camera.position.z = 2000;
var cameraCube = new THREE.PerspectiveCamera( 50, 1, 1, 100 );
var scene = new THREE.Scene();
var sceneCube = new THREE.Scene();
// LIGHTS
var ambient = new THREE.AmbientLight(0xffffff);
scene.add(ambient);
var pointLight = new THREE.PointLight(0xffffff, 2);
scene.add(pointLight);
// light representation
var sphere = new THREE.SphereGeometry(100, 16, 8);
var mesh = new THREE.Mesh(sphere, new THREE.MeshBasicMaterial({ color: 0xffaa00 }));
mesh.scale.set(0.05, 0.05, 0.05);
pointLight.add(mesh);
var path = "js/three/textures/cube/SwedishRoyalCastle/";
var format = '.jpg';
var urls = [
path + 'px' + format, path + 'nx' + format,
path + 'py' + format, path + 'ny' + format,
path + 'pz' + format, path + 'nz' + format
];
var reflectionCube = new THREE.CubeTextureLoader().load( urls );
reflectionCube.format = THREE.RGBFormat;
var refractionCube = new THREE.CubeTextureLoader().load(urls);
refractionCube.mapping = THREE.CubeRefractionMapping;
refractionCube.format = THREE.RGBFormat;
// no box sky box
var viewDirectionProjectionInverse = new THREE.Matrix4();
var geometry = new THREE.PlaneBufferGeometry(2, 2);
var uniforms = {
skybox: { type: "t", value: reflectionCube },
viewDirectionProjectionInverse: { type: "m4", value: viewDirectionProjectionInverse },
};
var material = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: document.getElementById( 'vs' ).text,
fragmentShader: document.getElementById( 'fs' ).text,
depthWrite: false,
});
var mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
var cubeMaterial3 = new THREE.MeshLambertMaterial({ color: 0xff6600, envMap: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 });
var cubeMaterial2 = new THREE.MeshLambertMaterial({ color: 0xffee00, envMap: refractionCube, refractionRatio: 0.95 });
var cubeMaterial1 = new THREE.MeshLambertMaterial({ color: 0xffffff, envMap: reflectionCube });
//
var renderer = new THREE.WebGLRenderer();
renderer.autoClear = false;
document.body.insertBefore(renderer.domElement, document.body.firstChild);
//
var geometry = new THREE.SphereGeometry(30, 30, 30);
var s = 15;
mesh = new THREE.Mesh(geometry, cubeMaterial1);
mesh.position.z = - 100;
mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
scene.add(mesh);
mesh = new THREE.Mesh(geometry, cubeMaterial2);
mesh.position.x = - 900;
mesh.position.z = - 100;
mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
scene.add(mesh);
mesh = new THREE.Mesh(geometry, cubeMaterial3);
mesh.position.x = 900;
mesh.position.z = - 100;
mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
scene.add(mesh);
document.addEventListener('mousemove', onDocumentMouseMove);
function resize() {
var canvas = renderer.domElement;
var width = canvas.clientWidth * window.devicePixelRatio;
var height = canvas.clientHeight * window.devicePixelRatio;
if (canvas.width !== width || canvas.height !== height) {
renderer.setSize(width, height, false);
aspect = width / height;
camera.aspect = aspect;
camera.updateProjectionMatrix();
cameraCube.aspect = aspect;
cameraCube.updateProjectionMatrix();
}
}
function onDocumentMouseMove(event) {
mouseX = (event.clientX - event.target.clientWidth / 2) * 4;
mouseY = (event.clientY - event.target.clientHeight / 2) * 4;
}
function animate(time) {
render(time);
requestAnimationFrame(animate);
}
animate(0);
function render(time) {
var timer = -0.0002 * time;
resize();
pointLight.position.x = 1500 * Math.cos(timer);
pointLight.position.z = 1500 * Math.sin(timer);
camera.position.x += (mouseX - camera.position.x) * .05;
camera.position.y += (- mouseY - camera.position.y) * .05;
camera.lookAt(scene.position);
cameraCube.rotation.copy(camera.rotation);
cameraCube.matrixWorldInverse.getInverse( cameraCube.matrixWorld );
viewDirectionProjectionInverse.multiplyMatrices( cameraCube.projectionMatrix, cameraCube.matrixWorldInverse );
viewDirectionProjectionInverse.getInverse(viewDirectionProjectionInverse);
renderer.render(sceneCube, cameraCube);
renderer.render(scene, camera);
}
</script>
</body>
</html>