Skip to content

Commit

Permalink
Merge pull request #99 from MilchRatchet/dev-branch
Browse files Browse the repository at this point in the history
General maintenance
  • Loading branch information
MilchRatchet authored Jan 31, 2024
2 parents 46cd7c6 + 1a1fe56 commit 2838712
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 84 deletions.
5 changes: 5 additions & 0 deletions LibReplanetizer/Level Objects/Gameplay/DirectionalLight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ namespace LibReplanetizer.LevelObjects
{
public class DirectionalLight : LevelObject
{
// This is the implementation for the DirectionalLights in the gameplay_ntsc file.
// These are unused and in parts still in little endian.
// This here is merely for documentation and serialization.
// Do not mistake this for the actual directional lights which are stored in the engine file.

public const int ELEMENTSIZE = 0x40;

public Vector4 colorA;
Expand Down
74 changes: 52 additions & 22 deletions LibReplanetizer/Level Objects/Gameplay/Moby.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2018-2023, The Replanetizer Contributors.
// Copyright (C) 2018-2023, The Replanetizer Contributors.
// Replanetizer is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation,
Expand Down Expand Up @@ -694,6 +694,8 @@ public class IngameMobyMemory
public byte group { get; set; }
public byte mClass { get; set; }
public byte alpha { get; set; }
public uint pClass { get; set; }
public uint pChain { get; set; }
public float scale { get; set; }
public byte updateDistance { get; set; }
public byte visible { get; set; }
Expand All @@ -703,22 +705,28 @@ public class IngameMobyMemory
public Rgb24 color { get; set; }
public int light { get; set; }
public Vector4 rotation { get; set; }
public byte previousAnimationFrame { get; set; }
public byte animationFrame { get; set; }
public byte updateID { get; set; }
public byte previousAnimationID { get; set; }
public byte animationID { get; set; }
public float unk54 { get; set; }
public float animationBlend { get; set; }
public float unk58 { get; set; }
public float framerate { get; set; }
public float frameSpeed { get; set; }
public uint pAnimationLayers { get; set; }
public uint pPreviousAnimationData { get; set; }
public uint pCurrentAnimationData { get; set; }
public uint pUpdate { get; set; }
public uint pVars { get; set; }
public byte unk7C { get; set; }
public byte unk7D { get; set; }
public byte unk7E { get; set; }
public byte animState { get; set; }
public byte shadow { get; set; }
public uint unk80 { get; set; }
public int unk84 { get; set; }
public int unk88 { get; set; }
public ushort oClass { get; set; }
public ushort UID { get; set; }
public Matrix3x4 transformation { get; set; }

public IngameMobyMemory()
{
Expand Down Expand Up @@ -750,6 +758,8 @@ public void UpdateRC1(byte[] memory, int offset)
group = memory[offset + 0x21];
mClass = memory[offset + 0x22];
alpha = memory[offset + 0x23];
pClass = ReadUint(memory, offset + 0x24);
pChain = ReadUint(memory, offset + 0x28);
scale = ReadFloat(memory, offset + 0x2C);

updateDistance = memory[offset + 0x30];
Expand All @@ -768,18 +778,24 @@ public void UpdateRC1(byte[] memory, int offset)
float rotZ = ReadFloat(memory, offset + 0x48);
float rotW = ReadFloat(memory, offset + 0x4C);

previousAnimationFrame = memory[offset + 0x50];
animationFrame = memory[offset + 0x51];
updateID = memory[offset + 0x52];
previousAnimationID = memory[offset + 0x52];
animationID = memory[offset + 0x53];
unk54 = ReadFloat(memory, offset + 0x54);
animationBlend = ReadFloat(memory, offset + 0x54);
unk58 = ReadFloat(memory, offset + 0x58);
framerate = ReadFloat(memory, offset + 0x5C);
frameSpeed = ReadFloat(memory, offset + 0x5C);

pAnimationLayers = ReadUint(memory, offset + 0x60);
pPreviousAnimationData = ReadUint(memory, offset + 0x68);
pCurrentAnimationData = ReadUint(memory, offset + 0x6C);

pUpdate = ReadUint(memory, offset + 0x74);
pVars = ReadUint(memory, offset + 0x78);
unk7C = memory[offset + 0x7C];
unk7D = memory[offset + 0x7D];
unk7E = memory[offset + 0x7E];
animState = memory[offset + 0x7F];
shadow = memory[offset + 0x7F];

unk80 = ReadUint(memory, offset + 0x80);
unk84 = ReadInt(memory, offset + 0x84);
Expand All @@ -789,16 +805,12 @@ public void UpdateRC1(byte[] memory, int offset)

UID = ReadUshort(memory, offset + 0xB2);

transformation = ReadMatrix3x4(memory, offset + 0xC0);

collPos = new Vector4(collX, collY, collZ, collW);
position = new Vector4(X, Y, Z, W);
rotation = new Vector4(rotX, rotY, rotZ, rotW);
color = Color.FromRgb((byte) red, (byte) green, (byte) blue).ToPixel<Rgb24>();

// TODO: Understand this better, this is just a hack for now.
if (oClass == 71 || oClass == 190 || oClass == 192 || oClass == 208 || oClass == 229)
{
rotation = rotation + new Vector4(1.5707964f, 0.0f, 0.0f, 0.0f);
}
}

public void UpdateRC23(byte[] memory, int offset)
Expand Down Expand Up @@ -830,15 +842,18 @@ public void UpdateRC23(byte[] memory, int offset)
byte red = memory[offset + 0x3B];
light = ReadInt(memory, offset + 0x3C);

previousAnimationFrame = memory[offset + 0x40];
animationFrame = memory[offset + 0x41];
updateID = memory[offset + 0x42];
previousAnimationID = memory[offset + 0x42];
animationID = memory[offset + 0x43];
unk54 = ReadFloat(memory, offset + 0x44);
animationBlend = ReadFloat(memory, offset + 0x44);
unk58 = ReadFloat(memory, offset + 0x48);
framerate = ReadFloat(memory, offset + 0x4C);
frameSpeed = ReadFloat(memory, offset + 0x4C);

oClass = ReadUshort(memory, offset + 0xAA);

transformation = ReadMatrix3x4(memory, offset + 0xC0);

float rotX = ReadFloat(memory, offset + 0xF0);
float rotY = ReadFloat(memory, offset + 0xF4);
float rotZ = ReadFloat(memory, offset + 0xF8);
Expand Down Expand Up @@ -902,13 +917,28 @@ public void UpdateFromMemory(byte[] mobyMemory, int offset, List<Model> models)

mobyID = memory.UID;
groupIndex = memory.group;
position = memory.position.Xyz;
rotation = Quaternion.FromEulerAngles(memory.rotation.Xyz);
scale = new Vector3(memory.scale / modelSize);
color = memory.color;
light = memory.light;

UpdateTransformMatrix();
modelMatrix.M11 = memory.transformation.M11 * memory.scale;
modelMatrix.M12 = memory.transformation.M12 * memory.scale;
modelMatrix.M13 = memory.transformation.M13 * memory.scale;
modelMatrix.M14 = 0.0f;
modelMatrix.M21 = memory.transformation.M21 * memory.scale;
modelMatrix.M22 = memory.transformation.M22 * memory.scale;
modelMatrix.M23 = memory.transformation.M23 * memory.scale;
modelMatrix.M24 = 0.0f;
modelMatrix.M31 = memory.transformation.M31 * memory.scale;
modelMatrix.M32 = memory.transformation.M32 * memory.scale;
modelMatrix.M33 = memory.transformation.M33 * memory.scale;
modelMatrix.M34 = 0.0f;
modelMatrix.M41 = memory.transformation.M14 + position.X;
modelMatrix.M42 = memory.transformation.M24 + position.Y;
modelMatrix.M43 = memory.transformation.M34 + position.Z;
modelMatrix.M44 = 1.0f;
position = modelMatrix.ExtractTranslation();
rotation = modelMatrix.ExtractRotation();
scale = modelMatrix.ExtractScale();
}
}
}
10 changes: 5 additions & 5 deletions LibReplanetizer/LibReplanetizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.89.9.3" />
<PackageReference Include="NLog" Version="5.2.5" />
<PackageReference Include="OpenTK.Mathematics" Version="4.8.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.2" />
<PackageReference Include="System.Resources.Extensions" Version="7.0.0" />
<PackageReference Include="ImGui.NET" Version="1.90.1.1" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="OpenTK.Mathematics" Version="4.8.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.2" />
<PackageReference Include="System.Resources.Extensions" Version="8.0.0" />
</ItemGroup>

