public static Matrix ReadCompressed(Reader reader, Pointer offset) { MapLoader l = MapLoader.Loader; ushort type = reader.ReadUInt16(); Vector4 vec = new Vector4(1f, 1f, 1f, 1f); Vector3? pos = null; Quaternion?rot = null; Matrix4x4? sclM = null; float x, y, z, w; /*the first byte & 0xF is the type * 1: translation * 2: rotation only * 3: translation & rotation * 7: translation, rotation, zoom (1 word, conv to float). zoom is basically scale for all axes * 11: translation, rotation, scale per axis (3 words, conv to float) * 15: translation, rotation, scale (6 words, conv to float)*/ int actualType = type < 128 ? (type & 0xF) : 128; if (actualType == 1 || actualType == 3 || actualType == 7 || actualType == 11 || actualType == 15) { // Translation x = (float)reader.ReadInt16() / (float)512; y = (float)reader.ReadInt16() / (float)512; z = (float)reader.ReadInt16() / (float)512; pos = new Vector3(x, y, z); } if (actualType == 2 || actualType == 3 || actualType == 7 || actualType == 11 || actualType == 15) { // Rotation w = (float)reader.ReadInt16() / (float)Int16.MaxValue; x = (float)reader.ReadInt16() / (float)Int16.MaxValue; y = (float)reader.ReadInt16() / (float)Int16.MaxValue; z = (float)reader.ReadInt16() / (float)Int16.MaxValue; //rot = new Quaternion(x, y, z, w); //rot = Quaternion.Euler(rot.Value.eulerAngles); Animation.Component.AnimQuaternion q = new Animation.Component.AnimQuaternion(); q.quaternion = new Quaternion(x, y, z, w); Matrix rotM = q.ToMatrix(); rot = rotM.GetRotation(convertAxes: false); } if (actualType == 7) { // Zoom scale x = (float)reader.ReadInt16() / (float)256; sclM = Matrix4x4.Scale(new Vector3(x, x, x)); } else if (actualType == 11) { // Axial scale x = (float)reader.ReadInt16() / (float)256; y = (float)reader.ReadInt16() / (float)256; z = (float)reader.ReadInt16() / (float)256; sclM = Matrix4x4.Scale(new Vector3(x, y, z)); } else if (actualType == 15) { // Matrix scale float m0 = (float)reader.ReadInt16() / (float)256; float m1 = (float)reader.ReadInt16() / (float)256; float m2 = (float)reader.ReadInt16() / (float)256; float m3 = (float)reader.ReadInt16() / (float)256; float m4 = (float)reader.ReadInt16() / (float)256; float m5 = (float)reader.ReadInt16() / (float)256; // Actually it should be like this, but oh well: /*sclM = new Matrix4x4(); * sclM.Value.SetColumn(0, new Vector4(m0, m1, m2, 0f)); * sclM.Value.SetColumn(1, new Vector4(m1, m3, m4, 0f)); * sclM.Value.SetColumn(2, new Vector4(m2, m4, m5, 0f)); * sclM.Value.SetColumn(3, new Vector4(0f, 0f, 0f, 1f));*/ sclM = Matrix4x4.Scale(new Vector3(m0, m3, m5)); } if (!pos.HasValue) { pos = Vector3.zero; } if (!rot.HasValue) { rot = Quaternion.identity; } Matrix mat = new Matrix(offset, type, Matrix4x4.TRS(pos.Value, rot.Value, Vector3.one), vec); if (sclM.HasValue) { mat.SetScaleMatrix(sclM.Value); } return(mat); }
public static Matrix Read(Reader reader, Pointer offset) { MapLoader l = MapLoader.Loader; //l.print("MATRIX " + offset); UInt32 type = Settings.s.game != Settings.Game.R2Revolution ? reader.ReadUInt32() : 0; // 0x02: always at the start of a transformation matrix Matrix mat = new Matrix(offset, type, new Matrix4x4(), null); if (Settings.s.engineVersion < Settings.EngineVersion.R3 && Settings.s.game != Settings.Game.R2Revolution) { Vector3 pos = Vector3.zero; if (Settings.s.platform != Settings.Platform.DC) { pos = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); } mat.m.SetColumn(0, new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), 0f)); mat.m.SetColumn(1, new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), 0f)); mat.m.SetColumn(2, new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), 0f)); Matrix4x4 sclMatrix = new Matrix4x4(); sclMatrix.SetColumn(0, new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), Settings.s.platform == Settings.Platform.DC ? reader.ReadSingle() : 0f)); sclMatrix.SetColumn(1, new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), Settings.s.platform == Settings.Platform.DC ? reader.ReadSingle() : 0f)); sclMatrix.SetColumn(2, new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), Settings.s.platform == Settings.Platform.DC ? reader.ReadSingle() : 0f)); if (Settings.s.platform == Settings.Platform.DC) { pos = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); } mat.m.SetColumn(3, new Vector4(pos.x, pos.y, pos.z, 1f)); sclMatrix.SetColumn(3, new Vector4(0, 0, 0, 1f)); mat.SetScaleMatrix(sclMatrix); /*if (type != 4) { * transMatrix.SetColumn(0, rotColX); * transMatrix.SetColumn(1, rotColY); * transMatrix.SetColumn(2, rotColZ); * } else { * transMatrix.SetColumn(0, sclColX); * transMatrix.SetColumn(1, sclColY); * transMatrix.SetColumn(2, sclColZ); * }*/ } else { mat.m.SetColumn(0, new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle())); mat.m.SetColumn(1, new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle())); mat.m.SetColumn(2, new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle())); mat.m.SetColumn(3, new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle())); mat.v = new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); /*mat.SetScaleMatrix(new Matrix4x4( * new Vector4(v.x, 0, 0, 0), * new Vector4(0,v.y, 0, 0), * new Vector4(0, 0, v.z, 0), * new Vector4(0, 0, 0, v.w)));*/ } if (Settings.s.game == Settings.Game.R2Revolution) { mat.type = reader.ReadUInt32(); // There's 0x8c more? } return(mat); }