Skip to content

Commit

Permalink
✨ Implement missing sections of mat1
Browse files Browse the repository at this point in the history
  • Loading branch information
pleonex committed Jan 14, 2020
1 parent b98b276 commit 0658b93
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 21 deletions.
1 change: 1 addition & 0 deletions Clypo.Console/Clypo.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<AssemblyName>Clypo.Console</AssemblyName>

<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
Expand Down
57 changes: 41 additions & 16 deletions Clypo/Binary2Clyt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,14 @@ void ReadMaterial()
uint numTexMatrix = (flags >> 2) & 0x3;
uint numTexCoordGen = (flags >> 4) & 0x3;
uint numTevStage = (flags >> 6) & 0x7;
uint hasAlphaCompare = (flags >> 9) & 0x01;
uint hasBlendMode = (flags >> 10) & 0x01;
bool hasAlphaCompare = ((flags >> 9) & 0x01) == 1;
bool hasBlendMode = ((flags >> 10) & 0x01) == 1;
material.UseTextureOnly = ((flags >> 11) & 0x01) == 1;
uint separateBlendMode = (flags >> 12) & 0x01;
uint hasIndirectParam = (flags >> 13) & 0x01;
bool separateBlendMode = ((flags >> 12) & 0x01) == 1;
bool hasIndirectParam = ((flags >> 13) & 0x01) == 1;
uint numProjTexGenParam = (flags >> 14) & 0x03;
uint hasFontShadowParam = (flags >> 16) & 0x01;
bool hasFontShadowParam = ((flags >> 16) & 0x01) == 1;

uint texMapOffset = 0x34;
uint texMatrixOffset = texMapOffset + (numTexMap * 4);
uint texCoordGenOffset = texMatrixOffset + (numTexMatrix * 0x14);
uint tevStageOffset = texCoordGenOffset + (numTexCoordGen * 4);
uint alphaCompareOffset = tevStageOffset + (numTevStage * 0xC);
uint blendModeOffset = alphaCompareOffset + 0x8;

reader.Stream.Seek(offset + texMapOffset);
for (int i = 0; i < numTexMap; i++) {
var entry = new TextureMapEntry();
entry.Index = reader.ReadUInt16();
Expand All @@ -189,7 +181,6 @@ void ReadMaterial()
material.TexMapEntries.Add(entry);
}

reader.Stream.Seek(offset + texMatrixOffset);
for (int i = 0; i < numTexMatrix; i++) {
var entry = new TextureMatrixEntry();
entry.Translation = new Vector2(reader.ReadSingle(), reader.ReadSingle());
Expand All @@ -199,12 +190,46 @@ void ReadMaterial()
material.TexMatrixEntries.Add(entry);
}

reader.Stream.Seek(offset + texCoordGenOffset);
for (int i = 0; i < numTexCoordGen; i++) {
material.TextureCoordGen.Add(reader.ReadSingle());
}

// TODO: Find a bclyt with the rest of sections
for (int i = 0; i < numTevStage; i++) {
var stage = new TevStage();
stage.Param1 = reader.ReadUInt32();
stage.Param2 = reader.ReadUInt32();
stage.Param3 = reader.ReadUInt32();
material.TevStages.Add(stage);
}

if (hasAlphaCompare) {
material.AlphaCompare = new AlphaCompare {
Function = reader.ReadUInt32(),
Reference = reader.ReadSingle(),
};
}

if (hasBlendMode) {
material.ColorBlendMode = new BlendMode {
BlendOperator = reader.ReadByte(),
SourceFactor = reader.ReadByte(),
DestinationFactor = reader.ReadByte(),
LogicOperator = reader.ReadByte(),
};
}

if (separateBlendMode) {
material.AlphaBlendMode = new BlendMode {
BlendOperator = reader.ReadByte(),
SourceFactor = reader.ReadByte(),
DestinationFactor = reader.ReadByte(),
LogicOperator = reader.ReadByte(),
};
}

if (hasIndirectParam || numProjTexGenParam > 0 || hasFontShadowParam) {
Console.WriteLine($"WARN: Material ({material.Name}) with unknown sections.");
}

clyt.Materials.Add(material);
reader.Stream.PopPosition();
Expand Down
37 changes: 33 additions & 4 deletions Clypo/Clyt2Binary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,13 @@ void WriteMaterials(Collection<Material> materials)

