Computing normal matrix

This commit is contained in:
Szymon Nowakowski 2023-08-04 21:45:10 +02:00
parent 0698cbdcde
commit be350c5f4f
2 changed files with 51 additions and 2 deletions

View File

@ -506,6 +506,40 @@ export class Matrix4x4 {
this.tw = tw; this.tw = tw;
return this; 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" }); Object.defineProperty(Matrix4x4.prototype, "type", { value: "Matrix4x4" });

View File

@ -9,10 +9,17 @@ export * from "./shader";
import { _BinaryWriter as BinaryWriter } from "./_BinaryWriter"; import { _BinaryWriter as BinaryWriter } from "./_BinaryWriter";
import { _Mapping as Mapping } from "./_Mapping"; 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 { IndexBuffer, IndexBufferProps, Texture2D, Texture2DProps, VertexBuffer, VertexBufferProps } from "./resources";
import { ShaderFlagKey, ShaderFlags, createPipeline, shaderFlagsKey } from "./shader"; 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 { export class Renderer {
_adapter: GPUAdapter; _adapter: GPUAdapter;
@ -391,10 +398,18 @@ export class Renderer {
const offset = this._uniformWriter._length; const offset = this._uniformWriter._length;
object._updateWorldMatrix(); object._updateWorldMatrix();
this._uniformWriter.writeMatrix4x4(object._worldMatrix); this._uniformWriter.writeMatrix4x4(object._worldMatrix);
this._uniformWriter.writeMatrix4x4(_normalMatrix.setObject(object._worldMatrix).inverseTransposeAffine());
return offset; return offset;
}); });
// directional lights gather
// TODO
// point lights gather
// TODO
void materialBindGroups; void materialBindGroups;
void objectOffsets; void objectOffsets;