[ts][pixi] Performance improvements.

This commit is contained in:
Davide Tantillo 2024-07-04 15:15:25 +02:00
parent f93c563a3f
commit 52cc1d44d8
2 changed files with 40 additions and 30 deletions

View File

@ -54,26 +54,30 @@ export class DarkSlotMesh extends DarkTintMesh implements ISlotMesh {
const vertLenght = (finalVerticesLength / (darkTint ? 12 : 8)) * 2; const vertLenght = (finalVerticesLength / (darkTint ? 12 : 8)) * 2;
if (this.geometry.getBuffer("aTextureCoord").data?.length !== vertLenght) { const textureCoord = this.geometry.getBuffer("aTextureCoord");
this.geometry.getBuffer("aTextureCoord").data = new Float32Array(vertLenght); if (textureCoord.data?.length !== vertLenght) {
textureCoord.data = new Float32Array(vertLenght);
} }
if (this.geometry.getBuffer("aVertexPosition").data?.length !== vertLenght) { const vertexCoord = this.geometry.getBuffer("aVertexPosition");
this.geometry.getBuffer("aVertexPosition").data = new Float32Array(vertLenght); if (vertexCoord.data?.length !== vertLenght) {
vertexCoord.data = new Float32Array(vertLenght);
} }
let vertIndex = 0; let vertIndex = 0;
let textureCoordData = textureCoord.data;
let vertexCoordData = vertexCoord.data;
for (let i = 0; i < finalVerticesLength; i += darkTint ? 12 : 8) { for (let i = 0; i < finalVerticesLength; i += darkTint ? 12 : 8) {
let auxi = i; let auxi = i;
this.geometry.getBuffer("aVertexPosition").data[vertIndex] = finalVertices[auxi++]; vertexCoordData[vertIndex] = finalVertices[auxi++];
this.geometry.getBuffer("aVertexPosition").data[vertIndex + 1] = finalVertices[auxi++]; vertexCoordData[vertIndex + 1] = finalVertices[auxi++];
auxi += 4; // color auxi += 4; // color
this.geometry.getBuffer("aTextureCoord").data[vertIndex] = finalVertices[auxi++]; textureCoordData[vertIndex] = finalVertices[auxi++];
this.geometry.getBuffer("aTextureCoord").data[vertIndex + 1] = finalVertices[auxi++]; textureCoordData[vertIndex + 1] = finalVertices[auxi++];
vertIndex += 2; vertIndex += 2;
} }
@ -101,18 +105,20 @@ export class DarkSlotMesh extends DarkTintMesh implements ISlotMesh {
this.blendMode = SpineTexture.toPixiBlending(slotBlendMode); this.blendMode = SpineTexture.toPixiBlending(slotBlendMode);
this.alpha = DarkSlotMesh.auxColor[3]; this.alpha = DarkSlotMesh.auxColor[3];
if (this.geometry.indexBuffer.data.length !== finalIndices.length) { const indexBuffer = this.geometry.indexBuffer;
this.geometry.indexBuffer.data = new Uint32Array(finalIndices); if (indexBuffer.data.length !== finalIndices.length) {
indexBuffer.data = new Uint32Array(finalIndices);
} else { } else {
const indexBufferData = indexBuffer.data;
for (let i = 0; i < finalIndicesLength; i++) { for (let i = 0; i < finalIndicesLength; i++) {
this.geometry.indexBuffer.data[i] = finalIndices[i]; indexBufferData[i] = finalIndices[i];
} }
} }
this.name = slotName; this.name = slotName;
this.geometry.getBuffer("aVertexPosition").update(); textureCoord.update();
this.geometry.getBuffer("aTextureCoord").update(); vertexCoord.update();
this.geometry.indexBuffer.update(); indexBuffer.update();
} }
} }

View File

@ -62,32 +62,34 @@ export class SlotMesh extends Mesh implements ISlotMesh {
const vertLenght = (finalVerticesLength / (darkTint ? 12 : 8)) * 2; const vertLenght = (finalVerticesLength / (darkTint ? 12 : 8)) * 2;
if (this.geometry.getBuffer("aTextureCoord").data?.length !== vertLenght) { const textureCoord = this.geometry.getBuffer("aTextureCoord");
this.geometry.getBuffer("aTextureCoord").data = new Float32Array(vertLenght); if (textureCoord.data?.length !== vertLenght) {
textureCoord.data = new Float32Array(vertLenght);
} }
if (this.geometry.getBuffer("aVertexPosition").data?.length !== vertLenght) { const vertexCoord = this.geometry.getBuffer("aVertexPosition");
this.geometry.getBuffer("aVertexPosition").data = new Float32Array(vertLenght); if (vertexCoord.data?.length !== vertLenght) {
vertexCoord.data = new Float32Array(vertLenght);
} }
let vertIndex = 0; let vertIndex = 0;
let textureCoordData = textureCoord.data;
let vertexCoordData = vertexCoord.data;
for (let i = 0; i < finalVerticesLength; i += darkTint ? 12 : 8) { for (let i = 0; i < finalVerticesLength; i += darkTint ? 12 : 8) {
let auxi = i; let auxi = i;
this.geometry.getBuffer("aVertexPosition").data[vertIndex] = finalVertices[auxi++]; vertexCoordData[vertIndex] = finalVertices[auxi++];
this.geometry.getBuffer("aVertexPosition").data[vertIndex + 1] = finalVertices[auxi++]; vertexCoordData[vertIndex + 1] = finalVertices[auxi++];
auxi += 4; // color auxi += 4; // color
this.geometry.getBuffer("aTextureCoord").data[vertIndex] = finalVertices[auxi++]; textureCoordData[vertIndex] = finalVertices[auxi++];
this.geometry.getBuffer("aTextureCoord").data[vertIndex + 1] = finalVertices[auxi++]; textureCoordData[vertIndex + 1] = finalVertices[auxi++];
vertIndex += 2; vertIndex += 2;
} }
// console.log(vertLenght, auxVert.length);
if (darkTint && !this.warnedTwoTint) { if (darkTint && !this.warnedTwoTint) {
console.warn("DarkTint is not enabled by default. To enable use a DarkSlotMesh factory while creating the Spine object."); console.warn("DarkTint is not enabled by default. To enable use a DarkSlotMesh factory while creating the Spine object.");
this.warnedTwoTint = true; this.warnedTwoTint = true;
@ -102,18 +104,20 @@ export class SlotMesh extends Mesh implements ISlotMesh {
this.alpha = SlotMesh.auxColor[3]; this.alpha = SlotMesh.auxColor[3];
this.blendMode = SpineTexture.toPixiBlending(slotBlendMode); this.blendMode = SpineTexture.toPixiBlending(slotBlendMode);
if (this.geometry.indexBuffer.data.length !== finalIndices.length) { const indexBuffer = this.geometry.indexBuffer;
this.geometry.indexBuffer.data = new Uint32Array(finalIndices); if (indexBuffer.data.length !== finalIndices.length) {
indexBuffer.data = new Uint32Array(finalIndices);
} else { } else {
const indexBufferData = indexBuffer.data;
for (let i = 0; i < finalIndicesLength; i++) { for (let i = 0; i < finalIndicesLength; i++) {
this.geometry.indexBuffer.data[i] = finalIndices[i]; indexBufferData[i] = finalIndices[i];
} }
} }
this.name = slotName; this.name = slotName;
this.geometry.getBuffer("aVertexPosition").update(); textureCoord.update();
this.geometry.getBuffer("aTextureCoord").update(); vertexCoord.update();
this.geometry.indexBuffer.update(); indexBuffer.update();
} }
} }