Skip to content

Commit 37367de

Browse files
authored
Refactor of debug rendering private API (playcanvas#2845)
* Refactor of debug rendering private API * fix to texture, as quad is -0.5 to 0.5, so only needs to offset, no scaling
1 parent f270857 commit 37367de

File tree

2 files changed

+219
-141
lines changed

2 files changed

+219
-141
lines changed

src/framework/application.js

Lines changed: 32 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { EventHandler } from '../core/event-handler.js';
66
import { math } from '../math/math.js';
77
import { Color } from '../math/color.js';
88
import { Vec3 } from '../math/vec3.js';
9+
import { Mat4 } from '../math/mat4.js';
910

1011
import { http } from '../net/http.js';
1112

@@ -14,17 +15,12 @@ import {
1415
CLEARFLAG_COLOR, CLEARFLAG_DEPTH,
1516
FILTER_NEAREST,
1617
PIXELFORMAT_DEPTHSTENCIL, PIXELFORMAT_R8_G8_B8_A8,
17-
PRIMITIVE_TRIANGLES, PRIMITIVE_TRIFAN, PRIMITIVE_TRISTRIP,
18-
SEMANTIC_POSITION,
19-
TYPE_FLOAT32
18+
PRIMITIVE_TRIANGLES, PRIMITIVE_TRIFAN, PRIMITIVE_TRISTRIP
2019
} from '../graphics/constants.js';
2120
import { destroyPostEffectQuad } from '../graphics/simple-post-effect.js';
2221
import { GraphicsDevice } from '../graphics/graphics-device.js';
2322
import { RenderTarget } from '../graphics/render-target.js';
2423
import { Texture } from '../graphics/texture.js';
25-
import { VertexBuffer } from '../graphics/vertex-buffer.js';
26-
import { VertexFormat } from '../graphics/vertex-format.js';
27-
import { VertexIterator } from '../graphics/vertex-iterator.js';
2824

2925
import {
3026
LAYERID_DEPTH, LAYERID_IMMEDIATE, LAYERID_SKYBOX, LAYERID_UI, LAYERID_WORLD,
@@ -34,13 +30,10 @@ import {
3430
} from '../scene/constants.js';
3531
import { BatchManager } from '../scene/batching/batch-manager.js';
3632
import { ForwardRenderer } from '../scene/forward-renderer.js';
37-
import { GraphNode } from '../scene/graph-node.js';
38-
import { ImmediateData, LineBatch } from '../scene/immediate.js';
33+
import { ImmediateData } from '../scene/immediate.js';
3934
import { Layer } from '../scene/layer.js';
4035
import { LayerComposition } from '../scene/layer-composition.js';
4136
import { Lightmapper } from '../scene/lightmapper.js';
42-
import { Mesh } from '../scene/mesh.js';
43-
import { MeshInstance } from '../scene/mesh-instance.js';
4437
import { ParticleEmitter } from '../scene/particle-system/particle-emitter.js';
4538
import { Scene } from '../scene/scene.js';
4639

@@ -143,7 +136,6 @@ class Progress {
143136
}
144137

145138
var _deprecationWarning = false;
146-
var tempGraphNode = new GraphNode();
147139

148140
/**
149141
* @class
@@ -809,8 +801,6 @@ class Application extends EventHandler {
809801
}
810802
}
811803

812-
this.meshInstanceArray = [];
813-
814804
// bind tick function to current scope
815805

816806
/* eslint-disable-next-line no-use-before-define */
@@ -1813,19 +1803,11 @@ class Application extends EventHandler {
18131803

18141804
// IMMEDIATE MODE API
18151805
_preRenderImmediate() {
1816-
for (var i = 0; i < this._immediateData.lineBatches.length; i++) {
1817-
if (this._immediateData.lineBatches[i]) {
1818-
this._immediateData.lineBatches[i].finalize(this.meshInstanceArray);
1819-
}
1820-
}
1806+
this._immediateData.finalize();
18211807
}
18221808

18231809
_postRenderImmediate() {
1824-
for (var i = 0; i < this._immediateData.layers.length; i++) {
1825-
this._immediateData.layers[i].clearMeshInstances(true);
1826-
}
1827-
1828-
this._immediateData.layers.length = 0;
1810+
this._immediateData.clear();
18291811
}
18301812

18311813
_initImmediate() {
@@ -1844,27 +1826,10 @@ class Application extends EventHandler {
18441826
var mask = (options && options.mask) ? options.mask : undefined;
18451827

18461828
this._initImmediate();
1829+
let lineBatch = this._immediateData.prepareLineBatch(layer, depthTest, mask, position.length / 2);
18471830

1848-
this._immediateData.addLayer(layer);
1849-
1850-
var idx = this._immediateData.getLayerIdx(layer);
1851-
if (idx === undefined) {
1852-
// Init used batch once
1853-
var batch = new LineBatch();
1854-
batch.init(this.graphicsDevice, this._immediateData.lineVertexFormat, layer, position.length / 2);
1855-
batch.material.depthTest = depthTest;
1856-
if (mask) batch.meshInstance.mask = mask;
1857-
1858-
idx = this._immediateData.lineBatches.push(batch) - 1; // push into list and get index
1859-
this._immediateData.addLayerIdx(idx, layer);
1860-
} else {
1861-
// Possibly reallocate buffer if it's small
1862-
this._immediateData.lineBatches[idx].init(this.graphicsDevice, this._immediateData.lineVertexFormat, layer, position.length / 2);
1863-
this._immediateData.lineBatches[idx].material.depthTest = depthTest;
1864-
if (mask) this._immediateData.lineBatches[idx].meshInstance.mask = mask;
1865-
}
18661831
// Append
1867-
this._immediateData.lineBatches[idx].addLines(position, color);
1832+
lineBatch.addLines(position, color);
18681833
}
18691834

18701835
/**
@@ -2094,88 +2059,45 @@ class Application extends EventHandler {
20942059
], color, options);
20952060
}
20962061

2097-
// Draw meshInstance at this frame
2098-
renderMeshInstance(meshInstance, options) {
2099-
if (!options) {
2100-
options = {
2101-
layer: this.scene.layers.getLayerById(LAYERID_IMMEDIATE)
2102-
};
2103-
}
2062+
_getDefaultImmediateOptions() {
2063+
return {
2064+
layer: this.scene.layers.getLayerById(LAYERID_IMMEDIATE)
2065+
};
2066+
}
21042067

2068+
// Draw meshInstance at this frame
2069+
renderMeshInstance(meshInstance, options = this._getDefaultImmediateOptions()) {
21052070
this._initImmediate();
2106-
2107-
this._immediateData.addLayer(options.layer);
2108-
2109-
this.meshInstanceArray[0] = meshInstance;
2110-
options.layer.addMeshInstances(this.meshInstanceArray, true);
2071+
this._immediateData.renderMesh(null, null, null, meshInstance, options);
21112072
}
21122073

21132074
// Draw mesh at this frame
2114-
renderMesh(mesh, material, matrix, options) {
2115-
if (!options) {
2116-
options = {
2117-
layer: this.scene.layers.getLayerById(LAYERID_IMMEDIATE)
2118-
};
2119-
}
2120-
2075+
renderMesh(mesh, material, matrix, options = this._getDefaultImmediateOptions()) {
21212076
this._initImmediate();
2122-
tempGraphNode.worldTransform = matrix;
2123-
tempGraphNode._dirtyWorld = tempGraphNode._dirtyNormal = false;
2124-
2125-
var instance = new MeshInstance(mesh, material, tempGraphNode);
2126-
instance.cull = false;
2127-
2128-
if (options.mask) instance.mask = options.mask;
2129-
this._immediateData.addLayer(options.layer);
2130-
2131-
this.meshInstanceArray[0] = instance;
2132-
options.layer.addMeshInstances(this.meshInstanceArray, true);
2077+
this._immediateData.renderMesh(material, matrix, mesh, null, options);
21332078
}
21342079

21352080
// Draw quad of size [-0.5, 0.5] at this frame
2136-
renderQuad(matrix, material, options) {
2137-
if (!options) {
2138-
options = {
2139-
layer: this.scene.layers.getLayerById(LAYERID_IMMEDIATE)
2140-
};
2141-
}
2142-
2081+
renderQuad(matrix, material, options = this._getDefaultImmediateOptions()) {
21432082
this._initImmediate();
2083+
this._immediateData.renderMesh(material, matrix, this._immediateData.getQuadMesh(), null, options);
2084+
}
21442085

2145-
// Init quad data once
2146-
if (!this._immediateData.quadMesh) {
2147-
var format = new VertexFormat(this.graphicsDevice, [
2148-
{ semantic: SEMANTIC_POSITION, components: 3, type: TYPE_FLOAT32 }
2149-
]);
2150-
var quadVb = new VertexBuffer(this.graphicsDevice, format, 4);
2151-
var iterator = new VertexIterator(quadVb);
2152-
iterator.element[SEMANTIC_POSITION].set(-0.5, -0.5, 0);
2153-
iterator.next();
2154-
iterator.element[SEMANTIC_POSITION].set(0.5, -0.5, 0);
2155-
iterator.next();
2156-
iterator.element[SEMANTIC_POSITION].set(-0.5, 0.5, 0);
2157-
iterator.next();
2158-
iterator.element[SEMANTIC_POSITION].set(0.5, 0.5, 0);
2159-
iterator.end();
2160-
this._immediateData.quadMesh = new Mesh(this.graphicsDevice);
2161-
this._immediateData.quadMesh.vertexBuffer = quadVb;
2162-
this._immediateData.quadMesh.primitive[0].type = PRIMITIVE_TRISTRIP;
2163-
this._immediateData.quadMesh.primitive[0].base = 0;
2164-
this._immediateData.quadMesh.primitive[0].count = 4;
2165-
this._immediateData.quadMesh.primitive[0].indexed = false;
2166-
}
2167-
2168-
// Issue quad drawcall
2169-
tempGraphNode.worldTransform = matrix;
2170-
tempGraphNode._dirtyWorld = tempGraphNode._dirtyNormal = false;
2086+
// draws a texture on [x,y] position on screen, with size [width, height].
2087+
// Coordinates / sizes are in projected space (-1 .. 1)
2088+
renderTexture(x, y, width, height, texture, options) {
2089+
this._initImmediate();
21712090

2172-
var quad = new MeshInstance(this._immediateData.quadMesh, material, tempGraphNode);
2173-
quad.cull = false;
2174-
this.meshInstanceArray[0] = quad;
2091+
// TODO: if this is used for anything other than debug texture display, we should optimize this to avoid allocations
2092+
let matrix = new Mat4();
2093+
matrix.setTRS(new Vec3(x, y, 0.0), pc.Quat.IDENTITY, new Vec3(width, height, 0.0));
21752094

2176-
this._immediateData.addLayer(options.layer);
2095+
let material = new pc.Material();
2096+
material.setParameter("colorMap", texture);
2097+
material.shader = this._immediateData.getTextureShader();
2098+
material.update();
21772099

2178-
options.layer.addMeshInstances(this.meshInstanceArray, true);
2100+
this.renderQuad(matrix, material, options);
21792101
}
21802102

21812103
/**

0 commit comments

Comments
 (0)