static void Collect(AnimatorEditor editor, HashSet <Component> collection) { foreach (Transform frame in editor.Frames) { GameObject gameObj = frame.m_GameObject.instance; collection.Add(gameObj); foreach (var comp in gameObj.m_Component) { collection.Add(comp.Value.asset); if (comp.Value.asset.classID1 == UnityClassID.Animator) { Animator anim = (Animator)comp.Value.asset; if (anim.m_Avatar.instance != null) { collection.Add(anim.m_Avatar.instance); } } } if (frame.UnknownChilds != null) { foreach (PPtr <Transform> childPtr in frame.UnknownChilds) { if (!(childPtr.asset is Transform)) { collection.Add(childPtr.asset); MultiLink mLink = childPtr.asset as MultiLink; if (mLink != null) { foreach (PPtr <Object> objPtr in mLink.Links) { collection.Add(objPtr.asset); } } } } } } foreach (MeshRenderer meshR in editor.Meshes) { collection.Add(Operations.GetMesh(meshR)); foreach (PPtr <Material> matPtr in meshR.m_Materials) { if (matPtr.instance != null) { Material mat = matPtr.instance; collection.Add(mat); Collect(mat.m_Shader.instance, collection); foreach (var texPair in mat.m_SavedProperties.m_TexEnvs) { collection.Add(texPair.Value.m_Texture.asset); } } } } }
private void Export(string dest, Material mat, Texture2D tex) { DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(dest)); if (!dir.Exists) { dir.Create(); } usedTextures = new List <Texture2D>(1); using (StreamWriter writer = new StreamWriter(dest, false)) { writer.WriteLine("Metasequoia Document"); writer.WriteLine("Format Text Ver 1.0"); writer.WriteLine(); if (mat != null) { writer.WriteLine("Material 1 {"); string s = "\t\"" + mat.m_Name + "\" vcol(1) col(0.800 0.800 0.800 1.000) dif(0.500) amb(0.100) emi(0.500) spc(0.100) power(30.00)"; if (tex != null) { string extension = tex.m_TextureFormat == TextureFormat.DXT1 || tex.m_TextureFormat == TextureFormat.DXT5 ? ".dds" : ".tga"; s += " tex(\"" + tex.m_Name + extension + "\")"; usedTextures.Add(tex); } writer.WriteLine(s); writer.WriteLine("}"); } Mesh mesh = Operations.GetMesh(morphObj); Random rand = new Random(); int vertListIdx = 0; for (int i = 0; i < mesh.m_Shapes.shapes.Count; i++) { float[] color = new float[3]; for (int k = 0; k < color.Length; k++) { color[k] = (float)((rand.NextDouble() / 2) + 0.5); } writer.WriteLine("Object \"" + mesh.m_Shapes.channels[i].name + "\" {"); writer.WriteLine("\tvisible 0"); writer.WriteLine("\tshading 1"); writer.WriteLine("\tcolor " + color[0].ToFloatString() + " " + color[1].ToFloatString() + " " + color[2].ToFloatString()); writer.WriteLine("\tcolor_type 1"); SB3Utility.Mqo.ExporterCommon.WriteMeshObject(writer, vertLists[vertListIdx++], faceList, mat != null ? 0 : -1, colorLists[i]); writer.WriteLine("}"); } writer.WriteLine("Eof"); } }
private void Export(DirectoryInfo dir) { try { List <MeshRenderer> meshList = new List <MeshRenderer>(1); meshList.Add(morphObj); Mesh mesh = Operations.GetMesh(morphObj); colorLists = new bool[mesh.m_Shapes.shapes.Count][]; vertLists = new List <ImportedVertex> [mesh.m_Shapes.shapes.Count]; for (int i = 0; i < mesh.m_Shapes.shapes.Count; i++) { Plugins.UnityConverter conv = new Plugins.UnityConverter(parser, meshList, false, false); ImportedMesh meshObjBase = conv.MeshList[0]; if (faceList == null) { faceList = meshObjBase.SubmeshList[0].FaceList; } List <ImportedVertex> vertList = conv.MeshList[0].SubmeshList[0].VertexList; vertLists[i] = vertList; colorLists[i] = new bool[meshObjBase.SubmeshList[0].VertexList.Count]; int lastVertIndex = (int)(mesh.m_Shapes.shapes[i].firstVertex + mesh.m_Shapes.shapes[i].vertexCount); for (int j = (int)mesh.m_Shapes.shapes[i].firstVertex; j < lastVertIndex; j++) { BlendShapeVertex srcVert = mesh.m_Shapes.vertices[j]; ImportedVertex vert = vertList[(int)srcVert.index]; vert.Position += srcVert.vertex; colorLists[i][(int)srcVert.index] = true; } } string dest = Utility.GetDestFile(dir, morphObj.m_GameObject.instance.m_Name + "-" + mesh.m_Name + "-", ".morph.mqo"); Material mat = morphObj.m_Materials[0].instance; Export(dest, mat, mat.m_SavedProperties.m_TexEnvs[0].Value.m_Texture.instance); foreach (Texture2D tex in usedTextures) { try { tex.Export(dir.FullName); } catch (Exception ex) { Utility.ReportException(ex); } } Report.ReportLog("Finished exporting morph to " + dest); } catch (Exception ex) { Report.ReportLog("Error exporting morph: " + ex.Message); } }
public void CopyTo(MeshCollider dest) { dest.m_Material = new PPtr <Object>(m_Material.asset, dest.file); dest.m_IsTrigger = m_IsTrigger; dest.m_Enabled = m_Enabled; dest.m_SmoothSphereCollisions = m_SmoothSphereCollisions; dest.m_Convex = m_Convex; dest.m_InflateMesh = m_InflateMesh; dest.m_SkinWidth = m_SkinWidth; MeshRenderer meshR = dest.m_GameObject.instance.FindLinkedComponent(typeof(MeshRenderer)); dest.m_Mesh = new PPtr <Mesh>(Operations.GetMesh(meshR)); }
static void Collect(AnimatorEditor editor, HashSet <Component> collection) { foreach (Transform frame in editor.Frames) { GameObject gameObj = frame.m_GameObject.instance; collection.Add(gameObj); foreach (var comp in gameObj.m_Component) { collection.Add(comp.Value.asset); if (comp.Value.asset.classID() == UnityClassID.Animator) { Animator anim = (Animator)comp.Value.asset; if (anim.m_Avatar.instance != null && anim.m_Avatar.instance.classID() == UnityClassID.Avatar) { collection.Add(anim.m_Avatar.instance); } } } } foreach (MeshRenderer meshR in editor.Meshes) { collection.Add(Operations.GetMesh(meshR)); foreach (PPtr <Material> matPtr in meshR.m_Materials) { if (matPtr.instance != null) { Material mat = matPtr.instance; collection.Add(mat); Collect(mat.m_Shader.instance, collection); foreach (var texPair in mat.m_SavedProperties.m_TexEnvs) { collection.Add(texPair.Value.m_Texture.asset); } } } } }
private void UpdateComponent(Component asset, List <Tuple <List <PPtr <Object> >, List <KeyValuePair <string, AssetInfo> > > > containerGroups) { List <Component> assets = new List <Component>(); List <Component> transforms = new List <Component>(); List <Component> containerRelated = new List <Component>(); GetDependantAssets(asset, assets, transforms, containerRelated); if (asset.classID1 == UnityClassID.GameObject) { GameObject gameObj = (GameObject)asset; Animator animator = gameObj.FindLinkedComponent(UnityClassID.Animator); if (animator == null) { foreach (Component trans in transforms) { GetDependantAssets(trans, assets, null, null); } animator = new Animator(null, 0, UnityClassID.Animator, UnityClassID.Animator); animator.m_Avatar = new PPtr <Avatar>((Component)null); animator.m_Controller = new PPtr <RuntimeAnimatorController>((Component)null); GetDependantAssets(animator, assets, transforms, containerRelated); assets.Remove(animator); } } foreach (var group in containerGroups) { var preloadPart = group.Item1; var containerEntries = group.Item2; for (int i = 0; i < containerEntries.Count; i++) { var assetPair = containerEntries[i]; if (assetPair.Value.asset.asset == asset) { preloadPart.Clear(); for (int j = 0; j < assets.Count; j++) { if (assets[j] is ExternalAsset) { ExternalAsset extAsset = (ExternalAsset)assets[j]; PPtr <Object> preloadEntry = new PPtr <Object>((Component)null); preloadEntry.m_FileID = extAsset.FileID; preloadEntry.m_PathID = extAsset.PathID; preloadPart.Add(preloadEntry); } else { preloadPart.Add(new PPtr <Object>(assets[j])); } } string groupName = containerEntries[0].Key; string assetName = AssetCabinet.ToString(asset); if (String.Compare(groupName, assetName, true) != 0) { groupName = assetName.ToLower(); } if (containerEntries.Count > 1) { for (int j = 0; j < containerEntries.Count; j++) { switch (containerEntries[j].Value.asset.asset.classID1) { case UnityClassID.Mesh: case UnityClassID.AnimationClip: containerEntries.RemoveAt(j); j--; break; } } for (int j = containerRelated.Count - 1; j >= 0; j--) { AnimationClip clip = containerRelated[j] as AnimationClip; if (clip != null) { AssetInfo info = new AssetInfo(file); info.asset = new PPtr <Object>(clip); containerEntries.Insert(1, new KeyValuePair <string, AssetInfo>(groupName, info)); } } for (int j = containerRelated.Count - 1; j >= 0; j--) { MeshRenderer meshR = containerRelated[j] as MeshRenderer; if (meshR != null) { Mesh mesh = Operations.GetMesh(meshR); if (mesh != null) { AssetInfo info = new AssetInfo(file); info.asset = new PPtr <Object>(mesh); containerEntries.Insert(1, new KeyValuePair <string, AssetInfo>(groupName, info)); } } } for (int j = 0; j < containerEntries.Count; j++) { containerEntries[j].Value.preloadSize = assets.Count; } } else { containerEntries[0].Value.preloadSize = assets.Count; } for (int j = 0; j < containerEntries.Count; j++) { if (containerEntries[j].Key != groupName) { containerEntries[j] = new KeyValuePair <string, AssetInfo>(groupName, containerEntries[j].Value); } } return; } } } }
private void Export(DirectoryInfo dir) { try { List <MeshRenderer> meshList = new List <MeshRenderer>(1); meshList.Add(morphObj); Mesh mesh = Operations.GetMesh(morphObj); ImageFileFormat preferredUncompressedFormat = (string)Properties.Settings.Default["ExportUncompressedAs"] == "BMP" ? ImageFileFormat.Bmp : (ImageFileFormat)(-1); colorLists = new List <bool[]>(mesh.m_Shapes.shapes.Count); vertLists = new List <List <ImportedVertex> >(mesh.m_Shapes.shapes.Count); HashSet <int> morphIndices = morphList != null && morphList[0] != null ? new HashSet <int>(morphList[0]) : null; for (int i = 0; i < mesh.m_Shapes.channels.Count; i++) { if (morphIndices != null && !morphIndices.Contains(i)) { continue; } for (int frameIdx = 0; frameIdx < mesh.m_Shapes.channels[i].frameCount; frameIdx++) { int shapeIdx = mesh.m_Shapes.channels[i].frameIndex + frameIdx; Operations.UnityConverter conv = new Operations.UnityConverter(parser, meshList, false, null, false, preferredUncompressedFormat, false); ImportedMesh meshObjBase = conv.MeshList[0]; if (faceList == null) { faceList = meshObjBase.SubmeshList[0].FaceList; } List <ImportedVertex> vertList = conv.MeshList[0].SubmeshList[0].VertexList; vertLists.Add(vertList); bool[] colours = new bool[meshObjBase.SubmeshList[0].VertexList.Count]; colorLists.Add(colours); int lastVertIndex = (int)(mesh.m_Shapes.shapes[shapeIdx].firstVertex + mesh.m_Shapes.shapes[shapeIdx].vertexCount); for (int j = (int)mesh.m_Shapes.shapes[shapeIdx].firstVertex; j < lastVertIndex; j++) { BlendShapeVertex srcVert = mesh.m_Shapes.vertices[j]; ImportedVertex vert = vertList[(int)srcVert.index]; vert.Position = new Vector3 ( vert.Position.X - srcVert.vertex.X, vert.Position.Y + srcVert.vertex.Y, vert.Position.Z + srcVert.vertex.Z ); colours[(int)srcVert.index] = true; } } } string dest = Utility.GetDestFile(dir, morphObj.m_GameObject.instance.m_Name + "-" + mesh.m_Name + "-", ".morph.mqo"); Material mat = null; Texture2D matTex = null; if (morphObj.m_Materials.Count > 0) { mat = morphObj.m_Materials[0].instance; if (mat != null && mat.m_SavedProperties.m_TexEnvs.Count > 0) { matTex = mat.m_SavedProperties.m_TexEnvs[0].Value.m_Texture.instance; for (int i = 1; i < mat.m_SavedProperties.m_TexEnvs.Count; i++) { var texProp = mat.m_SavedProperties.m_TexEnvs[i]; if (texProp.Key.name == "_MainTex") { matTex = texProp.Value.m_Texture.instance; break; } } } } Export(dest, mat, matTex); foreach (Texture2D tex in usedTextures) { try { tex.Export(dir.FullName, preferredUncompressedFormat); } catch (Exception ex) { Utility.ReportException(ex); } } Report.ReportLog("Finished exporting morph to " + dest); } catch (Exception ex) { Report.ReportLog("Error exporting morph: " + ex.Message); } }
public FormRendererMeshAttributes(MeshRenderer meshR, int selectedSubmesh) { InitializeComponent(); startSize = new SizeF(Width, Height); this.SaveDesignSizes(); this.AdjustSize((Size)Properties.Settings.Default["DialogRendererMeshAttributesSize"], startSize); Text = meshR.classID1 + " " + meshR.m_GameObject.instance.m_Name + " Attributes"; editTextBoxRendererCastShadows.Text = meshR.m_CastShadows.ToString(); editTextBoxRendererReceiveShadows.Text = meshR.m_ReceiveShadows.ToString(); editTextBoxRendererLightMap.Text = meshR.m_LightmapIndex.ToString(); editTextBoxRendererTilingOffset.Text = "X:" + meshR.m_LightmapTilingOffset.X.ToFloatString() + ", Y:" + meshR.m_LightmapTilingOffset.Y.ToFloatString() + ", Z:" + meshR.m_LightmapTilingOffset.Z.ToFloatString() + ", W:" + meshR.m_LightmapTilingOffset.W.ToFloatString(); checkBoxRendererSubsetIndices.Checked = meshR.m_SubsetIndices != null && meshR.m_SubsetIndices.Length > 0; editTextBox1stNumSubs.Text = meshR.m_StaticBatchInfo != null ? meshR.m_StaticBatchInfo.firstSubMesh + "," + meshR.m_StaticBatchInfo.subMeshCount : String.Empty; editTextBoxRendererStaticBatchRoot.Text = meshR.m_StaticBatchRoot.instance != null ? meshR.m_StaticBatchRoot.instance.m_GameObject.instance.m_Name : String.Empty; if (meshR.file.VersionNumber >= AssetCabinet.VERSION_5_4_1) { editTextBoxRendererMotionVectors.Text = meshR.m_MotionVectors.ToString(); } else { editTextBoxRendererMotionVectors.Enabled = false; } editTextBoxRendererLightProbeUsage.Text = meshR.m_LightProbeUsage.ToString(); editTextBoxRendererLightProbeAnchor.Text = meshR.m_ProbeAnchor.instance != null ? meshR.m_ProbeAnchor.instance.m_GameObject.instance.m_Name : String.Empty; editTextBoxRendererReflectionProbeUsage.Text = meshR.m_ReflectionProbeUsage.ToString(); if (meshR.file.VersionNumber >= AssetCabinet.VERSION_5_4_1) { editTextBoxRendererLightProbeVolumeOverride.Text = meshR.m_LightProbeVolumeOverride.instance != null ? meshR.m_LightProbeVolumeOverride.instance.m_Name : String.Empty; } editTextBoxRendererSortingLayerID.Text = meshR.m_SortingLayerID.ToString(); if (meshR.file.VersionNumber >= AssetCabinet.VERSION_5_6_2) { editTextBoxRendererSortingLayer.Text = meshR.m_SortingLayer.ToString(); } else { editTextBoxRendererSortingLayer.Enabled = false; } editTextBoxRendererSortingOrder.Text = meshR.m_SortingOrder.ToString(); if (meshR is SkinnedMeshRenderer) { SkinnedMeshRenderer smr = (SkinnedMeshRenderer)meshR; editTextBoxSkinnedMeshRendererQuality.Text = smr.m_Quality.ToString(); checkBoxSkinnedMeshRendererUpdateWhenOffScreen.Checked = smr.m_UpdateWhenOffScreen; if (meshR.file.VersionNumber >= AssetCabinet.VERSION_5_4_1) { checkBoxSkinnedMeshRendererSkinnedMotionVectors.Checked = smr.m_SkinnedMotionVectors; } else { checkBoxSkinnedMeshRendererSkinnedMotionVectors.Enabled = false; } editTextBoxSkinnedMeshRendererBones.Text = smr.m_Bones.Count.ToString(); editTextBoxSkinnedMeshRendererBlendShapeWeights.Text = smr.m_BlendShapeWeights.Count.ToString(); editTextBoxSkinnedMeshRendererAABBCenter.Text = "X:" + smr.m_AABB.m_Center.X.ToFloatString() + ", Y:" + smr.m_AABB.m_Center.Y.ToFloatString() + ", Z:" + smr.m_AABB.m_Center.Z.ToFloatString(); editTextBoxSkinnedMeshRendererAABBExtend.Text = "X:" + smr.m_AABB.m_Extend.X.ToFloatString() + ", Y:" + smr.m_AABB.m_Extend.Y.ToFloatString() + ", Z:" + smr.m_AABB.m_Extend.Z.ToFloatString(); checkBoxSkinnedMeshRendererDirtyAABB.Checked = smr.m_DirtyAABB; } else { groupBoxSkinnedMeshRenderer.Enabled = false; } Mesh mesh = Operations.GetMesh(meshR); if (mesh != null) { editTextBoxMeshBlendShape.Text = mesh.m_Shapes.vertices.Count + "/" + mesh.m_Shapes.shapes.Count + "/" + mesh.m_Shapes.fullWeights.Count; editTextBoxMeshBindPose.Text = mesh.m_BindPose.Count.ToString(); editTextBoxMeshBoneHashes.Text = mesh.m_BoneNameHashes.Count.ToString(); checkBoxMeshCompression.Checked = mesh.m_MeshCompression > 0; checkBoxMeshStreamCompression.Checked = mesh.m_StreamCompression > 0; checkBoxMeshReadable.Checked = mesh.m_IsReadable; checkBoxMeshKeepVertices.Checked = mesh.m_KeepVertices; checkBoxMeshKeepIndices.Checked = mesh.m_KeepIndices; editTextBoxMeshInfluences.Text = mesh.m_Skin.Count.ToString(); editTextBoxMeshUsageFlags.Text = mesh.m_MeshUsageFlags.ToString(); editTextBoxMeshCenter.Text = "X:" + mesh.m_LocalAABB.m_Center.X.ToFloatString() + ", Y:" + mesh.m_LocalAABB.m_Center.Y.ToFloatString() + ", Z:" + mesh.m_LocalAABB.m_Center.Z.ToFloatString(); editTextBoxMeshExtend.Text = "X:" + mesh.m_LocalAABB.m_Extend.X.ToFloatString() + ", Y:" + mesh.m_LocalAABB.m_Extend.Y.ToFloatString() + ", Z:" + mesh.m_LocalAABB.m_Extend.Z.ToFloatString(); checkBoxMeshVertexColours.Checked = mesh.HasVertexColours(); checkBoxMeshNormals.Checked = (mesh.m_VertexData.m_CurrentChannels & 2) != 0; checkBoxMeshTangents.Checked = mesh.file.VersionNumber < AssetCabinet.VERSION_5_0_0 ? (mesh.m_VertexData.m_CurrentChannels & 0x20) != 0 : (mesh.m_VertexData.m_CurrentChannels & 0x80) != 0; editTextBoxMeshUVSets.Text = mesh.NumUVSets().ToString(); HashSet <int> unsupportedChannels = new HashSet <int>(); for (int str = 0; str < mesh.m_VertexData.m_Streams.Count; str++) { StreamInfo sInfo = mesh.m_VertexData.m_Streams[str]; if (sInfo.channelMask == 0) { continue; } for (int chn = 0; chn < mesh.m_VertexData.m_Channels.Count; chn++) { ChannelInfo cInfo = mesh.m_VertexData.m_Channels[chn]; if ((sInfo.channelMask & (1 << chn)) == 0) { continue; } if (chn > 7) { unsupportedChannels.Add(chn); } } } if (unsupportedChannels.Count > 0) { foreach (int uChn in unsupportedChannels) { editTextBoxMeshUnsupportedChannels.Text += (editTextBoxMeshUnsupportedChannels.Text.Length != 0 ? ", " : string.Empty) + uChn.ToString() + "(" + mesh.m_VertexData.m_Channels[uChn].dimension * 4 + ")"; } editTextBoxMeshUnsupportedChannels.BackColor = Color.Red; } if (selectedSubmesh >= 0) { editTextBoxSubmeshCenter.Text = "X:" + mesh.m_SubMeshes[selectedSubmesh].localAABB.m_Center.X.ToFloatString() + ", Y:" + mesh.m_SubMeshes[selectedSubmesh].localAABB.m_Center.Y.ToFloatString() + ", Z:" + mesh.m_SubMeshes[selectedSubmesh].localAABB.m_Center.Z.ToFloatString(); editTextBoxSubmeshExtend.Text = "X:" + mesh.m_SubMeshes[selectedSubmesh].localAABB.m_Extend.X.ToFloatString() + ", Y:" + mesh.m_SubMeshes[selectedSubmesh].localAABB.m_Extend.Y.ToFloatString() + ", Z:" + mesh.m_SubMeshes[selectedSubmesh].localAABB.m_Extend.Z.ToFloatString(); } } else { groupBoxMesh.Enabled = false; } editTextBoxRendererCastShadows.AfterEditTextChanged += AttributeChanged; editTextBoxRendererReceiveShadows.AfterEditTextChanged += AttributeChanged; editTextBoxRendererLightMap.AfterEditTextChanged += AttributeChanged; editTextBoxRendererTilingOffset.AfterEditTextChanged += AttributeChanged; checkBoxRendererSubsetIndices.CheckedChanged += AttributeChanged; editTextBoxRendererStaticBatchRoot.AfterEditTextChanged += AttributeChanged; editTextBoxRendererMotionVectors.AfterEditTextChanged += AttributeChanged; editTextBoxRendererLightProbeUsage.AfterEditTextChanged += AttributeChanged; editTextBoxRendererLightProbeAnchor.AfterEditTextChanged += AttributeChanged; editTextBoxRendererReflectionProbeUsage.AfterEditTextChanged += AttributeChanged; editTextBoxRendererLightProbeVolumeOverride.AfterEditTextChanged += AttributeChanged; editTextBoxRendererSortingLayerID.AfterEditTextChanged += AttributeChanged; editTextBoxRendererSortingLayer.AfterEditTextChanged += AttributeChanged; editTextBoxRendererSortingOrder.AfterEditTextChanged += AttributeChanged; editTextBoxSkinnedMeshRendererQuality.AfterEditTextChanged += AttributeChanged; checkBoxSkinnedMeshRendererUpdateWhenOffScreen.CheckedChanged += AttributeChanged; checkBoxSkinnedMeshRendererSkinnedMotionVectors.CheckedChanged += AttributeChanged; editTextBoxSkinnedMeshRendererAABBCenter.AfterEditTextChanged += AttributeChanged; editTextBoxSkinnedMeshRendererAABBExtend.AfterEditTextChanged += AttributeChanged; checkBoxSkinnedMeshRendererDirtyAABB.CheckedChanged += AttributeChanged; checkBoxMeshCompression.CheckedChanged += AttributeChanged; checkBoxMeshStreamCompression.CheckedChanged += AttributeChanged; checkBoxMeshReadable.CheckedChanged += AttributeChanged; checkBoxMeshKeepVertices.CheckedChanged += AttributeChanged; checkBoxMeshKeepIndices.CheckedChanged += AttributeChanged; editTextBoxMeshUsageFlags.AfterEditTextChanged += AttributeChanged; editTextBoxMeshCenter.AfterEditTextChanged += AttributeChanged; editTextBoxMeshExtend.AfterEditTextChanged += AttributeChanged; checkBoxMeshComputeCenterExtend.CheckedChanged += AttributeChanged; checkBoxMeshDestroyCenterExtend.CheckedChanged += AttributeChanged; }
private void ConvertMeshRenderers(List <MeshRenderer> meshList, bool skins, bool morphs) { MeshList = new List <ImportedMesh>(meshList.Count); MaterialList = new List <ImportedMaterial>(meshList.Count); TextureList = new List <ImportedTexture>(meshList.Count); MorphList = new List <ImportedMorph>(meshList.Count); foreach (MeshRenderer meshR in meshList) { Mesh mesh = Operations.GetMesh(meshR); if (mesh == null) { Report.ReportLog("skipping " + meshR.m_GameObject.instance.m_Name + " - no mesh"); continue; } ImportedMesh iMesh = new ImportedMesh(); Transform meshTransform = meshR.m_GameObject.instance.FindLinkedComponent(UnityClassID.Transform); iMesh.Name = meshTransform.GetTransformPath(); iMesh.SubmeshList = new List <ImportedSubmesh>(mesh.m_SubMeshes.Count); using (BinaryReader vertReader = new BinaryReader(new MemoryStream(mesh.m_VertexData.m_DataSize)), indexReader = new BinaryReader(new MemoryStream(mesh.m_IndexBuffer))) { for (int i = 0; i < mesh.m_SubMeshes.Count; i++) { SubMesh submesh = mesh.m_SubMeshes[i]; ImportedSubmesh iSubmesh = new ImportedSubmesh(); iSubmesh.Index = i; iSubmesh.Visible = true; Material mat = meshR.m_Materials[i].instance; ConvertMaterial(mat); iSubmesh.Material = mat.m_Name; iSubmesh.VertexList = new List <ImportedVertex>((int)submesh.vertexCount); for (int str = 0; str < mesh.m_VertexData.m_Streams.Count; str++) { StreamInfo sInfo = mesh.m_VertexData.m_Streams[str]; if (sInfo.channelMask == 0) { continue; } for (int j = 0; j < mesh.m_SubMeshes[i].vertexCount; j++) { ImportedVertex iVertex; if (iSubmesh.VertexList.Count < mesh.m_SubMeshes[i].vertexCount) { iVertex = new ImportedVertex(); iSubmesh.VertexList.Add(iVertex); } else { iVertex = iSubmesh.VertexList[j]; } for (int chn = 0; chn < mesh.m_VertexData.m_Channels.Count; chn++) { ChannelInfo cInfo = mesh.m_VertexData.m_Channels[chn]; if ((sInfo.channelMask & (1 << chn)) == 0) { continue; } vertReader.BaseStream.Position = sInfo.offset + (submesh.firstVertex + j) * sInfo.stride + cInfo.offset; switch (chn) { case 0: iVertex.Position = new SlimDX.Vector3(-vertReader.ReadSingle(), vertReader.ReadSingle(), vertReader.ReadSingle()); break; case 1: iVertex.Normal = new SlimDX.Vector3(-vertReader.ReadSingle(), vertReader.ReadSingle(), vertReader.ReadSingle()); break; case 3: iVertex.UV = new float[2] { vertReader.ReadSingle(), vertReader.ReadSingle() }; break; case 5: iVertex.Tangent = new SlimDX.Vector4(-vertReader.ReadSingle(), vertReader.ReadSingle(), vertReader.ReadSingle(), -vertReader.ReadSingle()); break; } } if (skins && iVertex.BoneIndices == null && mesh.m_Skin.Count > 0) { BoneInfluence inf = mesh.m_Skin[(int)submesh.firstVertex + j]; iVertex.BoneIndices = new byte[inf.boneIndex.Length]; for (int k = 0; k < iVertex.BoneIndices.Length; k++) { iVertex.BoneIndices[k] = (byte)inf.boneIndex[k]; } iVertex.Weights = (float[])inf.weight.Clone(); } } } int numFaces = (int)(submesh.indexCount / 3); iSubmesh.FaceList = new List <ImportedFace>(numFaces); indexReader.BaseStream.Position = submesh.firstByte; for (int j = 0; j < numFaces; j++) { ImportedFace face = new ImportedFace(); face.VertexIndices = new int[3]; face.VertexIndices[0] = indexReader.ReadUInt16() - (int)submesh.firstVertex; face.VertexIndices[2] = indexReader.ReadUInt16() - (int)submesh.firstVertex; face.VertexIndices[1] = indexReader.ReadUInt16() - (int)submesh.firstVertex; iSubmesh.FaceList.Add(face); } iMesh.SubmeshList.Add(iSubmesh); } } if (skins && meshR is SkinnedMeshRenderer) { SkinnedMeshRenderer sMesh = (SkinnedMeshRenderer)meshR; if (sMesh.m_Bones.Count >= 256) { throw new Exception("Too many bones (" + mesh.m_BindPose.Count + ")"); } if (sMesh.m_Bones.Count != mesh.m_BindPose.Count || sMesh.m_Bones.Count != mesh.m_BoneNameHashes.Count) { throw new Exception("Mismatching number of bones bind pose=" + mesh.m_BindPose.Count + " hashes=" + mesh.m_BoneNameHashes.Count + " numBones=" + sMesh.m_Bones.Count); } iMesh.BoneList = new List <ImportedBone>(sMesh.m_Bones.Count); for (int i = 0; i < sMesh.m_Bones.Count; i++) { ImportedBone bone = new ImportedBone(); uint boneHash = mesh.m_BoneNameHashes[i]; bone.Name = avatar.FindBoneName(boneHash); Matrix m = Matrix.Transpose(mesh.m_BindPose[i]); Vector3 s, t; Quaternion q; m.Decompose(out s, out q, out t); t.X *= -1; Vector3 euler = FbxUtility.QuaternionToEuler(q); euler.Y *= -1; euler.Z *= -1; q = FbxUtility.EulerToQuaternion(euler); bone.Matrix = Matrix.Scaling(s) * Matrix.RotationQuaternion(q) * Matrix.Translation(t); iMesh.BoneList.Add(bone); } } if (morphs && mesh.m_Shapes.shapes.Count > 0) { ImportedMorph morph = new ImportedMorph(); morph.Name = iMesh.Name; morph.ClipName = Operations.BlendShapeName(mesh); morph.KeyframeList = new List <ImportedMorphKeyframe>(mesh.m_Shapes.shapes.Count); for (int i = 0; i < mesh.m_Shapes.shapes.Count; i++) { ImportedMorphKeyframe keyframe = new ImportedMorphKeyframe(); keyframe.Name = Operations.BlendShapeKeyframeName(mesh, i); keyframe.VertexList = new List <ImportedVertex>((int)mesh.m_Shapes.shapes[i].vertexCount); keyframe.MorphedVertexIndices = new List <ushort>((int)mesh.m_Shapes.shapes[i].vertexCount); int lastVertIndex = (int)(mesh.m_Shapes.shapes[i].firstVertex + mesh.m_Shapes.shapes[i].vertexCount); for (int j = (int)mesh.m_Shapes.shapes[i].firstVertex; j < lastVertIndex; j++) { BlendShapeVertex morphVert = mesh.m_Shapes.vertices[j]; ImportedVertex vert = GetSourceVertex(iMesh.SubmeshList, (int)morphVert.index); ImportedVertex destVert = new ImportedVertex(); Vector3 morphPos = morphVert.vertex; morphPos.X *= -1; destVert.Position = vert.Position + morphPos; Vector3 morphNormal = morphVert.normal; morphNormal.X *= -1; destVert.Normal = morphNormal; Vector4 morphTangent = new Vector4(morphVert.tangent, 0); morphTangent.X *= -1; destVert.Tangent = morphTangent; keyframe.VertexList.Add(destVert); keyframe.MorphedVertexIndices.Add((ushort)morphVert.index); } morph.KeyframeList.Add(keyframe); } MorphList.Add(morph); } MeshList.Add(iMesh); } }
public static void ReplaceMeshRenderer(Transform frame, Transform rootBone, Animator parser, List <Material> materials, WorkspaceMesh mesh, bool merge, CopyMeshMethod normalsMethod, CopyMeshMethod bonesMethod, bool targetFullMesh) { Matrix transform = Transform.WorldTransform(frame); int[] indices; bool[] worldCoords; bool[] replaceSubmeshesOption; SkinnedMeshRenderer sMesh = CreateSkinnedMeshRenderer(parser, materials, mesh, transform, out indices, out worldCoords, out replaceSubmeshesOption); vMesh destMesh = new Operations.vMesh(sMesh, true, false); SkinnedMeshRenderer sFrameMesh = frame.m_GameObject.instance.FindLinkedComponent(UnityClassID.SkinnedMeshRenderer); MeshRenderer frameMeshR = sFrameMesh; if (sFrameMesh == null) { frameMeshR = frame.m_GameObject.instance.FindLinkedComponent(UnityClassID.MeshRenderer); } Mesh frameMesh = frameMeshR != null?Operations.GetMesh(frameMeshR) : null; vMesh srcMesh = null; List <vVertex> allVertices = null; if (frameMeshR == null || frameMesh == null) { sMesh.m_RootBone = new PPtr <Transform>(rootBone); if (rootBone != null) { sMesh.m_Mesh.instance.m_RootBoneNameHash = parser.m_Avatar.instance.BoneHash(rootBone.m_GameObject.instance.m_Name); } if (frameMeshR != null) { CopyUnknowns(frameMeshR, sMesh); } } else { if (sFrameMesh != null) { sMesh.m_RootBone = new PPtr <Transform>(sFrameMesh.m_RootBone.instance); sMesh.m_Mesh.instance.m_RootBoneNameHash = frameMesh.m_RootBoneNameHash; } else { sMesh.m_RootBone = new PPtr <Transform>((Component)null); } srcMesh = new Operations.vMesh(frameMeshR, true, false); CopyUnknowns(frameMeshR, sMesh); if (targetFullMesh && (normalsMethod == CopyMeshMethod.CopyNear || bonesMethod == CopyMeshMethod.CopyNear)) { allVertices = new List <vVertex>(); HashSet <Vector3> posSet = new HashSet <Vector3>(); foreach (vSubmesh submesh in srcMesh.submeshes) { allVertices.Capacity = allVertices.Count + submesh.vertexList.Count; foreach (vVertex vertex in submesh.vertexList) { if (!posSet.Contains(vertex.position)) { posSet.Add(vertex.position); allVertices.Add(vertex); } } } } } transform.Invert(); vSubmesh[] replaceSubmeshes = (srcMesh == null) ? null : new vSubmesh[srcMesh.submeshes.Count]; List <vSubmesh> addSubmeshes = new List <vSubmesh>(destMesh.submeshes.Count); for (int i = 0; i < destMesh.submeshes.Count; i++) { vSubmesh submesh = destMesh.submeshes[i]; List <vVertex> vVertexList = submesh.vertexList; if (worldCoords[i]) { for (int j = 0; j < vVertexList.Count; j++) { vVertexList[j].position = Vector3.TransformCoordinate(vVertexList[j].position, transform); } } vSubmesh baseSubmesh = null; int idx = indices[i]; if ((srcMesh != null) && (idx >= 0) && (idx < frameMesh.m_SubMeshes.Count)) { baseSubmesh = srcMesh.submeshes[idx]; CopyUnknowns(frameMesh.m_SubMeshes[idx], sMesh.m_Mesh.instance.m_SubMeshes[i]); } if (baseSubmesh != null) { if (normalsMethod == CopyMeshMethod.CopyOrder) { Operations.CopyNormalsOrder(baseSubmesh.vertexList, submesh.vertexList); } else if (normalsMethod == CopyMeshMethod.CopyNear) { Operations.CopyNormalsNear(targetFullMesh ? allVertices : baseSubmesh.vertexList, submesh.vertexList); } if (baseSubmesh.vertexList[0].weights != null) { if (bonesMethod == CopyMeshMethod.CopyOrder) { Operations.CopyBonesOrder(baseSubmesh.vertexList, submesh.vertexList); } else if (bonesMethod == CopyMeshMethod.CopyNear) { Operations.CopyBonesNear(targetFullMesh ? allVertices : baseSubmesh.vertexList, submesh.vertexList); } } } if ((baseSubmesh != null) && merge && replaceSubmeshesOption[i]) { replaceSubmeshes[idx] = submesh; } else { addSubmeshes.Add(submesh); } } if ((srcMesh != null) && merge) { destMesh.submeshes = new List <vSubmesh>(replaceSubmeshes.Length + addSubmeshes.Count); List <vSubmesh> copiedSubmeshes = new List <vSubmesh>(replaceSubmeshes.Length); for (int i = 0; i < replaceSubmeshes.Length; i++) { if (replaceSubmeshes[i] == null) { vSubmesh srcSubmesh = srcMesh.submeshes[i]; copiedSubmeshes.Add(srcSubmesh); destMesh.submeshes.Add(srcSubmesh); } else { destMesh.submeshes.Add(replaceSubmeshes[i]); } } destMesh.submeshes.AddRange(addSubmeshes); if ((sFrameMesh == null || sFrameMesh.m_Bones.Count == 0) && (sMesh.m_Bones.Count > 0)) { for (int i = 0; i < copiedSubmeshes.Count; i++) { List <vVertex> vertexList = copiedSubmeshes[i].vertexList; for (int j = 0; j < vertexList.Count; j++) { vertexList[j].boneIndices = new int[4] { 0, 0, 0, 0 }; vertexList[j].weights = new float[4] { 0, 0, 0, 0 }; } } } else if (sFrameMesh != null && sFrameMesh.m_Bones.Count > 0) { int[] boneIdxMap; sMesh.m_Bones = MergeBoneList(sFrameMesh.m_Bones, sMesh.m_Bones, out boneIdxMap); uint[] boneHashes = new uint[sMesh.m_Bones.Count]; Matrix[] poseMatrices = new Matrix[sMesh.m_Bones.Count]; for (int i = 0; i < sFrameMesh.m_Bones.Count; i++) { boneHashes[i] = sFrameMesh.m_Mesh.instance.m_BoneNameHashes[i]; poseMatrices[i] = sFrameMesh.m_Mesh.instance.m_BindPose[i]; } for (int i = 0; i < boneIdxMap.Length; i++) { boneHashes[boneIdxMap[i]] = sMesh.m_Mesh.instance.m_BoneNameHashes[i]; poseMatrices[boneIdxMap[i]] = sMesh.m_Mesh.instance.m_BindPose[i]; } sMesh.m_Mesh.instance.m_BoneNameHashes.Clear(); sMesh.m_Mesh.instance.m_BoneNameHashes.AddRange(boneHashes); sMesh.m_Mesh.instance.m_BindPose.Clear(); sMesh.m_Mesh.instance.m_BindPose.AddRange(poseMatrices); if (bonesMethod == CopyMeshMethod.Replace) { for (int i = 0; i < replaceSubmeshes.Length; i++) { if (replaceSubmeshes[i] != null) { List <vVertex> vertexList = replaceSubmeshes[i].vertexList; if (vertexList[0].boneIndices != null) { for (int j = 0; j < vertexList.Count; j++) { int[] boneIndices = vertexList[j].boneIndices; vertexList[j].boneIndices = new int[4]; for (int k = 0; k < 4; k++) { vertexList[j].boneIndices[k] = boneIdxMap[boneIndices[k]]; } } } } } for (int i = 0; i < addSubmeshes.Count; i++) { List <vVertex> vertexList = addSubmeshes[i].vertexList; if (vertexList[0].boneIndices != null) { for (int j = 0; j < vertexList.Count; j++) { int[] boneIndices = vertexList[j].boneIndices; vertexList[j].boneIndices = new int[4]; for (int k = 0; k < 4; k++) { vertexList[j].boneIndices[k] = boneIdxMap[boneIndices[k]]; } } } } } } } destMesh.Flush(); if (frameMeshR != null) { frame.m_GameObject.instance.RemoveLinkedComponent(frameMeshR); //parser.file.RemoveSubfile(frameMeshR); if (frameMesh != null) { //parser.file.RemoveSubfile(frameMesh); parser.file.ReplaceSubfile(frameMesh, sMesh.m_Mesh.asset); } parser.file.ReplaceSubfile(frameMeshR, sMesh); } frame.m_GameObject.instance.AddLinkedComponent(sMesh); AssetBundle bundle = parser.file.Bundle; if (bundle != null) { if (frameMeshR != null) { if (frameMesh != null) { bundle.ReplaceComponent(frameMesh, sMesh.m_Mesh.asset); } bundle.ReplaceComponent(frameMeshR, sMesh); } else { bundle.RegisterForUpdate(parser.m_GameObject.asset); } } }
public FormRendererMeshAttributes(MeshRenderer meshR, int selectedSubmesh) { InitializeComponent(); startSize = new SizeF(Width, Height); this.SaveDesignSizes(); Text = meshR.classID1 + " " + meshR.m_GameObject.instance.m_Name + " Attributes"; checkBoxRendererCastShadows.Checked = meshR.m_CastShadows; checkBoxRendererReceiveShadows.Checked = meshR.m_ReceiveShadows; editTextBoxRendererLightMap.Text = meshR.m_LightmapIndex.ToString(); editTextBoxRendererTilingOffset.Text = "X:" + meshR.m_LightmapTilingOffset.X.ToFloatString() + ", Y:" + meshR.m_LightmapTilingOffset.Y.ToFloatString() + ", Z:" + meshR.m_LightmapTilingOffset.Z.ToFloatString() + ", W:" + meshR.m_LightmapTilingOffset.W.ToFloatString(); checkBoxRendererSubsetIndices.Checked = meshR.m_SubsetIndices.Length > 0; editTextBoxRendererStaticBatchRoot.Text = meshR.m_StaticBatchRoot.instance != null ? meshR.m_StaticBatchRoot.instance.m_GameObject.instance.m_Name : String.Empty; checkBoxRendererUseLightProbes.Checked = meshR.m_UseLightProbes; editTextBoxRendererLightProbeAnchor.Text = meshR.m_LightProbeAnchor.instance != null ? meshR.m_LightProbeAnchor.instance.m_GameObject.instance.m_Name : String.Empty; editTextBoxRendererSortingLayerID.Text = meshR.m_SortingLayerID.ToString(); editTextBoxRendererSortingOrder.Text = meshR.m_SortingOrder.ToString(); if (meshR is SkinnedMeshRenderer) { SkinnedMeshRenderer smr = (SkinnedMeshRenderer)meshR; editTextBoxSkinnedMeshRendererQuality.Text = smr.m_Quality.ToString(); checkBoxSkinnedMeshRendererUpdateWhenOffScreen.Checked = smr.m_UpdateWhenOffScreen; editTextBoxSkinnedMeshRendererBones.Text = smr.m_Bones.Count.ToString(); editTextBoxSkinnedMeshRendererBlendShapeWeights.Text = smr.m_BlendShapeWeights.Count.ToString(); editTextBoxSkinnedMeshRendererAABBCenter.Text = "X:" + smr.m_AABB.m_Center.X.ToFloatString() + ", Y:" + smr.m_AABB.m_Center.Y.ToFloatString() + ", Z:" + smr.m_AABB.m_Center.Z.ToFloatString(); editTextBoxSkinnedMeshRendererAABBExtend.Text = "X:" + smr.m_AABB.m_Extend.X.ToFloatString() + ", Y:" + smr.m_AABB.m_Extend.Y.ToFloatString() + ", Z:" + smr.m_AABB.m_Extend.Z.ToFloatString(); checkBoxSkinnedMeshRendererDirtyAABB.Checked = smr.m_DirtyAABB; } else { groupBoxSkinnedMeshRenderer.Enabled = false; } Mesh mesh = Operations.GetMesh(meshR); if (mesh != null) { editTextBoxMeshBlendShape.Text = mesh.m_Shapes.vertices.Count + "/" + mesh.m_Shapes.shapes.Count + "/" + mesh.m_Shapes.fullWeights.Count; editTextBoxMeshBindPose.Text = mesh.m_BindPose.Count.ToString(); editTextBoxMeshBoneHashes.Text = mesh.m_BoneNameHashes.Count.ToString(); checkBoxMeshCompression.Checked = mesh.m_MeshCompression > 0; checkBoxMeshStreamCompression.Checked = mesh.m_StreamCompression > 0; checkBoxMeshReadable.Checked = mesh.m_IsReadable; checkBoxMeshKeepVertices.Checked = mesh.m_KeepVertices; checkBoxMeshKeepIndices.Checked = mesh.m_KeepIndices; editTextBoxMeshInfluences.Text = mesh.m_Skin.Count.ToString(); editTextBoxMeshUsageFlags.Text = mesh.m_MeshUsageFlags.ToString(); editTextBoxMeshCenter.Text = "X:" + mesh.m_LocalAABB.m_Center.X.ToFloatString() + ", Y:" + mesh.m_LocalAABB.m_Center.Y.ToFloatString() + ", Z:" + mesh.m_LocalAABB.m_Center.Z.ToFloatString(); editTextBoxMeshExtend.Text = "X:" + mesh.m_LocalAABB.m_Extend.X.ToFloatString() + ", Y:" + mesh.m_LocalAABB.m_Extend.Y.ToFloatString() + ", Z:" + mesh.m_LocalAABB.m_Extend.Z.ToFloatString(); if (selectedSubmesh >= 0) { editTextBoxSubmeshCenter.Text = "X:" + mesh.m_SubMeshes[selectedSubmesh].localAABB.m_Center.X.ToFloatString() + ", Y:" + mesh.m_SubMeshes[selectedSubmesh].localAABB.m_Center.Y.ToFloatString() + ", Z:" + mesh.m_SubMeshes[selectedSubmesh].localAABB.m_Center.Z.ToFloatString(); editTextBoxSubmeshExtend.Text = "X:" + mesh.m_SubMeshes[selectedSubmesh].localAABB.m_Extend.X.ToFloatString() + ", Y:" + mesh.m_SubMeshes[selectedSubmesh].localAABB.m_Extend.Y.ToFloatString() + ", Z:" + mesh.m_SubMeshes[selectedSubmesh].localAABB.m_Extend.Z.ToFloatString(); } } else { groupBoxMesh.Enabled = false; } checkBoxRendererCastShadows.CheckedChanged += AttributeChanged; checkBoxRendererReceiveShadows.CheckedChanged += AttributeChanged; editTextBoxRendererLightMap.AfterEditTextChanged += AttributeChanged; editTextBoxRendererTilingOffset.AfterEditTextChanged += AttributeChanged; checkBoxRendererSubsetIndices.CheckedChanged += AttributeChanged; editTextBoxRendererStaticBatchRoot.AfterEditTextChanged += AttributeChanged; checkBoxRendererUseLightProbes.CheckedChanged += AttributeChanged; editTextBoxRendererSortingLayerID.AfterEditTextChanged += AttributeChanged; editTextBoxRendererSortingOrder.AfterEditTextChanged += AttributeChanged; editTextBoxRendererLightProbeAnchor.AfterEditTextChanged += AttributeChanged; editTextBoxSkinnedMeshRendererQuality.AfterEditTextChanged += AttributeChanged; checkBoxSkinnedMeshRendererUpdateWhenOffScreen.CheckedChanged += AttributeChanged; editTextBoxSkinnedMeshRendererAABBCenter.AfterEditTextChanged += AttributeChanged; editTextBoxSkinnedMeshRendererAABBExtend.AfterEditTextChanged += AttributeChanged; checkBoxSkinnedMeshRendererDirtyAABB.CheckedChanged += AttributeChanged; checkBoxMeshCompression.CheckedChanged += AttributeChanged; checkBoxMeshStreamCompression.CheckedChanged += AttributeChanged; checkBoxMeshReadable.CheckedChanged += AttributeChanged; checkBoxMeshKeepVertices.CheckedChanged += AttributeChanged; checkBoxMeshKeepIndices.CheckedChanged += AttributeChanged; editTextBoxMeshUsageFlags.AfterEditTextChanged += AttributeChanged; editTextBoxMeshCenter.AfterEditTextChanged += AttributeChanged; editTextBoxMeshExtend.AfterEditTextChanged += AttributeChanged; }