poppygl 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -37,6 +37,7 @@ interface DrawCall {
37
37
  indices: Uint32Array | null;
38
38
  model: mat4;
39
39
  material: Material;
40
+ colors?: Float32Array | null;
40
41
  }
41
42
  interface GLTFResources {
42
43
  buffers: Uint8Array[];
package/dist/index.js CHANGED
@@ -244,7 +244,7 @@ var SoftwareRenderer = class {
244
244
  return out;
245
245
  }
246
246
  drawMesh(mesh, camera, light, material, cullBackFaces = true, gammaOut = true) {
247
- const { positions, normals, uvs, indices, model } = mesh;
247
+ const { positions, normals, uvs, indices, model, colors } = mesh;
248
248
  const view = camera.view;
249
249
  const proj = camera.proj;
250
250
  const mvp = mat42.create();
@@ -265,6 +265,7 @@ var SoftwareRenderer = class {
265
265
  const vInvW = new Float32Array(vertexCount);
266
266
  const vNDCz = new Float32Array(vertexCount);
267
267
  const vWorldN = new Array(vertexCount);
268
+ const vColor = new Array(vertexCount);
268
269
  for (let i = 0; i < vertexCount; i++) {
269
270
  const p = vec42.fromValues(
270
271
  positions[i * 3 + 0],
@@ -291,6 +292,11 @@ var SoftwareRenderer = class {
291
292
  const nw = vec32.create();
292
293
  vec32.transformMat3(nw, n, normalMat);
293
294
  vWorldN[i] = [nw[0], nw[1], nw[2]];
295
+ if (colors && colors.length >= (i + 1) * 3) {
296
+ vColor[i] = [colors[i * 3 + 0], colors[i * 3 + 1], colors[i * 3 + 2]];
297
+ } else {
298
+ vColor[i] = [1, 1, 1];
299
+ }
294
300
  }
295
301
  for (let i = 0; i < idx.length; i += 3) {
296
302
  const i0 = idx[i + 0];
@@ -328,6 +334,11 @@ var SoftwareRenderer = class {
328
334
  [uvs[i1 * 2 + 0], uvs[i1 * 2 + 1]],
329
335
  [uvs[i2 * 2 + 0], uvs[i2 * 2 + 1]]
330
336
  ] : null;
337
+ const cs = [
338
+ vColor[i0],
339
+ vColor[i1],
340
+ vColor[i2]
341
+ ];
331
342
  for (let y = minY; y <= maxY; y++) {
332
343
  for (let x = minX; x <= maxX; x++) {
333
344
  const p = [x + 0.5, y + 0.5];
@@ -360,6 +371,13 @@ var SoftwareRenderer = class {
360
371
  );
361
372
  baseColor = mulColor(baseColor, texel);
362
373
  }
374
+ const [cr, cg, cb] = this.perspInterp(cs, invW, [l0, l1, l2]);
375
+ baseColor = [
376
+ baseColor[0] * cr,
377
+ baseColor[1] * cg,
378
+ baseColor[2] * cb,
379
+ baseColor[3]
380
+ ];
363
381
  const [np0, np1, np2] = this.perspInterp(nws, invW, [l0, l1, l2]);
364
382
  const nlen = Math.hypot(np0, np1, np2) || 1;
365
383
  const nrm = [
@@ -707,6 +725,8 @@ function createSceneFromGLTF(gltf, resources) {
707
725
  const normals = normalsAcc != null ? readAccessorAsFloat32(ctx, normalsAcc) : null;
708
726
  const uvs = uvAcc != null ? readAccessorAsFloat32(ctx, uvAcc) : null;
709
727
  const indices = primitive.indices != null ? readIndices(ctx, primitive.indices) : null;
728
+ const colorAcc = primitive.attributes?.COLOR_0;
729
+ const colors = colorAcc != null ? readAccessorAsFloat32(ctx, colorAcc) : null;
710
730
  const material = getMaterial(
711
731
  gltf,
712
732
  textures,
@@ -719,7 +739,8 @@ function createSceneFromGLTF(gltf, resources) {
719
739
  uvs,
720
740
  indices,
721
741
  model: world,
722
- material
742
+ material,
743
+ colors
723
744
  });
724
745
  }
725
746
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "poppygl",
3
3
  "main": "dist/index.js",
4
- "version": "0.0.6",
4
+ "version": "0.0.8",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/tscircuit/poppygl.git"