Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from magcius:master #124

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/DiddyKongRacing/DkrTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class DkrTexture {

private hasBeenDestroyed = false;

constructor(device: GfxDevice, cache: GfxRenderCache, private pixels: Uint8ClampedArray, headerData: Uint8Array) {
constructor(device: GfxDevice, cache: GfxRenderCache, private pixels: Uint8ClampedArray, headerData: Uint8Array, private debugName: string) {
const view = new DataView(headerData.buffer);
this.width = view.getUint8(0x00);
this.height = view.getUint8(0x01);
Expand Down Expand Up @@ -70,17 +70,17 @@ export class DkrTexture {

const gfxTexture = device.createTexture(makeTextureDescriptor2D(GfxFormat.U8_RGBA_NORM, this.width, this.height, 1));
device.uploadTextureData(gfxTexture, 0, [this.pixels.slice(frameStart, frameEnd)]);
device.setResourceName(gfxTexture, `${debugName} / ${i}`);

this.textureMappingsArray[i][0].gfxSampler = sampler;
this.textureMappingsArray[i][0].gfxTexture = gfxTexture;
}
}

public destroy(device: GfxDevice): void {
if(!this.hasBeenDestroyed) {
for(const textureMapping of this.textureMappingsArray) {
if (!this.hasBeenDestroyed) {
for (const textureMapping of this.textureMappingsArray)
device.destroyTexture(textureMapping[0].gfxTexture!);
}
this.hasBeenDestroyed = true;
}
}
Expand Down
67 changes: 28 additions & 39 deletions src/DiddyKongRacing/DkrTextureCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { GfxDevice } from "../gfx/platform/GfxPlatform.js";
import { GfxRenderCache } from "../gfx/render/GfxRenderCache.js";

export class DkrTextureCache {
private textures3d: { [k: string]: DkrTexture } = {};
private textures2d: { [k: string]: DkrTexture } = {};
private textures3d = new Map<number, DkrTexture>();
private textures2d = new Map<number, DkrTexture>();

constructor(private device: GfxDevice, private cache: GfxRenderCache, private dataManager: DataManager) {
}
Expand All @@ -19,78 +19,67 @@ export class DkrTextureCache {

for (let i = 0; i < indices.length; i++) {
const index = indices[i];
if (this.textures3d.has(index))
continue;

const header = this.dataManager.get3dTextureHeader(index).createTypedArray(Uint8Array);
const imageData = imageDatas[i];
this.textures3d[index] = new DkrTexture(this.device, this.cache, imageData.data, header);
this.textures3d.set(index, new DkrTexture(this.device, this.cache, imageData.data, header, `DkrTexture 3D ${index}`));
}

callback();
}

// 3D texture = texture used mainly in 3d geometry.
public get3dTexture(index: number, callback: Function): void {
if(!!this.textures3d[index]) {
// Texture was found in cache, so just return it.
callback(this.textures3d[index]);
public get3dTexture(index: number, callback: (texture: DkrTexture) => void): void {
if (this.textures3d.has(index)) {
callback(this.textures3d.get(index)!);
} else {
// Texture was not found, so it needs to be loaded.
this.dataManager.get3dTexture(index).then((tex) => {
if (!!this.textures3d[index]) {
// Texture has already been loaded, so just return it.
callback(this.textures3d[index]);
} else {
if (!this.textures3d.has(index)) {
const headerData = this.dataManager.get3dTextureHeader(index).createTypedArray(Uint8Array);
this.textures3d[index] = new DkrTexture(this.device, this.cache, tex.data, headerData);
callback(this.textures3d[index]);
this.textures3d.set(index, new DkrTexture(this.device, this.cache, tex.data, headerData, `DkrTexture 3D ${index}`));
}

callback(this.textures3d.get(index)!);
});
}
}

// 2D texture = texture used mainly in sprites & particles.
public get2dTexture(index: number, callback: Function): void {
if(!!this.textures2d[index]) {
// Texture was found in cache, so just return it.
callback(this.textures2d[index]);
if (this.textures2d.has(index)) {
callback(this.textures2d.get(index)!);
} else {
// Texture was not found, so it needs to be loaded.
this.dataManager.get2dTexture(index).then((tex) => {
if (!!this.textures2d[index]) {
// Texture has already been loaded, so just return it.
callback(this.textures2d[index]);
} else {
if (!this.textures2d.has(index)) {
const headerData = this.dataManager.get2dTextureHeader(index).createTypedArray(Uint8Array);
this.textures2d[index] = new DkrTexture(this.device, this.cache, tex.data, headerData);
callback(this.textures2d[index]);
this.textures2d.set(index, new DkrTexture(this.device, this.cache, tex.data, headerData, `DkrTexture 2D ${index}`));
}

callback(this.textures2d.get(index)!);
});
}
}

public advanceTextureFrames(deltaTime: number): void {
const keys = Object.keys(this.textures3d);
for(const key of keys) {
this.textures3d[key].advanceFrame(deltaTime);
}
for (const v of this.textures3d.values())
v.advanceFrame(deltaTime);
}

public scrollTextures(texScrollers: { texIndex: number, scrollU: number, scrollV: number }[], dt: number): void {
for(const texScroller of texScrollers) {
if(!!this.textures3d[texScroller.texIndex]){
const tex: DkrTexture = this.textures3d[texScroller.texIndex];
for (const texScroller of texScrollers) {
if (this.textures3d.has(texScroller.texIndex)) {
const tex = this.textures3d.get(texScroller.texIndex)!;
tex.scrollTexture(texScroller.scrollU, texScroller.scrollV, dt);
}
}
}

public destroy(device: GfxDevice): void {
const keys3d = Object.keys(this.textures3d);
for(const key of keys3d) {
this.textures3d[key].destroy(device);
}
const keys2d = Object.keys(this.textures2d);
for(const key of keys2d) {
this.textures2d[key].destroy(device);
}
for (const v of this.textures3d.values())
v.destroy(device);
for (const v of this.textures2d.values())
v.destroy(device);
}
}
4 changes: 2 additions & 2 deletions src/ZeldaTwilightPrincess/LegacyActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { GfxRenderInstManager } from '../gfx/render/GfxRenderInstManager.js';
import { ColorKind } from '../gx/gx_render.js';
import { colorNewFromRGBA8 } from '../Color.js';
import { calc_mtx, MtxTrans, mDoMtx_ZXYrotM, mDoMtx_YrotM } from '../ZeldaWindWaker/m_do_mtx.js';
import { cM__Short2Rad } from '../ZeldaWindWaker/SComponent.js';
import { cM_s2rad } from '../ZeldaWindWaker/SComponent.js';

const scratchVec3a = vec3.create();

Expand Down Expand Up @@ -886,7 +886,7 @@ function spawnLegacyActor(globals: dGlobals, legacy: d_a_noclip_legacy, actor: f
} else if (type >= 2) {
const child = buildChildModel(rarc, `bmdr/rd_bow.bmd`);
child.setParentJoint(m, `yubiL`);
mat4.rotateX(child.modelMatrix, child.modelMatrix, cM__Short2Rad(0x4000));
mat4.rotateX(child.modelMatrix, child.modelMatrix, cM_s2rad(0x4000));
}

m.bindANK1(parseBCK(rarc, `bck/rd_wait01.bck`));
Expand Down
10 changes: 5 additions & 5 deletions src/ZeldaTwilightPrincess/d_a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { JPABaseEmitter } from "../Common/JSYSTEM/JPA.js";
import { BTIData } from "../Common/JSYSTEM/JUTTexture.js";
import { MathConstants, Vec3UnitY, invlerp, saturate, scaleMatrix } from "../MathHelpers.js";
import { TSDraw } from "../SuperMarioGalaxy/DDraw.js";
import { cLib_addCalc, cLib_addCalc2, cLib_addCalcAngleS2, cLib_addCalcAngleS_, cLib_chaseF, cLib_targetAngleX, cLib_targetAngleY, cM__Short2Rad, cM__Deg2Short, cM_atan2s } from "../ZeldaWindWaker/SComponent.js";
import { cLib_addCalc, cLib_addCalc2, cLib_addCalcAngleS2, cLib_addCalcAngleS_, cLib_chaseF, cLib_targetAngleX, cLib_targetAngleY, cM_s2rad, cM_deg2s, cM_atan2s } from "../ZeldaWindWaker/SComponent.js";
import { dBgW } from "../ZeldaWindWaker/d_bg.js";
import { MtxPosition, MtxTrans, calc_mtx, mDoMtx_XrotM, mDoMtx_YrotM, mDoMtx_YrotS, mDoMtx_ZXYrotM, mDoMtx_ZrotM } from "../ZeldaWindWaker/m_do_mtx.js";
import { GfxDevice } from "../gfx/platform/GfxPlatform.js";
Expand Down Expand Up @@ -3100,7 +3100,7 @@ class d_a_obj_magLiftRot extends fopAc_ac_c {
private modeMove(deltaTimeFrames: number): void {
this.speedF = cLib_chaseF(this.speedF, 8.0, 0.05 * deltaTimeFrames);

const speed = cM__Deg2Short(this.speedF);
const speed = cM_deg2s(this.speedF);
this.rot[2] = cLib_addCalcAngleS_(this.rot[2], this.rotTarget, 1, speed * deltaTimeFrames, 1);

if ((this.rotTarget - this.rot[2]) === 0) {
Expand Down Expand Up @@ -3213,7 +3213,7 @@ class d_a_e_hp extends fopAc_ac_c {
public override execute(globals: dGlobals, deltaTimeFrames: number): void {
super.execute(globals, deltaTimeFrames);

this.height = 170.0 + Math.sin(cM__Short2Rad(this.counter * 1000)) * 20;
this.height = 170.0 + Math.sin(cM_s2rad(this.counter * 1000)) * 20;

this.mtx_set();

Expand Down Expand Up @@ -3290,8 +3290,8 @@ class d_a_e_hp extends fopAc_ac_c {
swing = 6000.0

this.swingRate = cLib_addCalc2(this.swingRate, swing, 0.1, swing * 0.1);
this.swingAngle[0] = cLib_addCalcAngleS2(this.swingAngle[0], this.swingRate * Math.sin(cM__Short2Rad(this.counter * 2000)), 8, 0x400);
this.swingAngle[2] = this.swingRate * Math.sin(cM__Short2Rad(this.counter * 2500.0));
this.swingAngle[0] = cLib_addCalcAngleS2(this.swingAngle[0], this.swingRate * Math.sin(cM_s2rad(this.counter * 2000)), 8, 0x400);
this.swingAngle[2] = this.swingRate * Math.sin(cM_s2rad(this.counter * 2500.0));
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/ZeldaTwilightPrincess/d_kankyo_wether.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { MathConstants, computeMatrixWithoutTranslation, invlerp, saturate } fro
import { DeviceProgram } from "../Program.js";
import { TDDraw } from "../SuperMarioGalaxy/DDraw.js";
import { TextureMapping } from "../TextureHolder.js";
import { cLib_addCalc, cM__Short2Rad, cM_rndF, cM_rndFX } from "../ZeldaWindWaker/SComponent.js";
import { cLib_addCalc, cM_s2rad, cM_rndF, cM_rndFX } from "../ZeldaWindWaker/SComponent.js";
import { PeekZManager, PeekZResult } from "../ZeldaWindWaker/d_dlst_peekZ.js";
import { mDoLib_project, mDoLib_projectFB } from "../ZeldaWindWaker/m_do_ext.js";
import { MtxTrans, calc_mtx, mDoMtx_XrotM, mDoMtx_ZrotM } from "../ZeldaWindWaker/m_do_mtx.js";
Expand Down Expand Up @@ -520,9 +520,9 @@ export class dKankyo_sun_Packet {

private lensflareBaseSize: number = 160.0;
private lensflareCount: number = 16.0;
private lensflareAngleSteps: number[] = [cM__Short2Rad(0x1000), cM__Short2Rad(0x1C71)];
private lensflareAngleSteps: number[] = [cM_s2rad(0x1000), cM_s2rad(0x1C71)];
private lensflareSizes: number[] = [0.1, 1.1, 0.2, 0.4];
private lensflareWidth: number = cM__Short2Rad(1000.0);
private lensflareWidth: number = cM_s2rad(1000.0);

private drawLenzflare(globals: dGlobals, ddraw: TDDraw, renderInstManager: GfxRenderInstManager, viewerInput: ViewerRenderInput): void {
if (this.visibility <= 0.1)
Expand Down Expand Up @@ -607,8 +607,8 @@ export class dKankyo_sun_Packet {
submitScratchRenderInst(renderInstManager, materialHelper, renderInst, viewerInput);
}

let angle0 = cM__Short2Rad(globals.counter * 0x00 - 0x07F6);
let angle1 = cM__Short2Rad(globals.counter * -0x0E + 0x416B);
let angle0 = cM_s2rad(globals.counter * 0x00 - 0x07F6);
let angle1 = cM_s2rad(globals.counter * -0x0E + 0x416B);
for (let i = 0; i < this.lensflareCount; i++) {
ddraw.begin(GX.Command.DRAW_TRIANGLES);

Expand Down Expand Up @@ -1393,7 +1393,7 @@ export class dKankyo_star_Packet {
scratchVec3a[2] = radiusXZ * 300.0 * Math.cos(angle);

angle += angleIncr;
angleIncr += cM__Short2Rad(0x09C4);
angleIncr += cM_s2rad(0x09C4);

radius += (1.0 + 3.0 * (radius / 200.0 ** 3.0));
if (radius > 200.0)
Expand Down Expand Up @@ -2076,7 +2076,7 @@ function wether_move_vrkumo(globals: dGlobals, deltaTimeFrames: number): void {
windPower = 0.3;

const windDir = dKyw_get_wind_vec(envLight);
const windPitch = vecPitch(windDir), windAngle = vecAngle(windDir) + cM__Short2Rad(24575.0);
const windPitch = vecPitch(windDir), windAngle = vecAngle(windDir) + cM_s2rad(24575.0);
const cosPitch = Math.cos(windPitch);
const sinAngle = Math.sin(windAngle), cosAngle = Math.cos(windAngle);
pkt.cloudScrollX = (pkt.cloudScrollX + cosPitch * sinAngle * windPower * 0.0014 * deltaTimeFrames) % 1.0;
Expand Down Expand Up @@ -2115,11 +2115,11 @@ export function dKyw_wind_set(globals: dGlobals): void {

let windAngleXZ = 0, windAngleY = 0;
if (windDirFlag === 2)
windAngleXZ = cM__Short2Rad(-0x4000);
windAngleXZ = cM_s2rad(-0x4000);
else if (windDirFlag === 4)
windAngleXZ = cM__Short2Rad(0x4000);
windAngleXZ = cM_s2rad(0x4000);
else if (windDirFlag === 5)
windAngleXZ = cM__Short2Rad(0x7FFF);
windAngleXZ = cM_s2rad(0x7FFF);

const targetWindVecX = Math.sin(windAngleXZ) * Math.cos(windAngleY);
const targetWindVecY = Math.sin(windAngleY);
Expand Down
4 changes: 2 additions & 2 deletions src/ZeldaTwilightPrincess/d_particle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { TDDraw } from "../SuperMarioGalaxy/DDraw.js";
import { TextureMapping } from "../TextureHolder.js";
import { nArray } from "../util.js";
import { ViewerRenderInput } from "../viewer.js";
import { cLib_addCalc2, cM__Short2Rad } from "../ZeldaWindWaker/SComponent.js";
import { cLib_addCalc2, cM_s2rad } from "../ZeldaWindWaker/SComponent.js";
import { dGlobals } from "./Main.js";
import * as GX from '../gx/gx_enum.js';
import { ColorKind } from "../gx/gx_render.js";
Expand Down Expand Up @@ -142,7 +142,7 @@ export class dPa_control_c {
if (pos !== null)
vec3.copy(baseEmitter.globalTranslation, pos);
if (rot !== null)
computeModelMatrixR(baseEmitter.globalRotation, cM__Short2Rad(rot[0]), cM__Short2Rad(rot[1]), cM__Short2Rad(rot[2]));
computeModelMatrixR(baseEmitter.globalRotation, cM_s2rad(rot[0]), cM_s2rad(rot[1]), cM_s2rad(rot[2]));
if (scale !== null)
baseEmitter.setGlobalScale(scale);

Expand Down
22 changes: 11 additions & 11 deletions src/ZeldaWindWaker/Grass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { colorCopy, colorFromRGBA } from '../Color.js';
import { dKy_GxFog_set } from './d_kankyo.js';
import { dBgS_GndChk } from './d_bg.js';
import { getMatrixTranslation } from '../MathHelpers.js';
import { cM__Short2Rad } from './SComponent.js';
import { cM_s2rad } from './SComponent.js';

function createMaterialHelper(material: GXMaterial): GXMaterialHelperGfx {
// Patch material.
Expand Down Expand Up @@ -326,8 +326,8 @@ export class FlowerPacket {
public calc(frameCount: number): void {
// Idle animation updates
for (let i = 0; i < 8; i++) {
const theta = Math.cos(cM__Short2Rad(1000.0 * (frameCount + 0xfa * i)));
this.anims[i].rotationX = cM__Short2Rad(1000.0 + 1000.0 * theta);
const theta = Math.cos(cM_s2rad(1000.0 * (frameCount + 0xfa * i)));
this.anims[i].rotationX = cM_s2rad(1000.0 + 1000.0 * theta);
}

// @TODO: Hit checks
Expand Down Expand Up @@ -643,11 +643,11 @@ export class TreePacket {
public calc(frameCount: number): void {
// Idle animation updates
for (let i = 0; i < 8; i++) {
let theta = Math.cos(cM__Short2Rad(4000.0 * (frameCount + 0xfa * i)));
this.anims[i].topRotationY = cM__Short2Rad(100.0 + this.anims[i].initialRotationShort + 100.0 * theta);
let theta = Math.cos(cM_s2rad(4000.0 * (frameCount + 0xfa * i)));
this.anims[i].topRotationY = cM_s2rad(100.0 + this.anims[i].initialRotationShort + 100.0 * theta);

theta = Math.cos(cM__Short2Rad(1000.0 * (frameCount + 0xfa * i)));
this.anims[i].topRotationX = cM__Short2Rad(100 + 100 * theta);
theta = Math.cos(cM_s2rad(1000.0 * (frameCount + 0xfa * i)));
this.anims[i].topRotationX = cM_s2rad(100 + 100 * theta);
}

// @TODO: Hit checks
Expand Down Expand Up @@ -700,7 +700,7 @@ export class TreePacket {

mat4.fromYRotation(anim.trunkMtx, anim.trunkFallYaw);
mat4.rotateX(anim.trunkMtx, anim.trunkMtx, anim.trunkRotationX);
mat4.rotateY(anim.trunkMtx, anim.trunkMtx, cM__Short2Rad(anim.initialRotationShort) - anim.trunkFallYaw);
mat4.rotateY(anim.trunkMtx, anim.trunkMtx, cM_s2rad(anim.initialRotationShort) - anim.trunkFallYaw);
}

// Update grass packets
Expand Down Expand Up @@ -919,7 +919,7 @@ export class GrassPacket {
for (let i = 0; i < 8; i++) {
this.anims[i] = {
active: true,
rotationY: cM__Short2Rad(0x2000 * i),
rotationY: cM_s2rad(0x2000 * i),
rotationX: 0,
modelMtx: mat4.create(),
}
Expand Down Expand Up @@ -951,8 +951,8 @@ export class GrassPacket {

// Idle animation updates
for (let i = 0; i < 8; i++) {
let theta = Math.cos(cM__Short2Rad(windPower * (frameCount + 0xfa * i)));
this.anims[i].rotationX = cM__Short2Rad(windPower + windPower * theta);
let theta = Math.cos(cM_s2rad(windPower * (frameCount + 0xfa * i)));
this.anims[i].rotationX = cM_s2rad(windPower + windPower * theta);
}

// @TODO: Hit checks
Expand Down
18 changes: 11 additions & 7 deletions src/ZeldaWindWaker/SComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,23 @@ export function cM_rndFX(max: number): number {
}

export function cM_atan2s(y: number, x: number): number {
return cM__Rad2Short(Math.atan2(y, x));
return cM_rad2s(Math.atan2(y, x));
}

export function cM__Short2Rad(v: number): number {
return v * (Math.PI / 0x8000);
export function cM_rad2s(v: number): number {
return v * (0x8000 / Math.PI);
}

export function cM__Rad2Short(v: number): number {
return v * (0x8000 / Math.PI);
export function cM_deg2s(v: number): number {
return cM_rad2s(v * MathConstants.DEG_TO_RAD);
}

export function cM_s2rad(v: number): number {
return v * (Math.PI / 0x8000);
}

export function cM__Deg2Short(v: number): number {
return cM__Rad2Short(v * MathConstants.DEG_TO_RAD);
export function cM_sht2d(v: number): number {
return cM_s2rad(v) * MathConstants.RAD_TO_DEG;
}

export function cLib_targetAngleX(p0: ReadonlyVec3, p1: ReadonlyVec3): number {
Expand Down
Loading