int flag = 0x00;
flag |= mat.TexMapEntries.Count;
flag |= (mat.TexMatrixEntries.Count << 2);
flag |= (mat.TextureCoordGen.Count << 4);
flag |= ((mat.UseTextureOnly ? 1 : 0) << 11);
flag |= mat.TexMatrixEntries.Count << 2;
flag |= mat.TextureCoordGen.Count << 4;
flag |= mat.TevStages.Count << 6;
flag |= ((mat.AlphaCompare != null) ? 1 : 0) << 9;
flag |= ((mat.ColorBlendMode != null) ? 1 : 0) << 10;
flag |= (mat.UseTextureOnly ? 1 : 0) << 11;
flag |= ((mat.AlphaBlendMode != null) ? 1 : 0) << 12;
// TODO: Find a bclyt with the rest of sections

writer.Write(flag);
Expand Down Expand Up @@ -250,6 +254,31 @@ void WriteMaterials(Collection<Material> materials)
foreach (var coord in mat.TextureCoordGen) {
writer.Write(coord);
}

foreach (var tev in mat.TevStages) {
writer.Write(tev.Param1);
writer.Write(tev.Param2);
writer.Write(tev.Param3);
}

if (mat.AlphaCompare != null) {
writer.Write(mat.AlphaCompare.Function);
writer.Write(mat.AlphaCompare.Reference);
}

if (mat.ColorBlendMode != null) {
writer.Write(mat.ColorBlendMode.BlendOperator);
writer.Write(mat.ColorBlendMode.SourceFactor);
writer.Write(mat.ColorBlendMode.DestinationFactor);
writer.Write(mat.ColorBlendMode.LogicOperator);
}

if (mat.AlphaBlendMode != null) {
writer.Write(mat.AlphaBlendMode.BlendOperator);
writer.Write(mat.AlphaBlendMode.SourceFactor);
writer.Write(mat.AlphaBlendMode.DestinationFactor);
writer.Write(mat.AlphaBlendMode.LogicOperator);
}
}
}

Expand All @@ -258,7 +287,7 @@ void WriteGroup(Group group)
writer.Write(group.Name, 0x10);
writer.Write((uint)group.Panels.Count);
foreach (var panel in group.Panels) {
writer.Write(panel, 0x10);
writer.Write(panel, 0x10, false);
}
}

Expand Down
28 changes: 28 additions & 0 deletions Clypo/Layout/AlphaCompare.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2019 SceneGate

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
namespace Clypo.Layout
{
public class AlphaCompare
{
public uint Function { get; set; }

public float Reference { get; set; }
}
}
32 changes: 32 additions & 0 deletions Clypo/Layout/BlendMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2019 SceneGate

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
namespace Clypo.Layout
{
public class BlendMode
{
public byte BlendOperator { get; set; }

public byte SourceFactor { get; set; }

public byte DestinationFactor { get; set; }

public byte LogicOperator { get; set; }
}
}
8 changes: 8 additions & 0 deletions Clypo/Layout/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,13 @@ public class Material
public Collection<TextureMatrixEntry> TexMatrixEntries { get; } = new Collection<TextureMatrixEntry>();

public Collection<float> TextureCoordGen { get; } = new Collection<float>();

public Collection<TevStage> TevStages { get; } = new Collection<TevStage>();

public AlphaCompare AlphaCompare { get; set; }

public BlendMode ColorBlendMode { get; set; }

public BlendMode AlphaBlendMode { get; set; }
}
}
30 changes: 30 additions & 0 deletions Clypo/Layout/TevStage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2019 SceneGate

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
namespace Clypo.Layout
{
public class TevStage
{
public uint Param1 { get; set; }

public uint Param2 { get; set; }

public uint Param3 { get; set; }
}
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ Clypo.Console.exe import-dir original input output
This software is license under the [MIT](https://choosealicense.com/licenses/mit/) license.

The specification of the BCLYT is based on assembly research in the game
_Attack of the Friday Monsters_ and information from [3dbrew](https://www.3dbrew.org/wiki/CLYT_format)
_Attack of the Friday Monsters_, information from [3dbrew](https://www.3dbrew.org/wiki/CLYT_format)
and part of the material section from [Every File Explorer](https://github.com/Gericom/EveryFileExplorer)
by Gericom.

0 comments on commit 0658b93

Please sign in to comment.