From d171961df21bd15749a18378f159ce512c3c8b0e Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Tue, 24 Dec 2024 12:50:05 -0800 Subject: [PATCH] Star Fox Adventures: minor cleanup --- src/StarFoxAdventures/SphereMaps.ts | 7 +++-- src/StarFoxAdventures/materials.ts | 42 ++++++++++++++--------------- src/StarFoxAdventures/shapes.ts | 5 ++-- src/StarFoxAdventures/util.ts | 10 ------- src/StarFoxAdventures/world.ts | 6 ++--- 5 files changed, 29 insertions(+), 41 deletions(-) diff --git a/src/StarFoxAdventures/SphereMaps.ts b/src/StarFoxAdventures/SphereMaps.ts index 29db71156..065b6efb2 100644 --- a/src/StarFoxAdventures/SphereMaps.ts +++ b/src/StarFoxAdventures/SphereMaps.ts @@ -8,7 +8,7 @@ import { GfxRenderInstList, GfxRenderInstManager } from '../gfx/render/GfxRender import * as GX from '../gx/gx_enum.js'; import * as GX_Material from '../gx/gx_material.js'; import { ColorKind, DrawParams, fillSceneParams, fillSceneParamsData, gxBindingLayouts, GXRenderHelperGfx, MaterialParams, SceneParams, ub_SceneParamsBufferSize } from '../gx/gx_render.js'; -import { projectionMatrixForCuboid } from '../MathHelpers.js'; +import { projectionMatrixForCuboid, setMatrixTranslation, Vec3Zero } from '../MathHelpers.js'; import { TSDraw } from "../SuperMarioGalaxy/DDraw.js"; import { TextureMapping } from '../TextureHolder.js'; import { nArray } from '../util.js'; @@ -16,7 +16,6 @@ import { SFAMaterialBuilder } from './MaterialBuilder.js'; import { makeMaterialTexture, MaterialFactory } from './materials.js'; import { SceneRenderContext, setGXMaterialOnRenderInst } from './render.js'; import { TextureFetcher } from './textures.js'; -import { mat4SetTranslation } from './util.js'; import { World } from './world.js'; import { LightType } from './WorldLights.js'; @@ -86,7 +85,7 @@ function createReflectiveProbeMaterial(materialFactory: MaterialFactory, texFetc mb.setTevDirect(stage0); mb.setTexMtx(0, (dst: mat4) => { mat4.fromScaling(dst, [0.5, -0.5, 0.5]); - mat4SetTranslation(dst, 0.5, 0.5, 0.0); + setMatrixTranslation(dst, [0.5, 0.5, 0.0]); }); const texCoord = mb.genTexCoord(GX.TexGenType.MTX2x4, GX.TexGenSrc.NRM, GX.TexGenMatrix.TEXMTX0); const texMap = mb.genTexMap(makeMaterialTexture(texFetcher.getTexture(materialFactory.cache, 0x5dc, false))); @@ -229,7 +228,7 @@ export class SphereMapManager { const worldViewSR = scratchMtx0; mat4.copy(worldViewSR, sceneCtx.viewToWorldMtx); - mat4SetTranslation(worldViewSR, 0, 0, 0); + setMatrixTranslation(worldViewSR, Vec3Zero); const skyLightVec = scratchVec0; vec3.transformMat4(skyLightVec, skyLight.direction, worldViewSR); diff --git a/src/StarFoxAdventures/materials.ts b/src/StarFoxAdventures/materials.ts index 2b5d286e1..1b6493e0a 100644 --- a/src/StarFoxAdventures/materials.ts +++ b/src/StarFoxAdventures/materials.ts @@ -7,14 +7,14 @@ import { TextureMapping } from '../TextureHolder.js'; import { texProjCameraSceneTex } from '../Camera.js'; import { SFATexture, TextureFetcher } from './textures.js'; -import { mat4SetRow, mat4FromRowMajor, mat4SetValue, mat4SetRowMajor, mat4SetTranslation } from './util.js'; +import { mat4SetRow, mat4FromRowMajor, mat4SetRowMajor } from './util.js'; import { mat4, vec3 } from 'gl-matrix'; import { FurFactory } from './fur.js'; import { SFAAnimationController } from './animation.js'; import { colorFromRGBA, Color, colorCopy, White, OpaqueBlack, colorNewCopy, TransparentBlack, Red, Blue } from '../Color.js'; import { SceneRenderContext } from './render.js'; import { ColorFunc, getGXIndTexMtxID, getGXIndTexMtxID_S, getGXIndTexMtxID_T, getGXKonstAlphaSel, getGXKonstColorSel, getGXPostTexGenMatrix, IndTexStage, SFAMaterialBuilder, TevStage, TexCoord, TexFunc, TexMap } from './MaterialBuilder.js'; -import { clamp } from '../MathHelpers.js'; +import { clamp, setMatrixTranslation } from '../MathHelpers.js'; import { GfxRenderCache } from '../gfx/render/GfxRenderCache.js'; export interface ShaderLayer { @@ -479,7 +479,7 @@ export class StandardMapMaterial extends StandardMaterial { mat4.fromScaling(pttexmtx0, [0.9, 0.9, 1.0]); const postTexMtx0 = this.mb.genPostTexMtx((dst: mat4, matCtx: MaterialRenderContext) => { mat4.copy(dst, pttexmtx0); - mat4SetValue(dst, 1, 3, 0.125 * matCtx.sceneCtx.animController.envAnimValue1); + dst[13] = 0.125 * matCtx.sceneCtx.animController.envAnimValue1; }); const pttexmtx1 = mat4.create(); @@ -490,8 +490,8 @@ export class StandardMapMaterial extends StandardMaterial { const postTexMtx1 = this.mb.genPostTexMtx((dst: mat4, matCtx: MaterialRenderContext) => { mat4.copy(dst, pttexmtx1); const v = 0.0625 * matCtx.sceneCtx.animController.envAnimValue0; - mat4SetValue(dst, 0, 3, v); - mat4SetValue(dst, 1, 3, v); + dst[12] = v; + dst[13] = v; }); const postTexMtx2 = this.mb.genPostTexMtx((dst: mat4) => { @@ -553,7 +553,7 @@ export class StandardMapMaterial extends StandardMaterial { const pttexmtx0 = mat4.create(); mat4.fromScaling(pttexmtx0, [0.008, 0.008, 0.008]); - mat4SetTranslation(pttexmtx0, 0.8 * 0.01 * mapOriginX, 0.0, 0.8 * 0.01 * mapOriginZ); + setMatrixTranslation(pttexmtx0, [0.8 * 0.01 * mapOriginX, 0.0, 0.8 * 0.01 * mapOriginZ]); const postRotate0 = mat4.create(); mat4.fromRotation(postRotate0, 1.0, [3, -1, 1]); const postTexMtx0 = this.mb.genPostTexMtx((dst: mat4, ctx: MaterialRenderContext) => { @@ -565,7 +565,7 @@ export class StandardMapMaterial extends StandardMaterial { const pttexmtx1 = mat4.create(); mat4.fromScaling(pttexmtx1, [0.005, 0.005, 0.005]); - mat4SetTranslation(pttexmtx1, 0.5 * 0.01 * mapOriginX, 0.0, 0.5 * 0.01 * mapOriginZ); + setMatrixTranslation(pttexmtx1, [0.5 * 0.01 * mapOriginX, 0.0, 0.5 * 0.01 * mapOriginZ]); const postRotate1 = mat4.create(); mat4.fromRotation(postRotate1, 1.0, [1, -1, 3]); const postTexMtx1 = this.mb.genPostTexMtx((dst: mat4, ctx: MaterialRenderContext) => { @@ -585,7 +585,7 @@ export class StandardMapMaterial extends StandardMaterial { const pttexmtx2 = mat4.create(); const postTexMtx2 = this.mb.genPostTexMtx((dst: mat4, ctx: MaterialRenderContext) => { mat4.fromScaling(pttexmtx2, [0.01, 0.01, 0.01]); - mat4SetTranslation(pttexmtx2, 0.01 * mapOriginX + ctx.sceneCtx.animController.envAnimValue0, 0.0, 0.01 * mapOriginZ); + setMatrixTranslation(pttexmtx2, [0.01 * mapOriginX + ctx.sceneCtx.animController.envAnimValue0, 0.0, 0.01 * mapOriginZ]); mat4.mul(pttexmtx2, rot67deg, pttexmtx2); mat4.mul(dst, pttexmtx2, ctx.sceneCtx.viewToWorldMtx); mat4.mul(dst, postRotate2, dst); @@ -607,7 +607,7 @@ export class StandardMapMaterial extends StandardMaterial { const pttexmtx3 = mat4.create(); const postTexMtx3 = this.mb.genPostTexMtx((dst: mat4, ctx: MaterialRenderContext) => { mat4.fromScaling(pttexmtx3, [0.01, 0.01, 0.01]); - mat4SetTranslation(pttexmtx3, 0.01 * mapOriginX, 0.0, 0.01 * mapOriginZ + ctx.sceneCtx.animController.envAnimValue1); + setMatrixTranslation(pttexmtx3, [0.01 * mapOriginX, 0.0, 0.01 * mapOriginZ + ctx.sceneCtx.animController.envAnimValue1]); mat4.mul(dst, pttexmtx3, ctx.sceneCtx.viewToWorldMtx); mat4.mul(dst, postRotate3, dst); mat4SetRow(dst, 2, 0.0, 0.0, 0.0, 1.0); @@ -863,7 +863,7 @@ class StandardObjectMaterial extends StandardMaterial { const indStage = this.mb.genIndTexStage(); const indTexMtx = this.mb.genIndTexMtx((dst: mat4, ctx: MaterialRenderContext) => { mat4.fromScaling(dst, [0.5, 0.5, 0.0]); - mat4SetValue(dst, 0, 3, 0.125); // The matrix scale must be stored in the first row's w + dst[12] = 0.125; // The matrix scale must be stored in the first row's w }); const nbtTex = this.texFetcher.getTexture(this.cache, this.shader.nbtTexId!, true)!; const nbtTexMap = this.mb.genTexMap(makeMaterialTexture(nbtTex)); @@ -890,7 +890,7 @@ class StandardObjectMaterial extends StandardMaterial { const pttexmtx = this.mb.genPostTexMtx((dst: mat4, ctx: MaterialRenderContext) => { const s = 0.5 * 3.0 * ((this.shader.nbtParams >> 4) / 7.0 - 1.0); mat4.fromScaling(dst, [s, s, 0.0]); - mat4SetTranslation(dst, 0.0, 0.0, 1.0); + setMatrixTranslation(dst, [0.0, 0.0, 1.0]); }); // Matrix comes from TEX0MTXIDX const binrmTexCoord = this.mb.genTexCoord(GX.TexGenType.MTX2x4, GX.TexGenSrc.BINRM, GX.TexGenMatrix.TEXMTX0, false, getGXPostTexGenMatrix(pttexmtx)); @@ -1394,7 +1394,7 @@ class WaterMaterial extends MaterialBase { this.mb.setTexMtx(3, (dst: mat4, ctx: MaterialRenderContext) => { mat4.identity(dst); - mat4SetValue(dst, 1, 3, ctx.sceneCtx.animController.envAnimValue0); + dst[13] = ctx.sceneCtx.animController.envAnimValue0; }); const texMap0 = this.mb.genTexMap(makeOpaqueColorTextureDownscale2x()); @@ -1418,8 +1418,8 @@ class WaterMaterial extends MaterialBase { mat4.mul(texmtx4, rot45deg, texmtx4); this.mb.setTexMtx(4, (dst: mat4, matCtx: MaterialRenderContext) => { mat4.copy(dst, texmtx4); - mat4SetValue(dst, 0, 3, matCtx.sceneCtx.animController.envAnimValue1); - mat4SetValue(dst, 1, 3, matCtx.sceneCtx.animController.envAnimValue1); + dst[12] = matCtx.sceneCtx.animController.envAnimValue1; + dst[13] = matCtx.sceneCtx.animController.envAnimValue1; }); const texCoord2 = this.mb.genTexCoord(GX.TexGenType.MTX2x4, GX.TexGenSrc.TEX0, GX.TexGenMatrix.TEXMTX4); @@ -1516,8 +1516,8 @@ class FurMaterial extends MaterialBase { const texMap2 = this.mb.genTexMap(this.factory.getWavyTexture()); this.mb.setTexMtx(1, (dst: mat4, matCtx: MaterialRenderContext) => { mat4.fromTranslation(dst, [0.25 * matCtx.sceneCtx.animController.envAnimValue0, 0.25 * matCtx.sceneCtx.animController.envAnimValue1, 0.0]); - mat4SetValue(dst, 0, 0, 0.0125); - mat4SetValue(dst, 1, 1, 0.0125); + dst[0] = 0.0125; + dst[5] = 0.0125; }); const texCoord2 = this.mb.genTexCoord(GX.TexGenType.MTX2x4, GX.TexGenSrc.POS, GX.TexGenMatrix.TEXMTX1); @@ -1613,8 +1613,8 @@ export class HeatShimmerMaterial extends MaterialBase { const pttexmtx0 = this.mb.genPostTexMtx((dst: mat4, matCtx: MaterialRenderContext) => { mat4.fromScaling(dst, [7.0, 7.0, 1.0]); - mat4SetValue(dst, 0, 3, matCtx.sceneCtx.animController.envAnimValue0 * 10.0); - mat4SetValue(dst, 1, 3, -matCtx.sceneCtx.animController.envAnimValue1 * 10.0); + dst[12] = matCtx.sceneCtx.animController.envAnimValue0 * 10.0; + dst[13] = matCtx.sceneCtx.animController.envAnimValue1 * -10.0; }); const texCoord1 = this.mb.genTexCoord(GX.TexGenType.MTX3x4, GX.TexGenSrc.TEX0, undefined, undefined, getGXPostTexGenMatrix(pttexmtx0)); @@ -1698,7 +1698,7 @@ export class FaultyTVMaterial extends MaterialBase { this.mb.setTexMtx(0, (dst: mat4, matCtx: MaterialRenderContext) => { mat4.fromScaling(dst, [0.2, 0.2, 1.0]); - mat4SetValue(dst, 1, 3, -matCtx.sceneCtx.animController.envAnimValue0); + dst[13] = -matCtx.sceneCtx.animController.envAnimValue0; }); const texCoord1 = this.mb.genTexCoord(GX.TexGenType.MTX2x4, GX.TexGenSrc.TEX0, GX.TexGenMatrix.TEXMTX0); @@ -1707,8 +1707,8 @@ export class FaultyTVMaterial extends MaterialBase { this.mb.setTexMtx(1, (dst: mat4, matCtx: MaterialRenderContext) => { mat4.fromScaling(dst, [0.25, 0.25, 1.0]); mat4.mul(dst, rot45, dst); - mat4SetValue(dst, 0, 3, matCtx.sceneCtx.animController.envAnimValue1); - mat4SetValue(dst, 1, 3, matCtx.sceneCtx.animController.envAnimValue1); + dst[12] = matCtx.sceneCtx.animController.envAnimValue1; + dst[13] = matCtx.sceneCtx.animController.envAnimValue1; }); const texCoord2 = this.mb.genTexCoord(GX.TexGenType.MTX2x4, GX.TexGenSrc.TEX0, GX.TexGenMatrix.TEXMTX1); diff --git a/src/StarFoxAdventures/shapes.ts b/src/StarFoxAdventures/shapes.ts index 0c00925f9..219525c1a 100644 --- a/src/StarFoxAdventures/shapes.ts +++ b/src/StarFoxAdventures/shapes.ts @@ -10,12 +10,11 @@ import { GfxRendererLayer, GfxRenderInst, GfxRenderInstManager, setSortKeyDepth, import { compilePartialVtxLoader, compileVtxLoaderMultiVat, GX_Array, GX_VtxAttrFmt, GX_VtxDesc, LoadedVertexData, LoadedVertexDraw, LoadedVertexLayout, VtxLoader } from '../gx/gx_displaylist.js'; import * as GX_Material from '../gx/gx_material.js'; import { createInputLayout, DrawParams, MaterialParams } from '../gx/gx_render.js'; -import { transformVec3Mat4w1 } from '../MathHelpers.js'; +import { setMatrixTranslation, transformVec3Mat4w1, Vec3Zero } from '../MathHelpers.js'; import { nArray } from '../util.js'; import { MaterialRenderContext, SFAMaterial, StandardMapMaterial } from './materials.js'; import { ModelRenderContext } from './models.js'; import { setGXMaterialOnRenderInst } from './render.js'; -import { mat4SetTranslation } from './util.js'; import { LightType } from './WorldLights.js'; export interface ShapeRenderContext { @@ -293,7 +292,7 @@ export class Shape { for (let i = 0; i < drawParams.u_PosMtx.length; i++) { // XXX: this is the game's peculiar way of creating normal matrices mat4.copy(scratchMaterialParams.u_TexMtx[i], drawParams.u_PosMtx[i]); - mat4SetTranslation(scratchMaterialParams.u_TexMtx[i], 0, 0, 0); + setMatrixTranslation(scratchMaterialParams.u_TexMtx[i], Vec3Zero); mat4.mul(scratchMaterialParams.u_TexMtx[i], scratchMaterialParams.u_TexMtx[i], descaleMtx); // The following line causes glitches due to an issue related to computeNormalMatrix's method of detecting uniform scaling. // computeNormalMatrix(scratchMaterialParams.u_TexMtx[i], drawParams.u_PosMtx[i]); diff --git a/src/StarFoxAdventures/util.ts b/src/StarFoxAdventures/util.ts index 2e1ae16c4..8617d84fd 100644 --- a/src/StarFoxAdventures/util.ts +++ b/src/StarFoxAdventures/util.ts @@ -71,16 +71,6 @@ export function mat4FromRowMajor( ); } -export function mat4SetValue(mtx: mat4, row: number, col: number, m: number): void { - mtx[4 * col + row] = m; -} - -export function mat4SetTranslation(mtx: mat4, x: number, y: number, z: number): void { - mat4SetValue(mtx, 0, 3, x); - mat4SetValue(mtx, 1, 3, y); - mat4SetValue(mtx, 2, 3, z); -} - // Compute model matrix from scale, rotation, and translation. // This version is unique to SFA: // Rotations are applied in the order: Z (roll), then X (pitch), then Y (yaw). diff --git a/src/StarFoxAdventures/world.ts b/src/StarFoxAdventures/world.ts index 73d56c3a6..1cc6c1ef4 100644 --- a/src/StarFoxAdventures/world.ts +++ b/src/StarFoxAdventures/world.ts @@ -17,7 +17,7 @@ import { ObjectManager, ObjectInstance, ObjectUpdateContext } from './objects.js import { EnvfxManager } from './envfx.js'; import { SFARenderer, SceneRenderContext, SFARenderLists } from './render.js'; import { MapInstance, loadMap } from './maps.js'; -import { dataSubarray, mat4SetTranslation, readVec3 } from './util.js'; +import { dataSubarray, readVec3 } from './util.js'; import { ModelRenderContext } from './models.js'; import { MaterialFactory } from './materials.js'; import { SFAAnimationController } from './animation.js'; @@ -28,7 +28,7 @@ import { SFATextureFetcher } from './textures.js'; import { SphereMapManager } from './SphereMaps.js'; import { computeViewMatrix } from '../Camera.js'; import { nArray } from '../util.js'; -import { transformVec3Mat4w0, transformVec3Mat4w1 } from '../MathHelpers.js'; +import { setMatrixTranslation, transformVec3Mat4w0, transformVec3Mat4w1, Vec3Zero } from '../MathHelpers.js'; import { GfxRenderCache } from '../gfx/render/GfxRenderCache.js'; const scratchVec0 = vec3.create(); @@ -131,7 +131,7 @@ export class World { computeViewMatrix(worldView, sceneCtx.viewerInput.camera); const worldViewSR = scratchMtx1; mat4.copy(worldViewSR, worldView); - mat4SetTranslation(worldViewSR, 0, 0, 0); + setMatrixTranslation(worldViewSR, Vec3Zero); for (let light of probedLights) { if (light.type & typeMask) {