-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram.mjs
180 lines (145 loc) · 5.54 KB
/
histogram.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
import * as THREE from 'three';
import ShaderLoader from './shader_loader.mjs';
export default class Histogram {
#loader;
#histogrambuckets;
#bucketsdownsample;
#bucketmaterial;
#downsamplematerial;
#histogrammaterial;
#histogroup;
#offscreenscene;
#offscreencamera;
#quad;
#points;
constructor(vidmgr) {
this.#loader = new ShaderLoader();
this.#createRenderTargets();
this.#loadBucketingMaterial();
this.#loadDownsampleMaterial();
this.#loadHistogramMaterial();
this.#createScene();
this.#createGroup();
vidmgr.addTextureUser(this, 'videoTex');
}
get histogramGroup() {
return this.#histogroup;
}
set videoTex(tex) {
console.log(tex.userData.width);
console.log(tex.userData.height);
let num_pixels = tex.userData.height * tex.userData.width;
let geometry = new THREE.BufferGeometry();
const vertices = new Float32Array(num_pixels * 3);
geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
if (this.#points) {
this.#offscreenscene.remove(this.#points);
this.#points.geometry.dispose();
}
this.#points = new THREE.Points(geometry, this.#bucketmaterial);
this.#points.layers.set(1);
this.#offscreenscene.add(this.#points);
this.#bucketmaterial.uniforms.tex.value = tex;
}
runProcessing(renderer) {
const clearColor = renderer.getClearColor(new THREE.Color());
renderer.setClearColor(new THREE.Color(0,0,0));
if (this.#points) {
this.#computeBuckets(renderer);
this.#downsample(renderer);
}
renderer.setClearColor(clearColor);
}
#computeBuckets(renderer) {
renderer.setRenderTarget(this.#histogrambuckets);
renderer.clear();
this.#offscreencamera.layers.set(1);
for (let i = 0; i < 3; i++) {
const c_array = [0,0,0];
c_array[i] = 1;
this.#bucketmaterial.uniforms.color.value.fromArray(c_array);
renderer.render(this.#offscreenscene, this.#offscreencamera);
}
renderer.setRenderTarget(null);
}
#downsample(renderer) {
this.#offscreencamera.layers.set(2);
for (let i = 3; i >= 0; i--) {
renderer.setRenderTarget(this.#bucketsdownsample[i]);
if (i == 3) {
this.#downsamplematerial.uniforms.tex.value = this.#histogrambuckets.texture;
}
else {
this.#downsamplematerial.uniforms.tex.value = this.#bucketsdownsample[i+1].texture;
}
renderer.clear();
renderer.render(this.#offscreenscene, this.#offscreencamera);
}
renderer.setRenderTarget(null);
}
#createRenderTargets() {
this.#histogrambuckets = new THREE.WebGLRenderTarget(256, 1, {
magFilter: THREE.NearestFilter,
minFilter: THREE.NearestFilter,
type: THREE.FloatType
});
this.#bucketsdownsample = [];
for (let i = 0; i < 4; i++) {
this.#bucketsdownsample.push(this.#histogrambuckets.clone());
this.#bucketsdownsample[i].setSize(Math.pow(4, i), 1);
}
}
#loadBucketingMaterial() {
this.#bucketmaterial = new THREE.ShaderMaterial({
uniforms: {
color: { value: new THREE.Color(1.,0.,0.) },
tex: {value: null},
},
blending: THREE.CustomBlending,
blendEquation: THREE.AddEquation,
blendSrc: THREE.OneFactor,
blendDst: THREE.OneFactor,
depthTest: false,
depthWrite: false,
});
this.#loader.load('./shaders/count_buckets.vert.glsl', './shaders/count_buckets.frag.glsl', this.#bucketmaterial);
}
#loadDownsampleMaterial() {
this.#downsamplematerial = new THREE.ShaderMaterial({
uniforms: {
tex: {value: null},
}
});
this.#loader.load('./shaders/pass_through.vert.glsl', './shaders/maxpool_x4.frag.glsl', this.#downsamplematerial);
}
#loadHistogramMaterial() {
this.#histogrammaterial = new THREE.ShaderMaterial({
uniforms: {
histo_buckets: {value: this.#histogrambuckets.texture},
max_val: {value: this.#bucketsdownsample[0].texture},
}
});
this.#loader.load('./shaders/basic.vert.glsl', './shaders/render_histo.frag.glsl', this.#histogrammaterial);
}
#createScene() {
this.#offscreenscene = new THREE.Scene();
// processing rendering ignored camera parameters, just need one to make three.js happy
this.#offscreencamera = new THREE.OrthographicCamera(0,1,0,1,0,1);
this.#offscreenscene.add(this.#offscreencamera);
this.#quad = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), this.#downsamplematerial);
this.#quad.layers.set(2);
this.#offscreenscene.add(this.#quad);
}
#createGroup() {
this.#histogroup = new THREE.Group();
this.#histogroup.name = "Histogram";
this.#histogroup.add(new THREE.Mesh(new THREE.PlaneGeometry(2,2), this.#histogrammaterial));
const background = new THREE.Mesh(new THREE.PlaneGeometry(2.2, 2.2), new THREE.MeshBasicMaterial({
color: 0x333333,
transparent: true,
opacity: 0.5,
}));
background.position.z = -0.01;
this.#histogroup.add(background);
}
}