-
Notifications
You must be signed in to change notification settings - Fork 1
/
linePlane.js
155 lines (131 loc) · 3.5 KB
/
linePlane.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
152
153
154
155
import {createBuffers, createProgram, createVertexArray, getAttribLocations} from "./webgl/webglHelper.js";
import {SceneObject} from "./webgl/SceneObject.js";
/**
* 平面描画に必要なオブジェクト群を作成します。
*
* @public
* @param {WebGL2RenderingContext} gl2
* @return {LinePlaneRenderProgramSet | null}
*/
export const createLinePlaneRenderProgramSet = (gl2) => {
// language=GLSL
const vertexShaderSource = `#version 300 es
in vec3 position;
in vec3 color;
out vec3 vColor;
uniform mat4 mMatrix;
uniform mat4 vpMatrix;
void main(void)
{
gl_Position = (vpMatrix * mMatrix) * vec4(position, 1.0);
vColor = color;
}
`;
// language=GLSL
const fragmentShaderSource = `#version 300 es
precision mediump float;
in vec3 vColor;
out vec4 outColor;
void main(void)
{
outColor = vec4(vColor, 1.0);
}
`;
const program = createProgram(gl2, vertexShaderSource, fragmentShaderSource);
if (!program) {
return null;
}
const uniformLocation = {
mMatrix: gl2.getUniformLocation(program, "mMatrix"),
vpMatrix: gl2.getUniformLocation(program, "vpMatrix"),
};
const verticesArr = [];
const indicesArr = [];
const numLines = 20;
const blue = 0.5
for (let i = 0; i < numLines; i++) {
const ratio = i / (numLines - 1);
const z = -0.5 + ratio;
verticesArr.push(
// x, y, z
-0.5, 0.0, z,
// r, g, b
0.0, ratio, blue);
verticesArr.push(
// x, y, z
0.5, 0.0, z,
// r, g, b
1.0, ratio, blue);
indicesArr.push(i * 4, i * 4 + 1);
verticesArr.push(
// x, y, z
z, 0.0, -0.5,
// r, g, b
ratio, 0.0, blue);
verticesArr.push(
// x, y, z
z, 0.0, 0.5,
// r, g, b
ratio, 1.0, blue);
indicesArr.push(i * 4 + 2, i * 4 + 3);
}
const vertices = new Float32Array(verticesArr);
const indices = new Uint16Array(indicesArr);
/** @type {GeometrySet} */
const geometrySet = {
attributeSetList: [{
attributeList: [
{
name: "position",
size: 3,
type: gl2.FLOAT,
divisor: -1,
locationIndex: undefined
},
{
name: "color",
size: 3,
type: gl2.FLOAT,
divisor: -1,
locationIndex: undefined
}
],
bufferSet: {
usage: gl2.STATIC_DRAW,
data: vertices,
buffer: undefined
}
}],
indices: {
usage: gl2.STATIC_DRAW,
data: indices,
buffer: undefined,
length: indices.length,
componentType: gl2.UNSIGNED_SHORT
}
};
geometrySet.attributeSetList.forEach(attributeSet => {
attributeSet.attributeList.forEach(attribute => {
attribute.locationIndex = gl2.getAttribLocation(program, attribute.name);
});
});
const locationIndexList = getAttribLocations(gl2, geometrySet, program);
createBuffers(gl2, geometrySet);
const vertexArray = createVertexArray(gl2, geometrySet, locationIndexList);
return {
program,
uniformLocation,
vertexArray,
geometrySet,
container: new SceneObject()
};
};
/**
* @typedef {Object} LinePlaneRenderProgramSet
*
* @property {WebGLProgram} program
* @property {UniformLocation} uniformLocation
* @property {WebGLVertexArrayObject} vertexArray
* @property {GeometrySet} geometrySet
* @property {SceneObject} container
*/