-
Notifications
You must be signed in to change notification settings - Fork 208
/
Mesh.ts
590 lines (480 loc) · 25.5 KB
/
Mesh.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2019 Bentley Systems, Incorporated. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license terms.
*--------------------------------------------------------------------------------------------*/
/** @module WebGL */
import { dispose, assert } from "@bentley/bentleyjs-core";
import { Point3d } from "@bentley/geometry-core";
import { SurfaceFlags, RenderPass, RenderOrder } from "./RenderFlags";
import { LUTGeometry, PolylineBuffers, CachedGeometry } from "./CachedGeometry";
import { VertexIndices, SurfaceType, MeshParams, SegmentEdgeParams, SilhouetteParams, TesselatedPolyline } from "../primitives/VertexTable";
import { LineCode } from "./EdgeOverrides";
import { ColorInfo } from "./ColorInfo";
import { Graphic, Batch } from "./Graphic";
import { VertexLUT } from "./VertexLUT";
import { Primitive } from "./Primitive";
import { FloatRgba } from "./FloatRGBA";
import { ShaderProgramParams, RenderCommands } from "./DrawCommand";
import { Target } from "./Target";
import { createMaterialInfo, MaterialInfo } from "./Material";
import { Texture } from "./Texture";
import { FeatureIndexType, FillFlags, RenderMode, LinePixels, ViewFlags } from "@bentley/imodeljs-common";
import { System } from "./System";
import { BufferHandle, BuffersContainer, BufferParameters } from "./Handle";
import { GL } from "./GL";
import { TechniqueId } from "./TechniqueId";
import { InstancedGraphicParams, RenderMemory } from "../System";
import { InstanceBuffers } from "./InstancedGeometry";
import { AttributeMap } from "./AttributeMap";
import { WebGlDisposable } from "./Disposable";
/** @internal */
export class MeshData implements WebGlDisposable {
public readonly edgeWidth: number;
public readonly hasFeatures: boolean;
public readonly uniformFeatureId?: number; // Used strictly by BatchPrimitiveCommand.computeisFlashed for flashing volume classification primitives.
public readonly texture?: Texture;
public readonly materialInfo?: MaterialInfo;
public readonly type: SurfaceType;
public readonly fillFlags: FillFlags;
public readonly edgeLineCode: number; // Must call LineCode.valueFromLinePixels(val: LinePixels) and set the output to edgeLineCode
public readonly isPlanar: boolean;
public readonly hasBakedLighting: boolean;
public readonly hasFixedNormals: boolean; // Fixed normals will not be flipped to face front (Terrain skirts).
public readonly lut: VertexLUT;
public readonly viewIndependentOrigin?: Point3d;
private constructor(lut: VertexLUT, params: MeshParams, viOrigin: Point3d | undefined) {
this.lut = lut;
this.viewIndependentOrigin = viOrigin;
this.hasFeatures = FeatureIndexType.Empty !== params.vertices.featureIndexType;
if (FeatureIndexType.Uniform === params.vertices.featureIndexType)
this.uniformFeatureId = params.vertices.uniformFeatureID;
this.texture = params.surface.texture as Texture;
this.materialInfo = createMaterialInfo(params.surface.material);
this.type = params.surface.type;
this.fillFlags = params.surface.fillFlags;
this.isPlanar = params.isPlanar;
this.hasBakedLighting = params.surface.hasBakedLighting;
this.hasFixedNormals = params.surface.hasFixedNormals;
const edges = params.edges;
this.edgeWidth = undefined !== edges ? edges.weight : 1;
this.edgeLineCode = LineCode.valueFromLinePixels(undefined !== edges ? edges.linePixels : LinePixels.Solid);
}
public static create(params: MeshParams, viOrigin: Point3d | undefined): MeshData | undefined {
const lut = VertexLUT.createFromVertexTable(params.vertices, params.auxChannels);
return undefined !== lut ? new MeshData(lut, params, viOrigin) : undefined;
}
public get isDisposed(): boolean { return undefined === this.texture && this.lut.isDisposed; }
public dispose() {
dispose(this.lut);
if (this._ownsTexture)
this.texture!.dispose();
}
// Returns true if no one else owns this texture. Implies that the texture should be disposed when this object is disposed, and the texture's memory should be tracked as belonging to this object.
private get _ownsTexture(): boolean {
return undefined !== this.texture && undefined === this.texture.key && !this.texture.isOwned;
}
public collectStatistics(stats: RenderMemory.Statistics): void {
stats.addVertexTable(this.lut.bytesUsed);
if (this._ownsTexture)
stats.addTexture(this.texture!.bytesUsed);
}
}
/** @internal */
export class MeshGraphic extends Graphic {
public readonly meshData: MeshData;
private readonly _primitives: Primitive[] = [];
private readonly _instances?: InstanceBuffers;
public static create(params: MeshParams, instancesOrVIOrigin?: InstancedGraphicParams | Point3d): MeshGraphic | undefined {
const viOrigin = instancesOrVIOrigin instanceof Point3d ? instancesOrVIOrigin : undefined;
const instances = undefined === viOrigin ? instancesOrVIOrigin as InstancedGraphicParams : undefined;
const buffers = undefined !== instances ? InstanceBuffers.create(instances, true) : undefined;
if (undefined === buffers && undefined !== instances)
return undefined;
const data = MeshData.create(params, viOrigin);
return undefined !== data ? new MeshGraphic(data, params, buffers) : undefined;
}
private addPrimitive(createGeom: () => CachedGeometry | undefined, instances?: InstanceBuffers) {
const primitive = Primitive.createShared(createGeom, instances);
if (undefined !== primitive)
this._primitives.push(primitive);
}
private constructor(data: MeshData, params: MeshParams, instances?: InstanceBuffers) {
super();
this.meshData = data;
this._instances = instances;
this.addPrimitive(() => SurfaceGeometry.create(this.meshData, params.surface.indices), instances);
// Classifiers are surfaces only...no edges.
if (this.surfaceType === SurfaceType.VolumeClassifier || undefined === params.edges)
return;
const edges = params.edges;
if (undefined !== edges.silhouettes)
this.addPrimitive(() => SilhouetteEdgeGeometry.createSilhouettes(this.meshData, edges.silhouettes!), instances);
if (undefined !== edges.segments)
this.addPrimitive(() => EdgeGeometry.create(this.meshData, edges.segments!), instances);
if (undefined !== edges.polylines)
this.addPrimitive(() => PolylineEdgeGeometry.create(this.meshData, edges.polylines!), instances);
}
public get isDisposed(): boolean { return this.meshData.isDisposed && 0 === this._primitives.length; }
public dispose() {
dispose(this.meshData);
for (const primitive of this._primitives)
dispose(primitive);
this._primitives.length = 0;
}
public collectStatistics(stats: RenderMemory.Statistics): void {
this.meshData.collectStatistics(stats);
this._primitives.forEach((prim) => prim.collectStatistics(stats));
// Only count the shared instance buffers once...
if (undefined !== this._instances)
this._instances.collectStatistics(stats);
}
public addCommands(cmds: RenderCommands): void { this._primitives.forEach((prim) => prim.addCommands(cmds)); }
public addHiliteCommands(cmds: RenderCommands, batch: Batch, pass: RenderPass): void { this._primitives.forEach((prim) => prim.addHiliteCommands(cmds, batch, pass)); }
public get surfaceType(): SurfaceType { return this.meshData.type; }
}
/** Defines one aspect of the geometry of a mesh (surface or edges)
* @internal
*/
export abstract class MeshGeometry extends LUTGeometry {
public readonly mesh: MeshData;
protected readonly _numIndices: number;
public get asMesh() { return this; }
protected _getLineWeight(params: ShaderProgramParams): number { return this.computeEdgeWeight(params); }
// Convenience accessors...
public get edgeWidth() { return this.mesh.edgeWidth; }
public get edgeLineCode() { return this.mesh.edgeLineCode; }
public get hasFeatures() { return this.mesh.hasFeatures; }
public get surfaceType() { return this.mesh.type; }
public get fillFlags() { return this.mesh.fillFlags; }
public get isPlanar() { return this.mesh.isPlanar; }
public get colorInfo(): ColorInfo { return this.mesh.lut.colorInfo; }
public get uniformColor(): FloatRgba | undefined { return this.colorInfo.isUniform ? this.colorInfo.uniform : undefined; }
public get texture() { return this.mesh.texture; }
public get hasBakedLighting() { return this.mesh.hasBakedLighting; }
public get hasFixedNormals() { return this.mesh.hasFixedNormals; }
public get lut() { return this.mesh.lut; }
public get hasScalarAnimation() { return this.mesh.lut.hasScalarAnimation; }
protected constructor(mesh: MeshData, numIndices: number) {
super(mesh.viewIndependentOrigin);
this._numIndices = numIndices;
this.mesh = mesh;
}
protected computeEdgeWeight(params: ShaderProgramParams): number { return params.target.getEdgeWeight(params, this.edgeWidth); }
protected computeEdgeLineCode(params: ShaderProgramParams): number { return params.target.getEdgeLineCode(params, this.edgeLineCode); }
protected computeEdgeColor(target: Target): ColorInfo { return target.isEdgeColorOverridden ? target.edgeColor : this.colorInfo; }
protected computeEdgePass(target: Target): RenderPass {
if (target.isDrawingShadowMap)
return RenderPass.None;
const vf = target.currentViewFlags;
if (RenderMode.SmoothShade === vf.renderMode && !vf.visibleEdges) {
return RenderPass.None;
}
// Only want translucent edges in wireframe mode.
const isTranslucent = RenderMode.Wireframe === vf.renderMode && vf.transparency && this.colorInfo.hasTranslucency;
return isTranslucent ? RenderPass.Translucent : RenderPass.OpaqueLinear;
}
}
/** @internal */
export class EdgeGeometry extends MeshGeometry {
public readonly buffers: BuffersContainer;
protected readonly _indices: BufferHandle;
protected readonly _endPointAndQuadIndices: BufferHandle;
public get lutBuffers() { return this.buffers; }
public get asSurface() { return undefined; }
public get asEdge() { return this; }
public get asSilhouette(): SilhouetteEdgeGeometry | undefined { return undefined; }
public static create(mesh: MeshData, edges: SegmentEdgeParams): EdgeGeometry | undefined {
const indexBuffer = BufferHandle.createArrayBuffer(edges.indices.data);
const endPointBuffer = BufferHandle.createArrayBuffer(edges.endPointAndQuadIndices);
return undefined !== indexBuffer && undefined !== endPointBuffer ? new EdgeGeometry(indexBuffer, endPointBuffer, edges.indices.length, mesh) : undefined;
}
public get isDisposed(): boolean {
return this.buffers.isDisposed
&& this._indices.isDisposed
&& this._endPointAndQuadIndices.isDisposed;
}
public dispose() {
dispose(this.buffers);
dispose(this._indices);
dispose(this._endPointAndQuadIndices);
}
public collectStatistics(stats: RenderMemory.Statistics): void {
stats.addVisibleEdges(this._indices.bytesUsed + this._endPointAndQuadIndices.bytesUsed);
}
protected _draw(numInstances: number, instanceBuffersContainer?: BuffersContainer): void {
const bufs = instanceBuffersContainer !== undefined ? instanceBuffersContainer : this.buffers;
bufs.bind();
System.instance.drawArrays(GL.PrimitiveType.Triangles, 0, this._numIndices, numInstances);
bufs.unbind();
}
protected _wantWoWReversal(_target: Target): boolean { return true; }
protected _getLineCode(params: ShaderProgramParams): number { return this.computeEdgeLineCode(params); }
public get techniqueId(): TechniqueId { return TechniqueId.Edge; }
public getRenderPass(target: Target): RenderPass { return this.computeEdgePass(target); }
public get renderOrder(): RenderOrder { return this.isPlanar ? RenderOrder.PlanarEdge : RenderOrder.Edge; }
public getColor(target: Target): ColorInfo { return this.computeEdgeColor(target); }
public get endPointAndQuadIndices(): BufferHandle { return this._endPointAndQuadIndices; }
protected constructor(indices: BufferHandle, endPointAndQuadsIndices: BufferHandle, numIndices: number, mesh: MeshData) {
super(mesh, numIndices);
this.buffers = BuffersContainer.create();
const attrPos = AttributeMap.findAttribute("a_pos", TechniqueId.Edge, false);
const attrEndPointAndQuadIndices = AttributeMap.findAttribute("a_endPointAndQuadIndices", TechniqueId.Edge, false);
assert(attrPos !== undefined);
assert(attrEndPointAndQuadIndices !== undefined);
this.buffers.addBuffer(indices, [BufferParameters.create(attrPos!.location, 3, GL.DataType.UnsignedByte, false, 0, 0, false)]);
this.buffers.addBuffer(endPointAndQuadsIndices, [BufferParameters.create(attrEndPointAndQuadIndices!.location, 4, GL.DataType.UnsignedByte, false, 0, 0, false)]);
this._indices = indices;
this._endPointAndQuadIndices = endPointAndQuadsIndices;
}
}
/** @internal */
export class SilhouetteEdgeGeometry extends EdgeGeometry {
private readonly _normalPairs: BufferHandle;
public get asSilhouette() { return this; }
public static createSilhouettes(mesh: MeshData, params: SilhouetteParams): SilhouetteEdgeGeometry | undefined {
const indexBuffer = BufferHandle.createArrayBuffer(params.indices.data);
const endPointBuffer = BufferHandle.createArrayBuffer(params.endPointAndQuadIndices);
const normalsBuffer = BufferHandle.createArrayBuffer(params.normalPairs);
return undefined !== indexBuffer && undefined !== endPointBuffer && undefined !== normalsBuffer ? new SilhouetteEdgeGeometry(indexBuffer, endPointBuffer, normalsBuffer, params.indices.length, mesh) : undefined;
}
public get isDisposed(): boolean { return super.isDisposed && this._normalPairs.isDisposed; }
public dispose() {
super.dispose();
dispose(this._normalPairs);
}
public collectStatistics(stats: RenderMemory.Statistics): void {
stats.addSilhouetteEdges(this._indices.bytesUsed + this._endPointAndQuadIndices.bytesUsed + this._normalPairs.bytesUsed);
}
public get techniqueId(): TechniqueId { return TechniqueId.SilhouetteEdge; }
public get renderOrder(): RenderOrder { return this.isPlanar ? RenderOrder.PlanarSilhouette : RenderOrder.Silhouette; }
public get normalPairs(): BufferHandle { return this._normalPairs; }
private constructor(indices: BufferHandle, endPointAndQuadsIndices: BufferHandle, normalPairs: BufferHandle, numIndices: number, mesh: MeshData) {
super(indices, endPointAndQuadsIndices, numIndices, mesh);
const attrNormals = AttributeMap.findAttribute("a_normals", TechniqueId.SilhouetteEdge, false);
assert(attrNormals !== undefined);
this.buffers.addBuffer(normalPairs, [BufferParameters.create(attrNormals!.location, 4, GL.DataType.UnsignedByte, false, 0, 0, false)]);
this._normalPairs = normalPairs;
}
}
/** @internal */
export class PolylineEdgeGeometry extends MeshGeometry {
private _buffers: PolylineBuffers;
public get lutBuffers() { return this._buffers.buffers; }
public static create(mesh: MeshData, polyline: TesselatedPolyline): PolylineEdgeGeometry | undefined {
const buffers = PolylineBuffers.create(polyline);
return undefined !== buffers ? new PolylineEdgeGeometry(polyline.indices.length, buffers, mesh) : undefined;
}
public get isDisposed(): boolean { return this._buffers.isDisposed; }
public dispose() {
dispose(this._buffers);
}
public collectStatistics(stats: RenderMemory.Statistics): void {
this._buffers.collectStatistics(stats, RenderMemory.BufferType.PolylineEdges);
}
protected _wantWoWReversal(_target: Target): boolean { return true; }
protected _getLineWeight(params: ShaderProgramParams): number { return this.computeEdgeWeight(params); }
protected _getLineCode(params: ShaderProgramParams): number { return this.computeEdgeLineCode(params); }
public get techniqueId(): TechniqueId { return TechniqueId.Polyline; }
public getRenderPass(target: Target): RenderPass { return this.computeEdgePass(target); }
public get renderOrder(): RenderOrder { return this.isPlanar ? RenderOrder.PlanarEdge : RenderOrder.Edge; }
public get polylineBuffers(): PolylineBuffers { return this._buffers; }
protected _draw(numInstances: number, instanceBuffersContainer?: BuffersContainer): void {
const gl = System.instance;
const bufs = instanceBuffersContainer !== undefined ? instanceBuffersContainer : this._buffers.buffers;
bufs.bind();
gl.drawArrays(GL.PrimitiveType.Triangles, 0, this._numIndices, numInstances);
bufs.unbind();
}
private constructor(numIndices: number, buffers: PolylineBuffers, mesh: MeshData) {
super(mesh, numIndices);
this._buffers = buffers;
}
}
function wantMaterials(vf: ViewFlags) { return vf.materials && RenderMode.SmoothShade === vf.renderMode; }
function wantLighting(vf: ViewFlags) {
return RenderMode.SmoothShade === vf.renderMode && vf.lighting;
}
/** @internal */
export class SurfaceGeometry extends MeshGeometry {
private readonly _buffers: BuffersContainer;
private readonly _indices: BufferHandle;
public get lutBuffers() { return this._buffers; }
public static create(mesh: MeshData, indices: VertexIndices): SurfaceGeometry | undefined {
const indexBuffer = BufferHandle.createArrayBuffer(indices.data);
return undefined !== indexBuffer ? new SurfaceGeometry(indexBuffer, indices.length, mesh) : undefined;
}
public get isDisposed(): boolean {
return this._buffers.isDisposed
&& this._indices.isDisposed;
}
public dispose() {
dispose(this._buffers);
dispose(this._indices);
}
public collectStatistics(stats: RenderMemory.Statistics): void {
stats.addSurface(this._indices.bytesUsed);
}
public get isLit() { return SurfaceType.Lit === this.surfaceType || SurfaceType.TexturedLit === this.surfaceType; }
public get isTextured() { return SurfaceType.Textured === this.surfaceType || SurfaceType.TexturedLit === this.surfaceType; }
public get isGlyph() { return undefined !== this.texture && this.texture.isGlyph; }
public get isTileSection() { return undefined !== this.texture && this.texture.isTileSection; }
public get isClassifier() { return SurfaceType.VolumeClassifier === this.surfaceType; }
public get asSurface() { return this; }
public get asEdge() { return undefined; }
public get asSilhouette() { return undefined; }
protected _draw(numInstances: number, instanceBuffersContainer?: BuffersContainer): void {
const system = System.instance;
const gl = system.context;
const offset = RenderOrder.BlankingRegion === this.renderOrder;
const bufs = instanceBuffersContainer !== undefined ? instanceBuffersContainer : this._buffers;
if (offset) {
gl.enable(GL.POLYGON_OFFSET_FILL);
gl.polygonOffset(1.0, 1.0);
}
bufs.bind();
const primType = system.drawSurfacesAsWiremesh ? GL.PrimitiveType.Lines : GL.PrimitiveType.Triangles;
system.drawArrays(primType, 0, this._numIndices, numInstances);
bufs.unbind();
if (offset) {
gl.disable(GL.POLYGON_OFFSET_FILL);
}
}
public get techniqueId(): TechniqueId { return TechniqueId.Surface; }
public get isLitSurface() { return this.isLit; }
public get hasBakedLighting() { return this.mesh.hasBakedLighting; }
public get hasFixedNormals() { return this.mesh.hasFixedNormals; }
public get renderOrder(): RenderOrder {
if (FillFlags.Behind === (this.fillFlags & FillFlags.Behind))
return RenderOrder.BlankingRegion;
let order = this.isLit ? RenderOrder.LitSurface : RenderOrder.UnlitSurface;
if (this.isPlanar)
order = order | RenderOrder.PlanarBit;
return order;
}
public getColor(target: Target) {
if (FillFlags.Background === (this.fillFlags & FillFlags.Background))
return ColorInfo.createUniform(target.bgColor);
else
return this.colorInfo;
}
public getRenderPass(target: Target): RenderPass {
// Classifiers have a dedicated pass
if (this.isClassifier)
return RenderPass.Classification;
const opaquePass = this.isPlanar ? RenderPass.OpaquePlanar : RenderPass.OpaqueGeneral;
// When reading pixels, glyphs are always opaque. Otherwise always transparent (for anti-aliasing).
if (this.isGlyph)
return target.isReadPixelsInProgress ? opaquePass : RenderPass.Translucent;
const vf = target.currentViewFlags;
// In wireframe, unless fill is explicitly enabled for planar region, surface does not draw
if (RenderMode.Wireframe === vf.renderMode) {
const fillFlags = this.fillFlags;
const showFill = FillFlags.Always === (fillFlags & FillFlags.Always) || (vf.fill && FillFlags.ByView === (fillFlags & FillFlags.ByView));
if (!showFill)
return RenderPass.None;
}
// If transparency disabled by render mode or view flag, always draw opaque.
if (!vf.transparency || RenderMode.SolidFill === vf.renderMode || RenderMode.HiddenLine === vf.renderMode)
return opaquePass;
let hasAlpha = false;
// If the material overrides alpha (currently, everything except the default - aka "no" - material), alpha comes from the material
const mat = this.isLit && wantMaterials(vf) ? this.mesh.materialInfo : undefined;
if (undefined !== mat && mat.overridesAlpha)
hasAlpha = mat.hasTranslucency;
// A texture can contain translucent pixels. Its alpha is also always multiplied by the material's alpha
const tex = this.wantTextures(target, true) ? this.texture : undefined;
if (!hasAlpha && undefined !== tex)
hasAlpha = tex.hasTranslucency;
// If we have a material overriding transparency, OR a texture, transparency comes solely from them. Otherwise, use element transparency.
if (undefined === tex && (undefined === mat || !mat.overridesAlpha))
hasAlpha = this.getColor(target).hasTranslucency;
return hasAlpha ? RenderPass.Translucent : opaquePass;
}
protected _wantWoWReversal(target: Target): boolean {
const fillFlags = this.fillFlags;
if (FillFlags.None !== (fillFlags & FillFlags.Background))
return false; // fill color explicitly from background
if (FillFlags.None !== (fillFlags & FillFlags.Always))
return true; // fill displayed even in wireframe
const vf = target.currentViewFlags;
if (RenderMode.Wireframe === vf.renderMode || vf.visibleEdges)
return false; // never invert surfaces when edges are displayed
if (this.isLit && wantLighting(vf))
return false;
// Don't invert white pixels of textures...
return !this.wantTextures(target, this.isTextured);
}
public get materialInfo(): MaterialInfo | undefined { return this.mesh.materialInfo; }
public computeSurfaceFlags(params: ShaderProgramParams): SurfaceFlags {
const target = params.target;
const vf = target.currentViewFlags;
const useMaterial = wantMaterials(vf);
let flags = useMaterial ? SurfaceFlags.None : SurfaceFlags.IgnoreMaterial;
if (this.isLit) {
flags |= SurfaceFlags.HasNormals;
if (wantLighting(vf)) {
flags |= SurfaceFlags.ApplyLighting;
if (this.hasFixedNormals)
flags |= SurfaceFlags.NoFaceFront;
}
// Textured meshes store normal in place of color index.
// Untextured lit meshes store normal where textured meshes would store UV coords.
// Tell shader where to find normal.
if (!this.isTextured) {
flags |= SurfaceFlags.HasColorAndNormal;
}
}
if (this.wantTextures(target, this.isTextured)) {
flags |= SurfaceFlags.HasTexture;
if (useMaterial && undefined !== this.mesh.materialInfo && this.mesh.materialInfo.overridesAlpha && RenderPass.Translucent === params.renderPass)
flags |= SurfaceFlags.MultiplyAlpha;
}
switch (params.renderPass) {
// NB: We need this for opaque pass due to SolidFill (must compute transparency, discard below threshold, render opaque at or above threshold)
case RenderPass.OpaqueLinear:
case RenderPass.OpaquePlanar:
case RenderPass.OpaqueGeneral:
case RenderPass.Translucent: {
const mode = vf.renderMode;
if (!this.isGlyph && (RenderMode.HiddenLine === mode || RenderMode.SolidFill === mode)) {
flags |= SurfaceFlags.TransparencyThreshold;
if (RenderMode.HiddenLine === mode && FillFlags.Always !== (this.fillFlags & FillFlags.Always)) {
// fill flags test for text - doesn't render with bg fill in hidden line mode.
flags |= SurfaceFlags.BackgroundFill;
}
break;
}
}
}
if (params.target.isDrawingShadowMap)
flags |= SurfaceFlags.TransparencyThreshold;
return flags;
}
private constructor(indices: BufferHandle, numIndices: number, mesh: MeshData) {
super(mesh, numIndices);
this._buffers = BuffersContainer.create();
const attrPos = AttributeMap.findAttribute("a_pos", TechniqueId.Surface, false);
assert(undefined !== attrPos);
this._buffers.addBuffer(indices, [BufferParameters.create(attrPos!.location, 3, GL.DataType.UnsignedByte, false, 0, 0, false)]);
this._indices = indices;
}
private wantTextures(target: Target, surfaceTextureExists: boolean): boolean {
if (this.hasScalarAnimation && undefined !== target.analysisTexture)
return true;
if (!surfaceTextureExists)
return false;
if (this.isGlyph)
return true;
const fill = this.fillFlags;
const flags = target.currentViewFlags;
// ###TODO need to distinguish between gradient fill and actual textures...
switch (flags.renderMode) {
case RenderMode.SmoothShade:
return flags.textures;
case RenderMode.Wireframe:
return FillFlags.Always === (fill & FillFlags.Always) || (flags.fill && FillFlags.ByView === (fill & FillFlags.ByView));
default:
return FillFlags.Always === (fill & FillFlags.Always);
}
}
}