-
Notifications
You must be signed in to change notification settings - Fork 0
/
StarBox.js
91 lines (76 loc) · 1.99 KB
/
StarBox.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
import CubeMap from "./gl/CubeMap.js";
import VAO from "./gl/Vao.js";
import Matrix4f from "./gl/Matrix4f.js";
import ShaderProgram from "./gl/ShaderProgram.js";
/** @type {HTMLImageElement} */
const starMap = await new Promise(resolve=>{
let img = document.createElement("img");
img.onload = ()=>resolve(img);
img.src = "./res/starmap_2020_4k.png";
});
/**
* Like a skybox, but with stars.
*/
export default class StarBox {
/**
* @param {WebGL2RenderingContext} gl
*/
constructor(gl){
this._gl = gl;
this._cube = VAO.createCube(gl);
this._shader = new StarBoxShader(gl);
this._cubeMap = CubeMap.fromEquirectangularProjection(this._gl,starMap);
this._gl.generateMipmap(this._gl.TEXTURE_CUBE_MAP);
this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP,this._gl.TEXTURE_MIN_FILTER,this._gl.LINEAR_MIPMAP_LINEAR);
}
/**
* @param {Matrix4f} viewMatrix
* @param {Matrix4f} projectionMatrix
*/
render(viewMatrix,projectionMatrix){
let viewDirection = viewMatrix.copy();
viewDirection.m03 = 0;
viewDirection.m13 = 0;
viewDirection.m23 = 0;
this._shader.use();
this._shader.uniforms.viewProjection = projectionMatrix.copy().mul(viewDirection);
this._cubeMap.bind();
this._cube.render();
}
/** @readonly */
get cubeMap(){
return this._cubeMap;
}
}
const vertexSource = /* glsl */`
#version 300 es
precision highp float;
in vec3 position;
out vec3 pass_position;
uniform mat4 viewProjection;
void main(void){
pass_position = position;
gl_Position = viewProjection*vec4(position,1.0);
}
`;
const fragmentSource = /* glsl */ `
#version 300 es
precision highp float;
in vec3 pass_position;
out vec4 out_color;
uniform samplerCube cubeMap;
void main(void){
out_color = texture(cubeMap,pass_position);
}
`;
/**
* @extends {ShaderProgram<{viewProjection:Matrix4f},{}>}
*/
class StarBoxShader extends ShaderProgram {
/**
* @param {WebGL2RenderingContext} gl
*/
constructor(gl){
super(gl,vertexSource,fragmentSource,{attribs:["position"],textures:["cubeMap"]});
}
}