@geastack/gea3d 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,315 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ // Material subset. Lambert/Phong/Standard shade as lambert: per vertex when
3
+ // the geometry has normals (Gouraud), per face with flatShading or without
4
+ // normals. Vertex colours interpolate across each face. Parameter objects
5
+ // take hex numbers for color (not Color/string unions).
6
+
7
+ import { currentNativeFrame } from './cache'
8
+ import { Color } from './math'
9
+ import {
10
+ createMaterial,
11
+ deleteMaterial,
12
+ materialSet,
13
+ MATERIAL_ADDITIVE,
14
+ MATERIAL_DEPTH_TEST,
15
+ MATERIAL_DEPTH_WRITE,
16
+ MATERIAL_FLAT_SHADING,
17
+ MATERIAL_FOG,
18
+ MATERIAL_SIZE_ATTENUATION,
19
+ MATERIAL_TRANSPARENT,
20
+ MATERIAL_VERTEX_COLORS,
21
+ MATERIAL_VISIBLE,
22
+ MATERIAL_WIREFRAME,
23
+ } from '../native/api'
24
+
25
+ export const FrontSide = 0
26
+ export const BackSide = 1
27
+ export const DoubleSide = 2
28
+
29
+ // Blending modes (three.js values). Additive adds the colour (times opacity)
30
+ // to what is behind it, drawn after the opaque pass like transparent faces.
31
+ export const NoBlending = 0
32
+ export const NormalBlending = 1
33
+ export const AdditiveBlending = 2
34
+
35
+ export const SHADE_UNLIT = 0
36
+ export const SHADE_LAMBERT = 1
37
+ export const SHADE_NORMAL = 2
38
+
39
+ export interface MaterialParameters {
40
+ color?: number
41
+ vertexColors?: boolean
42
+ side?: number
43
+ transparent?: boolean
44
+ opacity?: number
45
+ blending?: number
46
+ fog?: boolean
47
+ flatShading?: boolean
48
+ wireframe?: boolean
49
+ depthTest?: boolean
50
+ depthWrite?: boolean
51
+ // PointsMaterial: size in pixels, or world units with sizeAttenuation.
52
+ size?: number
53
+ sizeAttenuation?: boolean
54
+ // LineBasicMaterial: pixels across (1 to 3).
55
+ linewidth?: number
56
+ }
57
+
58
+ export type MeshMaterialParameters = MaterialParameters
59
+ export type LineBasicMaterialParameters = MaterialParameters
60
+ export type PointsMaterialParameters = MaterialParameters
61
+ export type SpriteMaterialParameters = MaterialParameters
62
+
63
+ // The fields of a Material as last written to the native engine.
64
+ class SentMaterial {
65
+ valid: boolean
66
+ r: number
67
+ g: number
68
+ b: number
69
+ opacity: number
70
+ side: number
71
+ size: number
72
+ blending: number
73
+ vertexColors: boolean
74
+ transparent: boolean
75
+ visible: boolean
76
+ fog: boolean
77
+ flatShading: boolean
78
+ wireframe: boolean
79
+ depthTest: boolean
80
+ depthWrite: boolean
81
+ sizeAttenuation: boolean
82
+
83
+ constructor() {
84
+ this.valid = false
85
+ this.r = 0
86
+ this.g = 0
87
+ this.b = 0
88
+ this.opacity = 0
89
+ this.side = 0
90
+ this.size = 0
91
+ this.blending = 0
92
+ this.vertexColors = false
93
+ this.transparent = false
94
+ this.visible = false
95
+ this.fog = false
96
+ this.flatShading = false
97
+ this.wireframe = false
98
+ this.depthTest = false
99
+ this.depthWrite = false
100
+ this.sizeAttenuation = false
101
+ }
102
+
103
+ matches(m: Material, size: number): boolean {
104
+ const c = m.color
105
+ return (
106
+ this.valid &&
107
+ this.r === c.r && this.g === c.g && this.b === c.b &&
108
+ this.opacity === m.opacity && this.side === m.side && this.size === size && this.blending === m.blending &&
109
+ this.vertexColors === m.vertexColors && this.transparent === m.transparent && this.visible === m.visible &&
110
+ this.fog === m.fog && this.flatShading === m.flatShading && this.wireframe === m.wireframe &&
111
+ this.depthTest === m.depthTest && this.depthWrite === m.depthWrite && this.sizeAttenuation === m.sizeAttenuation
112
+ )
113
+ }
114
+
115
+ take(m: Material, size: number): void {
116
+ const c = m.color
117
+ this.valid = true
118
+ this.r = c.r
119
+ this.g = c.g
120
+ this.b = c.b
121
+ this.opacity = m.opacity
122
+ this.side = m.side
123
+ this.size = size
124
+ this.blending = m.blending
125
+ this.vertexColors = m.vertexColors
126
+ this.transparent = m.transparent
127
+ this.visible = m.visible
128
+ this.fog = m.fog
129
+ this.flatShading = m.flatShading
130
+ this.wireframe = m.wireframe
131
+ this.depthTest = m.depthTest
132
+ this.depthWrite = m.depthWrite
133
+ this.sizeAttenuation = m.sizeAttenuation
134
+ }
135
+ }
136
+
137
+ export class Material {
138
+ readonly color: Color
139
+ vertexColors: boolean
140
+ side: number
141
+ transparent: boolean
142
+ opacity: number
143
+ blending: number
144
+ visible: boolean
145
+ fog: boolean
146
+ flatShading: boolean
147
+ wireframe: boolean
148
+ depthTest: boolean
149
+ depthWrite: boolean
150
+ size: number
151
+ sizeAttenuation: boolean
152
+ linewidth: number
153
+ // Shading selector used by the renderer: 0 unlit, 1 lambert, 2 normal.
154
+ readonly shadeMode: number
155
+ // Native material id (-1 = not created yet).
156
+ nativeMaterial: number
157
+ private readonly sent: SentMaterial
158
+ private syncedFrame: number
159
+
160
+ constructor(shadeMode: number, params?: MaterialParameters) {
161
+ this.color = new Color()
162
+ this.vertexColors = false
163
+ this.side = FrontSide
164
+ this.transparent = false
165
+ this.opacity = 1
166
+ this.blending = NormalBlending
167
+ this.visible = true
168
+ this.fog = true
169
+ this.flatShading = false
170
+ this.wireframe = false
171
+ this.depthTest = true
172
+ this.depthWrite = true
173
+ this.size = 1
174
+ this.sizeAttenuation = true
175
+ this.linewidth = 1
176
+ this.shadeMode = shadeMode
177
+ this.nativeMaterial = -1
178
+ this.sent = new SentMaterial()
179
+ this.syncedFrame = -1
180
+ if (params !== undefined) this.setValues(params)
181
+ }
182
+
183
+ setValues(params: MaterialParameters): void {
184
+ const c = params.color
185
+ if (c !== undefined) this.color.setHex(c)
186
+ const vc = params.vertexColors
187
+ if (vc !== undefined) this.vertexColors = vc
188
+ const side = params.side
189
+ if (side !== undefined) this.side = side
190
+ const transparent = params.transparent
191
+ if (transparent !== undefined) this.transparent = transparent
192
+ const opacity = params.opacity
193
+ if (opacity !== undefined) this.opacity = opacity
194
+ const blending = params.blending
195
+ if (blending !== undefined) this.blending = blending
196
+ const fog = params.fog
197
+ if (fog !== undefined) this.fog = fog
198
+ const flatShading = params.flatShading
199
+ if (flatShading !== undefined) this.flatShading = flatShading
200
+ const wireframe = params.wireframe
201
+ if (wireframe !== undefined) this.wireframe = wireframe
202
+ const depthTest = params.depthTest
203
+ if (depthTest !== undefined) this.depthTest = depthTest
204
+ const depthWrite = params.depthWrite
205
+ if (depthWrite !== undefined) this.depthWrite = depthWrite
206
+ const size = params.size
207
+ if (size !== undefined) this.size = size
208
+ const sizeAttenuation = params.sizeAttenuation
209
+ if (sizeAttenuation !== undefined) this.sizeAttenuation = sizeAttenuation
210
+ const linewidth = params.linewidth
211
+ if (linewidth !== undefined) this.linewidth = linewidth
212
+ }
213
+
214
+ // Writes the material if it changed (at most once per frame); returns its
215
+ // native id.
216
+ syncNative(): number {
217
+ if (this.nativeMaterial < 0) {
218
+ this.nativeMaterial = createMaterial()
219
+ this.sent.valid = false
220
+ }
221
+ const frame = currentNativeFrame()
222
+ if (this.syncedFrame === frame) return this.nativeMaterial
223
+
224
+ this.syncedFrame = frame
225
+ const size = this.nativeSize()
226
+ if (this.sent.matches(this, size)) return this.nativeMaterial
227
+
228
+ const c = this.color
229
+ materialSet(this.nativeMaterial, this.shadeMode, c.r, c.g, c.b, this.opacity, this.side, this.nativeFlags(), size)
230
+ this.sent.take(this, size)
231
+ return this.nativeMaterial
232
+ }
233
+
234
+ // The native size slot: point size, or a line material's width.
235
+ protected nativeSize(): number {
236
+ return this.size
237
+ }
238
+
239
+ // Frees the native material; the next render recreates it.
240
+ dispose(): void {
241
+ if (this.nativeMaterial >= 0) deleteMaterial(this.nativeMaterial)
242
+ this.nativeMaterial = -1
243
+ this.syncedFrame = -1
244
+ }
245
+
246
+ private nativeFlags(): number {
247
+ let flags = 0
248
+ if (this.vertexColors) flags |= MATERIAL_VERTEX_COLORS
249
+ if (this.transparent) flags |= MATERIAL_TRANSPARENT
250
+ if (this.blending === AdditiveBlending) flags |= MATERIAL_ADDITIVE
251
+ if (this.fog) flags |= MATERIAL_FOG
252
+ if (this.flatShading) flags |= MATERIAL_FLAT_SHADING
253
+ if (this.wireframe) flags |= MATERIAL_WIREFRAME
254
+ if (this.depthTest) flags |= MATERIAL_DEPTH_TEST
255
+ if (this.depthWrite) flags |= MATERIAL_DEPTH_WRITE
256
+ if (this.sizeAttenuation) flags |= MATERIAL_SIZE_ATTENUATION
257
+ if (this.visible) flags |= MATERIAL_VISIBLE
258
+ return flags
259
+ }
260
+ }
261
+
262
+ export class MeshBasicMaterial extends Material {
263
+ constructor(params?: MaterialParameters) {
264
+ super(SHADE_UNLIT, params)
265
+ }
266
+ }
267
+
268
+ export class MeshLambertMaterial extends Material {
269
+ constructor(params?: MaterialParameters) {
270
+ super(SHADE_LAMBERT, params)
271
+ }
272
+ }
273
+
274
+ export class MeshPhongMaterial extends Material {
275
+ constructor(params?: MaterialParameters) {
276
+ super(SHADE_LAMBERT, params)
277
+ }
278
+ }
279
+
280
+ export class MeshStandardMaterial extends Material {
281
+ constructor(params?: MaterialParameters) {
282
+ super(SHADE_LAMBERT, params)
283
+ }
284
+ }
285
+
286
+ export class MeshNormalMaterial extends Material {
287
+ constructor(params?: MaterialParameters) {
288
+ super(SHADE_NORMAL, params)
289
+ }
290
+ }
291
+
292
+ // Unlit colour for Line and LineSegments, `linewidth` pixels across (1 to 3).
293
+ export class LineBasicMaterial extends Material {
294
+ constructor(params?: MaterialParameters) {
295
+ super(SHADE_UNLIT, params)
296
+ }
297
+
298
+ protected nativeSize(): number {
299
+ return this.linewidth
300
+ }
301
+ }
302
+
303
+ // Unlit colour for Points; `size` sets the square's extent.
304
+ export class PointsMaterial extends Material {
305
+ constructor(params?: MaterialParameters) {
306
+ super(SHADE_UNLIT, params)
307
+ }
308
+ }
309
+
310
+ // Unlit colour for Sprite.
311
+ export class SpriteMaterial extends Material {
312
+ constructor(params?: MaterialParameters) {
313
+ super(SHADE_UNLIT, params)
314
+ }
315
+ }