Skip to content

Commit

Permalink
Merge pull request #12 from emperorOfBug/fix-main
Browse files Browse the repository at this point in the history
feat(indices): indices compatible with Uint32Array
  • Loading branch information
ruofeng618 committed Jul 14, 2023
2 parents 3981a52 + 1380040 commit 1ac583c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/loader/GLTFLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export class GLTF {
weights = accessor.getArray();
}
const geo = new Geometry({ type: "pbrGeomtry" });
if (indices) geo.setIndice(Array.from(indices));
if (indices) geo.setIndice(indices);
if (positions) geo.setAttribute(new Float32Attribute("position", Array.from(positions), 3));
if (normals) geo.setAttribute(new Float32Attribute("normal", Array.from(normals), 3));
if (colors) geo.setAttribute(new Float32Attribute("color", Array.from(colors), colorSize));
Expand Down
13 changes: 8 additions & 5 deletions src/render/IndexBuffer.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { IndexFormat } from "../core/WebGPUConstant";
import Buffer from "./Buffer";
import {TypedArray} from "../utils/gltfUtils";
export default class IndexBuffer {
buffer: Buffer;
indices: Array<number>;
indices: TypedArray;
indexFormat: GPUIndexFormat;
dirty: boolean;
private label: string;
constructor(label: string, indices?: Array<number>) {
constructor(label: string, indices?: TypedArray) {
this.label = label;
this.indices = indices;
this.indexFormat = IndexFormat.Uint16;
this.indexFormat = indices instanceof Uint32Array ? IndexFormat.Uint32 : IndexFormat.Uint16;
this.dirty = true;
}
setIndices(indices) {
this.indices = indices;
this.indexFormat = indices instanceof Uint32Array ? IndexFormat.Uint32 : IndexFormat.Uint16;
this.dirty = true;
}
bind(device: GPUDevice, passEncoder: GPURenderPassEncoder) {
Expand All @@ -22,8 +24,9 @@ export default class IndexBuffer {
this.buffer = Buffer.createIndexBuffer(
this.label,
device,
this.indexFormat == IndexFormat.Uint16 ? new Uint16Array(this.indices) : new Uint32Array(this.indices)
);
this.indices instanceof Array ?
(IndexFormat.Uint16 ? new Uint16Array(this.indices) : new Uint32Array(this.indices)):
this.indices);
}
passEncoder.setIndexBuffer(this.buffer.gpuBuffer, this.indexFormat);
}
Expand Down
10 changes: 9 additions & 1 deletion src/utils/gltfUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ export function toIndices(array: TypedArray): Uint16Array | Uint32Array {
if (array instanceof Float32Array) {
toArray = new Uint32Array(array.length);
} else {
toArray = new Uint16Array(array.length);
let max = 0;
for (let i = 0; i < array.length; i++) {
max = max < array[i] ? array[i] : max;
}
if (max < 65536) {
toArray = new Uint16Array(array.length);
} else {
toArray = new Uint32Array(array.length);
}
}
array.forEach((element, index) => {
toArray[index] = element;
Expand Down

0 comments on commit 1ac583c

Please sign in to comment.