コード例 #1
0
        static int CreateBone(Document doc, Metasequoia.Object obj, CreateBonePointInfo info, bool newMaterial, string boneName)
        {
            var vertices = new[]
            {
                info.BeginVertexIndex != -1
                                        ? info.BeginVertexIndex
                                        : obj.AddVertex(info.BeginWorld)
            }.Concat(new[]
            {
                info.EndWorld,
                info.HasSize ? info.SizeWorld : (Point?)null,
            }
                     .Where(_ => _.HasValue)
                     .Select(_ => obj.AddVertex(_.Value)))
            .ToArray();

            if (vertices.Length == 3 && info.Flip)
            {
                vertices = vertices.Reverse().ToArray();
            }

            var face = obj.AddFace(vertices);

            if (info.HasSize && newMaterial)
            {
                var r   = new Random();
                var mat = doc.Materials.Where(_ => _.Name == boneName)
                          .Select(_ => (int?)_.Index)
                          .FirstOrDefault()
                          ?? doc.AddMaterial(new Material
                {
                    Name  = boneName,
                    Alpha = (float)(0.5 + r.NextDouble() * 0.4),
                    Color = new Color((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble()),
                });

                obj.SetFaceMaterial(face, mat);
                doc.CurrentMaterialIndex = mat;
            }
            else
            {
                obj.SetFaceMaterial(face, doc.CurrentMaterialIndex);
            }

            foreach (var i in vertices)
            {
                doc.AddSelectVertex(obj.Index, i);
            }

            for (int i = 0; i < (vertices.Length == 2 ? 1 : vertices.Length); i++)
            {
                doc.AddSelectLine(obj.Index, face, i);
            }

            doc.AddSelectFace(obj.Index, face);

            return(vertices[1]);
        }
コード例 #2
0
        void CreateAnchor(Document doc, float margin, bool snap, bool autoMirror)
        {
            if (doc.CurrentObjectIndex == -1 ||
                doc.CurrentMaterialIndex == -1)
            {
                return;
            }

            var targetObject   = doc.Objects[doc.CurrentObjectIndex];
            var targetBoneFace = bone == null ? null : bone.Faces.FirstOrDefault(_ => _.Material == doc.CurrentMaterialIndex && _.PointCount == 3);
            var targetBone     = targetBoneFace == null ? null : new Bone(doc, targetBoneFace);
            var beginToEnd     = targetBone == null ? Point.Zero : targetBone.End.Point - targetBone.Begin.Point;
            var unit           = new Point(0, 1, 0);
            var axis           = Point.GetCrossProduct(unit, beginToEnd).Normalize();
            var angle          = Point.GetCrossingAngle(unit, beginToEnd);

            if (axis == Point.Zero)
            {
                axis = new Point(0, 1, 0);
            }

            var targetBoneMatrix = targetBone == null || !snap
                                ? Matrix.Identity
                                : Matrix.CreateFromAxisAngle(axis, angle) * Matrix.CreateTranslation(targetBone.Begin.Point);
            var targetBoneInverseMatrix = targetBone == null || !snap
                                ? Matrix.Identity
                                : Matrix.Invert(targetBoneMatrix);
            var anchorName = "anchor|" + targetObject.Name.Split(':').Last();

            while (doc.Objects.Any(_ => _.Name == anchorName))
            {
                anchorName = anchorName.Replace("|", "_|");
            }

            var vertices = targetObject.Vertices
                           .Where(_ => _.IsSelected)
                           .Select(_ => Point.Transform(_.Point, targetBoneInverseMatrix))
                           .ToArray();

            if (!vertices.Any())
            {
                return;
            }

            var min = vertices.Aggregate(vertices.First(), (x, y) => new Point(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y), Math.Min(x.Z, y.Z))) - margin;
            var max = vertices.Aggregate(vertices.First(), (x, y) => new Point(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y), Math.Max(x.Z, y.Z))) + margin;
            var obj = new Metasequoia.Object
            {
                Name = anchorName,
            };
            var newVertices = new[]
            {
                new Point(min.X, max.Y, max.Z),
                new Point(min.X, min.Y, max.Z),
                new Point(max.X, max.Y, max.Z),
                new Point(max.X, min.Y, max.Z),
                new Point(max.X, max.Y, min.Z),
                new Point(max.X, min.Y, min.Z),
                new Point(min.X, max.Y, min.Z),
                new Point(min.X, min.Y, min.Z),
            }.Select(_ => Point.Transform(_, targetBoneMatrix)).Select(obj.AddVertex).ToArray();

            foreach (var i in new[]
            {
                new[] { 0, 2, 3, 1 },
                new[] { 2, 4, 5, 3 },
                new[] { 4, 6, 7, 5 },
                new[] { 6, 0, 1, 7 },
                new[] { 6, 4, 2, 0 },
                new[] { 1, 3, 5, 7 },
            })
            {
                obj.Faces[obj.AddFace(i.Select(_ => newVertices[_]))].Material = doc.CurrentMaterialIndex;
            }

            if (autoMirror && doc.Materials[doc.CurrentMaterialIndex].Name.EndsWith("[]"))
            {
                obj.MirrorType = ObjectMirror.Normal;
                obj.MirrorAxis = ObjectMirrorAxis.X;
            }

            doc.AddObject(obj);
        }
