Esempio n. 1
0
        public void Spatial()
        {
            var par1 = new CFrame(1, 2, 3) * CFrame.Angles(4, 5, 6);
            var par2 = new CFrame(4, 5, 6) * CFrame.Angles(1, 2, 3);

            var expected1 = new CFrame(2.284338f, -3.74210596f, -2.78898597f, -0.723131239f, -0.25124836f, 0.643393695f,
                                       -0.223633111f, 0.966485798f, 0.126068801f, -0.653505504f, -0.0527198762f, -0.755083561f);
            var actual1 = par1.toObjectSpace(par2);

            Assert.AreEqual(expected1, actual1, "toObjectSpace did not return expected results.");

            var expected2 = new CFrame(-3.26779175f, 4.68169117f, -4.18292999f, -0.522057056f, 0.697389245f,
                                       0.491024077f, 0.781638145f, 0.160808325f, 0.602646112f, 0.341318101f, 0.698418856f, -0.629057229f);
            var actual2 = par1.toWorldSpace(par2);

            Assert.AreEqual(expected2, actual2, "toWorldSpace did not return expected results.");

            var expected3 = new Vector3(2.284338f, -3.74210548f, -2.78898621f);
            var actual3   = par1.pointToObjectSpace(par2.p);

            Assert.AreEqual(expected3, actual3, "pointToObjectSpace did not return expected results.");

            var expected4 = new Vector3(-3.26779175f, 4.68169117f, -4.18292999f);
            var actual4   = par1.pointToWorldSpace(par2.p);

            Assert.AreEqual(expected4, actual4, "pointToWorldSpace did not return expected results.");

            var expected5 = new Vector3(3.14449883f, -7.21789789f, -3.87479973f);
            var actual5   = par1.vectorToObjectSpace(par2.p);

            Assert.AreEqual(expected5, actual5, "vectorToObjectSpace did not return expected results.");

            var expected6 = new Vector3(-4.26779175f, 2.68169117f, -7.18292999f);
            var actual6   = par1.vectorToWorldSpace(par2.p);

            Assert.AreEqual(expected6, actual6, "vectorToWorldSpace did not return expected results.");
        }
        public static StudioMdlWriter AssembleModel(Asset asset)
        {
            Folder content = RBXM.LoadFromAsset(asset);

            Rbx2Source.ScheduleTasks("GatherParts", "BuildMesh");
            Rbx2Source.PrintHeader("GATHERING PARTS");

            List <Part> parts = new List <Part>();

            AddParts(parts, content);

            if (parts.Count == 0)
            {
                throw new Exception("No parts were found inside of this asset!");
            }

            Part primaryPart = null;

            foreach (Part part in parts)
            {
                if (part.IsA("MeshPart") || part.Name == "Handle")
                {
                    primaryPart = part;
                    break;
                }
            }

            if (primaryPart == null) // k lol
            {
                primaryPart = parts[0];
            }

            primaryPart.Name = asset.ProductInfo.Name;

            // Mark the primaryPart's location as the center.
            CFrame rootCoord = primaryPart.CFrame;

            foreach (Part part in parts)
            {
                part.CFrame = rootCoord.toObjectSpace(part.CFrame);
            }

            Rbx2Source.MarkTaskCompleted("GatherParts");
            Rbx2Source.PrintHeader("BUILDING MESH");

            StudioMdlWriter writer = new StudioMdlWriter();

            BoneKeyframe skeleton = new BoneKeyframe();

            writer.Skeleton.Add(skeleton);

            List <Bone>     bones     = skeleton.Bones;
            List <Node>     nodes     = writer.Nodes;
            List <Triangle> triangles = writer.Triangles;

            Dictionary <string, Material> materials  = writer.Materials;
            Dictionary <string, int>      nameCounts = new Dictionary <string, int>();

            int numAssembledParts = 0;

            foreach (Part part in parts)
            {
                // Make sure this part has a unique name.
                string name = part.Name;
                if (nameCounts.ContainsKey(name))
                {
                    int count = ++nameCounts[name];
                    name     += count.ToString();
                    part.Name = name;
                }
                else
                {
                    nameCounts[name] = 0;
                }

                // Assemble the part.
                Material material = new Material();
                Mesh     geometry = Mesh.BakePart(part, material);

                if (geometry != null && geometry.FaceCount > 0)
                {
                    string task = "BuildGeometry_" + name;
                    Rbx2Source.ScheduleTasks(task);
                    Rbx2Source.Print("Building Geometry for {0}", name);

                    Bone bone = new Bone(name, primaryPart, part);
                    bone.C0 = part.CFrame;
                    bones.Add(bone);

                    Node node = bone.Node;
                    nodes.Add(node);

                    materials.Add(name, material);

                    for (int i = 0; i < geometry.FaceCount; i++)
                    {
                        Triangle tri = new Triangle();
                        tri.Node      = node;
                        tri.Mesh      = geometry;
                        tri.FaceIndex = i;
                        tri.Material  = name;
                        triangles.Add(tri);
                    }

                    Rbx2Source.MarkTaskCompleted(task);
                    numAssembledParts++;
                }
            }


            Rbx2Source.MarkTaskCompleted("BuildMesh");
            return(writer);
        }