Ejemplo n.º 1
0
        public static kDOPTreeCompact ToCompact(kDOPCollisionTriangle[] oldTriangles, Vector3[] vertices)
        {
            var rootBound = new kDopUnCompressedNode
            {
                Min = new float[3],
                Max = new float[3]
            };

            for (int i = 0; i < 3; i++)
            {
                rootBound.Max[i] = float.MaxValue;
                rootBound.Min[i] = -float.MaxValue;
            }

            if (oldTriangles.IsEmpty())
            {
                return(new kDOPTreeCompact
                {
                    RootBound = rootBound,
                    Nodes = new kDOPCompact[0],
                    Triangles = new kDOPCollisionTriangle[0]
                });
            }

            var buildTriangles = new kDopBuildTriangle[oldTriangles.Length];

            for (int i = 0; i < oldTriangles.Length; i++)
            {
                kDOPCollisionTriangle oldTri = oldTriangles[i];
                buildTriangles[i] = new kDopBuildTriangle(oldTri, vertices[oldTri.Vertex1], vertices[oldTri.Vertex2], vertices[oldTri.Vertex3]);
            }

            int numNodes = 0;

            if (buildTriangles.Length > 5)
            {
                numNodes = 1;
                while ((buildTriangles.Length + numNodes - 1) / numNodes > 10)
                {
                    numNodes *= 2;
                }
                numNodes = 2 * numNodes;
            }

            var nodes = new kDopCompressedNode[numNodes];

            for (int i = 0; i < numNodes; i++)
            {
                nodes[i].Min = new byte[3];
                nodes[i].Max = new byte[3];
                for (int j = 0; j < 3; j++)
                {
                    nodes[i].Min[j] = 0;
                    nodes[i].Max[j] = 0;
                }
            }

            Compact(buildTriangles.Length, buildTriangles, numNodes, nodes, ref rootBound);

            return(new kDOPTreeCompact
            {
                RootBound = rootBound,
                Nodes = nodes.Select(node => (kDOPCompact)node).ToArray(),
                Triangles = buildTriangles.Select(tri => (kDOPCollisionTriangle)tri).ToArray()
            });
        }
Ejemplo n.º 2
0
        public static StaticMesh ConvertToME3StaticMesh(this SkeletalMesh skeletalMesh)
        {
            StaticLODModel lodModel    = skeletalMesh.LODModels[0];
            uint           numVertices = lodModel.NumVertices;
            var            stm         = new StaticMesh
            {
                Bounds    = skeletalMesh.Bounds,
                BodySetup = new UIndex(0),
                LODModels = new[] { new StaticMeshRenderData
                                    {
                                        IndexBuffer                 = lodModel.IndexBuffer.TypedClone(),
                                        NumVertices                 = numVertices,
                                        Edges                       = new MeshEdge[0],
                                        RawTriangles                = new StaticMeshTriangle[0],
                                        ColorVertexBuffer           = new ColorVertexBuffer(),
                                        ShadowTriangleDoubleSided   = new byte[0],
                                        WireframeIndexBuffer        = new ushort[0],
                                        ShadowExtrusionVertexBuffer = new ExtrusionVertexBuffer
                                        {
                                            Stride     = 4,
                                            VertexData = new float[0]
                                        },
                                        PositionVertexBuffer = new PositionVertexBuffer
                                        {
                                            NumVertices = numVertices,
                                            Stride      = 12,
                                            VertexData  = new Vector3[numVertices]
                                        },
                                        VertexBuffer = new StaticMeshVertexBuffer
                                        {
                                            bUseFullPrecisionUVs = false,
                                            NumTexCoords         = 1,
                                            NumVertices          = numVertices,
                                            VertexData           = new StaticMeshVertexBuffer.StaticMeshFullVertex[numVertices]
                                        },
                                        Elements = lodModel.Sections.Select(sec =>
                        {
                            var indices = lodModel.IndexBuffer.Skip((int)sec.BaseIndex).Take(sec.NumTriangles * 3).ToList();
                            return(new StaticMeshElement
                            {
                                bEnableShadowCasting = true,
                                EnableCollision = true,
                                OldEnableCollision = true,
                                FirstIndex = sec.BaseIndex,
                                NumTriangles = (uint)sec.NumTriangles,
                                MaterialIndex = sec.MaterialIndex,
                                Material = skeletalMesh.Materials[sec.MaterialIndex],
                                Fragments = new FragmentRange[0],
                                MinVertexIndex = indices.Min(),
                                MaxVertexIndex = indices.Max()
                            });
                        }).ToArray()
                                    } },
                InternalVersion = 18,
                LightingGuid    = Guid.NewGuid()
            };

            Vector3[] posVertData = stm.LODModels[0].PositionVertexBuffer.VertexData;
            StaticMeshVertexBuffer.StaticMeshFullVertex[] stmVertData = stm.LODModels[0].VertexBuffer.VertexData;
            if (lodModel.ME1VertexBufferGPUSkin != null)
            {
                for (int i = 0; i < lodModel.ME1VertexBufferGPUSkin.Length; i++)
                {
                    SoftSkinVertex vert = lodModel.ME1VertexBufferGPUSkin[i];
                    posVertData[i] = vert.Position;
                    stmVertData[i] = new StaticMeshVertexBuffer.StaticMeshFullVertex
                    {
                        HalfPrecisionUVs = new Vector2DHalf[] { vert.UV },
                        TangentX         = vert.TangentX,
                        TangentZ         = vert.TangentZ
                    };
                }
            }
            else
            {
                for (int i = 0; i < lodModel.VertexBufferGPUSkin.VertexData.Length; i++)
                {
                    GPUSkinVertex vert = lodModel.VertexBufferGPUSkin.VertexData[i];
                    posVertData[i] = vert.Position;
                    stmVertData[i] = new StaticMeshVertexBuffer.StaticMeshFullVertex
                    {
                        HalfPrecisionUVs = new[] { vert.UV },
                        TangentX         = vert.TangentX,
                        TangentZ         = vert.TangentZ
                    };
                }
            }
            var tris = new kDOPCollisionTriangle[lodModel.IndexBuffer.Length / 3];

            for (int i = 0, elIdx = 0, triCount = 0; i < lodModel.IndexBuffer.Length; i += 3, ++triCount)
            {
                if (triCount > lodModel.Sections[elIdx].NumTriangles)
                {
                    triCount = 0;
                    ++elIdx;
                }
                tris[i / 3] = new kDOPCollisionTriangle(lodModel.IndexBuffer[i], lodModel.IndexBuffer[i + 1], lodModel.IndexBuffer[i + 2],
                                                        lodModel.Sections[elIdx].MaterialIndex);
            }

            stm.kDOPTreeME3UDK = KDOPTreeBuilder.ToCompact(tris, stm.LODModels[0].PositionVertexBuffer.VertexData);

            return(stm);
        }
