Skip to content

Commit

Permalink
Star Fox Adventures: minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
magcius committed Dec 24, 2024
1 parent 61da0ab commit d171961
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 41 deletions.
7 changes: 3 additions & 4 deletions src/StarFoxAdventures/SphereMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ 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';
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';

Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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);
Expand Down
42 changes: 21 additions & 21 deletions src/StarFoxAdventures/materials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
5 changes: 2 additions & 3 deletions src/StarFoxAdventures/shapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]);
Expand Down
10 changes: 0 additions & 10 deletions src/StarFoxAdventures/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
6 changes: 3 additions & 3 deletions src/StarFoxAdventures/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit d171961

Please sign in to comment.