Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batcher buffers merging optimization #7109

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions src/scene/batching/batch-manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Debug } from '../../core/debug.js';
import { now } from '../../core/time.js';
import { Mat3 } from '../../core/math/mat3.js';
import { Vec3 } from '../../core/math/vec3.js';
import { BoundingBox } from '../../core/shape/bounding-box.js';

import {
Expand Down Expand Up @@ -666,7 +665,6 @@ class BatchManager {
let verticesOffset = 0;
let indexOffset = 0;
let transform;
const vec = new Vec3();

// allocate indices
const indexArrayType = batchNumVerts <= 0xffff ? Uint16Array : Uint32Array;
Expand Down Expand Up @@ -706,24 +704,48 @@ class BatchManager {
// transform position, normal and tangent to world space
if (!dynamic && stream.numComponents >= 3) {
if (semantic === SEMANTIC_POSITION) {
const m = transform.data;
const m0 = m[0];
const m1 = m[1];
const m2 = m[2];
const m4 = m[4];
const m5 = m[5];
const m6 = m[6];
const m8 = m[8];
const m9 = m[9];
const m10 = m[10];
const m12 = m[12];
const m13 = m[13];
const m14 = m[14];

let x, y, z;

for (let j = 0; j < totalComponents; j += stream.numComponents) {
vec.set(subarray[j], subarray[j + 1], subarray[j + 2]);
transform.transformPoint(vec, vec);
subarray[j] = vec.x;
subarray[j + 1] = vec.y;
subarray[j + 2] = vec.z;
x = subarray[j];
y = subarray[j + 1];
z = subarray[j + 2];

// mat4.transformVector
subarray[j] = x * m0 + y * m4 + z * m8 + m12;
subarray[j + 1] = x * m1 + y * m5 + z * m9 + m13;
subarray[j + 2] = x * m2 + y * m6 + z * m10 + m14;
}
} else if (semantic === SEMANTIC_NORMAL || semantic === SEMANTIC_TANGENT) {

// handle non-uniform scale by using transposed inverse matrix to transform vectors
mat3.invertMat4(transform).transpose();

const [m0, m1, m2, m3, m4, m5, m6, m7, m8] = mat3.data;
let x, y, z;

for (let j = 0; j < totalComponents; j += stream.numComponents) {
vec.set(subarray[j], subarray[j + 1], subarray[j + 2]);
mat3.transformVector(vec, vec);
subarray[j] = vec.x;
subarray[j + 1] = vec.y;
subarray[j + 2] = vec.z;
x = subarray[j];
y = subarray[j + 1];
z = subarray[j + 2];

// mat3.transformVector
subarray[j] = x * m0 + y * m3 + z * m6;
subarray[j + 1] = x * m1 + y * m4 + z * m7;
subarray[j + 2] = x * m2 + y * m5 + z * m8;
}
}
}
Expand Down