public static xxMesh CreateMesh(WorkspaceMesh mesh, int xxFormat, out string[] materialNames, out int[] indices, out bool[] worldCoords, out bool[] replaceSubmeshesOption) { int numUncheckedSubmeshes = 0; foreach (ImportedSubmesh submesh in mesh.SubmeshList) { if (!mesh.isSubmeshEnabled(submesh)) { numUncheckedSubmeshes++; } } int numSubmeshes = mesh.SubmeshList.Count - numUncheckedSubmeshes; materialNames = new string[numSubmeshes]; indices = new int[numSubmeshes]; worldCoords = new bool[numSubmeshes]; replaceSubmeshesOption = new bool[numSubmeshes]; xxMesh xxMesh = new xxMesh(); xxMesh.BoneList = CreateBoneList(mesh.BoneList); xxMesh.SubmeshList = new List <xxSubmesh>(mesh.SubmeshList.Count); for (int i = 0, submeshIdx = 0; i < numSubmeshes; i++, submeshIdx++) { while (!mesh.isSubmeshEnabled(mesh.SubmeshList[submeshIdx])) { submeshIdx++; } xxSubmesh xxSubmesh = new xxSubmesh(); xxMesh.SubmeshList.Add(xxSubmesh); xxSubmesh.MaterialIndex = -1; materialNames[i] = mesh.SubmeshList[submeshIdx].Material; indices[i] = mesh.SubmeshList[submeshIdx].Index; worldCoords[i] = mesh.SubmeshList[submeshIdx].WorldCoords; replaceSubmeshesOption[i] = mesh.isSubmeshReplacingOriginal(mesh.SubmeshList[submeshIdx]); List <ImportedVertex> vertexList = mesh.SubmeshList[submeshIdx].VertexList; List <xxVertex> xxVertexList = new List <xxVertex>(vertexList.Count); for (int j = 0; j < vertexList.Count; j++) { ImportedVertex vert = vertexList[j]; xxVertex xxVertex; if (xxFormat >= 4) { xxVertex = new xxVertexUShort(); CreateUnknown(xxVertex); } else { xxVertex = new xxVertexInt(); } xxVertex.Index = j; xxVertex.Normal = vert.Normal; xxVertex.UV = (float[])vert.UV.Clone(); xxVertex.Weights3 = new float[3] { vert.Weights[0], vert.Weights[1], vert.Weights[2] }; xxVertex.BoneIndices = (byte[])vert.BoneIndices.Clone(); xxVertex.Position = vert.Position; xxVertexList.Add(xxVertex); } xxSubmesh.VertexList = xxVertexList; List <ImportedFace> faceList = mesh.SubmeshList[submeshIdx].FaceList; List <xxFace> xxFaceList = new List <xxFace>(faceList.Count); for (int j = 0; j < faceList.Count; j++) { int[] vertexIndices = faceList[j].VertexIndices; xxFace xxFace = new xxFace(); xxFace.VertexIndices = new ushort[3] { (ushort)vertexIndices[0], (ushort)vertexIndices[1], (ushort)vertexIndices[2] }; xxFaceList.Add(xxFace); } xxSubmesh.FaceList = xxFaceList; } xxMesh.VertexListDuplicate = CreateVertexListDup(xxMesh.SubmeshList); return(xxMesh); }
public static remMesh CreateMesh(WorkspaceMesh mesh, out string[] materialNames, out int[] indices, out bool[] worldCoords, out bool[] replaceSubmeshesOption) { int numUncheckedSubmeshes = 0; foreach (ImportedSubmesh submesh in mesh.SubmeshList) { if (!mesh.isSubmeshEnabled(submesh)) numUncheckedSubmeshes++; } int numSubmeshes = mesh.SubmeshList.Count - numUncheckedSubmeshes; materialNames = new string[numSubmeshes]; indices = new int[numSubmeshes]; worldCoords = new bool[numSubmeshes]; replaceSubmeshesOption = new bool[numSubmeshes]; remMesh newMesh = new remMesh(numSubmeshes); newMesh.name = new remId(mesh.Name); List<remVertex> newVertices = new List<remVertex>(); List<int> newFaces = new List<int>(); List<int> newFaceMarks = new List<int>(); for (int i = 0, submeshIdx = 0; i < numSubmeshes; i++, submeshIdx++) { while (!mesh.isSubmeshEnabled(mesh.SubmeshList[submeshIdx])) submeshIdx++; ImportedSubmesh submesh = mesh.SubmeshList[submeshIdx]; newMesh.AddMaterial(new remId(submesh.Material)); materialNames[i] = submesh.Material; indices[i] = submesh.Index; worldCoords[i] = submesh.WorldCoords; replaceSubmeshesOption[i] = mesh.isSubmeshReplacingOriginal(submesh); List<ImportedFace> faceList = submesh.FaceList; newFaces.Capacity += faceList.Count * 3; int[] faceMarks = new int[faceList.Count]; for (int j = 0; j < faceList.Count; j++) { ImportedFace face = faceList[j]; for (int k = 0; k < 3; k++) { newFaces.Add(face.VertexIndices[k] + newVertices.Count); } faceMarks[j] = i; } newFaceMarks.AddRange(faceMarks); List<ImportedVertex> vertexList = submesh.VertexList; newVertices.Capacity += vertexList.Count; for (int j = 0; j < vertexList.Count; j++) { ImportedVertex vert = vertexList[j]; remVertex newVertex = new remVertex(); if (submesh.WorldCoords) { newVertex.Position = vert.Position; newVertex.Normal = vert.Normal; } else { newVertex.Position = new Vector3(vert.Position.X, -vert.Position.Z, vert.Position.Y); newVertex.Normal = new Vector3(vert.Normal.X, -vert.Normal.Z, vert.Normal.Y); } newVertex.UV = new Vector2(vert.UV[0], vert.UV[1]); newVertices.Add(newVertex); } } newMesh.vertices = newVertices.ToArray(); newMesh.faces = newFaces.ToArray(); newMesh.faceMarks = newFaceMarks.ToArray(); return newMesh; }
public static odfMesh CreateMesh(WorkspaceMesh mesh, int subMeshFormat, out string[] materialNames, out int[] indices, out bool[] worldCoords, out bool[] replaceSubmeshesOption) { int numUncheckedSubmeshes = 0; foreach (ImportedSubmesh submesh in mesh.SubmeshList) { if (!mesh.isSubmeshEnabled(submesh)) numUncheckedSubmeshes++; } int numSubmeshes = mesh.SubmeshList.Count - numUncheckedSubmeshes; materialNames = new string[numSubmeshes]; indices = new int[numSubmeshes]; worldCoords = new bool[numSubmeshes]; replaceSubmeshesOption = new bool[numSubmeshes]; odfMesh newMesh = new odfMesh(new ObjectName(String.Empty, null), null, numSubmeshes); for (int i = 0, submeshIdx = 0; i < numSubmeshes; i++, submeshIdx++) { while (!mesh.isSubmeshEnabled(mesh.SubmeshList[submeshIdx])) submeshIdx++; ImportedSubmesh submesh = mesh.SubmeshList[submeshIdx]; odfSubmesh newSubmesh = new odfSubmesh(new ObjectName(String.Empty, null), null, subMeshFormat); newMesh.AddChild(newSubmesh); newSubmesh.MaterialId = ObjectID.INVALID; materialNames[i] = submesh.Material; indices[i] = submesh.Index; worldCoords[i] = submesh.WorldCoords; replaceSubmeshesOption[i] = mesh.isSubmeshReplacingOriginal(mesh.SubmeshList[submeshIdx]); List<ImportedVertex> vertexList = submesh.VertexList; List<odfVertex> newVertexList = new List<odfVertex>(vertexList.Count); for (int j = 0; j < vertexList.Count; j++) { ImportedVertex vert = vertexList[j]; odfVertex newVertex = new odfVertex(); newVertex.Normal = vert.Normal; newVertex.UV = new Vector2(vert.UV[0], vert.UV[1]); newVertex.Weights = (float[])vert.Weights.Clone(); newVertex.BoneIndices = (byte[])vert.BoneIndices.Clone(); newVertex.Position = vert.Position; newVertexList.Add(newVertex); } newSubmesh.VertexList = newVertexList; List<ImportedFace> faceList = submesh.FaceList; List<odfFace> newFaceList = new List<odfFace>(faceList.Count); for (int j = 0; j < faceList.Count; j++) { int[] vertexIndices = faceList[j].VertexIndices; odfFace newFace = new odfFace(); newFace.VertexIndices = new ushort[3] { (ushort)vertexIndices[0], (ushort)vertexIndices[1], (ushort)vertexIndices[2] }; newFaceList.Add(newFace); } newSubmesh.FaceList = newFaceList; } return newMesh; }
private void AddList <T>(List <T> list, string rootName, string editorVar) { if ((list != null) && (list.Count > 0)) { TreeNode root = new TreeNode(rootName); root.Checked = true; this.treeView.AddChild(root); for (int i = 0; i < list.Count; i++) { dynamic item = list[i]; TreeNode node = new TreeNode(item is WorkspaceAnimation ? ((WorkspaceAnimation)item).importedAnimation is ImportedKeyframedAnimation ? "Animation" + i : "Animation(Reduced Keys)" + i : item.Name); node.Checked = true; node.Tag = new DragSource(editorVar, typeof(T), i); this.treeView.AddChild(root, node); if (item is WorkspaceMesh) { WorkspaceMesh mesh = item; for (int j = 0; j < mesh.SubmeshList.Count; j++) { ImportedSubmesh submesh = mesh.SubmeshList[j]; TreeNode submeshNode = new TreeNode(); submeshNode.Checked = mesh.isSubmeshEnabled(submesh); submeshNode.Tag = submesh; submeshNode.ContextMenuStrip = this.contextMenuStripSubmesh; this.treeView.AddChild(node, submeshNode); UpdateSubmeshNode(submeshNode); } } else if (item is WorkspaceMorph) { WorkspaceMorph morph = item; string extraInfo = morph.MorphedVertexIndices != null ? " (morphed vertices: " + morph.MorphedVertexIndices.Count : String.Empty; extraInfo += (extraInfo.Length == 0 ? " (" : ", ") + "keyframes: " + morph.KeyframeList.Count + ")"; node.Text += extraInfo; for (int j = 0; j < morph.KeyframeList.Count; j++) { ImportedMorphKeyframe keyframe = morph.KeyframeList[j]; TreeNode keyframeNode = new TreeNode(); keyframeNode.Checked = morph.isMorphKeyframeEnabled(keyframe); keyframeNode.Tag = keyframe; keyframeNode.ContextMenuStrip = this.contextMenuStripMorphKeyframe; this.treeView.AddChild(node, keyframeNode); UpdateMorphKeyframeNode(keyframeNode); } } else if (item is WorkspaceAnimation) { WorkspaceAnimation animation = item; if (animation.importedAnimation is ImportedKeyframedAnimation) { List <ImportedAnimationKeyframedTrack> trackList = ((ImportedKeyframedAnimation)animation.importedAnimation).TrackList; for (int j = 0; j < trackList.Count; j++) { ImportedAnimationKeyframedTrack track = trackList[j]; TreeNode trackNode = new TreeNode(); trackNode.Checked = animation.isTrackEnabled(track); trackNode.Tag = track; int numKeyframes = 0; foreach (ImportedAnimationKeyframe keyframe in track.Keyframes) { if (keyframe != null) { numKeyframes++; } } trackNode.Text = "Track: " + track.Name + ", Keyframes: " + numKeyframes; this.treeView.AddChild(node, trackNode); } } else if (animation.importedAnimation is ImportedSampledAnimation) { List <ImportedAnimationSampledTrack> trackList = ((ImportedSampledAnimation)animation.importedAnimation).TrackList; for (int j = 0; j < trackList.Count; j++) { ImportedAnimationSampledTrack track = trackList[j]; TreeNode trackNode = new TreeNode(); trackNode.Checked = animation.isTrackEnabled(track); trackNode.Tag = track; int numScalings = 0; for (int k = 0; k < track.Scalings.Length; k++) { if (track.Scalings[k] != null) { numScalings++; } } int numRotations = 0; for (int k = 0; k < track.Rotations.Length; k++) { if (track.Rotations[k] != null) { numRotations++; } } int numTranslations = 0; for (int k = 0; k < track.Translations.Length; k++) { if (track.Translations[k] != null) { numTranslations++; } } trackNode.Text = "Track: " + track.Name + ", Length: " + track.Scalings.Length + ", Scalings: " + numScalings + ", Rotations: " + numRotations + ", Translations: " + numTranslations; this.treeView.AddChild(node, trackNode); } } } } } }
public static SkinnedMeshRenderer CreateSkinnedMeshRenderer(Animator parser, List<Material> materials, WorkspaceMesh mesh, Matrix meshMatrix, out int[] indices, out bool[] worldCoords, out bool[] replaceSubmeshesOption) { int numUncheckedSubmeshes = 0; foreach (ImportedSubmesh submesh in mesh.SubmeshList) { if (!mesh.isSubmeshEnabled(submesh)) { numUncheckedSubmeshes++; } } int numSubmeshes = mesh.SubmeshList.Count - numUncheckedSubmeshes; indices = new int[numSubmeshes]; worldCoords = new bool[numSubmeshes]; replaceSubmeshesOption = new bool[numSubmeshes]; List<Matrix> poseMatrices = new List<Matrix>(mesh.BoneList.Count); Transform animatorTransform = parser.m_GameObject.instance.FindLinkedComponent(UnityClassID.Transform); List<PPtr<Transform>> bones = CreateBoneList(animatorTransform, meshMatrix, mesh.BoneList, poseMatrices); SkinnedMeshRenderer sMesh = new SkinnedMeshRenderer(parser.file); int totVerts = 0, totFaces = 0; sMesh.m_Materials.Capacity = numSubmeshes; foreach (ImportedSubmesh submesh in mesh.SubmeshList) { if (!mesh.isSubmeshEnabled(submesh)) { continue; } Material matFound = materials.Find ( delegate(Material mat) { return mat.m_Name == submesh.Material; } ); sMesh.m_Materials.Add(new PPtr<Material>(matFound)); totVerts += submesh.VertexList.Count; totFaces += submesh.FaceList.Count; } Mesh uMesh = new Mesh(parser.file); uMesh.m_Name = mesh.Name; sMesh.m_Mesh = new PPtr<Mesh>(uMesh); sMesh.m_Bones = bones; uMesh.m_BindPose = poseMatrices; uMesh.m_BoneNameHashes = new List<uint>(poseMatrices.Count); for (int i = 0; i < mesh.BoneList.Count; i++) { string bone = mesh.BoneList[i].Name; uint hash = parser.m_Avatar.instance.BoneHash(bone); uMesh.m_BoneNameHashes.Add(hash); } uMesh.m_VertexData = new VertexData((uint)totVerts); uMesh.m_Skin = new List<BoneInfluence>(totVerts); uMesh.m_IndexBuffer = new byte[totFaces * 3 * sizeof(ushort)]; using (BinaryWriter vertWriter = new BinaryWriter(new MemoryStream(uMesh.m_VertexData.m_DataSize)), indexWriter = new BinaryWriter(new MemoryStream(uMesh.m_IndexBuffer))) { uMesh.m_LocalAABB.m_Center = new Vector3(Single.MaxValue, Single.MaxValue, Single.MaxValue); uMesh.m_LocalAABB.m_Extend = new Vector3(Single.MinValue, Single.MinValue, Single.MinValue); uMesh.m_SubMeshes = new List<SubMesh>(numSubmeshes); int vertIndex = 0; for (int i = 0, submeshIdx = 0; i < numSubmeshes; i++, submeshIdx++) { while (!mesh.isSubmeshEnabled(mesh.SubmeshList[submeshIdx])) { submeshIdx++; } SubMesh submesh = new SubMesh(); submesh.indexCount = (uint)mesh.SubmeshList[submeshIdx].FaceList.Count * 3; submesh.vertexCount = (uint)mesh.SubmeshList[submeshIdx].VertexList.Count; submesh.firstVertex = (uint)vertIndex; uMesh.m_SubMeshes.Add(submesh); indices[i] = mesh.SubmeshList[submeshIdx].Index; worldCoords[i] = mesh.SubmeshList[submeshIdx].WorldCoords; replaceSubmeshesOption[i] = mesh.isSubmeshReplacingOriginal(mesh.SubmeshList[submeshIdx]); List<ImportedVertex> vertexList = mesh.SubmeshList[submeshIdx].VertexList; Vector3 min = new Vector3(Single.MaxValue, Single.MaxValue, Single.MaxValue); Vector3 max = new Vector3(Single.MinValue, Single.MinValue, Single.MinValue); for (int str = 0; str < uMesh.m_VertexData.m_Streams.Count; str++) { StreamInfo sInfo = uMesh.m_VertexData.m_Streams[str]; if (sInfo.channelMask == 0) { continue; } for (int j = 0; j < vertexList.Count; j++) { ImportedVertex vert = vertexList[j]; for (int chn = 0; chn < uMesh.m_VertexData.m_Channels.Count; chn++) { ChannelInfo cInfo = uMesh.m_VertexData.m_Channels[chn]; if ((sInfo.channelMask & (1 << chn)) == 0) { continue; } vertWriter.BaseStream.Position = sInfo.offset + (j + submesh.firstVertex) * sInfo.stride + cInfo.offset; switch (chn) { case 0: Vector3 pos = vert.Position; pos.X *= -1; vertWriter.Write(pos); min = Vector3.Minimize(min, pos); max = Vector3.Maximize(max, pos); break; case 1: Vector3 normal = vert.Normal; normal.X *= -1; vertWriter.Write(normal); break; case 3: vertWriter.Write(vert.UV); break; case 5: Vector4 tangent = vert.Tangent; tangent.X *= -1; tangent.W *= -1; vertWriter.Write(tangent); break; } } if (sMesh.m_Bones.Count > 0 && sInfo.offset == 0 && uMesh.m_Skin.Count < totVerts) { BoneInfluence item = new BoneInfluence(); for (int k = 0; k < 4; k++) { item.boneIndex[k] = vert.BoneIndices[k] != 0xFF ? vert.BoneIndices[k] : 0; } vert.Weights.CopyTo(item.weight, 0); uMesh.m_Skin.Add(item); } } } vertIndex += (int)submesh.vertexCount; submesh.localAABB.m_Extend = max - min; submesh.localAABB.m_Center = min + submesh.localAABB.m_Extend / 2; uMesh.m_LocalAABB.m_Extend = Vector3.Maximize(uMesh.m_LocalAABB.m_Extend, max); uMesh.m_LocalAABB.m_Center = Vector3.Minimize(uMesh.m_LocalAABB.m_Center, min); List<ImportedFace> faceList = mesh.SubmeshList[submeshIdx].FaceList; submesh.firstByte = (uint)indexWriter.BaseStream.Position; for (int j = 0; j < faceList.Count; j++) { int[] vertexIndices = faceList[j].VertexIndices; indexWriter.Write((ushort)(vertexIndices[0] + submesh.firstVertex)); indexWriter.Write((ushort)(vertexIndices[2] + submesh.firstVertex)); indexWriter.Write((ushort)(vertexIndices[1] + submesh.firstVertex)); } } uMesh.m_LocalAABB.m_Extend -= uMesh.m_LocalAABB.m_Center; uMesh.m_LocalAABB.m_Center += uMesh.m_LocalAABB.m_Extend / 2; } return sMesh; }
private void AddList <T>(List <T> list, string rootName, string editorVar) { if ((list != null) && (list.Count > 0)) { TreeNode root = new TreeNode(rootName); root.Checked = true; this.treeView.AddChild(root); for (int i = 0; i < list.Count; i++) { dynamic item = list[i]; TreeNode node = new TreeNode(item is WorkspaceAnimation ? "Animation" + i : item.Name); node.Checked = true; node.Tag = new DragSource(editorVar, typeof(T), i); this.treeView.AddChild(root, node); if (item is WorkspaceMesh) { WorkspaceMesh mesh = item; for (int j = 0; j < mesh.SubmeshList.Count; j++) { ImportedSubmesh submesh = mesh.SubmeshList[j]; TreeNode submeshNode = new TreeNode(); submeshNode.Checked = mesh.isSubmeshEnabled(submesh); submeshNode.Tag = submesh; submeshNode.ContextMenuStrip = this.contextMenuStripSubmesh; this.treeView.AddChild(node, submeshNode); UpdateSubmeshNode(submeshNode); } } else if (item is WorkspaceMorph) { WorkspaceMorph morph = item; for (int j = 0; j < morph.KeyframeList.Count; j++) { ImportedMorphKeyframe keyframe = morph.KeyframeList[j]; TreeNode keyframeNode = new TreeNode(); keyframeNode.Checked = morph.isMorphKeyframeEnabled(keyframe); keyframeNode.Tag = keyframe; keyframeNode.ContextMenuStrip = this.contextMenuStripMorphKeyframe; this.treeView.AddChild(node, keyframeNode); UpdateMorphKeyframeNode(keyframeNode); } } else if (item is WorkspaceAnimation) { WorkspaceAnimation animation = item; for (int j = 0; j < animation.TrackList.Count; j++) { ImportedAnimationTrack track = animation.TrackList[j]; TreeNode trackNode = new TreeNode(); trackNode.Checked = animation.isTrackEnabled(track); trackNode.Tag = track; int numKeyframes = 0; foreach (ImportedAnimationKeyframe keyframe in track.Keyframes) { if (keyframe != null) { numKeyframes++; } } trackNode.Text = "Track: " + track.Name + ", Keyframes: " + numKeyframes; this.treeView.AddChild(node, trackNode); } } } } }
public static xxMesh CreateMesh(WorkspaceMesh mesh, int xxFormat, out string[] materialNames, out int[] indices, out bool[] worldCoords, out bool[] replaceSubmeshesOption) { int numUncheckedSubmeshes = 0; foreach (ImportedSubmesh submesh in mesh.SubmeshList) { if (!mesh.isSubmeshEnabled(submesh)) numUncheckedSubmeshes++; } int numSubmeshes = mesh.SubmeshList.Count - numUncheckedSubmeshes; materialNames = new string[numSubmeshes]; indices = new int[numSubmeshes]; worldCoords = new bool[numSubmeshes]; replaceSubmeshesOption = new bool[numSubmeshes]; xxMesh xxMesh = new xxMesh(); xxMesh.BoneList = CreateBoneList(mesh.BoneList); xxMesh.SubmeshList = new List<xxSubmesh>(mesh.SubmeshList.Count); for (int i = 0, submeshIdx = 0; i < numSubmeshes; i++, submeshIdx++) { while (!mesh.isSubmeshEnabled(mesh.SubmeshList[submeshIdx])) submeshIdx++; xxSubmesh xxSubmesh = new xxSubmesh(); xxMesh.SubmeshList.Add(xxSubmesh); xxSubmesh.MaterialIndex = -1; materialNames[i] = mesh.SubmeshList[submeshIdx].Material; indices[i] = mesh.SubmeshList[submeshIdx].Index; worldCoords[i] = mesh.SubmeshList[submeshIdx].WorldCoords; replaceSubmeshesOption[i] = mesh.isSubmeshReplacingOriginal(mesh.SubmeshList[submeshIdx]); List<ImportedVertex> vertexList = mesh.SubmeshList[submeshIdx].VertexList; List<xxVertex> xxVertexList = new List<xxVertex>(vertexList.Count); for (int j = 0; j < vertexList.Count; j++) { ImportedVertex vert = vertexList[j]; xxVertex xxVertex; if (xxFormat >= 4) { xxVertex = new xxVertexUShort(); CreateUnknown(xxVertex); } else { xxVertex = new xxVertexInt(); } xxVertex.Index = j; xxVertex.Normal = vert.Normal; xxVertex.UV = (float[])vert.UV.Clone(); xxVertex.Weights3 = new float[3] { vert.Weights[0], vert.Weights[1], vert.Weights[2] }; xxVertex.BoneIndices = (byte[])vert.BoneIndices.Clone(); xxVertex.Position = vert.Position; xxVertexList.Add(xxVertex); } xxSubmesh.VertexList = xxVertexList; List<ImportedFace> faceList = mesh.SubmeshList[submeshIdx].FaceList; List<xxFace> xxFaceList = new List<xxFace>(faceList.Count); for (int j = 0; j < faceList.Count; j++) { int[] vertexIndices = faceList[j].VertexIndices; xxFace xxFace = new xxFace(); xxFace.VertexIndices = new ushort[3] { (ushort)vertexIndices[0], (ushort)vertexIndices[1], (ushort)vertexIndices[2] }; xxFaceList.Add(xxFace); } xxSubmesh.FaceList = xxFaceList; } xxMesh.VertexListDuplicate = CreateVertexListDup(xxMesh.SubmeshList); return xxMesh; }