</Project>
30 changes: 22 additions & 8 deletions LibReplanetizer/Models/Animation/Frame.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2018-2023, The Replanetizer Contributors.
// Copyright (C) 2018-2023, The Replanetizer Contributors.
// Replanetizer is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation,
Expand Down Expand Up @@ -145,14 +145,28 @@ public Matrix4 GetRotationMatrix(int bone, Frame nextFrame, float blend)
Quaternion rot1 = (Quaternion) baseRotation;
Quaternion rot2 = (Quaternion) nextRotation;

// Quaternion.Slerp does not work for some reason, it always returns the first operand.
Quaternion rotation = new Quaternion(
rot1.X + (rot2.X - rot1.X) * blend,
rot1.Y + (rot2.Y - rot1.Y) * blend,
rot1.Z + (rot2.Z - rot1.Z) * blend,
rot1.W + (rot2.W - rot1.W) * blend);
rot1 *= (1.0f - blend);
rot2 *= blend;

return Matrix4.CreateFromQuaternion(rotation);
float dotProduct = rot1.X * rot2.X + rot1.Y * rot2.Y + rot1.Z * rot2.Z + rot1.W * rot2.W;

if (dotProduct >= 0.0f)
{
rot1 += rot2;
}
else
{
rot1 -= rot2;
}

