forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_simple_gi.html
255 lines (165 loc) · 6.05 KB
/
webgl_simple_gi.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - simple global illumination</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
margin: 0px;
background-color: #000000;
overflow: hidden;
}
a { color: #0078ff; }
#info {
position: absolute;
top: 5px;
width: 100%;
color:#fff;
font-family:Monospace;
font-size:13px;
font-weight: bold;
text-align: center;
}
</style>
</head>
<body>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - simple global illumination (<a href="http://www.iquilezles.org/www/articles/simplegi/simplegi.htm">article</a>)
</div>
<script src="../build/three.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script>
// HACK:
THREE.Mesh.prototype.clone = function () {
var newMaterial = ( this.material.isMaterial ) ? this.material.clone() : this.material.slice();
return new this.constructor( this.geometry.clone(), newMaterial ).copy( this );
};
//
var SimpleGI = function ( renderer, scene ) {
var SIZE = 32, SIZE2 = SIZE * SIZE;
var camera = new THREE.PerspectiveCamera( 90, 1, 0.01, 100 );
scene.updateMatrixWorld( true );
var clone = scene.clone();
clone.autoUpdate = false;
var rt = new THREE.WebGLRenderTarget( SIZE, SIZE, {
wrapS: THREE.ClampToEdgeWrapping,
wrapT: THREE.ClampToEdgeWrapping,
stencilBuffer: false,
depthBuffer: true
} );
var normalMatrix = new THREE.Matrix3();
var position = new THREE.Vector3();
var normal = new THREE.Vector3();
var bounces = 0;
var currentVertex = 0;
var color = new Float32Array( 3 );
var buffer = new Uint8Array( SIZE2 * 4 );
function compute() {
if ( bounces === 3 ) return;
var object = scene.children[ 0 ];
var geometry = object.geometry;
var attributes = geometry.attributes;
var positions = attributes.position.array;
var normals = attributes.normal.array;
if ( attributes.color === undefined ) {
var colors = new Float32Array( positions.length );
geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ).setDynamic( true ) );
}
var colors = attributes.color.array;
var startVertex = currentVertex;
var totalVertex = positions.length / 3;
for ( var i = 0; i < 32; i ++ ) {
if ( currentVertex >= totalVertex ) break;
position.fromArray( positions, currentVertex * 3 );
position.applyMatrix4( object.matrixWorld );
normal.fromArray( normals, currentVertex * 3 );
normal.applyMatrix3( normalMatrix.getNormalMatrix( object.matrixWorld ) ).normalize();
camera.position.copy( position );
camera.lookAt( position.add( normal ) );
renderer.setRenderTarget( rt );
renderer.render( clone, camera );
renderer.readRenderTargetPixels( rt, 0, 0, SIZE, SIZE, buffer );
color[ 0 ] = 0;
color[ 1 ] = 0;
color[ 2 ] = 0;
for ( var k = 0, kl = buffer.length; k < kl; k += 4 ) {
color[ 0 ] += buffer[ k + 0 ];
color[ 1 ] += buffer[ k + 1 ];
color[ 2 ] += buffer[ k + 2 ];
}
colors[ currentVertex * 3 + 0 ] = color[ 0 ] / ( SIZE2 * 255 );
colors[ currentVertex * 3 + 1 ] = color[ 1 ] / ( SIZE2 * 255 );
colors[ currentVertex * 3 + 2 ] = color[ 2 ] / ( SIZE2 * 255 );
currentVertex ++;
}
attributes.color.updateRange.offset = startVertex * 3;
attributes.color.updateRange.count = ( currentVertex - startVertex ) * 3;
attributes.color.needsUpdate = true;
if ( currentVertex >= totalVertex ) {
clone = scene.clone();
clone.autoUpdate = false;
bounces ++;
currentVertex = 0;
}
requestAnimationFrame( compute );
}
requestAnimationFrame( compute );
};
//
var camera, scene, renderer;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
camera.position.z = 4;
scene = new THREE.Scene();
// sphere
var geometry = new THREE.TorusKnotBufferGeometry( 0.75, 0.3, 128, 32, 1 );
// var geometry = new THREE.BoxBufferGeometry( 1, 1, 1, 10, 10, 10 )
var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.VertexColors } );
var mesh = new THREE.Mesh( geometry, material );
// mesh.position.y = 1;
scene.add( mesh );
/*
var geometry = new THREE.SphereBufferGeometry( 0.5, 16, 8 );
for ( var i = 0; i < 10; i ++ ) {
var material = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, side: THREE.DoubleSide } );
var mesh = new THREE.Mesh( geometry, material );
mesh.position.x = Math.random() * 3 - 1.5;
mesh.position.y = Math.random() * 3 - 1.5;
mesh.position.z = Math.random() * 3 - 1.5;
mesh.updateMatrix();
scene.add( mesh );
}
*/
// room
var materials = [];
for ( var i = 0; i < 8; i ++ ) {
materials.push( new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, side: THREE.BackSide } ) );
}
var geometry = new THREE.BoxBufferGeometry( 3, 3, 3 );
var mesh = new THREE.Mesh( geometry, materials );
scene.add( mesh );
//
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
new SimpleGI( renderer, scene );
var controls = new THREE.OrbitControls( camera );
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
renderer.setRenderTarget( null );
renderer.render( scene, camera );
}
</script>
</body>
</html>