public void AddFrame(xxFrame srcFrame, int srcFormat, int destParentId, int meshMatOffset) { var newFrame = srcFrame.Clone(true, true); xx.ConvertFormat(newFrame, srcFormat, Parser.Format); MeshMatOffset(newFrame, meshMatOffset); AddFrame(newFrame, destParentId); }
int forEachFrame(xxFrame f, int parent, Func <xxFrame, int, int> cb) { var node = cb(f, parent); foreach (var ff in f) { forEachFrame(ff, node, cb); } return(node); }
public bool RemoveSVI(xxFrame meshFrame, int submeshIdx) { sviParser svi = FindSVI(meshFrame.Name, submeshIdx); if (svi == null) { return(false); } return(Parser.sections.Remove(svi)); }
public static xxFrame CreateFrame(ImportedFrame frame) { xxFrame xxFrame = new xxFrame(); xxFrame.Matrix = frame.Matrix; xxFrame.Bounds = new BoundingBox(); xxFrame.Name = frame.Name; xxFrame.InitChildren(frame.Count); for (int i = 0; i < frame.Count; i++) { xxFrame.AddChild(CreateFrame(frame[i])); } return xxFrame; }
public bool RemoveSVI(xxFrame meshFrame) { bool removed = false; for (int i = 0; i < Parser.sections.Count; i++) { sviParser svi = Parser.sections[i]; if (svi.meshName == meshFrame.Name) { Parser.sections.Remove(svi); i--; removed = true; } } return(removed); }
public static void CopyOrCreateUnknowns(xxFrame dest, xxFrame root, int xxFormat) { xxFrame src = FindFrame(dest.Name, root); if (src == null) { CreateUnknowns(dest, xxFormat); } else { CopyUnknowns(src, dest); } for (int i = 0; i < dest.Count; i++) { CopyOrCreateUnknowns(dest[i], root, xxFormat); } }
public static void CalculateNormals(xaParser parser, xxFrame meshFrame, string morphClip, string keyframe, float threshold) { HashSet<Tuple<xaMorphClip, xaMorphKeyframe>> keyframes = new HashSet<Tuple<xaMorphClip, xaMorphKeyframe>>(); foreach (xaMorphClip clip in parser.MorphSection.ClipList) { if (morphClip != null && clip.Name != morphClip) continue; if (keyframe != null) { xaMorphKeyframe xaKeyframe = FindMorphKeyFrame(keyframe, parser.MorphSection); if (xaKeyframe == null) { throw new Exception("keyframe " + keyframe + " not found in morph clip " + morphClip); } keyframes.Add(new Tuple<xaMorphClip, xaMorphKeyframe>(clip, xaKeyframe)); break; } else { foreach (xaMorphKeyframeRef morphRef in clip.KeyframeRefList) { xaMorphKeyframe xaKeyframe = FindMorphKeyFrame(morphRef.Name, parser.MorphSection); keyframes.Add(new Tuple<xaMorphClip, xaMorphKeyframe>(clip, xaKeyframe)); } } } if (keyframes.Count == 0) { Report.ReportLog("No keyframe for mesh " + meshFrame.Name + " to calculate normals for found."); return; } foreach (var tup in keyframes) { xaMorphIndexSet set = FindMorphIndexSet(tup.Item1.Name, parser.MorphSection); CalculateNormals(parser, meshFrame, tup.Item2, set, threshold); } }
public void AddFrame(xxFrame srcFrame, int srcFormat, List<xxMaterial> srcMaterials, List<xxTexture> srcTextures, bool appendIfMissing, int destParentId) { int[] matTranslation = CreateMaterialTranslation(srcMaterials, srcFormat, srcTextures, appendIfMissing); var newFrame = srcFrame.Clone(true, true, matTranslation); xx.ConvertFormat(newFrame, srcFormat, Parser.Format); AddFrame(newFrame, destParentId); }
public float UnsetMorphKeyframe(xxFrame meshFrame, xaMorphIndexSet idxSet, bool asStart) { foreach (AnimationFrame frame in meshFrames) { if (frame.Name == meshFrame.Name) { xxMesh xxMesh = meshFrame.Mesh; int meshObjIdx = xa.MorphMeshObjIdx(idxSet.MeshIndices, xxMesh); if (meshObjIdx < 0) { Report.ReportLog("no valid mesh object was found for the morph"); return -1f; } MeshContainer animMesh = frame.MeshContainer; for (int i = 1; i < meshObjIdx; i++) { animMesh = animMesh.NextMeshContainer; if (animMesh == null) break; } if (animMesh == null) { Report.ReportLog("Bad submesh specified."); return -1f; } MorphMeshContainer morphMesh = (MorphMeshContainer)animMesh; if (asStart) { if (morphMesh.StartBuffer != morphMesh.EndBuffer) { morphMesh.StartBuffer.Dispose(); morphMesh.StartBuffer = morphMesh.EndBuffer; } else { frame.MeshContainer = morphMesh.NextMeshContainer; } morphMesh.TweenFactor = 1.0f; } else { if (morphMesh.StartBuffer != morphMesh.EndBuffer) { morphMesh.EndBuffer.Dispose(); morphMesh.EndBuffer = morphMesh.StartBuffer; } else { frame.MeshContainer = morphMesh.NextMeshContainer; } morphMesh.TweenFactor = 0.0f; } return morphMesh.TweenFactor; } } Report.ReportLog("Mesh frame " + meshFrame + " not displayed."); return -1f; }
public float SetMorphKeyframe(xxFrame meshFrame, xaMorphIndexSet idxSet, xaMorphKeyframe keyframe, bool asStart) { foreach (AnimationFrame frame in meshFrames) { if (frame.Name == meshFrame.Name) { xxMesh xxMesh = meshFrame.Mesh; int meshObjIdx = xa.MorphMeshObjIdx(idxSet.MeshIndices, xxMesh); if (meshObjIdx < 0) { Report.ReportLog("no valid mesh object was found for the morph"); return -1f; } MorphMeshContainer morphMesh = null; AnimationMeshContainer animMesh = frame.MeshContainer as AnimationMeshContainer; if (animMesh != null) { for (int i = 1; i < meshObjIdx; i++) { animMesh = (AnimationMeshContainer)animMesh.NextMeshContainer; if (animMesh == null) break; } if (animMesh == null) { Report.ReportLog("Bad submesh specified."); return -1f; } morphMesh = new MorphMeshContainer(); morphMesh.FaceCount = xxMesh.SubmeshList[meshObjIdx].FaceList.Count; morphMesh.IndexBuffer = animMesh.MeshData.Mesh.IndexBuffer; morphMesh.VertexCount = xxMesh.SubmeshList[meshObjIdx].VertexList.Count; List<xxVertex> vertexList = xxMesh.SubmeshList[meshObjIdx].VertexList; VertexBuffer vertBuffer = CreateMorphVertexBuffer(idxSet, keyframe, vertexList); morphMesh.StartBuffer = morphMesh.EndBuffer = vertBuffer; int vertBufferSize = morphMesh.VertexCount * Marshal.SizeOf(typeof(TweeningMeshesVertexBufferFormat.Stream2)); vertBuffer = new VertexBuffer(device, vertBufferSize, Usage.WriteOnly, VertexFormat.Texture1, Pool.Managed); using (DataStream vertexStream = vertBuffer.Lock(0, vertBufferSize, LockFlags.None)) { for (int i = 0; i < vertexList.Count; i++) { xxVertex vertex = vertexList[i]; vertexStream.Write(vertex.UV[0]); vertexStream.Write(vertex.UV[1]); } vertBuffer.Unlock(); } morphMesh.CommonBuffer = vertBuffer; morphMesh.MaterialIndex = animMesh.MaterialIndex; morphMesh.TextureIndex = animMesh.TextureIndex; morphMesh.NextMeshContainer = animMesh; frame.MeshContainer = morphMesh; morphMesh.TweenFactor = 0.0f; } else { morphMesh = frame.MeshContainer as MorphMeshContainer; List<xxVertex> vertexList = xxMesh.SubmeshList[meshObjIdx].VertexList; VertexBuffer vertBuffer = CreateMorphVertexBuffer(idxSet, keyframe, vertexList); if (asStart) { if (morphMesh.StartBuffer != morphMesh.EndBuffer) { morphMesh.StartBuffer.Dispose(); } morphMesh.StartBuffer = vertBuffer; morphMesh.TweenFactor = 0.0f; } else { if (morphMesh.StartBuffer != morphMesh.EndBuffer) { morphMesh.EndBuffer.Dispose(); } morphMesh.EndBuffer = vertBuffer; morphMesh.TweenFactor = 1.0f; } } return morphMesh.TweenFactor; } } Report.ReportLog("Mesh frame " + meshFrame + " not displayed."); return -1f; }
private void buttonSelectMeshes_Click(object sender, EventArgs e) { bool selectInvalidMeshes = Control.ModifierKeys == Keys.Control; List <DockContent> formXXList; if (!Gui.Docking.DockContents.TryGetValue(typeof(FormXX), out formXXList)) { return; } List <DockContent> formPPList; if (!Gui.Docking.DockContents.TryGetValue(typeof(FormPP), out formPPList)) { return; } try { List <string> srcParserVarList = new List <string>(); List <string> srcEditorVarList = new List <string>(); foreach (FormPP form in formPPList) { foreach (ListViewItem item in form.otherSubfilesList.SelectedItems) { if (item.Text.ToLower().EndsWith(".sviex")) { string srcParserVar = Gui.Scripting.GetNextVariable("sviexParser"); srcParserVarList.Add(srcParserVar); string parserCommand = srcParserVar + " = OpenSVIEX(parser=" + form.ParserVar + ", name=\"" + item.Text + "\")"; Gui.Scripting.RunScript(parserCommand); string srcEditorVar = Gui.Scripting.GetNextVariable("sviexEditor"); srcEditorVarList.Add(srcEditorVar); Gui.Scripting.RunScript(srcEditorVar + " = sviexEditor(parser=" + srcParserVar + ")"); } } } foreach (FormXX xxForm in formXXList) { while (xxForm.listViewMesh.SelectedItems.Count > 0) { xxForm.listViewMesh.SelectedItems[0].Selected = false; } } foreach (string srcEditorVar in srcEditorVarList) { sviexEditor editor = (sviexEditor)Gui.Scripting.Variables[srcEditorVar]; foreach (sviParser svi in editor.Parser.sections) { foreach (FormXX xxForm in formXXList) { if (selectInvalidMeshes) { xxFrame meshFrame = xx.FindFrame(svi.meshName, xxForm.Editor.Parser.Frame); if (meshFrame == null || meshFrame.Mesh == null) { Report.ReportLog("Mesh " + svi.meshName + "[" + svi.submeshIdx + "] is missing in " + xxForm.Editor.Parser.Name + "."); continue; } if (svi.submeshIdx >= meshFrame.Mesh.SubmeshList.Count || meshFrame.Mesh.SubmeshList[svi.submeshIdx].VertexList.Count != svi.indices.Length) { foreach (ListViewItem item in xxForm.listViewMesh.Items) { if (item.Text == svi.meshName) { item.Selected = true; break; } } } } else { foreach (ListViewItem item in xxForm.listViewMesh.Items) { if (!item.Selected) { xxFrame meshFrame = xxForm.Editor.Meshes[(int)item.Tag]; if (meshFrame.Name == svi.meshName) { item.Selected = true; break; } } } } } } } foreach (string parserVar in srcParserVarList) { Gui.Scripting.RunScript(parserVar + "=null"); } foreach (string editorVar in srcEditorVarList) { Gui.Scripting.RunScript(editorVar + "=null"); } } catch (Exception ex) { Utility.ReportException(ex); } }
private void ExportMesh(xxFrame frame, XmlElement parent, xxParser parser, List<xxMaterial> usedMaterials) { string frameName = EncodeName(frame.Name); xxMesh meshList = frame.Mesh; List<xxBone> boneList = meshList.BoneList; for (int i = 0; i < meshList.SubmeshList.Count; i++) { string meshName = frameName + "_" + i; xxSubmesh meshObj = meshList.SubmeshList[i]; List<xxFace> faceList = meshObj.FaceList; List<xxVertex> vertexList = meshObj.VertexList; XmlElement geometry = doc.CreateElement("geometry", uri); geometry.SetAttribute("id", meshName + "-lib"); geometry.SetAttribute("name", meshName + "Mesh"); libraries[(int)LibraryIdx.Geometries].AppendChild(geometry); XmlElement mesh = doc.CreateElement("mesh", uri); geometry.AppendChild(mesh); // positions XmlElement positions = doc.CreateElement("source", uri); positions.SetAttribute("id", meshName + "-lib-Position"); mesh.AppendChild(positions); XmlElement positionsArray = doc.CreateElement("float_array", uri); positionsArray.SetAttribute("id", meshName + "-lib-Position-array"); positionsArray.SetAttribute("count", (vertexList.Count * 3).ToString()); positions.AppendChild(positionsArray); XmlElement positionsTechnique = doc.CreateElement("technique_common", uri); positions.AppendChild(positionsTechnique); XmlElement positionsAccessor = doc.CreateElement("accessor", uri); positionsAccessor.SetAttribute("source", "#" + meshName + "-lib-Position-array"); positionsAccessor.SetAttribute("count", vertexList.Count.ToString()); positionsAccessor.SetAttribute("stride", "3"); positionsTechnique.AppendChild(positionsAccessor); XmlElement positionsAccessorParamX = doc.CreateElement("param", uri); positionsAccessorParamX.SetAttribute("name", "X"); positionsAccessorParamX.SetAttribute("type", "float"); positionsAccessor.AppendChild(positionsAccessorParamX); XmlElement positionsAccessorParamY = doc.CreateElement("param", uri); positionsAccessorParamY.SetAttribute("name", "Y"); positionsAccessorParamY.SetAttribute("type", "float"); positionsAccessor.AppendChild(positionsAccessorParamY); XmlElement positionsAccessorParamZ = doc.CreateElement("param", uri); positionsAccessorParamZ.SetAttribute("name", "Z"); positionsAccessorParamZ.SetAttribute("type", "float"); positionsAccessor.AppendChild(positionsAccessorParamZ); // normals XmlElement normals = doc.CreateElement("source", uri); normals.SetAttribute("id", meshName + "-lib-Normal0"); mesh.AppendChild(normals); XmlElement normalsArray = doc.CreateElement("float_array", uri); normalsArray.SetAttribute("id", meshName + "-lib-Normal0-array"); normalsArray.SetAttribute("count", (vertexList.Count * 3).ToString()); normals.AppendChild(normalsArray); XmlElement normalsTechnique = doc.CreateElement("technique_common", uri); normals.AppendChild(normalsTechnique); XmlElement normalsAccessor = doc.CreateElement("accessor", uri); normalsAccessor.SetAttribute("source", "#" + meshName + "-lib-Normal0-array"); normalsAccessor.SetAttribute("count", vertexList.Count.ToString()); normalsAccessor.SetAttribute("stride", "3"); normalsTechnique.AppendChild(normalsAccessor); XmlElement normalsAccessorParamX = doc.CreateElement("param", uri); normalsAccessorParamX.SetAttribute("name", "X"); normalsAccessorParamX.SetAttribute("type", "float"); normalsAccessor.AppendChild(normalsAccessorParamX); XmlElement normalsAccessorParamY = doc.CreateElement("param", uri); normalsAccessorParamY.SetAttribute("name", "Y"); normalsAccessorParamY.SetAttribute("type", "float"); normalsAccessor.AppendChild(normalsAccessorParamY); XmlElement normalsAccessorParamZ = doc.CreateElement("param", uri); normalsAccessorParamZ.SetAttribute("name", "Z"); normalsAccessorParamZ.SetAttribute("type", "float"); normalsAccessor.AppendChild(normalsAccessorParamZ); // uvs XmlElement uvs = doc.CreateElement("source", uri); uvs.SetAttribute("id", meshName + "-lib-UV0"); mesh.AppendChild(uvs); XmlElement uvsArray = doc.CreateElement("float_array", uri); uvsArray.SetAttribute("id", meshName + "-lib-UV0-array"); uvsArray.SetAttribute("count", (vertexList.Count * 2).ToString()); uvs.AppendChild(uvsArray); XmlElement uvsTechnique = doc.CreateElement("technique_common", uri); uvs.AppendChild(uvsTechnique); XmlElement uvsAccessor = doc.CreateElement("accessor", uri); uvsAccessor.SetAttribute("source", "#" + meshName + "-lib-UV0-array"); uvsAccessor.SetAttribute("count", vertexList.Count.ToString()); uvsAccessor.SetAttribute("stride", "2"); uvsTechnique.AppendChild(uvsAccessor); XmlElement uvsAccessorParamS = doc.CreateElement("param", uri); uvsAccessorParamS.SetAttribute("name", "S"); uvsAccessorParamS.SetAttribute("type", "float"); uvsAccessor.AppendChild(uvsAccessorParamS); XmlElement uvsAccessorParamT = doc.CreateElement("param", uri); uvsAccessorParamT.SetAttribute("name", "T"); uvsAccessorParamT.SetAttribute("type", "float"); uvsAccessor.AppendChild(uvsAccessorParamT); // faces XmlElement vertices = doc.CreateElement("vertices", uri); vertices.SetAttribute("id", meshName + "-lib-Vertex"); mesh.AppendChild(vertices); XmlElement verticesInput = doc.CreateElement("input", uri); verticesInput.SetAttribute("semantic", "POSITION"); verticesInput.SetAttribute("source", "#" + meshName + "-lib-Position"); vertices.AppendChild(verticesInput); XmlElement polygons = doc.CreateElement("polygons", uri); mesh.AppendChild(polygons); XmlElement polygonsVertex = doc.CreateElement("input", uri); polygonsVertex.SetAttribute("semantic", "VERTEX"); polygonsVertex.SetAttribute("offset", "0"); polygonsVertex.SetAttribute("source", "#" + meshName + "-lib-Vertex"); polygons.AppendChild(polygonsVertex); XmlElement polygonsNormal = doc.CreateElement("input", uri); polygonsNormal.SetAttribute("semantic", "NORMAL"); polygonsNormal.SetAttribute("offset", "1"); polygonsNormal.SetAttribute("source", "#" + meshName + "-lib-Normal0"); polygons.AppendChild(polygonsNormal); XmlElement polygonsUV = doc.CreateElement("input", uri); polygonsUV.SetAttribute("semantic", "TEXCOORD"); polygonsUV.SetAttribute("offset", "2"); polygonsUV.SetAttribute("set", "0"); polygonsUV.SetAttribute("source", "#" + meshName + "-lib-UV0"); polygons.AppendChild(polygonsUV); // text StringBuilder positionsString = new StringBuilder(12 * 3 * vertexList.Count); StringBuilder normalsString = new StringBuilder(12 * 3 * vertexList.Count); StringBuilder uvsString = new StringBuilder(12 * 2 * vertexList.Count); positionsString.AppendLine(); normalsString.AppendLine(); uvsString.AppendLine(); for (int j = 0; j < vertexList.Count; j++) { xxVertex vert = vertexList[j]; positionsString.Append(vert.Position[0].ToFloatString() + " "); positionsString.Append(vert.Position[1].ToFloatString() + " "); positionsString.AppendLine(vert.Position[2].ToFloatString()); normalsString.Append(vert.Normal[0].ToFloatString() + " "); normalsString.Append(vert.Normal[1].ToFloatString() + " "); normalsString.AppendLine(vert.Normal[2].ToFloatString()); uvsString.Append(vert.UV[0].ToFloatString() + " "); uvsString.AppendLine((1f - vert.UV[1]).ToFloatString()); } positionsArray.InnerText = positionsString.ToString(); normalsArray.InnerText = normalsString.ToString(); uvsArray.InnerText = uvsString.ToString(); for (int j = 0; j < faceList.Count; j++) { XmlElement polygonsP = doc.CreateElement("p", uri); polygons.AppendChild(polygonsP); xxFace face = faceList[j]; StringBuilder polygonsPText = new StringBuilder(12 * 3 * 3); for (int m = 0; m < face.VertexIndices.Length; m++) { polygonsPText.Append(face.VertexIndices[m] + " " + face.VertexIndices[m] + " " + face.VertexIndices[m] + " "); } polygonsP.InnerText = polygonsPText.ToString(0, polygonsPText.Length - 1); } // mesh binding XmlElement meshNode = doc.CreateElement("node", uri); meshNode.SetAttribute("id", meshName); meshNode.SetAttribute("name", meshName); parent.AppendChild(meshNode); XmlElement nodeGeometry; bool skinned = (boneList.Count > 0); if (skinned) { nodeGeometry = doc.CreateElement("instance_controller", uri); nodeGeometry.SetAttribute("url", "#" + meshName + "Controller"); meshNode.AppendChild(nodeGeometry); XmlElement controller = doc.CreateElement("controller", uri); controller.SetAttribute("id", meshName + "Controller"); libraries[(int)LibraryIdx.Controllers].AppendChild(controller); XmlElement skin = doc.CreateElement("skin", uri); skin.SetAttribute("source", "#" + meshName + "-lib"); controller.AppendChild(skin); Matrix combined = Matrix.Identity; /*ObjInfo parentFrame = frame; while (parentFrame is xxFrame) { combined = ((xxFrame)parentFrame).matrix.ToMatrix4() * combined; parentFrame = parentFrame.parent; }*/ XmlElement bindMatrix = doc.CreateElement("bind_shape_matrix", uri); string bindMatrixStr = String.Empty; for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { bindMatrixStr += combined[j, k].ToFloatString() + " "; } } bindMatrix.InnerText = bindMatrixStr.Substring(0, bindMatrixStr.Length - 1); skin.AppendChild(bindMatrix); XmlElement joints = doc.CreateElement("source", uri); joints.SetAttribute("id", meshName + "Controller-Joints"); skin.AppendChild(joints); XmlElement jointsArray = doc.CreateElement("Name_array", uri); jointsArray.SetAttribute("id", meshName + "Controller-Joints-array"); joints.AppendChild(jointsArray); XmlElement jointsTechnique = doc.CreateElement("technique_common", uri); joints.AppendChild(jointsTechnique); XmlElement jointsAccessor = doc.CreateElement("accessor", uri); jointsAccessor.SetAttribute("source", "#" + meshName + "Controller-Joints-array"); jointsTechnique.AppendChild(jointsAccessor); XmlElement jointsParam = doc.CreateElement("param", uri); jointsParam.SetAttribute("type", "name"); jointsAccessor.AppendChild(jointsParam); XmlElement bindPoses = doc.CreateElement("source", uri); bindPoses.SetAttribute("id", meshName + "Controller-Matrices"); skin.AppendChild(bindPoses); XmlElement bindPosesArray = doc.CreateElement("float_array", uri); bindPosesArray.SetAttribute("id", meshName + "Controller-Matrices-array"); bindPoses.AppendChild(bindPosesArray); XmlElement bindPosesTechnique = doc.CreateElement("technique_common", uri); bindPoses.AppendChild(bindPosesTechnique); XmlElement bindPosesAccessor = doc.CreateElement("accessor", uri); bindPosesAccessor.SetAttribute("source", "#" + meshName + "Controller-Matrices-array"); bindPosesTechnique.AppendChild(bindPosesAccessor); XmlElement bindPosesParam = doc.CreateElement("param", uri); bindPosesParam.SetAttribute("type", "float4x4"); bindPosesAccessor.AppendChild(bindPosesParam); XmlElement weights = doc.CreateElement("source", uri); weights.SetAttribute("id", meshName + "Controller-Weights"); skin.AppendChild(weights); XmlElement weightsArray = doc.CreateElement("float_array", uri); weightsArray.SetAttribute("id", meshName + "Controller-Weights-array"); weights.AppendChild(weightsArray); XmlElement weightsTechnique = doc.CreateElement("technique_common", uri); weights.AppendChild(weightsTechnique); XmlElement weightsAccessor = doc.CreateElement("accessor", uri); weightsAccessor.SetAttribute("source", "#" + meshName + "Controller-Weights-array"); weightsTechnique.AppendChild(weightsAccessor); XmlElement weightsParam = doc.CreateElement("param", uri); weightsParam.SetAttribute("type", "float"); weightsAccessor.AppendChild(weightsParam); XmlElement jointSemantic = doc.CreateElement("joints", uri); skin.AppendChild(jointSemantic); XmlElement jointSemanticJoint = doc.CreateElement("input", uri); jointSemanticJoint.SetAttribute("semantic", "JOINT"); jointSemanticJoint.SetAttribute("source", "#" + meshName + "Controller-Joints"); jointSemantic.AppendChild(jointSemanticJoint); XmlElement jointSemanticMatrix = doc.CreateElement("input", uri); jointSemanticMatrix.SetAttribute("semantic", "INV_BIND_MATRIX"); jointSemanticMatrix.SetAttribute("source", "#" + meshName + "Controller-Matrices"); jointSemantic.AppendChild(jointSemanticMatrix); XmlElement verticesArray = doc.CreateElement("vertex_weights", uri); verticesArray.SetAttribute("count", vertexList.Count.ToString()); skin.AppendChild(verticesArray); XmlElement verticesJoint = doc.CreateElement("input", uri); verticesJoint.SetAttribute("semantic", "JOINT"); verticesJoint.SetAttribute("offset", "0"); verticesJoint.SetAttribute("source", "#" + meshName + "Controller-Joints"); verticesArray.AppendChild(verticesJoint); XmlElement verticesWeight = doc.CreateElement("input", uri); verticesWeight.SetAttribute("semantic", "WEIGHT"); verticesWeight.SetAttribute("offset", "1"); verticesWeight.SetAttribute("source", "#" + meshName + "Controller-Weights"); verticesArray.AppendChild(verticesWeight); XmlElement verticesVCount = doc.CreateElement("vcount", uri); verticesArray.AppendChild(verticesVCount); XmlElement verticesV = doc.CreateElement("v", uri); verticesArray.AppendChild(verticesV); StringBuilder weightsArrayText = new StringBuilder(12 * 4 * vertexList.Count); StringBuilder vCountText = new StringBuilder(12 * 4 * vertexList.Count); weightsArrayText.AppendLine(); int[] numVertexWeights = new int[vertexList.Count]; int totalWeights = 0; for (int j = 0; j < vertexList.Count; j++) { xxVertex vert = vertexList[j]; byte[] indices = vert.BoneIndices; float[] weights4 = vert.Weights4(skinned); for (int k = 0; k < indices.Length; k++) { if ((indices[k] < boneList.Count) && (weights4[k] > 0)) { weightsArrayText.Append(weights4[k].ToFloatString() + " "); numVertexWeights[j]++; } } if (numVertexWeights[j] > 0) { weightsArrayText.Remove(weightsArrayText.Length - 1, 1); weightsArrayText.AppendLine(); } vCountText.Append(numVertexWeights[j] + " "); totalWeights += numVertexWeights[j]; } weightsArray.SetAttribute("count", totalWeights.ToString()); weightsArray.InnerText = weightsArrayText.ToString(); weightsAccessor.SetAttribute("count", totalWeights.ToString()); verticesVCount.InnerText = vCountText.ToString(0, vCountText.Length - 1); StringBuilder jointsArrayText = new StringBuilder(32 * boneList.Count); StringBuilder bindPosesArrayText = new StringBuilder(12 * 16 * boneList.Count); jointsArrayText.AppendLine(); bindPosesArrayText.AppendLine(); int totalBones = 0; int[] boneIdx = new int[boneList.Count]; List<string> usedBoneNames = new List<string>(boneList.Count); for (int j = 0; j < boneList.Count; j++) { xxBone bone = boneList[j]; string boneName = EncodeName(bone.Name); Matrix boneMatrix = bone.Matrix; for (int m = 0; m < 4; m++) { for (int n = 0; n < 4; n++) { bindPosesArrayText.Append(boneMatrix[m, n].ToFloatString() + " "); } } usedBoneNames.Add(boneName); bindPosesArrayText.Remove(bindPosesArrayText.Length - 1, 1); bindPosesArrayText.AppendLine(); jointsArrayText.Append(boneName + " "); boneIdx[j] = totalBones; totalBones++; } jointsArray.SetAttribute("count", totalBones.ToString()); if (jointsArrayText.Length > 0) { jointsArrayText.Remove(jointsArrayText.Length - 1, 1); jointsArrayText.AppendLine(); } jointsArray.InnerText = jointsArrayText.ToString(); jointsAccessor.SetAttribute("count", totalBones.ToString()); bindPosesArray.SetAttribute("count", (totalBones * 16).ToString()); bindPosesArray.InnerText = bindPosesArrayText.ToString(); bindPosesAccessor.SetAttribute("count", totalBones.ToString()); bindPosesAccessor.SetAttribute("stride", "16"); StringBuilder vText = new StringBuilder(12 * 2 * vertexList.Count); int weightsIdx = 0; for (int j = 0; j < vertexList.Count; j++) { xxVertex vert = vertexList[j]; byte[] indices = vert.BoneIndices; float[] weights4 = vert.Weights4(skinned); for (int k = 0; k < indices.Length; k++) { if ((indices[k] < boneList.Count) && (weights4[k] > 0)) { vText.Append(boneIdx[indices[k]] + " " + weightsIdx + " "); weightsIdx++; } } } verticesV.InnerText = vText.ToString(0, vText.Length - 1); } else { nodeGeometry = doc.CreateElement("instance_geometry", uri); nodeGeometry.SetAttribute("url", "#" + meshName + "-lib"); meshNode.AppendChild(nodeGeometry); } int matIdx = meshObj.MaterialIndex; if ((matIdx >= 0) && (matIdx < parser.MaterialList.Count)) { xxMaterial mat = parser.MaterialList[matIdx]; if (!usedMaterials.Contains(mat)) { usedMaterials.Add(mat); } string matName = EncodeName(mat.Name); polygons.SetAttribute("material", matName); XmlElement nodeMaterial = doc.CreateElement("bind_material", uri); nodeGeometry.AppendChild(nodeMaterial); XmlElement nodeMaterialTechnique = doc.CreateElement("technique_common", uri); nodeMaterial.AppendChild(nodeMaterialTechnique); XmlElement nodeMaterialInstance = doc.CreateElement("instance_material", uri); nodeMaterialInstance.SetAttribute("symbol", matName); nodeMaterialInstance.SetAttribute("target", "#" + matName); nodeMaterialTechnique.AppendChild(nodeMaterialInstance); } polygons.SetAttribute("count", faceList.Count.ToString()); } }
public bool RemoveSVI(xxFrame meshFrame) { bool removed = false; for (int i = 0; i < Parser.sections.Count; i++) { sviParser svi = Parser.sections[i]; if (svi.meshName == meshFrame.Name) { Parser.sections.Remove(svi); i--; removed = true; } } return removed; }
public bool AddSVI(xxFrame meshFrame, int submeshIdx, bool positions, bool bones, bool normals, bool uvs) { sviParser svi = FindSVI(meshFrame.Name, submeshIdx); if (svi != null) { return false; } svi = new sviParser(); svi.meshName = meshFrame.Name; svi.submeshIdx = submeshIdx; svi.indices = new ushort[meshFrame.Mesh.SubmeshList[submeshIdx].VertexList.Count]; for (ushort i = 0; i < svi.indices.Length; i++) { svi.indices[i] = i; } if (positions) { svi.positionsPresent = 1; svi.positions = new Vector3[svi.indices.Length]; List<xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList; for (int i = 0; i < svi.positions.Length; i++) { svi.positions[i] = verts[i].Position; } } if (bones) { svi.bonesPresent = 1; svi.boneWeights3 = new float[svi.indices.Length][]; svi.boneIndices = new byte[svi.indices.Length][]; List<xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList; for (int i = 0; i < svi.boneWeights3.Length; i++) { svi.boneWeights3[i] = (float[])verts[i].Weights3.Clone(); svi.boneIndices[i] = (byte[])verts[i].BoneIndices.Clone(); } svi.bones = new sviParser.sviBone[meshFrame.Mesh.BoneList.Count]; for (ushort i = 0; i < svi.bones.Length; i++) { sviParser.sviBone bone = new sviParser.sviBone(); bone.name = (string)meshFrame.Mesh.BoneList[i].Name.Clone(); bone.boneIdx = meshFrame.Mesh.BoneList[i].Index; bone.matrix = meshFrame.Mesh.BoneList[i].Matrix; svi.bones[i] = bone; } } if (normals) { svi.normalsPresent = 1; svi.normals = new Vector3[svi.indices.Length]; List<xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList; for (int i = 0; i < svi.normals.Length; i++) { svi.normals[i] = verts[i].Normal; } } if (uvs) { svi.uvsPresent = 1; svi.uvs = new Vector2[svi.indices.Length]; List<xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList; for (int i = 0; i < svi.uvs.Length; i++) { svi.uvs[i].X = verts[i].UV[0]; svi.uvs[i].Y = verts[i].UV[1]; } } Parser.sections.Add(svi); return true; }
public static void ExportMorphFbx([DefaultVar]xxParser xxparser, string path, xxFrame meshFrame, xaParser xaparser, xaMorphClip morphClip, string exportFormat, bool oneBlendShape) { Fbx.Exporter.ExportMorph(path, xxparser, meshFrame, morphClip, xaparser, exportFormat, oneBlendShape); }
private void buttonCopyToSVIEXes_Click(object sender, EventArgs e) { List <DockContent> formXXList; if (!Gui.Docking.DockContents.TryGetValue(typeof(FormXX), out formXXList)) { return; } List <DockContent> formPPList; if (!Gui.Docking.DockContents.TryGetValue(typeof(FormPP), out formPPList)) { return; } groupBoxAA2SVIEXJuggler.Enabled = false; try { List <string> srcParserVarList = new List <string>(); List <string> srcEditorVarList = new List <string>(); foreach (FormPP form in formPPList) { foreach (ListViewItem item in form.otherSubfilesList.SelectedItems) { if (item.Text.ToLower().EndsWith(".sviex")) { string srcParserVar = Gui.Scripting.GetNextVariable("sviexParser"); srcParserVarList.Add(srcParserVar); string parserCommand = srcParserVar + " = OpenSVIEX(parser=" + form.ParserVar + ", name=\"" + item.Text + "\")"; Gui.Scripting.RunScript(parserCommand); string srcEditorVar = Gui.Scripting.GetNextVariable("sviexEditor"); srcEditorVarList.Add(srcEditorVar); Gui.Scripting.RunScript(srcEditorVar + " = sviexEditor(parser=" + srcParserVar + ")"); } else if (item.Text.ToLower().EndsWith(".svi")) { string srcParserVar = Gui.Scripting.GetNextVariable("sviParser"); srcParserVarList.Add(srcParserVar); string parserCommand = srcParserVar + " = OpenSVI(parser=" + form.ParserVar + ", name=\"" + item.Text + "\")"; Gui.Scripting.RunScript(parserCommand); string srcEditorVar = Gui.Scripting.GetNextVariable("sviexEditor"); srcEditorVarList.Add(srcEditorVar); Gui.Scripting.RunScript(srcEditorVar + " = sviexEditor(parser=" + srcParserVar + ")"); } } } foreach (string srcEditorVar in srcEditorVarList) { sviexEditor editor = (sviexEditor)Gui.Scripting.Variables[srcEditorVar]; bool sviexChanged = false; for (ushort j = 0; j < editor.Parser.sections.Count; j++) { sviParser submeshSection = editor.Parser.sections[j]; foreach (FormXX xxForm in formXXList) { foreach (ListViewItem item in xxForm.listViewMesh.SelectedItems) { xxFrame meshFrame = xxForm.Editor.Meshes[(int)item.Tag]; if (submeshSection.meshName == meshFrame.Name) { for (ushort i = 0; i < meshFrame.Mesh.SubmeshList.Count; i++) { if (radioButtonSubmeshesAll.Checked || radioButtonSubmeshesSelected.Checked && xxForm.textBoxMeshName.Text == meshFrame.Name && i < xxForm.dataGridViewMesh.Rows.Count && xxForm.dataGridViewMesh.Rows[i].Selected) { if (submeshSection.submeshIdx == i) { bool copied = (bool)Gui.Scripting.RunScript(srcEditorVar + ".CopyIntoSVI(meshFrame=" + xxForm.EditorVar + ".Meshes[" + (int)item.Tag + "], submeshIdx=" + i + ", positions=" + checkBoxElementsPositions.Checked + ", bones=" + checkBoxElementsBonesWeights.Checked + ", normals=" + checkBoxElementsNormals.Checked + ", uvs=" + checkBoxElementsUVs.Checked + ", unrestricted=" + checkBoxUnrestricted.Checked + ", nearestBones=" + checkBoxNearestBones.Checked + ", nearestNormals=" + checkBoxNearestNormals.Checked + ", nearestUVs=" + checkBoxNearestUVs.Checked + ")"); if (copied) { sviexChanged = true; } break; } } } break; } } } } if (sviexChanged) { foreach (FormPP form in formPPList) { foreach (ListViewItem item in form.otherSubfilesList.SelectedItems) { if (item.Text == editor.Parser.Name) { int index = srcEditorVarList.IndexOf(srcEditorVar); string srcParserVar = srcParserVarList[index]; Gui.Scripting.RunScript(form.EditorVar + ".ReplaceSubfile(file=" + srcParserVar + ")"); form.Changed = true; item.Font = new Font(form.otherSubfilesList.Font, FontStyle.Bold); break; } } } } } foreach (string parserVar in srcParserVarList) { Gui.Scripting.RunScript(parserVar + "=null"); } foreach (string editorVar in srcEditorVarList) { Gui.Scripting.RunScript(editorVar + "=null"); } } catch (Exception ex) { Utility.ReportException(ex); } groupBoxAA2SVIEXJuggler.Enabled = true; }
public ComboBoxItemMesh(xxFrame meshFrame, string submeshes) { this.meshFrame = meshFrame; this.submeshes = submeshes; }
public bool AddSVI(xxFrame meshFrame, int submeshIdx, bool positions, bool bones, bool normals, bool uvs) { sviParser svi = FindSVI(meshFrame.Name, submeshIdx); if (svi != null) { return(false); } svi = new sviParser(); svi.meshName = meshFrame.Name; svi.submeshIdx = submeshIdx; svi.indices = new ushort[meshFrame.Mesh.SubmeshList[submeshIdx].VertexList.Count]; for (ushort i = 0; i < svi.indices.Length; i++) { svi.indices[i] = i; } if (positions) { svi.positionsPresent = 1; svi.positions = new Vector3[svi.indices.Length]; List <xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList; for (int i = 0; i < svi.positions.Length; i++) { svi.positions[i] = verts[i].Position; } } if (bones) { svi.bonesPresent = 1; svi.boneWeights3 = new float[svi.indices.Length][]; svi.boneIndices = new byte[svi.indices.Length][]; List <xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList; for (int i = 0; i < svi.boneWeights3.Length; i++) { svi.boneWeights3[i] = (float[])verts[i].Weights3.Clone(); svi.boneIndices[i] = (byte[])verts[i].BoneIndices.Clone(); } svi.bones = new sviParser.sviBone[meshFrame.Mesh.BoneList.Count]; for (ushort i = 0; i < svi.bones.Length; i++) { sviParser.sviBone bone = new sviParser.sviBone(); bone.name = (string)meshFrame.Mesh.BoneList[i].Name.Clone(); bone.boneIdx = meshFrame.Mesh.BoneList[i].Index; bone.matrix = meshFrame.Mesh.BoneList[i].Matrix; svi.bones[i] = bone; } } if (normals) { svi.normalsPresent = 1; svi.normals = new Vector3[svi.indices.Length]; List <xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList; for (int i = 0; i < svi.normals.Length; i++) { svi.normals[i] = verts[i].Normal; } } if (uvs) { svi.uvsPresent = 1; svi.uvs = new Vector2[svi.indices.Length]; List <xxVertex> verts = meshFrame.Mesh.SubmeshList[submeshIdx].VertexList; for (int i = 0; i < svi.uvs.Length; i++) { svi.uvs[i].X = verts[i].UV[0]; svi.uvs[i].Y = verts[i].UV[1]; } } Parser.sections.Add(svi); return(true); }
public bool CopyIntoSVI(xxFrame meshFrame, int submeshIdx, bool positions, bool bones, bool normals, bool uvs, bool unrestricted, bool nearestBones, bool nearestNormals, bool nearestUVs) { bool argPositions = positions, argBones = bones, argNormals = normals, argUVs = uvs; foreach (sviParser submeshSection in Parser.sections) { if (submeshSection.meshName == meshFrame.Name && submeshSection.submeshIdx == submeshIdx) { xxSubmesh submesh = meshFrame.Mesh.SubmeshList[submeshIdx]; int[] nearestVertexIndices = null; if (nearestBones || nearestNormals || nearestUVs) { if (submeshSection.positionsPresent == 1) { nearestVertexIndices = new int[submesh.VertexList.Count]; for (ushort i = 0; i < submesh.VertexList.Count; i++) { int bestIdx = -1; double nearestDist = Double.MaxValue; for (ushort j = 0; j < submeshSection.positions.Length; j++) { double distSquare = (submeshSection.positions[j].X - submesh.VertexList[i].Position.X) * (submeshSection.positions[j].X - submesh.VertexList[i].Position.X) + (submeshSection.positions[j].Y - submesh.VertexList[i].Position.Y) * (submeshSection.positions[j].Y - submesh.VertexList[i].Position.Y) + (submeshSection.positions[j].Z - submesh.VertexList[i].Position.Z) * (submeshSection.positions[j].Z - submesh.VertexList[i].Position.Z); if (distSquare < nearestDist) { bestIdx = j; nearestDist = distSquare; } } nearestVertexIndices[i] = bestIdx; } } else { Report.ReportLog(meshFrame.Name + "[" + submeshIdx + "] has no positions in " + Parser.Name + "."); return(false); } } Vector3[] newNormals = submeshSection.normals; Vector2[] newUVs = submeshSection.uvs; byte[][] newBoneIndices = submeshSection.boneIndices; float[][] newBoneWeights3 = submeshSection.boneWeights3; if (submeshSection.indices.Length != submesh.VertexList.Count) { if (unrestricted) { submeshSection.indices = new ushort[submesh.VertexList.Count]; for (ushort i = 0; i < submeshSection.indices.Length; i++) { submeshSection.indices[i] = i; } if (submeshSection.positionsPresent == 1) { submeshSection.positions = new Vector3[submesh.VertexList.Count]; } if (submeshSection.bonesPresent == 1) { newBoneWeights3 = new float[submesh.VertexList.Count][]; newBoneIndices = new byte[submesh.VertexList.Count][]; } if (submeshSection.normalsPresent == 1) { newNormals = new Vector3[submesh.VertexList.Count]; } if (submeshSection.uvsPresent == 1) { newUVs = new Vector2[submesh.VertexList.Count]; } positions = bones = normals = uvs = true; } else { Report.ReportLog(meshFrame.Name + "[" + submeshIdx + "] has a different number of vertices than defined in the sviex(" + submesh.VertexList.Count + "/" + submeshSection.indices.Length + ")."); return(false); } } if (positions && submeshSection.positionsPresent == 1) { for (ushort i = 0; i < submeshSection.positions.Length; i++) { submeshSection.positions[i] = submesh.VertexList[submeshSection.indices[i]].Position; } } if (bones && submeshSection.bonesPresent == 1) { if (nearestBones) { for (ushort i = 0; i < newBoneWeights3.Length; i++) { newBoneWeights3[i] = (float[])submeshSection.boneWeights3[nearestVertexIndices[submeshSection.indices[i]]].Clone(); newBoneIndices[i] = (byte[])submeshSection.boneIndices[nearestVertexIndices[submeshSection.indices[i]]].Clone(); } } else { for (ushort i = 0; i < newBoneWeights3.Length; i++) { newBoneWeights3[i] = (float[])submesh.VertexList[submeshSection.indices[i]].Weights3.Clone(); newBoneIndices[i] = (byte[])submesh.VertexList[submeshSection.indices[i]].BoneIndices.Clone(); } submeshSection.bones = new sviParser.sviBone[meshFrame.Mesh.BoneList.Count]; for (ushort i = 0; i < submeshSection.bones.Length; i++) { sviParser.sviBone bone = new sviParser.sviBone(); bone.name = (string)meshFrame.Mesh.BoneList[i].Name.Clone(); bone.boneIdx = meshFrame.Mesh.BoneList[i].Index; bone.matrix = meshFrame.Mesh.BoneList[i].Matrix; submeshSection.bones[i] = bone; } } submeshSection.boneWeights3 = newBoneWeights3; submeshSection.boneIndices = newBoneIndices; } if (normals && submeshSection.normalsPresent == 1) { if (nearestNormals) { for (ushort i = 0; i < submesh.VertexList.Count; i++) { newNormals[i] = submeshSection.normals[nearestVertexIndices[submeshSection.indices[i]]]; } } else { for (ushort i = 0; i < submesh.VertexList.Count; i++) { newNormals[i] = submesh.VertexList[submeshSection.indices[i]].Normal; } } submeshSection.normals = newNormals; } if (uvs && submeshSection.uvsPresent == 1) { if (nearestUVs) { for (ushort i = 0; i < submesh.VertexList.Count; i++) { newUVs[i] = submeshSection.uvs[nearestVertexIndices[submeshSection.indices[i]]]; } } else { for (ushort i = 0; i < submesh.VertexList.Count; i++) { float[] uv = submesh.VertexList[submeshSection.indices[i]].UV; newUVs[i] = new Vector2(uv[0], uv[1]); } } submeshSection.uvs = newUVs; } return(true); } } return(false); }
public bool CopyToSubmesh(xxFrame meshFrame, int submeshIdx, bool positions, bool bones, bool normals, bool uvs) { foreach (sviParser submeshSection in Parser.sections) { if (submeshSection.meshName == meshFrame.Name && submeshSection.submeshIdx == submeshIdx) { xxSubmesh submesh = meshFrame.Mesh.SubmeshList[submeshIdx]; if (submeshSection.indices.Length != submesh.VertexList.Count) { Report.ReportLog(meshFrame.Name + "[" + submeshIdx + "] has a different number of vertices than defined in the sviex(" + submesh.VertexList.Count + "/" + submeshSection.indices.Length + ")."); return(false); } if (positions && submeshSection.positionsPresent == 1) { for (ushort i = 0; i < submeshSection.positions.Length; i++) { submesh.VertexList[submeshSection.indices[i]].Position = submeshSection.positions[i]; } } if (bones && submeshSection.bonesPresent == 1) { for (ushort i = 0; i < submeshSection.boneWeights3.Length; i++) { submesh.VertexList[submeshSection.indices[i]].Weights3 = (float[])submeshSection.boneWeights3[i].Clone(); } for (ushort i = 0; i < submeshSection.boneIndices.Length; i++) { submesh.VertexList[submeshSection.indices[i]].BoneIndices = (byte[])submeshSection.boneIndices[i].Clone(); } meshFrame.Mesh.BoneList.Clear(); for (ushort i = 0; i < submeshSection.bones.Length; i++) { xxBone bone = new xxBone(); bone.Name = (string)submeshSection.bones[i].name.Clone(); bone.Index = submeshSection.bones[i].boneIdx; bone.Matrix = submeshSection.bones[i].matrix; meshFrame.Mesh.BoneList.Add(bone); } } if (normals && submeshSection.normalsPresent == 1) { for (ushort i = 0; i < submeshSection.normals.Length; i++) { submesh.VertexList[submeshSection.indices[i]].Normal = submeshSection.normals[i]; } } if (uvs && submeshSection.uvsPresent == 1) { for (ushort i = 0; i < submeshSection.uvs.Length; i++) { submesh.VertexList[submeshSection.indices[i]].UV = new float[2] { submeshSection.uvs[i].X, submeshSection.uvs[i].Y }; } } meshFrame.Mesh.VertexListDuplicate = xx.CreateVertexListDup(meshFrame.Mesh.SubmeshList); return(true); } } return(false); }
public void CopyNearestNormals(object[] srcMeshes, object[] srcSubmeshes, object[] dstMeshes, object[] dstSubmeshes, sviexParser dstParser, double nearVertexThreshold, bool nearestNormal, bool automatic) { xxFrame[] srcMeshArr = Utility.Convert <xxFrame>(srcMeshes); double[] srcSubmeshDoubleIndices = Utility.Convert <double>(srcSubmeshes); HashSet <xxSubmesh> srcSubmeshSet = new HashSet <xxSubmesh>(); int srcSubmeshIdxIdx = 0; int srcTotalSubmeshes = 0; foreach (xxFrame meshFrame in srcMeshArr) { int numSubmeshes = (int)srcSubmeshDoubleIndices[srcSubmeshIdxIdx++]; srcTotalSubmeshes += numSubmeshes; for (int i = 0; i < numSubmeshes; i++) { int srcSubmeshIdx = (int)srcSubmeshDoubleIndices[srcSubmeshIdxIdx++]; foreach (sviParser section in SortedParser.sections) { if (section.meshName == meshFrame.Name && section.submeshIdx == srcSubmeshIdx) { xxSubmesh submesh = meshFrame.Mesh.SubmeshList[srcSubmeshIdx]; srcSubmeshSet.Add(submesh); break; } } } } if (srcTotalSubmeshes != srcSubmeshSet.Count) { Report.ReportLog("Not all source submeshes exist in " + SortedParser.Name + ". Using only " + srcSubmeshSet.Count + "."); } xxFrame[] dstMeshArr = Utility.Convert <xxFrame>(dstMeshes); double[] dstSubmeshDoubleIndices = Utility.Convert <double>(dstSubmeshes); List <xxSubmesh> dstSubmeshList = new List <xxSubmesh>(); int dstSubmeshIdxIdx = 0; foreach (xxFrame meshFrame in dstMeshArr) { int numSubmeshes = (int)dstSubmeshDoubleIndices[dstSubmeshIdxIdx++]; if (numSubmeshes == -1) { dstSubmeshList.AddRange(meshFrame.Mesh.SubmeshList); } else { for (int i = 0; i < numSubmeshes; i++) { int dstSubmeshIdx = (int)dstSubmeshDoubleIndices[dstSubmeshIdxIdx++]; xxSubmesh submesh = meshFrame.Mesh.SubmeshList[dstSubmeshIdx]; dstSubmeshList.Add(submesh); } } } sviexParser newParser = new sviexParser(); foreach (xxFrame dstMeshFrame in dstMeshArr) { foreach (xxSubmesh dstSubmesh in dstMeshFrame.Mesh.SubmeshList) { if (!dstSubmeshList.Contains(dstSubmesh)) { continue; } if (progressBar != null) { progressBar.Maximum += dstSubmesh.VertexList.Count; } sviParser newSection = new sviParser(); newSection.meshName = dstMeshFrame.Name; newSection.submeshIdx = dstMeshFrame.Mesh.SubmeshList.IndexOf(dstSubmesh); newSection.indices = new ushort[dstSubmesh.VertexList.Count]; newSection.normalsPresent = 1; newSection.normals = new Vector3[dstSubmesh.VertexList.Count]; for (ushort i = 0; i < dstSubmesh.VertexList.Count; i++) { xxVertex dstVertex = dstSubmesh.VertexList[i]; if (automatic) { nearestNormal = false; for (int j = 0; j < dstSubmesh.VertexList.Count; j++) { if (j != i) { xxVertex vert = dstSubmesh.VertexList[j]; double distSquare = (vert.Position.X - dstVertex.Position.X) * (vert.Position.X - dstVertex.Position.X) + (vert.Position.Y - dstVertex.Position.Y) * (vert.Position.Y - dstVertex.Position.Y) + (vert.Position.Z - dstVertex.Position.Z) * (vert.Position.Z - dstVertex.Position.Z); if (distSquare <= nearVertexThreshold) { nearestNormal = true; break; } } } } Dictionary <xxFrame, Dictionary <xxSubmesh, List <int> > > bestFindings = new Dictionary <xxFrame, Dictionary <xxSubmesh, List <int> > >(); int totalFindings = 0; xxFrame bestMeshFrame = null; xxSubmesh bestSubmesh = null; int bestIdx = -1; double bestDist = double.MaxValue; foreach (xxFrame srcMeshFrame in srcMeshArr) { Dictionary <xxSubmesh, List <int> > bestSubmeshFindings = new Dictionary <xxSubmesh, List <int> >(); foreach (xxSubmesh srcSubmesh in srcMeshFrame.Mesh.SubmeshList) { if (!srcSubmeshSet.Contains(srcSubmesh)) { continue; } List <int> bestIndexFindings = new List <int>(srcSubmesh.VertexList.Count); for (int j = 0; j < srcSubmesh.VertexList.Count; j++) { xxVertex srcVertex = srcSubmesh.VertexList[j]; double distSquare = (srcVertex.Position.X - dstVertex.Position.X) * (srcVertex.Position.X - dstVertex.Position.X) + (srcVertex.Position.Y - dstVertex.Position.Y) * (srcVertex.Position.Y - dstVertex.Position.Y) + (srcVertex.Position.Z - dstVertex.Position.Z) * (srcVertex.Position.Z - dstVertex.Position.Z); if (distSquare <= nearVertexThreshold) { bestIndexFindings.Add(j); totalFindings++; continue; } if (totalFindings == 0 && distSquare < bestDist) { bestMeshFrame = srcMeshFrame; bestSubmesh = srcSubmesh; bestIdx = j; bestDist = distSquare; } } if (bestIndexFindings.Count > 0) { bestSubmeshFindings.Add(srcSubmesh, bestIndexFindings); } } if (bestSubmeshFindings.Count > 0) { bestFindings.Add(srcMeshFrame, bestSubmeshFindings); } } if (totalFindings > 0) { Vector3 normalSummed = new Vector3(); Vector3 normalNearest = new Vector3(); double nearestDist = Double.MaxValue; foreach (var finding in bestFindings) { foreach (sviParser srcSection in SortedParser.sections) { if (srcSection.meshName == finding.Key.Name) { foreach (var submeshFinding in finding.Value) { if (srcSection.submeshIdx == finding.Key.Mesh.SubmeshList.IndexOf(submeshFinding.Key)) { foreach (int j in submeshFinding.Value) { if (nearestNormal) { double distSquare = (srcSection.normals[j].X - dstVertex.Normal.X) * (srcSection.normals[j].X - dstVertex.Normal.X) + (srcSection.normals[j].Y - dstVertex.Normal.Y) * (srcSection.normals[j].Y - dstVertex.Normal.Y) + (srcSection.normals[j].Z - dstVertex.Normal.Z) * (srcSection.normals[j].Z - dstVertex.Normal.Z); if (distSquare < nearestDist) { normalNearest = srcSection.normals[j]; nearestDist = distSquare; } } else { normalSummed += srcSection.normals[j]; } } } } } } } if (totalFindings > 1) { normalSummed.Normalize(); } newSection.indices[i] = i; newSection.normals[i] = nearestNormal ? normalNearest : normalSummed; } else { int bestSubmeshIdx = bestMeshFrame.Mesh.SubmeshList.IndexOf(bestSubmesh); foreach (sviParser srcSection in SortedParser.sections) { if (srcSection.meshName == bestMeshFrame.Name && srcSection.submeshIdx == bestSubmeshIdx) { newSection.indices[i] = i; newSection.normals[i] = srcSection.normals[bestIdx]; break; } } } if (progressBar != null) { progressBar.PerformStep(); } } newParser.sections.Add(newSection); } } dstParser.sections = newParser.sections; }
public bool CopyIntoSVI(xxFrame meshFrame, int submeshIdx, bool positions, bool bones, bool normals, bool uvs, bool unrestricted, bool nearestBones, bool nearestNormals, bool nearestUVs) { bool argPositions = positions, argBones = bones, argNormals = normals, argUVs = uvs; foreach (sviParser submeshSection in Parser.sections) { if (submeshSection.meshName == meshFrame.Name && submeshSection.submeshIdx == submeshIdx) { xxSubmesh submesh = meshFrame.Mesh.SubmeshList[submeshIdx]; int[] nearestVertexIndices = null; if (nearestBones || nearestNormals || nearestUVs) { if (submeshSection.positionsPresent == 1) { nearestVertexIndices = new int[submesh.VertexList.Count]; for (ushort i = 0; i < submesh.VertexList.Count; i++) { int bestIdx = -1; double nearestDist = Double.MaxValue; for (ushort j = 0; j < submeshSection.positions.Length; j++) { double distSquare = (submeshSection.positions[j].X - submesh.VertexList[i].Position.X) * (submeshSection.positions[j].X - submesh.VertexList[i].Position.X) + (submeshSection.positions[j].Y - submesh.VertexList[i].Position.Y) * (submeshSection.positions[j].Y - submesh.VertexList[i].Position.Y) + (submeshSection.positions[j].Z - submesh.VertexList[i].Position.Z) * (submeshSection.positions[j].Z - submesh.VertexList[i].Position.Z); if (distSquare < nearestDist) { bestIdx = j; nearestDist = distSquare; } } nearestVertexIndices[i] = bestIdx; } } else { Report.ReportLog(meshFrame.Name + "[" + submeshIdx + "] has no positions in " + Parser.Name + "."); return false; } } Vector3[] newNormals = submeshSection.normals; Vector2[] newUVs = submeshSection.uvs; byte[][] newBoneIndices = submeshSection.boneIndices; float[][] newBoneWeights3 = submeshSection.boneWeights3; if (submeshSection.indices.Length != submesh.VertexList.Count) { if (unrestricted) { submeshSection.indices = new ushort[submesh.VertexList.Count]; for (ushort i = 0; i < submeshSection.indices.Length; i++) { submeshSection.indices[i] = i; } if (submeshSection.positionsPresent == 1) { submeshSection.positions = new Vector3[submesh.VertexList.Count]; } if (submeshSection.bonesPresent == 1) { newBoneWeights3 = new float[submesh.VertexList.Count][]; newBoneIndices = new byte[submesh.VertexList.Count][]; } if (submeshSection.normalsPresent == 1) { newNormals = new Vector3[submesh.VertexList.Count]; } if (submeshSection.uvsPresent == 1) { newUVs = new Vector2[submesh.VertexList.Count]; } positions = bones = normals = uvs = true; } else { Report.ReportLog(meshFrame.Name + "[" + submeshIdx + "] has a different number of vertices than defined in the sviex(" + submesh.VertexList.Count + "/" + submeshSection.indices.Length + ")."); return false; } } if (positions && submeshSection.positionsPresent == 1) { for (ushort i = 0; i < submeshSection.positions.Length; i++) { submeshSection.positions[i] = submesh.VertexList[submeshSection.indices[i]].Position; } } if (bones && submeshSection.bonesPresent == 1) { if (nearestBones) { for (ushort i = 0; i < newBoneWeights3.Length; i++) { newBoneWeights3[i] = (float[])submeshSection.boneWeights3[nearestVertexIndices[submeshSection.indices[i]]].Clone(); newBoneIndices[i] = (byte[])submeshSection.boneIndices[nearestVertexIndices[submeshSection.indices[i]]].Clone(); } } else { for (ushort i = 0; i < newBoneWeights3.Length; i++) { newBoneWeights3[i] = (float[])submesh.VertexList[submeshSection.indices[i]].Weights3.Clone(); newBoneIndices[i] = (byte[])submesh.VertexList[submeshSection.indices[i]].BoneIndices.Clone(); } submeshSection.bones = new sviParser.sviBone[meshFrame.Mesh.BoneList.Count]; for (ushort i = 0; i < submeshSection.bones.Length; i++) { sviParser.sviBone bone = new sviParser.sviBone(); bone.name = (string)meshFrame.Mesh.BoneList[i].Name.Clone(); bone.boneIdx = meshFrame.Mesh.BoneList[i].Index; bone.matrix = meshFrame.Mesh.BoneList[i].Matrix; submeshSection.bones[i] = bone; } } submeshSection.boneWeights3 = newBoneWeights3; submeshSection.boneIndices = newBoneIndices; } if (normals && submeshSection.normalsPresent == 1) { if (nearestNormals) { for (ushort i = 0; i < submesh.VertexList.Count; i++) { newNormals[i] = submeshSection.normals[nearestVertexIndices[submeshSection.indices[i]]]; } } else { for (ushort i = 0; i < submesh.VertexList.Count; i++) { newNormals[i] = submesh.VertexList[submeshSection.indices[i]].Normal; } } submeshSection.normals = newNormals; } if (uvs && submeshSection.uvsPresent == 1) { if (nearestUVs) { for (ushort i = 0; i < submesh.VertexList.Count; i++) { newUVs[i] = submeshSection.uvs[nearestVertexIndices[submeshSection.indices[i]]]; } } else { for (ushort i = 0; i < submesh.VertexList.Count; i++) { float[] uv = submesh.VertexList[submeshSection.indices[i]].UV; newUVs[i] = new Vector2(uv[0], uv[1]); } } submeshSection.uvs = newUVs; } return true; } } return false; }
public bool CopyToSubmesh(xxFrame meshFrame, int submeshIdx, bool positions, bool bones, bool normals, bool uvs) { foreach (sviParser submeshSection in Parser.sections) { if (submeshSection.meshName == meshFrame.Name && submeshSection.submeshIdx == submeshIdx) { xxSubmesh submesh = meshFrame.Mesh.SubmeshList[submeshIdx]; if (submeshSection.indices.Length != submesh.VertexList.Count) { Report.ReportLog(meshFrame.Name + "[" + submeshIdx + "] has a different number of vertices than defined in the sviex(" + submesh.VertexList.Count + "/" + submeshSection.indices.Length + ")."); return false; } if (positions && submeshSection.positionsPresent == 1) { for (ushort i = 0; i < submeshSection.positions.Length; i++) { submesh.VertexList[submeshSection.indices[i]].Position = submeshSection.positions[i]; } } if (bones && submeshSection.bonesPresent == 1) { for (ushort i = 0; i < submeshSection.boneWeights3.Length; i++) { submesh.VertexList[submeshSection.indices[i]].Weights3 = (float[])submeshSection.boneWeights3[i].Clone(); } for (ushort i = 0; i < submeshSection.boneIndices.Length; i++) { submesh.VertexList[submeshSection.indices[i]].BoneIndices = (byte[])submeshSection.boneIndices[i].Clone(); } meshFrame.Mesh.BoneList.Clear(); for (ushort i = 0; i < submeshSection.bones.Length; i++) { xxBone bone = new xxBone(); bone.Name = (string)submeshSection.bones[i].name.Clone(); bone.Index = submeshSection.bones[i].boneIdx; bone.Matrix = submeshSection.bones[i].matrix; meshFrame.Mesh.BoneList.Add(bone); } } if (normals && submeshSection.normalsPresent == 1) { for (ushort i = 0; i < submeshSection.normals.Length; i++) { submesh.VertexList[submeshSection.indices[i]].Normal = submeshSection.normals[i]; } } if (uvs && submeshSection.uvsPresent == 1) { for (ushort i = 0; i < submeshSection.uvs.Length; i++) { submesh.VertexList[submeshSection.indices[i]].UV = new float[2] { submeshSection.uvs[i].X, submeshSection.uvs[i].Y }; } } meshFrame.Mesh.VertexListDuplicate = xx.CreateVertexListDup(meshFrame.Mesh.SubmeshList); return true; } } return false; }
protected xxFrame ParseFrame() { xxFrame frame = new xxFrame(); frame.Name = reader.ReadName(); int numChildFrames = reader.ReadInt32(); frame.InitChildren(numChildFrames); frame.Matrix = reader.ReadMatrix(); frame.Unknown1 = (Format >= 7) ? reader.ReadBytes(32) : reader.ReadBytes(16); int numSubmeshes = reader.ReadInt32(); frame.Bounds = new BoundingBox(reader.ReadVector3(), reader.ReadVector3()); frame.Unknown2 = (Format >= 7) ? reader.ReadBytes(64) : reader.ReadBytes(16); if (Format >= 6) { frame.Name2 = reader.ReadName(); } if (numSubmeshes > 0) { xxMesh mesh = new xxMesh(); frame.Mesh = mesh; mesh.NumVector2PerVertex = reader.ReadByte(); mesh.SubmeshList = new List<xxSubmesh>(numSubmeshes); for (int i = 0; i < numSubmeshes; i++) { xxSubmesh submesh = new xxSubmesh(); mesh.SubmeshList.Add(submesh); submesh.Unknown1 = (Format >= 7) ? reader.ReadBytes(64) : reader.ReadBytes(16); submesh.MaterialIndex = reader.ReadInt32(); submesh.FaceList = ParseFaceList(); submesh.VertexList = ParseVertexList(); if (Format >= 7) { submesh.Unknown2 = reader.ReadBytes(20); } if (mesh.NumVector2PerVertex > 0) { submesh.Vector2Lists = new List<List<Vector2>>(submesh.VertexList.Count); for (int j = 0; j < submesh.VertexList.Count; j++) { List<Vector2> vectorList = new List<Vector2>(mesh.NumVector2PerVertex); submesh.Vector2Lists.Add(vectorList); for (byte k = 0; k < mesh.NumVector2PerVertex; k++) { vectorList.Add(reader.ReadVector2()); } } } if (Format >= 2) { submesh.Unknown3 = reader.ReadBytes(100); // 96 + 4 } if (Format >= 7) { submesh.Unknown4 = reader.ReadBytes(284); // 256 + 28 if (Format >= 8) { byte format = reader.ReadByte(); string nullFrame = reader.ReadName(); byte[] u5end = reader.ReadBytes(12 + 4); byte[] encryptedName = Utility.EncryptName(nullFrame); submesh.Unknown5 = new byte[1 + 4 + encryptedName.Length + 12 + 4]; submesh.Unknown5[0] = format; BitConverter.GetBytes(encryptedName.Length).CopyTo(submesh.Unknown5, 1); encryptedName.CopyTo(submesh.Unknown5, 1 + 4); u5end.CopyTo(submesh.Unknown5, 1 + 4 + encryptedName.Length); } } else { if (Format >= 3) { submesh.Unknown4 = reader.ReadBytes(64); } if (Format >= 5) { submesh.Unknown5 = reader.ReadBytes(20); } if (Format >= 6) { submesh.Unknown6 = reader.ReadBytes(28); } } } ushort numVerticesDup = reader.ReadUInt16(); mesh.VertexListDuplicate = new List<xxVertex>(numVerticesDup); mesh.VertexListDuplicateUnknown = reader.ReadBytes(8); // 4 + 4 for (int i = 0; i < numVerticesDup; i++) { mesh.VertexListDuplicate.Add(ParseVertex()); } mesh.BoneList = ParseBoneList(); } for (int i = 0; i < numChildFrames; i++) { frame.AddChild(ParseFrame()); } return frame; }
public bool RemoveSVI(xxFrame meshFrame, int submeshIdx) { sviParser svi = FindSVI(meshFrame.Name, submeshIdx); if (svi == null) { return false; } return Parser.sections.Remove(svi); }
private void ExportFrame(XmlElement parentNode, xxFrame frame, List<xxFrame> meshFrames, List<XmlElement> meshNodes) { string frameName = frame.Name; if ((frameNames == null) || frameNames.Contains(frameName)) { string frameNameCleaned = EncodeName(frameName); XmlElement node = doc.CreateElement("node", uri); node.SetAttribute("id", frameNameCleaned); node.SetAttribute("name", frameNameCleaned); parentNode.AppendChild(node); Vector3 scalePart; Quaternion rotatePart; Vector3 translatePart; if (!frame.Matrix.Decompose(out scalePart, out rotatePart, out translatePart)) { throw new Exception("Failed to decompose matrix"); } Vector3 rotateVector = FbxUtility.QuaternionToEuler(rotatePart); XmlElement translate = doc.CreateElement("translate", uri); translate.SetAttribute("sid", "translate"); translate.InnerText = translatePart.X.ToFloatString() + " " + translatePart.Y.ToFloatString() + " " + translatePart.Z.ToFloatString(); node.AppendChild(translate); XmlElement rotateZ = doc.CreateElement("rotate", uri); rotateZ.SetAttribute("sid", "rotateZ"); rotateZ.InnerText = "0 0 1 " + rotateVector.X.ToFloatString(); node.AppendChild(rotateZ); XmlElement rotateY = doc.CreateElement("rotate", uri); rotateY.SetAttribute("sid", "rotateY"); rotateY.InnerText = "0 1 0 " + rotateVector.Y.ToFloatString(); node.AppendChild(rotateY); XmlElement rotateX = doc.CreateElement("rotate", uri); rotateX.SetAttribute("sid", "rotateX"); rotateX.InnerText = "1 0 0 " + rotateVector.Z.ToFloatString(); node.AppendChild(rotateX); XmlElement scale = doc.CreateElement("scale", uri); scale.SetAttribute("sid", "scale"); scale.InnerText = scalePart.X.ToFloatString() + " " + scalePart.Y.ToFloatString() + " " + scalePart.Z.ToFloatString(); node.AppendChild(scale); if (meshNames.Contains(frameName) && (frame.Mesh != null)) { meshFrames.Add(frame); meshNodes.Add(node); } for (int i = 0; i < frame.Count; i++) { ExportFrame(node, frame[i], meshFrames, meshNodes); } } }
private void ExportFrame(xxFrame frame, HashSet<string> meshNames, int indent) { frameNames.Add(frame.Name); Matrix matrix = RHToLHMatrix(frame.Matrix); string spaces = GetStringSpaces(indent); writer.WriteLine(spaces + "Frame " + frame.Name + " {"); writer.WriteLine(spaces + " FrameTransformMatrix {"); writer.WriteLine(spaces + " " + GetStringMatrix(matrix) + ";;"); writer.WriteLine(spaces + " }"); writer.WriteLine(); if (meshNames.Contains(frame.Name)) { ExportMesh(frame, indent + 1); } for (int i = 0; i < frame.Count; i++) { ExportFrame(frame[i], meshNames, indent + 1); } writer.WriteLine(GetStringSpaces(indent) + "}"); writer.WriteLine(); }
private static void CalculateNormals(xaParser parser, xxFrame meshFrame, xaMorphKeyframe keyframe, xaMorphIndexSet set, float threshold) { xxMesh mesh = meshFrame.Mesh; ushort[] meshIndices = set.MeshIndices; ushort[] morphIndices = set.MorphIndices; int morphSubmeshIdx = MorphMeshObjIdx(meshIndices, mesh); if (morphSubmeshIdx < 0) { throw new Exception("no valid mesh object was found for the morph " + set.Name); } xxSubmesh submesh = mesh.SubmeshList[morphSubmeshIdx]; List<xxVertex> morphedVertices = new List<xxVertex>(submesh.VertexList.Count); for (ushort i = 0; i < submesh.VertexList.Count; i++) { xxVertex vert = new xxVertexUShort(); vert.Index = i; vert.Position = submesh.VertexList[i].Position; vert.Normal = submesh.VertexList[i].Normal; morphedVertices.Add(vert); } for (int i = 0; i < meshIndices.Length; i++) { morphedVertices[meshIndices[i]].Position = keyframe.PositionList[morphIndices[i]]; } var pairList = new List<Tuple<List<xxFace>, List<xxVertex>>>(1); pairList.Add(new Tuple<List<xxFace>, List<xxVertex>>(submesh.FaceList, morphedVertices)); xx.CalculateNormals(pairList, threshold); for (int i = 0; i < meshIndices.Length; i++) { keyframe.NormalList[morphIndices[i]] = morphedVertices[meshIndices[i]].Normal; } }
private void buttonRemoveSVIs_Click(object sender, EventArgs e) { List <DockContent> formXXList; if (!Gui.Docking.DockContents.TryGetValue(typeof(FormXX), out formXXList)) { return; } List <DockContent> formPPList; if (!Gui.Docking.DockContents.TryGetValue(typeof(FormPP), out formPPList)) { return; } groupBoxAA2SVIEXJuggler.Enabled = false; try { List <string> srcParserVarList = new List <string>(); List <string> srcEditorVarList = new List <string>(); foreach (FormPP form in formPPList) { foreach (ListViewItem item in form.otherSubfilesList.SelectedItems) { if (item.Text.ToLower().EndsWith(".sviex")) { string srcParserVar = Gui.Scripting.GetNextVariable("sviexParser"); srcParserVarList.Add(srcParserVar); string parserCommand = srcParserVar + " = OpenSVIEX(parser=" + form.ParserVar + ", name=\"" + item.Text + "\")"; Gui.Scripting.RunScript(parserCommand); string srcEditorVar = Gui.Scripting.GetNextVariable("sviexEditor"); srcEditorVarList.Add(srcEditorVar); Gui.Scripting.RunScript(srcEditorVar + " = sviexEditor(parser=" + srcParserVar + ")"); } } } foreach (string srcEditorVar in srcEditorVarList) { sviexEditor editor = (sviexEditor)Gui.Scripting.Variables[srcEditorVar]; bool sviexChanged = false; foreach (FormXX xxForm in formXXList) { foreach (ListViewItem item in xxForm.listViewMesh.SelectedItems) { xxFrame meshFrame = xxForm.Editor.Meshes[(int)item.Tag]; if (xxForm.textBoxMeshName.Text == meshFrame.Name) { for (int i = 0; i < xxForm.dataGridViewMesh.SelectedRows.Count; i++) { int submeshIdx = xxForm.dataGridViewMesh.SelectedRows[i].Index; bool removed = (bool)Gui.Scripting.RunScript(srcEditorVar + ".RemoveSVI(meshFrame=" + xxForm.EditorVar + ".Meshes[" + (int)item.Tag + "], submeshIdx=" + submeshIdx + ")"); if (removed) { sviexChanged = true; } } break; } } if (sviexChanged) { break; } DragSource?frame = null; if (xxForm.treeViewObjectTree.SelectedNode != null) { frame = xxForm.treeViewObjectTree.SelectedNode.Tag as DragSource?; if (frame != null && frame.Value.Type == typeof(xxFrame)) { bool removed = (bool)Gui.Scripting.RunScript(srcEditorVar + ".RemoveSVI(meshFrame=" + xxForm.EditorVar + ".Frames[" + (int)frame.Value.Id + "])"); if (removed) { sviexChanged = true; break; } } } } if (sviexChanged) { foreach (FormPP form in formPPList) { foreach (ListViewItem item in form.otherSubfilesList.SelectedItems) { if (item.Text == editor.Parser.Name) { int index = srcEditorVarList.IndexOf(srcEditorVar); string srcParserVar = srcParserVarList[index]; Gui.Scripting.RunScript(form.EditorVar + ".ReplaceSubfile(file=" + srcParserVar + ")"); form.Changed = true; item.Font = new Font(form.otherSubfilesList.Font, FontStyle.Bold); break; } } } } } foreach (string parserVar in srcParserVarList) { Gui.Scripting.RunScript(parserVar + "=null"); } foreach (string editorVar in srcEditorVarList) { Gui.Scripting.RunScript(editorVar + "=null"); } } catch (Exception ex) { Utility.ReportException(ex); } groupBoxAA2SVIEXJuggler.Enabled = true; }
public void CalculateNormals(xxFrame meshFrame, string morphClip, string keyframe, double threshold) { xa.CalculateNormals(Parser, meshFrame, morphClip, keyframe, (float)threshold); Changed = true; }
void AddFrame(xxFrame newFrame, int destParentId) { if (destParentId < 0) { Parser.Frame = newFrame; } else { Frames[destParentId].AddChild(newFrame); } Frames.Clear(); Meshes.Clear(); InitFrames(Parser.Frame); }
int makeNode(xxFrame f) { nodes ??= new List <Node>(); var index = nodes.Count; var node = new Node() { extras = new { flags = f.Unknown1, flags2 = f.Unknown2, }, name = f.Name, matrix = f.Matrix != Matrix.Identity ? f.Matrix.ColumnMajor() : null, }; nodes.Add(node); var mesh = NewMesh(out int meshidx); node.mesh = meshidx; mesh.name = f.Name; // Frame only, so that is all if (f.Mesh == null) { return(index); } // It's a skin, so record all bones we find if (f.Mesh.BoneList.Count > 0) { skins ??= new List <Skin>(); var skin = new Skin() { name = f.Name2, joints = new List <int>(), inverseBindMatrices = ibmView.NewAccessor("VEC4", ComponentType.FLOAT, f.Mesh.BoneList.Count), //skeleton = skeleton, }; foreach (var joint in f.Mesh.BoneList) { /* * int found = findNode(skeleton, joint.Name); * if (found == -1) * { * throw new Exception("broken skeleton at " + joint.Name); * } * skin.joints.Add(found); */ bone2skin.Add(joint.Name, skins.Count); ibmOutput.Write(joint.Matrix); ibmBufer.buf.byteLength += 64; ibmView.view.byteLength += 64; } node.skin = skins.Count; skins.Add(skin); } // No submeshes? if (f.Mesh.SubmeshList.Count == 0) { return(index); } // Construct a primitive template shared by all primitives (submeshes) under this mesh. var vertexLen = 0; var acc = new int[5]; var attrs = new Dictionary <string, int>() { ["POSITION"] = acc[0] = vertexView.NewAccessor("VEC3", ComponentType.FLOAT, ref vertexLen), ["WEIGHTS_0"] = acc[1] = vertexView.NewAccessor("VEC3", ComponentType.FLOAT, ref vertexLen), ["JOINTS_0"] = acc[2] = vertexView.NewAccessor("VEC4", ComponentType.BYTE, ref vertexLen), ["NORMAL"] = acc[3] = vertexView.NewAccessor("VEC3", ComponentType.FLOAT, ref vertexLen), ["TEXCOORD_0"] = acc[4] = vertexView.NewAccessor("VEC2", ComponentType.FLOAT, ref vertexLen), }; accessors[acc[0]].min = f.Bounds.Min.ToArray(); accessors[acc[0]].max = f.Bounds.Max.ToArray(); vertexView.view.byteStride = vertexLen; // Instantiate submeshes as primitives foreach (var sub in f.Mesh.SubmeshList) { // The indices always get a new accessor var iAcc = indicesView.NewAccessor("SCALAR", ComponentType.UNSIGNED_SHORT, sub.FaceList.Count); // Build the primitive mesh.primitives ??= new List <Primitive>(); mesh.primitives.Add(new Primitive() { extras = new { flags = sub.Unknown1, flags2 = sub.Unknown2, flags3 = sub.Unknown3, flags4 = sub.Unknown4, flags5 = sub.Unknown5, flags6 = sub.Unknown6, }, material = getMaterial(sub.MaterialIndex), indices = iAcc, attributes = attrs, }); // Save indices foreach (var ind in sub.FaceList) { indicesOutput.Write(ind.VertexIndices); accessors[iAcc].count += 3; indicesBuffer.buf.byteLength += 6; indicesView.view.byteLength += 6; } // Update vertex attribute accessors with vertex count for (var i = 0; i < acc.Length; i++) { accessors[acc[i]].count += sub.VertexList.Count; } // And dump vertices foreach (var vert in sub.VertexList) { vertexOutput.Write(vert.Position); vertexOutput.Write(vert.Weights3); vertexOutput.Write(vert.BoneIndices); vertexOutput.Write(vert.Normal); vertexOutput.Write(vert.UV); vertexBuffer.buf.byteLength += vertexLen; vertexView.view.byteLength += vertexLen; } } return(index); }
void InitFrames(xxFrame frame) { Frames.Add(frame); if (frame.Mesh != null) { Meshes.Add(frame); } for (int i = 0; i < frame.Count; i++) { InitFrames(frame[i]); } }
public static void Export(string dest, xxParser parser, xxFrame[] meshParents, List<xaParser> xaSubfiles, int ticksPerSecond, int keyframeLength) { HashSet<string> meshNames = new HashSet<string>(); foreach (xxFrame frame in meshParents) { meshNames.Add(frame.Name); } Exporter exporter = new Exporter(dest, parser, meshNames, xaSubfiles, ticksPerSecond, keyframeLength); }
void MergeFrame(xxFrame newFrame, int destParentId) { xxFrame srcParent = new xxFrame(); srcParent.InitChildren(1); srcParent.AddChild(newFrame); xxFrame destParent; if (destParentId < 0) { destParent = new xxFrame(); destParent.InitChildren(1); destParent.AddChild(Parser.Frame); } else { destParent = Frames[destParentId]; } MergeFrame(srcParent, destParent); if (destParentId < 0) { Parser.Frame = srcParent[0]; srcParent.RemoveChild(0); } Frames.Clear(); Meshes.Clear(); InitFrames(Parser.Frame); }
private void ExportMesh(xxFrame meshParent, int indent) { string meshSpaces = GetStringSpaces(indent); xxMesh mesh = meshParent.Mesh; List<xxBone> boneList = mesh.BoneList; bool skinned = (boneList.Count > 0); for (int i = 0; i < mesh.SubmeshList.Count; i++) { List<VertexBoneAssignment>[] boneAssignments = null; if (skinned) { boneAssignments = new List<VertexBoneAssignment>[256]; for (int j = 0; j < boneAssignments.Length; j++) { boneAssignments[j] = new List<VertexBoneAssignment>(); } } writer.WriteLine(meshSpaces + "Mesh " + meshParent.Name + i + " {"); xxSubmesh submesh = mesh.SubmeshList[i]; List<xxFace> faceList = submesh.FaceList; List<xxVertex> vertexList = submesh.VertexList; writer.WriteLine(meshSpaces + " " + vertexList.Count + ";"); for (int j = 0; j < vertexList.Count; j++) { string s = meshSpaces + " "; xxVertex vertex = vertexList[j]; Vector3 coords = vertex.Position; coords[2] = -coords[2]; for (int k = 0; k < 3; k++) { s += coords[k].ToFloat6String() + ";"; } if (j < (vertexList.Count - 1)) { s += ","; } else { s += ";"; } writer.WriteLine(s); if (skinned) { float[] weights4 = vertex.Weights4(skinned); byte[] boneIndices = vertex.BoneIndices; for (int k = 0; k < boneIndices.Length; k++) { if ((boneIndices[k] != 0xFF) && (weights4[k] > 0)) { VertexBoneAssignment vba = new VertexBoneAssignment(); vba.vertexIndex = j; vba.weight = weights4[k]; boneAssignments[boneIndices[k]].Add(vba); } } } } writer.WriteLine(meshSpaces + " " + faceList.Count + ";"); for (int j = 0; j < faceList.Count; j++) { string s = meshSpaces + " 3;"; xxFace face = faceList[j]; s += face.VertexIndices[0].ToString() + ","; s += face.VertexIndices[2].ToString() + ","; s += face.VertexIndices[1].ToString() + ";"; if (j < (faceList.Count - 1)) { s += ","; } else { s += ";"; } writer.WriteLine(s); } writer.WriteLine(); writer.WriteLine(meshSpaces + " MeshNormals {"); writer.WriteLine(meshSpaces + " " + vertexList.Count + ";"); for (int j = 0; j < vertexList.Count; j++) { xxVertex vertex = vertexList[j]; Vector3 normal = vertex.Normal; writer.Write(meshSpaces + " " + normal[0].ToFloat6String() + ";" + normal[1].ToFloat6String() + ";" + (-normal[2]).ToFloat6String() + ";"); if (j < (vertexList.Count - 1)) { writer.WriteLine(","); } else { writer.WriteLine(";"); } } writer.WriteLine(meshSpaces + " " + faceList.Count + ";"); for (int j = 0; j < faceList.Count; j++) { xxFace face = faceList[j]; writer.Write(meshSpaces + " 3;" + face.VertexIndices[0] + "," + face.VertexIndices[2] + "," + face.VertexIndices[1] + ";"); if (j < (faceList.Count - 1)) { writer.WriteLine(","); } else { writer.WriteLine(";"); } } writer.WriteLine(meshSpaces + " }"); writer.WriteLine(); writer.WriteLine(meshSpaces + " MeshTextureCoords {"); writer.WriteLine(meshSpaces + " " + vertexList.Count + ";"); for (int j = 0; j < vertexList.Count; j++) { xxVertex vertex = vertexList[j]; float[] uv = vertex.UV; writer.Write(meshSpaces + " " + uv[0].ToFloat6String() + ";" + uv[1].ToFloat6String() + ";"); if (j < (vertexList.Count - 1)) { writer.WriteLine(","); } else { writer.WriteLine(";"); } } writer.WriteLine(meshSpaces + " }"); writer.WriteLine(); int materialIdx = submesh.MaterialIndex; if ((materialIdx >= 0) && (materialIdx < xxParser.MaterialList.Count)) { writer.WriteLine(meshSpaces + " MeshMaterialList {"); writer.WriteLine(meshSpaces + " 1;"); writer.WriteLine(meshSpaces + " " + faceList.Count + ";"); for (int j = 0; j < faceList.Count; j++) { writer.Write(meshSpaces + " 0"); if (j < (faceList.Count - 1)) { writer.WriteLine(","); } else { writer.WriteLine(";"); } } xxMaterial mat = xxParser.MaterialList[materialIdx]; Color4 ambient = mat.Ambient; Color4 specular = mat.Specular; Color4 emissive = mat.Emissive; writer.WriteLine(); writer.WriteLine(meshSpaces + " Material " + mat.Name + " {"); writer.WriteLine(meshSpaces + " " + ambient.Red.ToFloat6String() + ";" + ambient.Green.ToFloat6String() + ";" + ambient.Blue.ToFloat6String() + ";" + ambient.Alpha.ToFloat6String() + ";;"); writer.WriteLine(meshSpaces + " " + mat.Power.ToFloat6String() + ";"); writer.WriteLine(meshSpaces + " " + specular.Red.ToFloat6String() + ";" + specular.Green.ToFloat6String() + ";" + specular.Blue.ToFloat6String() + ";;"); writer.WriteLine(meshSpaces + " " + emissive.Red.ToFloat6String() + ";" + emissive.Green.ToFloat6String() + ";" + emissive.Blue.ToFloat6String() + ";;"); xxMaterialTexture matTex = mat.Textures[0]; if (matTex.Name != String.Empty) { writer.WriteLine(); writer.WriteLine(meshSpaces + " TextureFilename {"); writer.WriteLine(meshSpaces + " \"" + matTex.Name + "\";"); writer.WriteLine(meshSpaces + " }"); for (int j = 0; j < xxParser.TextureList.Count; j++) { xxTexture tex = xxParser.TextureList[j]; if (matTex.Name == tex.Name) { if (!usedTextures.ContainsKey(tex.Name)) { usedTextures.Add(tex.Name, tex); } break; } } } writer.WriteLine(meshSpaces + " }"); writer.WriteLine(meshSpaces + " }" + Environment.NewLine); } else { Report.ReportLog("Warning: mesh " + meshParent.Name + " object " + i + " uses non-existant material index " + materialIdx); } if (skinned) { int numUsedBones = 0; for (int j = 0; j < boneList.Count; j++) { xxBone bone = boneList[j]; if (boneAssignments[bone.Index].Count > 0) { numUsedBones++; } } writer.WriteLine(meshSpaces + " XSkinMeshHeader {"); writer.WriteLine(meshSpaces + " 4;"); writer.WriteLine(meshSpaces + " 12;"); writer.WriteLine(meshSpaces + " " + numUsedBones + ";"); writer.WriteLine(meshSpaces + " }"); writer.WriteLine(); for (int j = 0; j < boneList.Count; j++) { xxBone bone = boneList[j]; List<VertexBoneAssignment> boneAssignmentList = boneAssignments[bone.Index]; if (boneAssignmentList.Count <= 0) { continue; } writer.WriteLine(meshSpaces + " SkinWeights {"); writer.WriteLine(meshSpaces + " \"" + bone.Name + "\";"); writer.WriteLine(meshSpaces + " " + boneAssignmentList.Count + ";"); string vertexString = String.Empty; string weightString = String.Empty; for (int k = 0; k < boneAssignmentList.Count; k++) { vertexString += meshSpaces + " " + boneAssignmentList[k].vertexIndex; weightString += meshSpaces + " " + boneAssignmentList[k].weight.ToFloat6String(); if (k < (boneAssignmentList.Count - 1)) { vertexString += "," + Environment.NewLine; weightString += "," + Environment.NewLine; } else { vertexString += ";" + Environment.NewLine; weightString += ";" + Environment.NewLine; } } Matrix matrix = RHToLHMatrix(bone.Matrix); writer.Write(vertexString); writer.Write(weightString); writer.WriteLine(meshSpaces + " " + GetStringMatrix(matrix) + ";;"); writer.WriteLine(meshSpaces + " }"); writer.WriteLine(); } } writer.WriteLine(meshSpaces + "}"); writer.WriteLine(); } }
void MergeFrame(xxFrame srcParent, xxFrame destParent) { for (int i = 0; i < destParent.Count; i++) { var dest = destParent[i]; for (int j = 0; j < srcParent.Count; j++) { var src = srcParent[j]; if (src.Name == dest.Name) { MergeFrame(src, dest); srcParent.RemoveChild(j); destParent.RemoveChild(i); destParent.InsertChild(i, src); break; } } } if (srcParent.Name == destParent.Name) { while (destParent.Count > 0) { var dest = destParent[0]; destParent.RemoveChild(0); srcParent.AddChild(dest); } } else { while (srcParent.Count > 0) { var src = srcParent[0]; srcParent.RemoveChild(0); destParent.AddChild(src); } } }
public void SetTweenFactor(xxFrame meshFrame, xaMorphIndexSet idxSet, float tweenFactor) { foreach (AnimationFrame frame in meshFrames) { if (frame.Name == meshFrame.Name) { xxMesh xxMesh = meshFrame.Mesh; int meshObjIdx = xa.MorphMeshObjIdx(idxSet.MeshIndices, xxMesh); if (meshObjIdx < 0) { Report.ReportLog("no valid mesh object was found for the morph"); return; } MeshContainer animMesh = frame.MeshContainer; for (int i = 1; i < meshObjIdx; i++) { animMesh = animMesh.NextMeshContainer; if (animMesh == null) break; } if (animMesh == null) { Report.ReportLog("Bad submesh specified."); return; } MorphMeshContainer morphMesh = (MorphMeshContainer)animMesh; morphMesh.TweenFactor = tweenFactor; return; } } Report.ReportLog("Mesh frame " + meshFrame + " not displayed."); return; }
void MeshMatOffset(xxFrame frame, int offset) { if (frame.Mesh != null) { var submeshes = frame.Mesh.SubmeshList; for (int i = 0; i < submeshes.Count; i++) { submeshes[i].MaterialIndex += offset; } } for (int i = 0; i < frame.Count; i++) { MeshMatOffset(frame[i], offset); } }
private AnimationFrame CreateFrame(xxFrame frame, xxParser parser, HashSet<string> extractFrames, HashSet<string> meshNames, Device device, Matrix combinedParent, List<AnimationFrame> meshFrames) { AnimationFrame animationFrame = new AnimationFrame(); animationFrame.Name = frame.Name; animationFrame.TransformationMatrix = frame.Matrix; animationFrame.OriginalTransform = animationFrame.TransformationMatrix; animationFrame.CombinedTransform = combinedParent * animationFrame.TransformationMatrix; xxMesh mesh = frame.Mesh; if (meshNames.Contains(frame.Name) && (mesh != null)) { List<xxBone> boneList = mesh.BoneList; string[] boneNames = new string[boneList.Count]; Matrix[] boneOffsets = new Matrix[boneList.Count]; for (int i = 0; i < boneList.Count; i++) { xxBone bone = boneList[i]; boneNames[i] = bone.Name; boneOffsets[i] = bone.Matrix; } AnimationMeshContainer[] meshContainers = new AnimationMeshContainer[mesh.SubmeshList.Count]; Vector3 min = new Vector3(Single.MaxValue); Vector3 max = new Vector3(Single.MinValue); for (int i = 0; i < mesh.SubmeshList.Count; i++) { xxSubmesh submesh = mesh.SubmeshList[i]; List<xxFace> faceList = submesh.FaceList; List<xxVertex> vertexList = submesh.VertexList; Mesh animationMesh = new Mesh(device, faceList.Count, vertexList.Count, MeshFlags.Managed, PositionBlendWeightsIndexedNormalTexturedColoured.Format); using (DataStream indexStream = animationMesh.LockIndexBuffer(LockFlags.None)) { for (int j = 0; j < faceList.Count; j++) { ushort[] indices = faceList[j].VertexIndices; indexStream.Write(indices[0]); indexStream.Write(indices[2]); indexStream.Write(indices[1]); } animationMesh.UnlockIndexBuffer(); } FillVertexBuffer(animationMesh, vertexList, -1); var normalLines = new PositionBlendWeightsIndexedColored[vertexList.Count * 2]; for (int j = 0; j < vertexList.Count; j++) { xxVertex vertex = vertexList[j]; normalLines[j * 2] = new PositionBlendWeightsIndexedColored(vertex.Position, vertex.Weights3, vertex.BoneIndices, Color.Yellow.ToArgb()); normalLines[(j * 2) + 1] = new PositionBlendWeightsIndexedColored(vertex.Position + (vertex.Normal / 16), vertex.Weights3, vertex.BoneIndices, Color.Yellow.ToArgb()); min = Vector3.Minimize(min, vertex.Position); max = Vector3.Maximize(max, vertex.Position); } AnimationMeshContainer meshContainer = new AnimationMeshContainer(); meshContainer.Name = animationFrame.Name; meshContainer.MeshData = new MeshData(animationMesh); meshContainer.NormalLines = normalLines; meshContainers[i] = meshContainer; int matIdx = submesh.MaterialIndex; if ((matIdx >= 0) && (matIdx < parser.MaterialList.Count)) { int texIdx; if (!MatTexIndices.TryGetValue(matIdx, out texIdx)) { texIdx = -1; xxMaterial mat = parser.MaterialList[matIdx]; Material materialD3D = new Material(); materialD3D.Ambient = mat.Ambient; materialD3D.Diffuse = mat.Diffuse; materialD3D.Emissive = mat.Emissive; materialD3D.Specular = mat.Specular; materialD3D.Power = mat.Power; Materials[matIdx] = materialD3D; xxMaterialTexture matTex = mat.Textures[0]; string matTexName = matTex.Name; if (matTexName != String.Empty) { for (int j = 0; j < parser.TextureList.Count; j++) { xxTexture tex = parser.TextureList[j]; if (tex.Name == matTexName) { texIdx = j; if (Textures[j] == null) { ImportedTexture importedTex = xx.ImportedTexture(tex); Textures[j] = Texture.FromMemory(device, importedTex.Data); } break; } } } MatTexIndices.Add(matIdx, texIdx); } meshContainer.MaterialIndex = matIdx; meshContainer.TextureIndex = texIdx; } } for (int i = 0; i < (meshContainers.Length - 1); i++) { meshContainers[i].NextMeshContainer = meshContainers[i + 1]; } for (int i = 0; i < meshContainers.Length; i++) { meshContainers[i].BoneNames = boneNames; meshContainers[i].BoneOffsets = boneOffsets; } min = Vector3.TransformCoordinate(min, animationFrame.CombinedTransform); max = Vector3.TransformCoordinate(max, animationFrame.CombinedTransform); animationFrame.Bounds = new BoundingBox(min, max); animationFrame.MeshContainer = meshContainers[0]; meshFrames.Add(animationFrame); } for (int i = 0; i < frame.Count; i++) { xxFrame child = frame[i]; if (extractFrames.Contains(child.Name)) { AnimationFrame childAnimationFrame = CreateFrame(child, parser, extractFrames, meshNames, device, animationFrame.CombinedTransform, meshFrames); childAnimationFrame.Parent = animationFrame; animationFrame.AppendChild(childAnimationFrame); } } numFrames++; return animationFrame; }
void ReplaceFrame(xxFrame newFrame, int destParentId) { if (destParentId < 0) { Parser.Frame = newFrame; } else { var destParent = Frames[destParentId]; bool found = false; for (int i = 0; i < destParent.Count; i++) { var dest = destParent[i]; if (dest.Name == newFrame.Name) { destParent.RemoveChild(i); destParent.InsertChild(i, newFrame); found = true; break; } } if (!found) { destParent.AddChild(newFrame); } } Frames.Clear(); Meshes.Clear(); InitFrames(Parser.Frame); }