Skip to content

Commit

Permalink
perf: optimize Accessor min/max
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuki Shimada committed Dec 24, 2022
1 parent 1df4611 commit e4f78e4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/foundation/exporter/Gltf2Exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,6 @@ function createOrReuseGltf2Accessor(
const accessorIdx = calcAccessorIdxToSet(existingUniqueRnAccessors, rnAccessor);
if (accessorIdx === -1) {
// create a Gltf2Accessor
rnAccessor.calcMinMax();
const gltf2Accessor: Gltf2AccessorEx = {
bufferView: bufferViewIdxToSet,
byteOffset: rnAccessor.byteOffsetInBufferView,
Expand Down
2 changes: 0 additions & 2 deletions src/foundation/geometry/Primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,6 @@ export class Primitive extends RnObject {
) {
const positionAccessor = this.__attributes.get(VertexAttribute.Position.XYZ)!;

positionAccessor.calcMinMax();

const min = positionAccessor.min as number[];
this.__aabb.minPoint = Primitive.__tmpVec3_0.setComponents(min[0], min[1], min[2]);
const max = positionAccessor.max as number[];
Expand Down
48 changes: 45 additions & 3 deletions src/foundation/memory/Accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
Array4,
} from '../../types/CommonTypes';
import { Matrix44 } from '../math/Matrix44';
import { Is } from '../misc';

type DataViewGetter = (byteOffset: Byte, littleEndian?: boolean) => number;
type DataViewSetter = (byteOffset: Byte, value: number, littleEndian?: boolean) => void;
Expand Down Expand Up @@ -99,7 +100,7 @@ export class Accessor {
this.__count = count;
this.__arrayLength = arrayLength;

if (max) {
if (Is.exist(max)) {
this.__max.setComponents(
max[0] || -Number.MAX_VALUE,
max[1] || -Number.MAX_VALUE,
Expand All @@ -108,7 +109,7 @@ export class Accessor {
);
}

if (min) {
if (Is.exist(min)) {
this.__min.setComponents(
min[0] || Number.MAX_VALUE,
min[1] || Number.MAX_VALUE,
Expand All @@ -117,6 +118,10 @@ export class Accessor {
);
}

if (Is.exist(max) && Is.exist(min)) {
this.__isMinMixDirty = false;
}

this.__raw = raw;
this.__normalized = normalized;

Expand Down Expand Up @@ -1026,7 +1031,41 @@ export class Accessor {
return this.__bufferView;
}

setMinMax(min: number[], max: number[]) {
const componentN = this.compositionType.getNumberOfComponents();
if (componentN === 1) {
this.__min._v[0] = min[0];
this.__max._v[0] = max[0];
} else if (componentN === 2) {
this.__min._v[0] = min[0];
this.__min._v[1] = min[1];
this.__max._v[0] = max[0];
this.__max._v[1] = max[1];
} else if (componentN === 3) {
this.__min._v[0] = min[0];
this.__min._v[1] = min[1];
this.__min._v[2] = min[2];
this.__max._v[0] = max[0];
this.__max._v[1] = max[1];
this.__max._v[2] = max[2];
} else if (componentN === 4) {
this.__min._v[0] = min[0];
this.__min._v[1] = min[1];
this.__min._v[2] = min[2];
this.__min._v[3] = min[3];
this.__max._v[0] = max[0];
this.__max._v[1] = max[1];
this.__max._v[2] = max[2];
this.__max._v[3] = max[3];
}
this.__isMinMixDirty = false;
}

get min(): number[] {
if (this.__isMinMixDirty) {
this.__calcMinMax();
}

const componentN = this.compositionType.getNumberOfComponents();
if (componentN === 4) {
return [this.__min._v[0], this.__min._v[1], this.__min._v[2], this.__min._v[3]];
Expand All @@ -1040,6 +1079,9 @@ export class Accessor {
}

get max(): number[] {
if (this.__isMinMixDirty) {
this.__calcMinMax();
}
const componentN = this.compositionType.getNumberOfComponents();
if (componentN === 4) {
return [this.__max._v[0], this.__max._v[1], this.__max._v[2], this.__max._v[3]];
Expand All @@ -1056,7 +1098,7 @@ export class Accessor {
return this.__normalized;
}

calcMinMax() {
private __calcMinMax() {
const componentN = this.compositionType.getNumberOfComponents();
if (componentN === 4) {
this.__max.setComponents(
Expand Down

0 comments on commit e4f78e4

Please sign in to comment.