Skip to content

Commit

Permalink
优化有色开关纹理显示
Browse files Browse the repository at this point in the history
  • Loading branch information
CS-LX authored Dec 29, 2023
1 parent ea60a34 commit e13f443
Showing 1 changed file with 77 additions and 32 deletions.
109 changes: 77 additions & 32 deletions Gigavolt/Block/Source/GVSwitchBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,35 @@
using Engine.Graphics;
using Engine.Media;

namespace Game {
public class GVSwitchBlock : MountedGVElectricElementBlock, IPaintableBlock {
namespace Game
{
public class GVSwitchBlock : MountedGVElectricElementBlock, IPaintableBlock
{
public const int Index = 820;

public readonly BlockMesh m_standaloneBlockMesh = new();
public readonly BlockMesh[] m_blockMeshesByIndex = new BlockMesh[12];
public readonly BlockMesh switchBodyMesh = new();
public readonly BlockMesh switchLeverMesh = new();
public readonly BlockMesh[] m_blockMeshesByIndex_Body = new BlockMesh[12];
public readonly BlockMesh[] m_blockMeshesByIndex_Lever = new BlockMesh[12];
public readonly BoundingBox[][] m_collisionBoxesByIndex = new BoundingBox[12][];

public readonly Texture2D WhiteTexture = Texture2D.Load(new Image(1, 1) { Pixels = { [0] = Color.White } });

public override void Initialize() {
public override void Initialize()
{
Model model = ContentManager.Get<Model>("Models/Switch");
Matrix boneAbsoluteTransform = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh("Body").ParentBone);
Matrix boneAbsoluteTransform2 = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh("Lever").ParentBone);
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 2; j++) {
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 2; j++)
{
int num = (i << 1) | j;
Matrix matrix = i >= 4 ? i != 4 ? Matrix.CreateRotationX((float)Math.PI) * Matrix.CreateTranslation(0.5f, 1f, 0.5f) : Matrix.CreateTranslation(0.5f, 0f, 0.5f) : Matrix.CreateRotationX((float)Math.PI / 2f) * Matrix.CreateTranslation(0f, 0f, -0.5f) * Matrix.CreateRotationY(i * (float)Math.PI / 2f) * Matrix.CreateTranslation(0.5f, 0.5f, 0.5f);
Matrix matrix2 = Matrix.CreateRotationX(j == 0 ? MathUtils.DegToRad(30f) : MathUtils.DegToRad(-75f));
m_blockMeshesByIndex[num] = new BlockMesh();
m_blockMeshesByIndex[num]
//主体模型
m_blockMeshesByIndex_Body[num] = new BlockMesh();
m_blockMeshesByIndex_Body[num]
.AppendModelMeshPart(
model.FindMesh("Body").MeshParts[0],
boneAbsoluteTransform * matrix,
Expand All @@ -34,7 +42,10 @@ public override void Initialize() {
false,
Color.White
);
m_blockMeshesByIndex[num]
m_collisionBoxesByIndex[num] = new[] { m_blockMeshesByIndex_Body[num].CalculateBoundingBox() };
//拉杆模型
m_blockMeshesByIndex_Lever[num] = new BlockMesh();
m_blockMeshesByIndex_Lever[num]
.AppendModelMeshPart(
model.FindMesh("Lever").MeshParts[0],
boneAbsoluteTransform2 * matrix2 * matrix,
Expand All @@ -44,11 +55,10 @@ public override void Initialize() {
false,
Color.White
);
m_collisionBoxesByIndex[num] = new[] { m_blockMeshesByIndex[num].CalculateBoundingBox() };
}
}
Matrix matrix3 = Matrix.CreateRotationY(-(float)Math.PI / 2f) * Matrix.CreateRotationZ((float)Math.PI / 2f);
m_standaloneBlockMesh.AppendModelMeshPart(
switchBodyMesh.AppendModelMeshPart(
model.FindMesh("Body").MeshParts[0],
boneAbsoluteTransform * matrix3,
false,
Expand All @@ -57,7 +67,7 @@ public override void Initialize() {
false,
Color.White
);
m_standaloneBlockMesh.AppendModelMeshPart(
switchLeverMesh.AppendModelMeshPart(
model.FindMesh("Lever").MeshParts[0],
boneAbsoluteTransform2 * matrix3,
false,
Expand All @@ -75,33 +85,48 @@ public override void Initialize() {
public override int GetFace(int value) => (Terrain.ExtractData(value) >> 1) & 7;
public static int SetFace(int value, int face) => Terrain.ReplaceData(value, (Terrain.ExtractData(value) & -15) | ((face & 7) << 1));

public override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain, ComponentMiner componentMiner, int value, TerrainRaycastResult raycastResult) {
public override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain, ComponentMiner componentMiner, int value, TerrainRaycastResult raycastResult)
{
BlockPlacementData result = default;
result.Value = SetFace(SetLeverState(value, false), raycastResult.CellFace.Face);
result.CellFace = raycastResult.CellFace;
return result;
}

public override void GetDropValues(SubsystemTerrain subsystemTerrain, int oldValue, int newValue, int toolLevel, List<BlockDropValue> dropValues, out bool showDebris) {
public override void GetDropValues(SubsystemTerrain subsystemTerrain, int oldValue, int newValue, int toolLevel, List<BlockDropValue> dropValues, out bool showDebris)
{
dropValues.Add(new BlockDropValue { Value = Terrain.MakeBlockValue(Index, 0, Terrain.ExtractData(SetFace(SetLeverState(oldValue, false), 0))), Count = 1 });
showDebris = true;
}

public override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value) {
public override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value)
{
int num = CalculateIndex(value);
return num >= m_collisionBoxesByIndex.Length ? null : m_collisionBoxesByIndex[num];
}

public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z) {
public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
{
int num = CalculateIndex(value);
if (num < m_blockMeshesByIndex.Length) {
if (num < m_blockMeshesByIndex_Lever.Length)
{
int? blockColor = GetColor(Terrain.ExtractData(value));
generator.GenerateMeshVertices(
this,
x,
y,
z,
m_blockMeshesByIndex[num],
m_blockMeshesByIndex_Body[num],
Color.White,
null,
geometry.SubsetOpaque
);
generator.GenerateMeshVertices(
this,
x,
y,
z,
m_blockMeshesByIndex_Lever[num],
blockColor.HasValue ? SubsystemPalette.GetColor(generator, blockColor) : Color.White,
null,
blockColor.HasValue ? geometry.GetGeometry(WhiteTexture).SubsetOpaque : geometry.SubsetOpaque
Expand All @@ -120,12 +145,22 @@ public override void GenerateTerrainVertices(BlockGeometryGenerator generator, T
}
}

public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData) {
public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
{
int? blockColor = GetColor(Terrain.ExtractData(value));
environmentData = environmentData ?? BlocksManager.m_defaultEnvironmentData;
BlocksManager.DrawMeshBlock(
primitivesRenderer,
m_standaloneBlockMesh,
switchBodyMesh,
environmentData.SubsystemTerrain == null ? BlocksTexturesManager.DefaultBlocksTexture : environmentData.SubsystemTerrain.SubsystemAnimatedTextures.AnimatedBlocksTexture,
color,
2f * size,
ref matrix,
environmentData
);
BlocksManager.DrawMeshBlock(
primitivesRenderer,
switchLeverMesh,
blockColor.HasValue ? WhiteTexture :
environmentData.SubsystemTerrain == null ? BlocksTexturesManager.DefaultBlocksTexture : environmentData.SubsystemTerrain.SubsystemAnimatedTextures.AnimatedBlocksTexture,
blockColor.HasValue ? color * SubsystemPalette.GetColor(environmentData, blockColor) : color,
Expand All @@ -147,10 +182,12 @@ public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int valu
value
);

public override GVElectricConnectorType? GetGVConnectorType(SubsystemTerrain terrain, int value, int face, int connectorFace, int x, int y, int z) {
public override GVElectricConnectorType? GetGVConnectorType(SubsystemTerrain terrain, int value, int face, int connectorFace, int x, int y, int z)
{
int face2 = GetFace(value);
if (face == face2
&& SubsystemGVElectricity.GetConnectorDirection(face2, 0, connectorFace).HasValue) {
&& SubsystemGVElectricity.GetConnectorDirection(face2, 0, connectorFace).HasValue)
{
return GVElectricConnectorType.Output;
}
return null;
Expand All @@ -160,7 +197,8 @@ public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int valu

public override bool IsNonDuplicable_(int value) => ((Terrain.ExtractData(value) >> 4) & 1023) > 0;

public override IEnumerable<int> GetCreativeValues() {
public override IEnumerable<int> GetCreativeValues()
{
yield return Terrain.MakeBlockValue(Index);
yield return Terrain.MakeBlockValue(Index, 0, SetColor(0, 0));
yield return Terrain.MakeBlockValue(Index, 0, SetColor(0, 8));
Expand All @@ -181,19 +219,23 @@ public override IEnumerable<int> GetCreativeValues() {
public override string GetCategory(int value) => GetColor(Terrain.ExtractData(value)).HasValue ? "GV Electrics Multiple" : "GV Electrics Regular";
public override int GetDisplayOrder(int value) => GetColor(Terrain.ExtractData(value)).HasValue ? 10 : base.GetDisplayOrder(value);

public override string GetDisplayName(SubsystemTerrain subsystemTerrain, int value) {
public override string GetDisplayName(SubsystemTerrain subsystemTerrain, int value)
{
int? paintColor = GetColor(Terrain.ExtractData(value));
return SubsystemPalette.GetName(subsystemTerrain, paintColor, base.GetDisplayName(subsystemTerrain, value));
}

public override int GetConnectionMask(int value) {
public override int GetConnectionMask(int value)
{
int? color = GetColor(Terrain.ExtractData(value));
return color.HasValue ? 1 << color.Value : int.MaxValue;
}

public static int? GetColor(int data) {
public static int? GetColor(int data)
{
int? result = (data >> 14) & 0xF;
switch (result.Value) {
switch (result.Value)
{
case 0: return null;
case <= 7:
result--;
Expand All @@ -202,9 +244,12 @@ public override int GetConnectionMask(int value) {
return result;
}

public static int SetColor(int data, int? color) {
if (color.HasValue) {
if (color.Value < 7) {
public static int SetColor(int data, int? color)
{
if (color.HasValue)
{
if (color.Value < 7)
{
color++;
}
return (data & -245761) | ((color.Value & 0xF) << 14);
Expand All @@ -216,4 +261,4 @@ public static int SetColor(int data, int? color) {

public int Paint(SubsystemTerrain subsystemTerrain, int value, int? color) => Terrain.ReplaceData(value, SetColor(Terrain.ExtractData(value), color));
}
}
}

0 comments on commit e13f443

Please sign in to comment.