/
proceduralRoundingCube.js
105 lines (87 loc) · 2.84 KB
/
proceduralRoundingCube.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
import { ProceduralCube } from './proceduralCube';
import { ArrayBuffer, Program } from 'tubugl-core';
import { vec3 } from 'gl-matrix/src/gl-matrix';
import {
base2ShaderVertSrc,
base2ShaderFragSrc,
normalShaderFragSrc,
normalShaderVertSrc
} from './shaders/base.shader';
export class ProceduralRoundingCube extends ProceduralCube {
constructor(
gl,
params = {},
width = 100,
height = 100,
depth = 100,
roundness = 2,
widthSegments = 1,
heightSegments = 1,
depthSegments = 1
) {
params.roundness = roundness;
super(gl, params, width, height, depth, widthSegments, heightSegments, depthSegments);
}
getNormals() {
return this._normalBuffer.dataArray;
}
_makeBuffers(params) {
super._makeBuffers();
let positionArray = this._positionBuffer.dataArray;
let normals = [];
let normal = vec3.create();
let inner = vec3.create();
let roundness = params.roundness;
for (let ii = 0; ii < positionArray.length / 3; ii++) {
let xx = positionArray[3 * ii];
let yy = positionArray[3 * ii + 1];
let zz = positionArray[3 * ii + 2];
// vec3.set(normal, xx, yy, zz);
vec3.set(inner, xx, yy, zz);
if (xx < -this._width / 2 + roundness) {
inner[0] = -this._width / 2 + roundness;
} else if (xx > this._width / 2 - roundness) {
inner[0] = this._width / 2 - roundness;
}
if (yy < -this._height / 2 + roundness) {
inner[1] = -this._height / 2 + roundness;
} else if (yy > this._height / 2 - roundness) {
inner[1] = this._height / 2 - roundness;
}
if (zz < -this._depth / 2 + roundness) {
inner[2] = -this._depth / 2 + roundness;
} else if (zz > this._depth / 2 - roundness) {
inner[2] = this._depth / 2 - roundness;
}
vec3.set(normal, xx - inner[0], yy - inner[1], zz - inner[2]);
vec3.normalize(normal, normal);
positionArray[3 * ii] = inner[0] + normal[0] * roundness;
positionArray[3 * ii + 1] = inner[1] + normal[1] * roundness;
positionArray[3 * ii + 2] = inner[2] + normal[2] * roundness;
normals.push(normal[0]);
normals.push(normal[1]);
normals.push(normal[2]);
}
this._positionBuffer.bind().setData(positionArray);
this._normalBuffer = new ArrayBuffer(this._gl, new Float32Array(normals));
this._normalBuffer.setAttribs('normal', 3);
}
_updateAttributes() {
if (this._vao) {
this._vao.bind();
} else {
this._positionBuffer.bind().attribPointer(this._program);
this._normalBuffer.bind().attribPointer(this._program);
this._indexBuffer.bind();
}
}
_makeProgram(params) {
const vertexShaderSrc = params.vertexShaderSrc
? params.vertexShaderSrc
: this._isGl2 ? base2ShaderVertSrc : normalShaderVertSrc;
const fragmentShaderSrc = params.fragmentShaderSrc
? params.fragmentShaderSrc
: this._isGl2 ? base2ShaderFragSrc : normalShaderFragSrc;
this._program = new Program(this._gl, vertexShaderSrc, fragmentShaderSrc);
}
}