/
gltf-environment.js
84 lines (76 loc) · 2.3 KB
/
gltf-environment.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
import GL from '@luma.gl/constants';
import {Texture2D, TextureCube, loadImage} from '@luma.gl/webgl';
export default class GLTFEnvironment {
constructor(gl, {brdfLutUrl, getTexUrl}) {
this.gl = gl;
this.brdfLutUrl = brdfLutUrl;
this.getTexUrl = getTexUrl;
}
makeCube({id, getTextureForFace, parameters}) {
const pixels = {};
TextureCube.FACES.forEach(face => {
pixels[face] = getTextureForFace(face);
});
return new TextureCube(this.gl, {
id,
mipmaps: false,
parameters,
pixels
});
}
getDiffuseEnvSampler() {
if (!this._DiffuseEnvSampler) {
this._DiffuseEnvSampler = this.makeCube({
id: 'DiffuseEnvSampler',
getTextureForFace: dir => loadImage(this.getTexUrl('diffuse', dir, 0)),
parameters: {
[GL.TEXTURE_WRAP_S]: GL.CLAMP_TO_EDGE,
[GL.TEXTURE_WRAP_T]: GL.CLAMP_TO_EDGE,
[GL.TEXTURE_MIN_FILTER]: GL.LINEAR,
[GL.TEXTURE_MAG_FILTER]: GL.LINEAR
}
});
}
return this._DiffuseEnvSampler;
}
getSpecularEnvSampler() {
if (!this._SpecularEnvSampler) {
this._SpecularEnvSampler = this.makeCube({
id: 'SpecularEnvSampler',
getTextureForFace: dir => {
const imageArray = [];
for (let lod = 0; lod <= 9; lod++) {
imageArray.push(loadImage(this.getTexUrl('specular', dir, lod)));
}
return imageArray;
},
parameters: {
[GL.TEXTURE_WRAP_S]: GL.CLAMP_TO_EDGE,
[GL.TEXTURE_WRAP_T]: GL.CLAMP_TO_EDGE,
[GL.TEXTURE_MIN_FILTER]: GL.LINEAR_MIPMAP_LINEAR,
[GL.TEXTURE_MAG_FILTER]: GL.LINEAR
}
});
}
return this._SpecularEnvSampler;
}
getBrdfTexture() {
if (!this._BrdfTexture) {
this._BrdfTexture = new Texture2D(this.gl, {
id: 'brdfLUT',
parameters: {
[GL.TEXTURE_WRAP_S]: GL.CLAMP_TO_EDGE,
[GL.TEXTURE_WRAP_T]: GL.CLAMP_TO_EDGE,
[GL.TEXTURE_MIN_FILTER]: GL.LINEAR,
[GL.TEXTURE_MAG_FILTER]: GL.LINEAR
},
pixelStore: {
[this.gl.UNPACK_FLIP_Y_WEBGL]: false
},
// Texture2D accepts a promise that returns an image as data (Async Textures)
data: loadImage(this.brdfLutUrl)
});
}
return this._BrdfTexture;
}
}