forked from google/neuroglancer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment_color.ts
180 lines (154 loc) · 4.63 KB
/
segment_color.ts
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
/**
* @license
* Copyright 2016 Google Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { hashCombine } from "#/gpu_hash/hash_function";
import {
glsl_hashCombine,
HashMapShaderManager,
GPUHashTable,
} from "#/gpu_hash/shader";
import { HashTableBase } from "#/gpu_hash/hash_table";
import { hsvToRgb } from "#/util/colorspace";
import { NullarySignal } from "#/util/signal";
import { Uint64 } from "#/util/uint64";
import { GL } from "#/webgl/context";
import { ShaderBuilder, ShaderProgram } from "#/webgl/shader";
import { glsl_hsvToRgb, glsl_uint64 } from "#/webgl/shader_lib";
import { getRandomUint32 } from "./util/random";
import { Trackable } from "./util/trackable";
const NUM_COMPONENTS = 2;
export class SegmentColorShaderManager {
seedName = this.prefix + "_seed";
constructor(public prefix: string) {}
defineShader(builder: ShaderBuilder) {
const { seedName } = this;
builder.addUniform("highp uint", seedName);
builder.addFragmentCode(glsl_uint64);
builder.addFragmentCode(glsl_hashCombine);
builder.addFragmentCode(glsl_hsvToRgb);
let s = `
vec3 ${this.prefix}(uint64_t x) {
uint h = hashCombine(${seedName}, x);
vec${NUM_COMPONENTS} v;
`;
for (let i = 0; i < NUM_COMPONENTS; ++i) {
s += `
v[${i}] = float(h & 0xFFu) / 255.0;
h >>= 8u;
`;
}
s += `
vec3 hsv = vec3(v.x, 0.5 + v.y * 0.5, 1.0);
return hsvToRgb(hsv);
}
`;
builder.addFragmentCode(s);
}
enable(gl: GL, shader: ShaderProgram, segmentColorHash: number) {
gl.uniform1ui(shader.uniform(this.seedName), segmentColorHash);
}
}
const tempColor = new Float32Array(3);
export function getCssColor(color: Float32Array) {
return `rgb(${color[0] * 100}%,${color[1] * 100}%,${color[2] * 100}%)`;
}
export class SegmentColorHash implements Trackable {
changed = new NullarySignal();
constructor(public hashSeed: number = getRandomUint32()) {}
static getDefault() {
return new SegmentColorHash(0);
}
get value() {
return this.hashSeed;
}
set value(value: number) {
if (value !== this.hashSeed) {
this.hashSeed = value;
this.changed.dispatch();
}
}
compute(out: Float32Array, x: Uint64) {
let h = hashCombine(this.hashSeed, x.low);
h = hashCombine(h, x.high);
const c0 = (h & 0xff) / 255;
const c1 = ((h >> 8) & 0xff) / 255;
hsvToRgb(out, c0, 0.5 + 0.5 * c1, 1.0);
return out;
}
computeCssColor(x: Uint64) {
this.compute(tempColor, x);
return getCssColor(tempColor);
}
randomize() {
this.hashSeed = getRandomUint32();
this.changed.dispatch();
}
toString() {
return `new SegmentColorHash(${this.hashSeed})`;
}
toJSON() {
return this.hashSeed === 0 ? undefined : this.hashSeed;
}
reset() {
this.restoreState(0);
}
restoreState(x: any) {
const newSeed = x >>> 0;
if (newSeed !== this.hashSeed) {
this.hashSeed = newSeed;
this.changed.dispatch();
}
}
}
/**
* Adds the shader code to get a segment's color if it is present in the map.
*/
export class SegmentStatedColorShaderManager {
private hashMapShaderManager = new HashMapShaderManager(
"segmentStatedColorHash",
);
constructor(public prefix: string) {}
defineShader(builder: ShaderBuilder) {
this.hashMapShaderManager.defineShader(builder);
const s = `
bool ${this.getFunctionName}(uint64_t x, out vec4 value) {
uint64_t uint64Value;
if (${this.hashMapShaderManager.getFunctionName}(x, uint64Value)) {
uint uintValue = uint64Value.value[0];
value.r = float((uintValue & 0x0000ffu)) / 255.0;
value.g = float((uintValue & 0x00ff00u) >> 8) / 255.0;
value.b = float((uintValue & 0xff0000u) >> 16) / 255.0;
value.a = float((uintValue & 0xff000000u) >> 24) / 255.0;
return true;
}
return false;
}
`;
builder.addFragmentCode(s);
}
get getFunctionName() {
return `${this.prefix}_get`;
}
enable<HashTable extends HashTableBase>(
gl: GL,
shader: ShaderProgram,
hashTable: GPUHashTable<HashTable>,
) {
this.hashMapShaderManager.enable(gl, shader, hashTable);
}
disable(gl: GL, shader: ShaderProgram) {
this.hashMapShaderManager.disable(gl, shader);
}
}