From 7bb7119d33b859b078dc2a1aeb825186c0e46973 Mon Sep 17 00:00:00 2001 From: SpiralP Date: Tue, 7 Jul 2020 01:53:08 -0700 Subject: [PATCH] Custom Models v2 (#541) * Custom Models v2 allows for more modifiers and more animations per part * anims now use a specified axis * remove default value for a,b,c,d --- MCGalaxy/Entity/CustomModel.cs | 43 +++++++++++++------ MCGalaxy/Network/Packets/Packet.cs | 68 +++++++++++++++++++++++------- MCGalaxy/Player/Player.CPE.cs | 8 ++-- 3 files changed, 86 insertions(+), 33 deletions(-) diff --git a/MCGalaxy/Entity/CustomModel.cs b/MCGalaxy/Entity/CustomModel.cs index 0247b3ca2..ad6c057c3 100644 --- a/MCGalaxy/Entity/CustomModel.cs +++ b/MCGalaxy/Entity/CustomModel.cs @@ -51,24 +51,41 @@ namespace MCGalaxy { Y = 0.0f, Z = 0.0f, }; - public CustomModelAnim anim = CustomModelAnim.None; - public float animModifier = 1.0f; + public CustomModelAnim[] anims; public bool fullbright = false; public bool firstPersonArm = false; } - public enum CustomModelAnim { + public class CustomModelAnim { + public CustomModelAnimType type = CustomModelAnimType.None; + public CustomModelAnimAxis axis; + + public float a; + public float b; + public float c; + public float d; + } + + public enum CustomModelAnimType { None = 0, Head = 1, - LeftLeg = 2, - RightLeg = 3, - LeftArm = 4, - RightArm = 5, - SpinX = 6, - SpinY = 7, - SpinZ = 8, - SpinXVelocity = 9, - SpinYVelocity = 10, - SpinZVelocity = 11, + LeftLegX = 2, + RightLegX = 3, + LeftArmX = 4, + LeftArmZ = 5, + RightArmX = 6, + RightArmZ = 7, + Spin = 8, + SpinVelocity = 9, + SinRotate = 10, + SinRotateVelocity = 11, + SinTranslate = 12, + SinTranslateVelocity = 13 + } + + public enum CustomModelAnimAxis { + X = 0, + Y = 1, + Z = 2, } } diff --git a/MCGalaxy/Network/Packets/Packet.cs b/MCGalaxy/Network/Packets/Packet.cs index a8d63f8de..ebb45c424 100644 --- a/MCGalaxy/Network/Packets/Packet.cs +++ b/MCGalaxy/Network/Packets/Packet.cs @@ -460,6 +460,7 @@ namespace MCGalaxy.Network { public const int MaxCustomModels = 64; public const int MaxCustomModelParts = 64; + public const int MaxCustomModelAnims = 4; public static byte[] DefineModel(byte modelId, CustomModel customModel) { // 116 = 1 + 1 + 64 + 1 + 2*4 + 3*4 + 2*3*4 + 2*2 + 1 byte[] buffer = new byte[116]; @@ -521,8 +522,57 @@ namespace MCGalaxy.Network { } public static byte[] DefineModelPart(byte modelId, CustomModelPart part) { - // 104 = 1 + 1 + 3*4 + 3*4 + 6*(2*2 + 2*2) + 3*4 + 3*4 + 1 + 4 + 1 + // v1: 104 = (1 + 1 + 3*4 + 3*4 + 6*(2*2 + 2*2) + 3*4 + 3*4) + 1 + 4 + 1 byte[] buffer = new byte[104]; + int i = WriteDefineModelPart(buffer, modelId, part); + + // ignore animations + i++; + i += 4; + + // write bool flags + byte flags = 0; + flags |= (byte)((part.fullbright ? 1 : 0) << 0); + flags |= (byte)((part.firstPersonArm ? 1 : 0) << 1); + + buffer[i++] = flags; + + return buffer; + } + + public static byte[] DefineModelPartV2(byte modelId, CustomModelPart part) { + // v2: 167 = (1 + 1 + 3*4 + 3*4 + 6*(2*2 + 2*2) + 3*4 + 3*4) + 4*(1 + 4*4) + 1 + byte[] buffer = new byte[167]; + int i = WriteDefineModelPart(buffer, modelId, part); + + for (int j = 0; j < MaxCustomModelAnims; j++) { + var anim = part.anims[j]; + + buffer[i++] = (byte)( + ((byte)anim.type & 0x3F) | ((byte)anim.axis << 6) + ); + + NetUtils.WriteF32(anim.a, buffer, i); + i += 4; + NetUtils.WriteF32(anim.b, buffer, i); + i += 4; + NetUtils.WriteF32(anim.c, buffer, i); + i += 4; + NetUtils.WriteF32(anim.d, buffer, i); + i += 4; + } + + // write bool flags + byte flags = 0; + flags |= (byte)((part.fullbright ? 1 : 0) << 0); + flags |= (byte)((part.firstPersonArm ? 1 : 0) << 1); + + buffer[i++] = flags; + + return buffer; + } + + public static int WriteDefineModelPart(byte[] buffer, byte modelId, CustomModelPart part) { int i = 0; buffer[i++] = Opcode.CpeDefineModelPart; buffer[i++] = modelId; @@ -571,21 +621,7 @@ namespace MCGalaxy.Network { NetUtils.WriteF32(part.rotation.Z, buffer, i); i += 4; - // write anim - buffer[i++] = (byte)part.anim; - - // write animModifier - NetUtils.WriteF32(part.animModifier, buffer, i); - i += 4; - - // write bool flags - byte flags = 0; - flags |= (byte)((part.fullbright ? 1 : 0) << 0); - flags |= (byte)((part.firstPersonArm ? 1 : 0) << 1); - - buffer[i++] = flags; - - return buffer; + return i; } public static byte[] UndefineModel(byte modelId) { diff --git a/MCGalaxy/Player/Player.CPE.cs b/MCGalaxy/Player/Player.CPE.cs index 999673e15..cb70e8150 100644 --- a/MCGalaxy/Player/Player.CPE.cs +++ b/MCGalaxy/Player/Player.CPE.cs @@ -15,9 +15,9 @@ or implied. See the Licenses for the specific language governing permissions and limitations under the Licenses. */ -using System; -using MCGalaxy.Network; -using BlockID = System.UInt16; +using System; +using MCGalaxy.Network; +using BlockID = System.UInt16; namespace MCGalaxy { public sealed class CpeExtension { @@ -49,7 +49,7 @@ namespace MCGalaxy { new CpeExtension(CpeExt.InstantMOTD), new CpeExtension(CpeExt.FastMap), new CpeExtension(CpeExt.ExtTextures), new CpeExtension(CpeExt.SetHotbar), new CpeExtension(CpeExt.SetSpawnpoint), new CpeExtension(CpeExt.VelocityControl), - new CpeExtension(CpeExt.CustomParticles), new CpeExtension(CpeExt.CustomModels), + new CpeExtension(CpeExt.CustomParticles), new CpeExtension(CpeExt.CustomModels, 2), #if TEN_BIT_BLOCKS new CpeExtension(CpeExt.ExtBlocks), #endif