148 lines
5.1 KiB
JavaScript
148 lines
5.1 KiB
JavaScript
import { ExtensionType } from '../../../extensions/Extensions.mjs';
|
|
import { Matrix } from '../../../maths/matrix/Matrix.mjs';
|
|
import { BindGroup } from '../../../rendering/renderers/gpu/shader/BindGroup.mjs';
|
|
import { UniformGroup } from '../../../rendering/renderers/shared/shader/UniformGroup.mjs';
|
|
import { getAdjustedBlendModeBlend } from '../../../rendering/renderers/shared/state/getAdjustedBlendModeBlend.mjs';
|
|
import { BigPool } from '../../../utils/pool/PoolGroup.mjs';
|
|
import { color32BitToUniform } from '../../graphics/gpu/colorToUniform.mjs';
|
|
import { BatchableMesh } from './BatchableMesh.mjs';
|
|
|
|
"use strict";
|
|
class MeshPipe {
|
|
constructor(renderer, adaptor) {
|
|
this.localUniforms = new UniformGroup({
|
|
uTransformMatrix: { value: new Matrix(), type: "mat3x3<f32>" },
|
|
uColor: { value: new Float32Array([1, 1, 1, 1]), type: "vec4<f32>" },
|
|
uRound: { value: 0, type: "f32" }
|
|
});
|
|
this.localUniformsBindGroup = new BindGroup({
|
|
0: this.localUniforms
|
|
});
|
|
this._meshDataHash = /* @__PURE__ */ Object.create(null);
|
|
this._gpuBatchableMeshHash = /* @__PURE__ */ Object.create(null);
|
|
this._destroyRenderableBound = this.destroyRenderable.bind(this);
|
|
this.renderer = renderer;
|
|
this._adaptor = adaptor;
|
|
this._adaptor.init();
|
|
}
|
|
validateRenderable(mesh) {
|
|
const meshData = this._getMeshData(mesh);
|
|
const wasBatched = meshData.batched;
|
|
const isBatched = mesh.batched;
|
|
meshData.batched = isBatched;
|
|
if (wasBatched !== isBatched) {
|
|
return true;
|
|
} else if (isBatched) {
|
|
const geometry = mesh._geometry;
|
|
if (geometry.indices.length !== meshData.indexSize || geometry.positions.length !== meshData.vertexSize) {
|
|
meshData.indexSize = geometry.indices.length;
|
|
meshData.vertexSize = geometry.positions.length;
|
|
return true;
|
|
}
|
|
const batchableMesh = this._getBatchableMesh(mesh);
|
|
const texture = mesh.texture;
|
|
if (batchableMesh.texture._source !== texture._source) {
|
|
if (batchableMesh.texture._source !== texture._source) {
|
|
return !batchableMesh._batcher.checkAndUpdateTexture(batchableMesh, texture);
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
addRenderable(mesh, instructionSet) {
|
|
const batcher = this.renderer.renderPipes.batch;
|
|
const { batched } = this._getMeshData(mesh);
|
|
if (batched) {
|
|
const gpuBatchableMesh = this._getBatchableMesh(mesh);
|
|
gpuBatchableMesh.texture = mesh._texture;
|
|
gpuBatchableMesh.geometry = mesh._geometry;
|
|
batcher.addToBatch(gpuBatchableMesh, instructionSet);
|
|
} else {
|
|
batcher.break(instructionSet);
|
|
instructionSet.add(mesh);
|
|
}
|
|
}
|
|
updateRenderable(mesh) {
|
|
if (mesh.batched) {
|
|
const gpuBatchableMesh = this._gpuBatchableMeshHash[mesh.uid];
|
|
gpuBatchableMesh.texture = mesh._texture;
|
|
gpuBatchableMesh.geometry = mesh._geometry;
|
|
gpuBatchableMesh._batcher.updateElement(gpuBatchableMesh);
|
|
}
|
|
}
|
|
destroyRenderable(mesh) {
|
|
this._meshDataHash[mesh.uid] = null;
|
|
const gpuMesh = this._gpuBatchableMeshHash[mesh.uid];
|
|
if (gpuMesh) {
|
|
BigPool.return(gpuMesh);
|
|
this._gpuBatchableMeshHash[mesh.uid] = null;
|
|
}
|
|
mesh.off("destroyed", this._destroyRenderableBound);
|
|
}
|
|
execute(mesh) {
|
|
if (!mesh.isRenderable)
|
|
return;
|
|
mesh.state.blendMode = getAdjustedBlendModeBlend(mesh.groupBlendMode, mesh.texture._source);
|
|
const localUniforms = this.localUniforms;
|
|
localUniforms.uniforms.uTransformMatrix = mesh.groupTransform;
|
|
localUniforms.uniforms.uRound = this.renderer._roundPixels | mesh._roundPixels;
|
|
localUniforms.update();
|
|
color32BitToUniform(
|
|
mesh.groupColorAlpha,
|
|
localUniforms.uniforms.uColor,
|
|
0
|
|
);
|
|
this._adaptor.execute(this, mesh);
|
|
}
|
|
_getMeshData(mesh) {
|
|
return this._meshDataHash[mesh.uid] || this._initMeshData(mesh);
|
|
}
|
|
_initMeshData(mesh) {
|
|
this._meshDataHash[mesh.uid] = {
|
|
batched: mesh.batched,
|
|
indexSize: mesh._geometry.indices?.length,
|
|
vertexSize: mesh._geometry.positions?.length
|
|
};
|
|
mesh.on("destroyed", this._destroyRenderableBound);
|
|
return this._meshDataHash[mesh.uid];
|
|
}
|
|
_getBatchableMesh(mesh) {
|
|
return this._gpuBatchableMeshHash[mesh.uid] || this._initBatchableMesh(mesh);
|
|
}
|
|
_initBatchableMesh(mesh) {
|
|
const gpuMesh = BigPool.get(BatchableMesh);
|
|
gpuMesh.renderable = mesh;
|
|
gpuMesh.texture = mesh._texture;
|
|
gpuMesh.transform = mesh.groupTransform;
|
|
gpuMesh.roundPixels = this.renderer._roundPixels | mesh._roundPixels;
|
|
this._gpuBatchableMeshHash[mesh.uid] = gpuMesh;
|
|
return gpuMesh;
|
|
}
|
|
destroy() {
|
|
for (const i in this._gpuBatchableMeshHash) {
|
|
if (this._gpuBatchableMeshHash[i]) {
|
|
BigPool.return(this._gpuBatchableMeshHash[i]);
|
|
}
|
|
}
|
|
this._gpuBatchableMeshHash = null;
|
|
this._meshDataHash = null;
|
|
this.localUniforms = null;
|
|
this.localUniformsBindGroup = null;
|
|
this._adaptor.destroy();
|
|
this._adaptor = null;
|
|
this.renderer = null;
|
|
}
|
|
}
|
|
/** @ignore */
|
|
MeshPipe.extension = {
|
|
type: [
|
|
ExtensionType.WebGLPipes,
|
|
ExtensionType.WebGPUPipes,
|
|
ExtensionType.CanvasPipes
|
|
],
|
|
name: "mesh"
|
|
};
|
|
|
|
export { MeshPipe };
|
|
//# sourceMappingURL=MeshPipe.mjs.map
|