-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_cloud.mjs
226 lines (183 loc) · 7.42 KB
/
color_cloud.mjs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import * as THREE from 'three';
import ShaderLoader from './shader_loader.mjs';
export default class ColorCloud {
static SRGB = 0;
static xyY = 1;
static Lab = 2;
#shaderLoader;
#pointcloudmat;
#pointcloudshadowmat;
#points;
#pointshadows;
#isDensity;
#colorspace;
#group;
constructor(colorspace, density, vidMgr) {
this.#group = new THREE.Group();
this.#group.name = "Color Cloud";
this.#shaderLoader = new ShaderLoader();
this.#isDensity = density;
this.#colorspace = colorspace;
this.#createAxes();
this.#createBoundingBox();
this.#createGrid();
this.#createPointCloud();
vidMgr.addTextureUser(this, 'vidTex');
}
get cloudGroup() {
return this.#group;
}
set vidTex(tex) {
const numVerts = tex.userData.width * tex.userData.height;
const verts = new Float32Array(numVerts * 3);
if (this.#points.geometry) {
this.#points.geometry.dispose();
}
this.#points.geometry = new THREE.BufferGeometry();
this.#points.geometry.setAttribute('position', new THREE.BufferAttribute(verts, 3));
this.#pointshadows.geometry = this.#points.geometry;
this.#pointcloudmat.uniforms.tex.value = tex;
this.#pointcloudshadowmat.uniforms.tex.value = tex;
}
#createAxes() {
const axes = new THREE.Group();
const rMat = new THREE.MeshBasicMaterial({color: 0xaa0000});
const rAx = this.#makeAxis(rMat);
rAx.rotation.z = -Math.PI / 2;
if (this.#colorspace == ColorCloud.Lab) {
rAx.position.z = 0;
}
axes.add(rAx);
const gMat = new THREE.MeshBasicMaterial({color: 0x00aa00});
const gAx = this.#makeAxis(gMat);
if (this.#colorspace == ColorCloud.Lab) {
gAx.position.x = 0;
gAx.position.z = 0;
}
axes.add(gAx);
const bMat = new THREE.MeshBasicMaterial({color: 0x0000aa});
const bAx = this.#makeAxis(bMat);
bAx.rotation.x = Math.PI / 2;
if (this.#colorspace == ColorCloud.Lab) {
bAx.position.x = 0;
}
axes.add(bAx);
this.#group.add(axes);
}
#makeAxis(material) {
const cylinder = new THREE.CylinderGeometry(0.01, 0.01, 1);
const cylMesh = new THREE.Mesh(cylinder, material);
cylMesh.position.y = 0.5;
const cone = new THREE.ConeGeometry(0.03, 0.1);
const coneMesh = new THREE.Mesh(cone, material);
coneMesh.position.y = 1;
const group = new THREE.Group();
group.add(cylMesh);
group.add(coneMesh);
group.position.set(-0.5, -0.5, -0.5);
return group;
}
#createBoundingBox() {
const mat = new THREE.MeshBasicMaterial({
color: 0x333333,
transparent: true,
opacity: 0.5,
side: THREE.BackSide,
});
this.#group.add(new THREE.Mesh(new THREE.BoxGeometry(1.1, 1.1, 1.1), mat));
}
#createGrid() {
const extents = {dataMin: {}, dataMax: {}, spaceMin: {}, spaceMax:{}};
this.#setbounds(extents);
const step = (this.#colorspace == ColorCloud.Lab) ? 10 : 0.1;
const vertex_data = [];
const y = extents.dataMin.value.y;
const primary_axes = [0,2];
const secondary_axes = [2,0];
for (let ax = 0; ax < 2; ax++) {
const primary = primary_axes[ax];
const secondary = secondary_axes[ax];
const lineStart = extents.dataMin.value.toArray()[secondary];
const lineEnd = extents.dataMax.value.toArray()[secondary];
const negative = Math.ceil(extents.dataMin.value.toArray()[primary]/ step);
const positive = Math.floor(extents.dataMax.value.toArray()[primary]/step);
for (let i = negative; i <= positive; ++i) {
let p = [i*step,y,lineStart];
vertex_data.push(p[primary]);
vertex_data.push(p[1]);
vertex_data.push(p[secondary]);
p = [i*step, y, lineEnd];
vertex_data.push(p[primary]);
vertex_data.push(p[1]);
vertex_data.push(p[secondary]);
}
}
const vertices = Float32Array.from(vertex_data);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
const lines = new THREE.LineSegments(geometry, new THREE.LineBasicMaterial({color: 0xaaaaaa}));
lines.scale.copy(extents.spaceMax.value.clone().sub(extents.spaceMin.value).divide(extents.dataMax.value.clone().sub(extents.dataMin.value)));
lines.position.copy(extents.spaceMin.value.clone().sub(extents.dataMin.value.clone().multiply(lines.scale)));
this.#group.add(lines);
}
#createPointCloud() {
this.#pointcloudmat = new THREE.ShaderMaterial({
uniforms: {
tex: {value: null},
transparency: {value: this.#isDensity},
mode: {value: this.#colorspace},
isShadow: {value: false},
dataMin: {},
dataMax: {},
spaceMin: {},
spaceMax: {},
},
blending: (this.#isDensity) ? THREE.AdditiveBlending : THREE.NormalBlending,
depthTest: !this.#isDensity,
transparent: this.#isDensity,
});
this.#shaderLoader.load('./shaders/color_cloud.vert.glsl', './shaders/color_cloud.frag.glsl', this.#pointcloudmat);
this.#setbounds(this.#pointcloudmat.uniforms);
this.#points = new THREE.Points();
this.#points.material = this.#pointcloudmat;
this.#points.renderOrder = 2;
this.#pointcloudshadowmat = new THREE.ShaderMaterial({
uniforms: {
tex: {value: null},
transparency: {value: this.#isDensity},
mode: {value: this.#colorspace},
isShadow: {value: true},
dataMin: {},
dataMax: {},
spaceMin: {},
spaceMax: {},
},
depthTest: true,
depthWrite: false,
transparent: true,
});
this.#shaderLoader.load('./shaders/color_cloud.vert.glsl', './shaders/color_cloud.frag.glsl', this.#pointcloudshadowmat);
this.#setbounds(this.#pointcloudshadowmat.uniforms);
this.#pointshadows = new THREE.Points();
this.#pointshadows.material = this.#pointcloudshadowmat;
this.#pointshadows.renderOrder = 1;
this.#group.add(this.#points);
this.#group.add(this.#pointshadows);
}
#setbounds(uniforms) {
if (this.#colorspace === ColorCloud.SRGB) {
uniforms.dataMin.value = new THREE.Vector3(0,0,0);
uniforms.dataMax.value = new THREE.Vector3(1,1,1);
}
if (this.#colorspace === ColorCloud.xyY) {
uniforms.dataMin.value = new THREE.Vector3(0, 0, 0);
uniforms.dataMax.value = new THREE.Vector3(0.7, 1, 0.7);
}
if (this.#colorspace === ColorCloud.Lab) {
uniforms.dataMin.value = new THREE.Vector3(-128, 0, -128);
uniforms.dataMax.value = new THREE.Vector3(127, 100, 127);
}
uniforms.spaceMin.value = new THREE.Vector3(-0.5, -0.5, -0.5);
uniforms.spaceMax.value = new THREE.Vector3(0.5, 0.5, 0.5);
}
}