float normSquared = rot1.X * rot1.X + rot1.Y * rot1.Y + rot1.Z * rot1.Z + rot1.W * rot1.W;

if (normSquared > 0.0f)
{
rot1 *= 1.0f / MathF.Sqrt(normSquared);
}

return Matrix4.CreateFromQuaternion(rot1);
}

public Vector3? GetScaling(int bone, Frame nextFrame, float blend)
Expand Down
8 changes: 4 additions & 4 deletions LibReplanetizer/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,12 @@ public static float[] GetVertices(FileStream fs, int vertexPointer, int uvPointe
}

//Get indices
public static ushort[] GetIndices(FileStream fs, int indexPointer, int faceCount, int offset = 0)
public static ushort[] GetIndices(FileStream fs, int indexPointer, int indexCount, int offset = 0)
{
ushort[] indexBuffer = new ushort[faceCount];
byte[] indexBlock = ReadBlock(fs, indexPointer, faceCount * sizeof(ushort));
ushort[] indexBuffer = new ushort[indexCount];
byte[] indexBlock = ReadBlock(fs, indexPointer, indexCount * sizeof(ushort));

for (int i = 0; i < faceCount; i++)
for (int i = 0; i < indexCount; i++)
{
ushort face = ReadUshort(indexBlock, i * sizeof(ushort));
indexBuffer[i] = (ushort) (face - offset);
Expand Down
4 changes: 2 additions & 2 deletions LibReplanetizer/Models/TieModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public TieModel(FileStream fs, byte[] tieBlock, int num)
size = 1.0f;

textureConfig = GetTextureConfigs(fs, texturePointer, textureCount, TIETEXELEMSIZE);
int faceCount = GetFaceCount();
int indexCount = GetFaceCount();

//Get vertex buffer float[vertX, vertY, vertZ, normX, normY, normZ] and UV array float[U, V] * vertexCount
vertexBuffer = GetVertices(fs, vertexPointer, uvPointer, vertexCount, TIEVERTELEMSIZE, TIEUVELEMSIZE);

//Get index buffer ushort[i] * faceCount
indexBuffer = GetIndices(fs, indexPointer, faceCount);
indexBuffer = GetIndices(fs, indexPointer, indexCount);
}

public byte[] SerializeHead(int offStart)
Expand Down
1 change: 0 additions & 1 deletion LibReplanetizer/Parsers/RatchetFileParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ protected List<Texture> GetTexturesMobyload(int texturePointer, int textureDataP

for (int i = 0; i < textureList.Count; i++)
{
textureList[i].mipMapCount = 0;
int length;
if (i < textureList.Count - 1)
{
Expand Down
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,23 @@ Replanetizer is still in an early state of development. Its feature set entails:
- Rigged models including animations (`.gltf`, `.dae`, `.obj`)
- Level as a model (`.obj`)
- Collision (`.obj`)
- Live inspection of a game running in emulator
- Live inspection of a game running in emulator ([RPCS3](https://github.com/RPCS3/rpcs3))

Support for Ratchet Deadlocked is available but low priority.

> If you are looking for a level editor for the PS2 versions of the games, use [Wrench](https://github.com/chaoticgd/wrench) instead.
# Binaries

You can find prebuilt binaries of Replanetizer in the releases section:

- [Release Builds](https://github.com/RatchetModding/Replanetizer/releases)

# Usage

Extract a `.psarc` file from a game using seperate tools (PSArcTool.exe for example).
Use File -> Open, and select a `engine.ps3` file inside a level folder. It is recommended to keep the folder structure present in the `.psarc` file.
First you need to unpack the game's files so that Replanetizer can access it. These files are packed in a file called `PS3Arc.psarc`. If you have the game installed in `RPCS3`, you can find the `PS3Arc.psarc` of Ratchet and Clank 1 (NPEA00385) in `[RPCS3 Directory]/dev_hdd0/game/NPEA00385/USRDIR/rc1/ps3data`. You can unpack this file using tools like [PSArc-cl](https://github.com/MilchRatchet/PSArcInterface). It is recommended to not change the folder structure of the `PS3Arc.psarc` contents after unpacking as Replanetizer may otherwise fail to find certain files.

To load a level, start Replanetizer and use File -> Open, then select a `engine.ps3` file inside one of the level folders.

To apply changes to the game, you need to either repack the files into a `.psarc` file or use an incomplete `.psarc` file. Any file missing in a `.psarc` file will be loaded from the correspondent location outside the `.psarc` file.

Expand Down Expand Up @@ -84,7 +93,7 @@ OpenGL 3.3 is used for the rendering.

# Licensing

Replanetizer is Copyright (C) 2018-2023, The Replanetizer Contributors.
Replanetizer is Copyright (C) 2018-2024, The Replanetizer Contributors.

Replanetizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
6 changes: 3 additions & 3 deletions Replanetizer/Frames/ModelFrame.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2018-2023, The Replanetizer Contributors.
// Copyright (C) 2018-2023, The Replanetizer Contributors.
// Replanetizer is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation,
Expand Down Expand Up @@ -226,7 +226,7 @@ private void RenderTree()
filterUpper = filter.ToUpper();
}

if (ImGui.BeginChild("TreeView", childSize, false, ImGuiWindowFlags.AlwaysVerticalScrollbar))
if (ImGui.BeginChild("TreeView", childSize, ImGuiChildFlags.None, ImGuiWindowFlags.AlwaysVerticalScrollbar))
{
RenderSubTree("Moby", sortedMobyModels, level.textures);
RenderSubTree("Tie", sortedTieModels, level.textures);
Expand Down Expand Up @@ -337,7 +337,7 @@ public override void Render(float deltaTime)
float colW = ImGui.GetColumnWidth() - 10.0f;
System.Numerics.Vector2 colSize = new System.Numerics.Vector2(colW, height);

if (ImGui.BeginChild("TextureAndPropertyView", colSize, false, ImGuiWindowFlags.AlwaysVerticalScrollbar))
if (ImGui.BeginChild("TextureAndPropertyView", colSize, ImGuiChildFlags.None, ImGuiWindowFlags.AlwaysVerticalScrollbar))
{
if (selectedModel != null)
{
Expand Down
4 changes: 2 additions & 2 deletions Replanetizer/Frames/TextureFrame.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2018-2021, The Replanetizer Contributors.
// Copyright (C) 2018-2021, The Replanetizer Contributors.
// Replanetizer is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation,
Expand Down Expand Up @@ -41,7 +41,7 @@ public static void RenderTextureList(List<Texture> textures, float itemSizeX, Di
{
Texture t = textures[i];

ImGui.BeginChild("imageChild_" + prefix + i, ITEM_SIZE, false);
ImGui.BeginChild("imageChild_" + prefix + i, ITEM_SIZE, ImGuiChildFlags.None);
ImGui.Image((IntPtr) textureIds[t].textureID, IMAGE_SIZE);
string idText = prefix + t.id;
float idWidth = ImGui.CalcTextSize(idText).X;
Expand Down
2 changes: 1 addition & 1 deletion Replanetizer/ModelLists/MobyModelsRC1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
025F=Helipack
025C=Sandmouse house
0260=Thrusterpack
0261=Hyropack
0261=Hydropack
0264=Yellow Enemy
0265=UNKNOWN (Usually in rivers)
0266=Mapper
Expand Down
2 changes: 1 addition & 1 deletion Replanetizer/Renderer/AnimationRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public override void Render(RendererPayload payload)

// For Example: RaC 1 bomb glove idles in the last frame of the animation despite the first one being selected.
// TODO: Understand what is happening in these cases.
if (anim != null && mob != null && mob.memory != null && mob.memory.animState == 0)
if (anim != null && mob != null && mob.memory != null)
{
animationFrame--;
if (animationFrame < 0)
Expand Down
12 changes: 6 additions & 6 deletions Replanetizer/Replanetizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.89.9.3" />
<PackageReference Include="ImGui.NET" Version="1.90.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.2.5" />
<PackageReference Include="OpenTK" Version="4.8.1" />
<PackageReference Include="OpenTK.Mathematics" Version="4.8.1" />
<PackageReference Include="OpenTK.Windowing.GraphicsLibraryFramework" Version="4.8.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.2" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="OpenTK" Version="4.8.2" />
<PackageReference Include="OpenTK.Mathematics" Version="4.8.2" />
<PackageReference Include="OpenTK.Windowing.GraphicsLibraryFramework" Version="4.8.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 2838712

Please sign in to comment.