Skip to content

Commit

Permalink
Merge pull request #5 from Spiri0/master
Browse files Browse the repository at this point in the history
Fix meshlet color bugs and localPosition buffer size
  • Loading branch information
AIFanatic authored Sep 2, 2024
2 parents 5128719 + df8b030 commit 0caf68e
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/MeshletObject3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class MeshletObject3D {
const positionAttribute = new THREE.InstancedBufferAttribute(new Float32Array(1152), 3);
this.instancedGeometry.setAttribute('position', positionAttribute);

this.localPositionAttribute = new THREE.InstancedBufferAttribute(new Float32Array(meshlets.length), 3);
this.localPositionAttribute = new THREE.InstancedBufferAttribute(new Float32Array(meshlets.length * 3), 3);
this.instancedGeometry.setAttribute('localPosition', this.localPositionAttribute);
this.localPositionAttribute.usage = THREE.StaticDrawUsage;

Expand Down Expand Up @@ -118,20 +118,23 @@ export class MeshletObject3D {
}
`,
fragmentShader: `
flat in int meshInstanceID;
flat in int meshletInstanceID;
flat in int vertexID;
float rand(float co) {
return fract(sin((co + 1.0) * 12.9898) * 43758.5453);
vec3 hashColor(int seed) {
uint x = uint(seed);
x = ((x >> 16u) ^ x) * 0x45d9f3bu;
x = ((x >> 16u) ^ x) * 0x45d9f3bu;
x = (x >> 16u) ^ x;
return vec3(
float((x & 0xFF0000u) >> 16u) / 255.0,
float((x & 0x00FF00u) >> 8u) / 255.0,
float(x & 0x0000FFu) / 255.0
);
}
void main() {
float id = float(meshletInstanceID);
float r = rand(id * 11.212);
float g = rand(id * 21.212);
float b = rand(id * 31.212);
gl_FragColor = vec4(r, g, b, 1.0);
vec3 color = hashColor(meshletInstanceID);
gl_FragColor = vec4(color, 1.0);
}
`,
uniforms: {
Expand Down Expand Up @@ -306,12 +309,12 @@ export class MeshletObject3D {



this.localPositionAttribute = new THREE.InstancedBufferAttribute(new Float32Array(this.meshlets.length * this.meshletMatrices.length), 3);
this.localPositionAttribute = new THREE.InstancedBufferAttribute(new Float32Array(this.meshlets.length * this.meshletMatrices.length * 3), 3);
this.instancedGeometry.setAttribute('localPosition', this.localPositionAttribute);
this.localPositionAttribute.usage = THREE.StaticDrawUsage;

this.indicesAttribute = new THREE.InstancedBufferAttribute(new Float32Array(this.meshlets.length * this.meshletMatrices.length), 1);
this.instancedGeometry.setAttribute('index', this.indicesAttribute);
this.indicesAttribute.usage = THREE.StaticDrawUsage;
}
}
}

0 comments on commit 0caf68e

Please sign in to comment.