Ejemplo n.º 1
0
        public static bool IsHit(PatchMesh mesh, PointF p)
        {
            if (mesh == null)
            {
                return(false);
            }

            List <PointF> path = new List <PointF>();

            for (int i = 0; i < 3; i++)
            {
                path.Add(new PointF());
            }

            foreach (var t in mesh.triangles)
            {
                path[0] = mesh.vertices[t.Idx0].position;
                path[1] = mesh.vertices[t.Idx1].position;
                path[2] = mesh.vertices[t.Idx2].position;
                if (FLib.FMath.IsPointInPolygon(p, path))
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// メッシュを描画。
        /// cameraPositionは x, y : [-∞, +∞], z : [-∞, 0)
        /// x, y が大きいほどカメラが右下に移動する
        /// zが小さいほどズームアウトする
        /// </summary>
        /// <param name="cameraPosition">x, y : [-∞, +∞], z : [-∞, 0).x, y が大きいほどカメラが右下に移動する.zが小さいほどズームアウトする</param>
        public void DrawMesh(PatchMesh mesh, string textureKey, PatchMeshRenderResources resources, DXColor col, Size formSize, Vector3 cameraPosition)
        {
            List <VertexPositionColorTexture> rawVertices = new List <VertexPositionColorTexture>();

            for (int i = 0; i < mesh.vertices.Count; i++)
            {
                Vector3 pos = vec3(mesh.vertices[i].position);
                pos.Y *= -1;
                Vector2 coord = vec2(mesh.vertices[i].GetTexcoord(textureKey));
                rawVertices.Add(new VertexPositionColorTexture(pos, col, coord));
            }

            List <int> rawIndices = new List <int>();

            for (int i = 0; i < mesh.triangles.Count; i++)
            {
                if (mesh.triangles[i].TextureKey != textureKey)
                {
                    continue;
                }
                rawIndices.Add(mesh.triangles[i].Idx0);
                rawIndices.Add(mesh.triangles[i].Idx1);
                rawIndices.Add(mesh.triangles[i].Idx2);
            }

            var texture = resources.GetTexture(PatchMeshRenderResources.GenerateResourceKey(mesh, textureKey));

            Draw(rawVertices, rawIndices, texture, formSize, cameraPosition, PrimitiveTopology.TriangleList);
        }
Ejemplo n.º 3
0
        public static PatchSkeletalMesh Copy(PatchSkeletalMesh org)
        {
            var m = PatchMesh.Copy(org.mesh);
            var s = PatchSkeleton.Copy(org.skl);
            PatchSkeletalMesh copy = new PatchSkeletalMesh(m, s, org.sections, false);

            return(copy);
        }
        public void RemoveResources(PatchMesh m)
        {
            string prefix = m.Id + ":";
            var    keys   = textureDict.Keys.Where(k => k.StartsWith(prefix)).ToList();

            foreach (var k in keys)
            {
                textureDict.Remove(k);
            }
        }
        /// <summary>
        /// メッシュとパッチを組み合わせたキーを作成する
        /// obj, patchKeyが同一なら常に同じキーが生成される
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="patchKey"></param>
        /// <returns></returns>
        public static string GenerateResourceKey(PatchMesh obj, string patchKey)
        {
            if (obj == null)
            {
                return(":" + patchKey);
            }
            string key = obj.Id + ":" + patchKey;

            System.Diagnostics.Debug.Assert(key.Count(c => c == ':') == 1);
            return(key);
        }
        public void DuplicateResources(PatchMesh from, PatchMesh to)
        {
            string prefix_from = from.Id + ":";
            string prefix_to   = to.Id + ":";
            var    keys        = textureDict.Keys.Where(k => k.StartsWith(prefix_from)).ToList();

            foreach (var k in keys)
            {
                var newKey = prefix_to + k.Substring(prefix_from.Length);
                textureDict[newKey] = textureDict[k];
            }
        }
        public List <string> GetResourceKeyByPatchMesh(PatchMesh m)
        {
            List <string> ls     = new List <string>();
            string        prefix = m.Id + ":";

            foreach (var key in textureDict.Keys)
            {
                if (key.StartsWith(prefix))
                {
                    ls.Add(key);
                }
            }
            return(ls);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// メッシュのワイヤフレームを描画
        /// </summary>
        public void DrawWireframe(PatchMesh mesh, string textureKey, DXColor col, Size formSize, Vector3 cameraPosition)
        {
            List <VertexPositionColorTexture> rawVertices = new List <VertexPositionColorTexture>();
            List <int> rawIndices = new List <int>();

            for (int i = 0; i < mesh.triangles.Count; i++)
            {
                if (mesh.triangles[i].TextureKey != textureKey)
                {
                    continue;
                }

                PointF p0 = mesh.vertices[mesh.triangles[i].Idx0].position;
                PointF p1 = mesh.vertices[mesh.triangles[i].Idx1].position;
                PointF p2 = mesh.vertices[mesh.triangles[i].Idx2].position;

                var pts = GetLineMeshPoint(p0, p1, 1);
                pts.AddRange(GetLineMeshPoint(p1, p2, 1));
                pts.AddRange(GetLineMeshPoint(p2, p0, 1));

                int offset = rawVertices.Count;

                foreach (var p in pts)
                {
                    Vector3 pos = vec3(p);
                    pos.Y *= -1;
                    pos.Z  = LineRenderDepth;
                    rawVertices.Add(new VertexPositionColorTexture(pos, col, Vector2.Zero));
                }

                rawIndices.AddRange(
                    new []
                {
                    0, 1, 2, 2, 1, 3,
                    4, 5, 6, 6, 5, 7,
                    8, 9, 10, 10, 9, 11,
                }.Select(val => val + offset).ToArray());
            }

            Draw(rawVertices, rawIndices, whitePixel, formSize, cameraPosition, PrimitiveTopology.TriangleList);
        }
Ejemplo n.º 9
0
        // コピーせずに参照をそのまま格納する
        public PatchSkeletalMesh(PatchMesh mesh, PatchSkeleton skl, List <PatchSection> sections, bool setSkeletalControlPoints = true)
        {
            this.mesh     = mesh;
            this.skl      = skl;
            this.sections = new List <PatchSection>(sections);
            this.skeletalControlPointDict = CreateSkeletalControlPoints(skl, 20, 3);

            // 既存の制御点を消して、スケルトン周りの制御点を追加
            if (setSkeletalControlPoints)
            {
                this.mesh.ClearControlPoints();
                foreach (var ls in this.skeletalControlPointDict.Values)
                {
                    foreach (var pt in ls)
                    {
                        this.mesh.AddControlPoint(pt, pt);
                    }
                }
            }

            srcStretchRatio = 0;
            endJoints       = new HashSet <string>();
        }
Ejemplo n.º 10
0
        //--------------------------------------------------------------------------
        // Combine()
        //--------------------------------------------------------------------------

        /// <summary>
        /// 2つのメッシュを統合して1つのARAP可能なメッシュを作成する
        /// TODO: さすがに関数にわけるべき
        /// </summary>
        static PatchSkeletalMesh Combine(PatchSkeletalMesh smesh1, PatchSkeletalMesh smesh2, PatchSection section1, PatchSection section2)
        {
            //
            // Meshを作成
            //

            List <PatchVertex> vertices = CombineVertices(smesh1.mesh.vertices, smesh2.mesh.vertices);

            // 頂点から各種インデックスへの辞書を作っておく
            Dictionary <PointF, int> p2i = new Dictionary <PointF, int>();

            for (int i = 0; i < vertices.Count; i++)
            {
                p2i[vertices[i].position] = i;
            }
            Dictionary <PointF, int> pt2part = vertices.ToDictionary(v => v.position, v => v.part);

            Dictionary <int, int> part2part_1 = new Dictionary <int, int>(); // smesh1の各パートが新しいメッシュのどのパートになるか

            foreach (var v in smesh1.mesh.vertices)
            {
                part2part_1[v.part] = pt2part[v.position];
            }

            Dictionary <int, int> part2part_2 = new Dictionary <int, int>(); // smesh2の各パートが新しいメッシュのどのパートになるか

            foreach (var v in smesh2.mesh.vertices)
            {
                part2part_2[v.part] = pt2part[v.position];
            }

            List <PatchControlPoint> controlPoints = CombineControlPoints(smesh1.mesh.CopyControlPoints(), smesh2.mesh.CopyControlPoints(), part2part_1, part2part_2);
            List <PatchTriangle>     triangles     = CombineTriangles(smesh1.mesh.triangles, smesh2.mesh.triangles, smesh1.mesh.vertices, smesh2.mesh.vertices, p2i);
            List <int> path = CombinePath(smesh1.mesh.pathIndices, smesh2.mesh.pathIndices, smesh1.mesh.vertices, smesh2.mesh.vertices, section1, section2, p2i);

            PatchMesh rawMesh = new PatchMesh(vertices, controlPoints, triangles, path);

            //
            // 骨格を統合
            //

            PatchSkeleton skl = CombineSkeleton(smesh1.skl, smesh2.skl);


            //
            // 切り口を統合.
            //
            List <PatchSection> sections = CombineSections(
                smesh1.sections, smesh2.sections,
                section1, section2,
                smesh1.mesh.vertices, smesh2.mesh.vertices,
                smesh1.Mesh.pathIndices, smesh2.Mesh.pathIndices,
                path, p2i);

            //
            // SkeletalMeshを作成.
            //
            PatchSkeletalMesh newMesh = new PatchSkeletalMesh(rawMesh, skl, sections);

            return(newMesh);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// メッシュを描画。
 /// cameraPositionは x, y : [-∞, +∞], z : [-∞, 0)
 /// x, y が大きいほどカメラが右下に移動する
 /// zが小さいほどズームアウトする
 /// </summary>
 /// <param name="cameraPosition">x, y : [-∞, +∞], z : [-∞, 0).x, y が大きいほどカメラが右下に移動する.zが小さいほどズームアウトする</param>
 public void DrawMesh(PatchMesh mesh, string textureKey, PatchMeshRenderResources resources, Size formSize, Vector3 cameraPosition)
 {
     DrawMesh(mesh, textureKey, resources, DXColor.White, formSize, cameraPosition);
 }
Ejemplo n.º 12
0
 internal static PatchMesh Copy(PatchMesh mesh)
 {
     return(new PatchMesh(mesh.vertices, mesh.controlPoints, mesh.triangles, mesh.pathIndices));
 }