-
Notifications
You must be signed in to change notification settings - Fork 30
/
scene.js
73 lines (59 loc) · 1.79 KB
/
scene.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
/*
Brings together the textures, mesh, and lights into a unified scene.
*/
var createTorus = require('./create-torus')
var createSphere = require('./create-sphere')
var createTexture = require('gl-texture2d')
var hex = require('hex-rgb')
var hex2rgb = (str) => {
return hex(str).map(x => x/255)
}
module.exports = function(gl, images) {
//the 3D objects for our scene
var mesh = createTorus(gl)
var sphere = createSphere(gl)
//upload our textures with mipmapping and repeat wrapping
var textures = images.map(image => {
var tex = createTexture(gl, image)
//setup smooth scaling
tex.bind()
tex.generateMipmap()
tex.minFilter = gl.LINEAR_MIPMAP_LINEAR
tex.magFilter = gl.LINEAR
//and repeat wrapping
tex.wrap = gl.REPEAT
//minimize distortion on hard angles
var ext = gl.getExtension('EXT_texture_filter_anisotropic')
if (ext) {
var maxAnistrophy = gl.getParameter(ext.MAX_TEXTURE_MAX_ANISOTROPY_EXT)
tex.bind()
gl.texParameterf(gl.TEXTURE_2D, ext.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(16, maxAnistrophy))
}
return tex
})
var [ diffuse, normal, specular ] = textures
var light = {
falloff: 0.15,
radius: 5,
position: [0, 0, 0],
color: hex2rgb('#ffc868'),
ambient: hex2rgb('#0a040b')
}
return function draw(time, camera) {
// move our light around
light.position[0] = -Math.sin(time/2)*0.9
light.position[1] = Math.sin(time/2)*0.3
light.position[2] = 0.5+Math.sin(time/2)*2
// bind our textures to the correct slots
diffuse.bind(0)
normal.bind(1)
specular.bind(2)
// draw our phong mesh
mesh.light = light
mesh.draw(camera)
// draw our light indicator
sphere.position = light.position
sphere.color = light.color
sphere.draw(camera)
}
}