From a37b66bb5ec18ed7b088c9bb191a004825b3aafc Mon Sep 17 00:00:00 2001 From: Szymon Nowakowski Date: Fri, 4 Aug 2023 21:45:10 +0200 Subject: [PATCH] Computing normal matrix --- src/data/Matrix4x4.ts | 34 ++++++++++++++++++++++++++++++++++ src/oktaeder.ts | 19 +++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/data/Matrix4x4.ts b/src/data/Matrix4x4.ts index bbcbe80..546e11a 100644 --- a/src/data/Matrix4x4.ts +++ b/src/data/Matrix4x4.ts @@ -506,6 +506,40 @@ export class Matrix4x4 { this.tw = tw; return this; } + + inverseTransposeAffine(): Matrix4x4 { + const ix = this.ix; + const iy = this.iy; + const iz = this.iz; + const jx = this.jx; + const jy = this.jy; + const jz = this.jz; + const kx = this.kx; + const ky = this.ky; + const kz = this.kz; + const tx = this.tx; + const ty = this.ty; + const tz = this.tz; + + const det = ix * jy * kz + iy * jz * kx + iz * jx * ky + - ix * jz * ky - iy * jx * kz - iz * jy * kx; + const invDet = 1 / det; + + this.ix = invDet * (jy * kz - jz * ky); + this.iy = invDet * (jz * kx - jx * kz); + this.iz = invDet * (jx * ky - jy * kx); + this.jx = invDet * (iz * ky - iy * kz); + this.jy = invDet * (ix * kz - iz * kx); + this.jz = invDet * (iy * kx - ix * ky); + this.kx = invDet * (iy * jz - iz * jy); + this.ky = invDet * (iz * jx - ix * jz); + this.kz = invDet * (ix * jy - iy * jx); + this.tx = -(this.ix * tx + this.iy * ty + this.iz * tz); + this.ty = -(this.jx * tx + this.jy * ty + this.jz * tz); + this.tz = -(this.kx * tx + this.ky * ty + this.kz * tz); + + return this; + } } Object.defineProperty(Matrix4x4.prototype, "type", { value: "Matrix4x4" }); diff --git a/src/oktaeder.ts b/src/oktaeder.ts index 1a9d0c9..ad7f2ec 100644 --- a/src/oktaeder.ts +++ b/src/oktaeder.ts @@ -9,10 +9,17 @@ export * from "./shader"; import { _BinaryWriter as BinaryWriter } from "./_BinaryWriter"; import { _Mapping as Mapping } from "./_Mapping"; -import { Camera, Material, Node, Scene, preOrder } from "./data"; +import { Camera, Material, Matrix4x4, Node, Scene, preOrder } from "./data"; import { IndexBuffer, IndexBufferProps, Texture2D, Texture2DProps, VertexBuffer, VertexBufferProps } from "./resources"; import { ShaderFlagKey, ShaderFlags, createPipeline, shaderFlagsKey } from "./shader"; +const _normalMatrix = new Matrix4x4( + NaN, NaN, NaN, NaN, + NaN, NaN, NaN, NaN, + NaN, NaN, NaN, NaN, + NaN, NaN, NaN, NaN, +); + export class Renderer { _adapter: GPUAdapter; @@ -391,10 +398,18 @@ export class Renderer { const offset = this._uniformWriter._length; object._updateWorldMatrix(); this._uniformWriter.writeMatrix4x4(object._worldMatrix); - + this._uniformWriter.writeMatrix4x4(_normalMatrix.setObject(object._worldMatrix).inverseTransposeAffine()); return offset; }); + // directional lights gather + + // TODO + + // point lights gather + + // TODO + void materialBindGroups; void objectOffsets;