-
Notifications
You must be signed in to change notification settings - Fork 0
/
skybox.js
151 lines (119 loc) · 5.23 KB
/
skybox.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const glmatrix = require('gl-matrix')
const vec3 = glmatrix.vec3
const vec4 = glmatrix.vec4
const mat4 = glmatrix.mat4
const geometry = require('./geometry')
const glutils = require('./glutils')
function Skybox(camera, size, environment) {
const cube = geometry.createSkybox(size)
let positionBuffer = null
let program = null
let attributes = null
let texture = null
this.initialize = (context, content) => {
program = content.programs['skybox']
positionBuffer = glutils.createArrayBuffer(context,
program,
new Float32Array(cube.vertices),
(context, program) => context.getAttribLocation(program, 'a_position'),
3)
attributes = {
'u_view': context.getUniformLocation(program, 'u_view'),
'u_reflection_view': context.getUniformLocation(program, 'u_reflection_view'),
'u_world': context.getUniformLocation(program, 'u_world'),
'u_projection': context.getUniformLocation(program, 'u_projection'),
'u_reflectionClipPlane': context.getUniformLocation(program, 'u_reflectionClipPlane'),
'u_enableReflectionClipping': context.getUniformLocation(program, 'u_enableReflectionClipping')
}
texture = createAndBindTexture(context, content)
}
const createAndBindTexture = (context, content) => {
let targets = { }
targets[context.TEXTURE_CUBE_MAP_POSITIVE_X] = content.resources['skybox_xp'].content
targets[context.TEXTURE_CUBE_MAP_NEGATIVE_X] = content.resources['skybox_xn'].content
targets[context.TEXTURE_CUBE_MAP_POSITIVE_Y] = content.resources['skybox_yp'].content
targets[context.TEXTURE_CUBE_MAP_NEGATIVE_Y] = content.resources['skybox_yn'].content
targets[context.TEXTURE_CUBE_MAP_POSITIVE_Z] = content.resources['skybox_zp'].content
targets[context.TEXTURE_CUBE_MAP_NEGATIVE_Z] = content.resources['skybox_zn'].content
texture = context.createTexture()
context.bindTexture(context.TEXTURE_CUBE_MAP, texture)
context.texParameteri(context.TEXTURE_CUBE_MAP, context.TEXTURE_MIN_FILTER, context.LINEAR)
context.texParameteri(context.TEXTURE_CUBE_MAP, context.TEXTURE_MAG_FILTER, context.LINEAR)
context.texParameteri(context.TEXTURE_CUBE_MAP, context.TEXTURE_WRAP_S, context.CLAMP_TO_EDGE)
context.texParameteri(context.TEXTURE_CUBE_MAP, context.TEXTURE_WRAP_T, context.CLAMP_TO_EDGE)
Object.keys(targets).forEach(key => {
context.texImage2D(key, 0, context.RGBA, context.RGBA, context.UNSIGNED_BYTE, targets[key])
})
context.generateMipmap(context.TEXTURE_CUBE_MAP)
return texture
}
this.update = (context, time) => {
context.useProgram(program)
const world = mat4.create()
const translation = mat4.create()
const position = camera.getPosition()
mat4.translate(translation, translation, position)
mat4.multiply(world, world, translation)
context.uniformMatrix4fv(attributes['u_view'],
false,
camera.getView())
context.uniformMatrix4fv(attributes['u_reflection_view'],
false,
getReflectionView())
context.uniformMatrix4fv(attributes['u_world'],
false,
world)
context.uniformMatrix4fv(attributes['u_projection'],
false,
camera.getProjection(context))
context.uniform4fv(attributes['u_reflectionClipPlane'],
vec4.fromValues(0.0, -1.0, 0.0, 1.0*environment.waterHeight))
}
const getReflectionView = () => {
const wh = environment.waterHeight
const position = camera.getPosition()
position[1] = -1.0*position[1]+wh*2.0
const target = camera.getTarget()
target[1] = -1.0*target[1]+wh*2.0
const right = vec3.transformMat4(vec3.create(),
vec3.fromValues(1.0, 0.0, 0.0),
camera.getRotation())
let up = vec3.cross(vec3.create(),
right,
vec3.subtract(vec3.create(), target, position))
vec3.normalize(up, up)
return mat4.lookAt(mat4.create(),
position,
target,
up)
}
this.draw = (context, time) => {
this.drawSkybox(context, time)
}
this.drawSkybox = (context, time) => {
context.useProgram(program)
context.uniform1f(attributes['u_enableReflectionClipping'], 0)
drawScene(context, time)
}
this.drawReflection = (context, time) => {
context.useProgram(program)
context.uniform1f(attributes['u_enableReflectionClipping'], 1)
drawScene(context, time)
}
const drawScene = (context, time) => {
// HACK: keeping CULL_FACE disabled (it
// prevents skybox rendering)
context.disable(context.CULL_FACE)
context.disable(context.DEPTH_TEST)
context.depthMask(false)
context.bindTexture(context.TEXTURE_CUBE_MAP, texture)
positionBuffer.bind(context)
context.drawArrays(context.TRIANGLES,
0,
cube.vertices.length/3)
context.depthMask(true)
context.enable(context.DEPTH_TEST)
context.enable(context.CULL_FACE)
}
}
module.exports = Skybox