forked from regl-project/regl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvmap.js
133 lines (124 loc) Β· 2.66 KB
/
envmap.js
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
/*
tags: advanced
<p>This example shows how you can render reflections with an environment map.</p>
*/
const regl = require('../regl')()
const mat4 = require('gl-mat4')
const bunny = require('bunny')
const normals = require('angle-normals')
const setupEnvMap = regl({
context: {
view: ({ tick }) => {
const t = 0.01 * tick
return mat4.lookAt([],
[30 * Math.cos(t), 2.5, 30 * Math.sin(t)],
[0, 2.5, 0],
[0, 1, 0])
}
},
frag: `
precision mediump float;
uniform samplerCube envmap;
varying vec3 reflectDir;
void main () {
gl_FragColor = textureCube(envmap, reflectDir);
}`,
uniforms: {
envmap: regl.prop('cube'),
view: regl.context('view'),
projection: ({ viewportWidth, viewportHeight }) =>
mat4.perspective([],
Math.PI / 4,
viewportWidth / viewportHeight,
0.01,
1000),
invView: ({ view }) => mat4.invert([], view)
}
})
const drawBackground = regl({
vert: `
precision mediump float;
attribute vec2 position;
uniform mat4 view;
varying vec3 reflectDir;
void main() {
reflectDir = (view * vec4(position, 1, 0)).xyz;
gl_Position = vec4(position, 0, 1);
}`,
attributes: {
position: [
-4, -4,
-4, 4,
8, 0]
},
depth: {
mask: false,
enable: false
},
count: 3
})
const drawBunny = regl({
vert: `
precision mediump float;
attribute vec3 position, normal;
uniform mat4 projection, view, invView;
varying vec3 reflectDir;
void main() {
vec4 eye = invView * vec4(0, 0, 0, 1);
reflectDir = reflect(
normalize(position.xyz - eye.xyz / eye.w),
normal);
gl_Position = projection * view * vec4(position, 1);
}`,
attributes: {
position: bunny.positions,
normal: normals(bunny.cells, bunny.positions)
},
elements: bunny.cells
})
require('resl')({
manifest: {
posx: {
type: 'image',
src: 'assets/posx.jpg'
},
negx: {
type: 'image',
src: 'assets/negx.jpg'
},
posy: {
type: 'image',
src: 'assets/posy.jpg'
},
negy: {
type: 'image',
src: 'assets/negy.jpg'
},
posz: {
type: 'image',
src: 'assets/posz.jpg'
},
negz: {
type: 'image',
src: 'assets/negz.jpg'
}
},
onDone: ({ posx, negx, posy, negy, posz, negz }) => {
const cube = regl.cube(
posx, negx,
posy, negy,
posz, negz)
regl.frame(() => {
setupEnvMap({ cube }, () => {
drawBackground()
drawBunny()
})
})
},
onProgress: (fraction) => {
const intensity = 1.0 - fraction
regl.clear({
color: [intensity, intensity, intensity, 1]
})
}
})