Skip to content

Support frontFace #155

@dmnsgn

Description

@dmnsgn

Why: negative normals in a glTF model can change winding order. See test model: https://github.com/KhronosGroup/glTF-Sample-Assets/tree/main/Models/NegativeScaleTest

Docs: MDN frontFace

Implementation:

pex-context:

  • Add state.frontFace (default CCW)
  • In applyPipeline (?), add if (pipeline.frontFace !== this.state.frontFace)

pex-renderer:

ctx.gl.frontFace(
  determinant(entity._transform.modelMatrix) < 0 ? ctx.gl.CW : ctx.gl.CCW,
);

pex-math:

// mat4.js

function determinant(a) {
  const a00 = a[0];
  const a01 = a[1];
  const a02 = a[2];
  const a03 = a[3];
  const a10 = a[4];
  const a11 = a[5];
  const a12 = a[6];
  const a13 = a[7];
  const a20 = a[8];
  const a21 = a[9];
  const a22 = a[10];
  const a23 = a[11];
  const a30 = a[12];
  const a31 = a[13];
  const a32 = a[14];
  const a33 = a[15];

  const b0 = a00 * a11 - a01 * a10;
  const b1 = a00 * a12 - a02 * a10;
  const b2 = a01 * a12 - a02 * a11;
  const b3 = a20 * a31 - a21 * a30;
  const b4 = a20 * a32 - a22 * a30;
  const b5 = a21 * a32 - a22 * a31;
  const b6 = a00 * b5 - a01 * b4 + a02 * b3;
  const b7 = a10 * b5 - a11 * b4 + a12 * b3;
  const b8 = a20 * b2 - a21 * b1 + a22 * b0;
  const b9 = a30 * b2 - a31 * b1 + a32 * b0;

  return a13 * b6 - a03 * b7 + a33 * b8 - a23 * b9;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions