-
Notifications
You must be signed in to change notification settings - Fork 168
/
point-buffer.ts
190 lines (152 loc) · 4.53 KB
/
point-buffer.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
181
182
183
184
185
186
187
188
189
190
/**
* @file Point Buffer
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @private
*/
// @ts-ignore: unused import Vector3, Matrix4 required for declaration only
import { DataTexture, Vector3, Matrix4 } from 'three'
import '../shader/Point.vert'
import '../shader/Point.frag'
import { BufferRegistry } from '../globals'
import { defaults } from '../utils'
import { smoothstep } from '../math/math-utils'
import Buffer, { BufferDefaultParameters, BufferParameterTypes, BufferData, BufferTypes, BufferParameters } from './buffer'
function distance (x0: number, y0: number, x1: number, y1: number) {
const dx = x1 - x0
const dy = y1 - y0
return Math.sqrt(dx * dx + dy * dy)
}
interface PointTextureParams {
width?: number
height?: number
delta?: number
}
function makePointTexture (params: PointTextureParams) {
const p = params || {}
const width = defaults(p.width, 256)
const height = defaults(p.height, 256)
const center = [ width / 2, height / 2 ]
const radius = Math.min(width / 2, height / 2)
const delta = defaults(p.delta, 1 / (radius + 1)) * radius
let x = 0
let y = 0
const data = new Uint8Array(width * height * 4)
for (let i = 0, il = data.length; i < il; i += 4) {
const dist = distance(x, y, center[ 0 ], center[ 1 ])
const value = 1 - smoothstep(radius - delta, radius, dist)
data[ i ] = value * 255
data[ i + 1 ] = value * 255
data[ i + 2 ] = value * 255
data[ i + 3 ] = value * 255
if (++x === width) {
x = 0
y++
}
}
const tex = new DataTexture(data, width, height)
tex.needsUpdate = true
return tex
}
export const PointBufferDefaultParameters = Object.assign({
pointSize: 1,
sizeAttenuation: true,
sortParticles: false,
alphaTest: 0.5,
useTexture: false,
forceTransparent: false,
edgeBleach: 0.0
}, BufferDefaultParameters)
export type PointBufferParameters = BufferParameters & {
pointSize: number,
sizeAttenuation: boolean,
sortParticles: boolean,
alphaTest: number,
useTexture: boolean,
forceTransparent: boolean,
edgeBleach: number
}
const PointBufferParameterTypes = Object.assign({
pointSize: { uniform: 'size' },
sizeAttenuation: { updateShader: true },
sortParticles: {},
alphaTest: { updateShader: true },
useTexture: { updateShader: true },
forceTransparent: {},
edgeBleach: { uniform: true }
}, BufferParameterTypes)
/**
* Point buffer. Draws points. Optionally textured.
*
* @example
* var pointBuffer = new PointBuffer( {
* position: new Float32Array( [ 0, 0, 0 ] ),
* color: new Float32Array( [ 1, 0, 0 ] )
* } );
*/
class PointBuffer extends Buffer {
parameterTypes = PointBufferParameterTypes
get defaultParameters() { return PointBufferDefaultParameters }
parameters: PointBufferParameters
vertexShader = 'Point.vert'
fragmentShader ='Point.frag'
isPoint = true
tex: DataTexture
/**
* @param {Object} data - attribute object
* @param {Float32Array} data.position - positions
* @param {Float32Array} data.color - colors
* @param {BufferParameters} params - parameter object
*/
constructor (data: BufferData, params: Partial<PointBufferParameters> = {}) {
super(data, params)
this.addUniforms({
'size': { value: this.parameters.pointSize },
'canvasHeight': { value: 1.0 },
'pixelRatio': { value: 1.0 },
'map': { value: null }
})
}
makeMaterial () {
super.makeMaterial()
this.makeTexture()
const m = this.material
const wm = this.wireframeMaterial
const pm = this.pickingMaterial
m.uniforms.map.value = this.tex
m.needsUpdate = true
wm.uniforms.map.value = this.tex
wm.needsUpdate = true
pm.uniforms.map.value = this.tex
pm.needsUpdate = true
}
makeTexture () {
if (this.tex) this.tex.dispose()
this.tex = makePointTexture({ delta: this.parameters.edgeBleach })
}
getDefines (type?: BufferTypes) {
const defines = super.getDefines(type)
if (this.parameters.sizeAttenuation) {
defines.USE_SIZEATTENUATION = 1
}
if (this.parameters.useTexture) {
defines.USE_MAP = 1
}
if (this.parameters.alphaTest > 0 && this.parameters.alphaTest <= 1) {
defines.ALPHATEST = this.parameters.alphaTest.toPrecision(2)
}
return defines
}
setUniforms (data: any) {
if (data && data.edgeBleach !== undefined) {
this.makeTexture()
data.map = this.tex
}
super.setUniforms(data)
}
dispose () {
super.dispose()
if (this.tex) this.tex.dispose()
}
}
BufferRegistry.add('point', PointBuffer)
export default PointBuffer