コード例 #3
0
		void CreateAnchor(Document doc, float margin, bool snap, bool autoMirror)
		{
			if (doc.CurrentObjectIndex == -1 ||
				doc.CurrentMaterialIndex == -1)
				return;

			var targetObject = doc.Objects[doc.CurrentObjectIndex];
			var targetBoneFace = bone == null ? null : bone.Faces.FirstOrDefault(_ => _.Material == doc.CurrentMaterialIndex && _.PointCount == 3);
			var targetBone = targetBoneFace == null ? null : new Bone(doc, targetBoneFace);
			var beginToEnd = targetBone == null ? Point.Zero : targetBone.End.Point - targetBone.Begin.Point;
			var unit = new Point(0, 1, 0);
			var axis = Point.GetCrossProduct(unit, beginToEnd).Normalize();
			var angle = Point.GetCrossingAngle(unit, beginToEnd);

			if (axis == Point.Zero)
				axis = new Point(0, 1, 0);

			var targetBoneMatrix = targetBone == null || !snap
				? Matrix.Identity
				: Matrix.CreateFromAxisAngle(axis, angle) * Matrix.CreateTranslation(targetBone.Begin.Point);
			var targetBoneInverseMatrix = targetBone == null || !snap
				? Matrix.Identity
				: Matrix.Invert(targetBoneMatrix);
			var anchorName = "anchor|" + targetObject.Name.Split(':').Last();

			while (doc.Objects.Any(_ => _.Name == anchorName))
				anchorName = anchorName.Replace("|", "_|");

			var vertices = targetObject.Vertices
									   .Where(_ => _.IsSelected)
									   .Select(_ => Point.Transform(_.Point, targetBoneInverseMatrix))
									   .ToArray();

			if (!vertices.Any())
				return;

			var min = vertices.Aggregate(vertices.First(), (x, y) => new Point(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y), Math.Min(x.Z, y.Z))) - margin;
			var max = vertices.Aggregate(vertices.First(), (x, y) => new Point(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y), Math.Max(x.Z, y.Z))) + margin;
			var obj = new Metasequoia.Object
			{
				Name = anchorName,
			};
			var newVertices = new[]
			{
				new Point(min.X, max.Y, max.Z),
				new Point(min.X, min.Y, max.Z),
				new Point(max.X, max.Y, max.Z),
				new Point(max.X, min.Y, max.Z),
				new Point(max.X, max.Y, min.Z),
				new Point(max.X, min.Y, min.Z),
				new Point(min.X, max.Y, min.Z),
				new Point(min.X, min.Y, min.Z),
			}.Select(_ => Point.Transform(_, targetBoneMatrix)).Select(obj.AddVertex).ToArray();

			foreach (var i in new[]
			{
				new[] { 0, 2, 3, 1 },
				new[] { 2, 4, 5, 3 },
				new[] { 4, 6, 7, 5 },
				new[] { 6, 0, 1, 7 },
				new[] { 6, 4, 2, 0 },
				new[] { 1, 3, 5, 7 },
			})
				obj.Faces[obj.AddFace(i.Select(_ => newVertices[_]))].Material = doc.CurrentMaterialIndex;

			if (autoMirror && doc.Materials[doc.CurrentMaterialIndex].Name.EndsWith("[]"))
			{
				obj.MirrorType = ObjectMirror.Normal;
				obj.MirrorAxis = ObjectMirrorAxis.X;
			}

			doc.AddObject(obj);
		}