Ejemplo n.º 3
0
 public kDopBuildTriangle(kDOPCollisionTriangle tri, Vector3 v0, Vector3 v1, Vector3 v2)
     : this(tri.Vertex1, tri.Vertex2, tri.Vertex3, tri.MaterialIndex, v0, v1, v2)
 {
 }
Ejemplo n.º 4
0
        public static kDOPTreeCompact ToCompact(kDOPCollisionTriangle[] oldTriangles, Vector3[] vertices)
        {
            var rootBound = new kDOP
            {
                Min = new float[3],
                Max = new float[3]
            };

            for (int i = 0; i < 3; i++)
            {
                rootBound.Max[i] = float.MaxValue;
                rootBound.Min[i] = -float.MaxValue;
            }

            if (oldTriangles.IsEmpty())
            {
                return(new kDOPTreeCompact
                {
                    RootBound = rootBound,
                    Nodes = new kDOPCompact[0],
                    Triangles = new kDOPCollisionTriangle[0]
                });
            }

            var buildTriangles = new kDopBuildTriangle[oldTriangles.Length];

            for (int i = 0; i < oldTriangles.Length; i++)
            {
                kDOPCollisionTriangle oldTri = oldTriangles[i];
                buildTriangles[i] = new kDopBuildTriangle(oldTri, vertices[oldTri.Vertex1], vertices[oldTri.Vertex2], vertices[oldTri.Vertex3]);
            }

            int numNodes = 0;

            if (buildTriangles.Length > 5)
            {
                numNodes = 1;
                while ((buildTriangles.Length + numNodes - 1) / numNodes > 10)
                {
                    numNodes *= 2;
                }
                numNodes = 2 * numNodes;
            }

            var nodes = new kDOPCompact[numNodes];

            for (int i = 0; i < numNodes; i++)
            {
                nodes[i] = new kDOPCompact();
                for (int j = 0; j < 3; j++)
                {
                    nodes[i].Min[j] = 0;
                    nodes[i].Max[j] = 0;
                }
            }

            rootBound.AddTriangles(buildTriangles, 0, buildTriangles.Length);

            if (numNodes > 1 && buildTriangles.Length > 1)
            {
                nodes[0].SplitTriangleList(0, 0, buildTriangles.Length, buildTriangles, rootBound, nodes);
            }

            return(new kDOPTreeCompact
            {
                RootBound = rootBound,
                Nodes = nodes,
                Triangles = buildTriangles.Select(tri => (kDOPCollisionTriangle)tri).ToArray()
            });
        }