public override void PostConvert(ref MeshData mesh) { //UV MeshData.VBuffer UVBuffer; if (mesh.Buffers.TryGetValue(ESemantic.Coord0, out UVBuffer)) { var uvBuffers = UVBuffer.mData; for (int i = 0; i < uvBuffers.Count; i++) { var uv = uvBuffers[i]; uvBuffers[i] = new Vector4(uv.x, 1.0f - uv.y, 0.0f, 0.0f); //Debug.Log(UVBuffer.mData[i]); } } else { Debug.Log("No UV channel can be found"); return; } //Normal ,BiNormal,Tangent MeshData.VBuffer NormalBuffer; bool hasNormal = mesh.Buffers.TryGetValue(ESemantic.Normal, out NormalBuffer); if (!hasNormal) { Debug.Log("No Normal channel can be found"); return; } else { if (NormalBuffer.mDimension < 4) { Debug.Log("No Normal.w can be found"); return; } List <Vector4> normals = new List <Vector4>(); List <Vector4> tangents = new List <Vector4>(); List <Vector4> binormals = new List <Vector4>(); for (int i = 0; i < NormalBuffer.mData.Count; i++) { var normalData = NormalBuffer.mData[i]; Vector3 tan = new Vector3(0.0f, 0.0f, 0.0f), bin = new Vector3(0.0f, 0.0f, 0.0f), nor = new Vector3(0.0f, 0.0f, 0.0f); DecodeTangentBinormal(normalData, ref tan, ref bin, ref nor); normals.Add(new Vector4(nor.x, nor.y, nor.z, 0.0f)); tangents.Add(new Vector4(tan.x, tan.y, tan.z, 0.0f)); binormals.Add(new Vector4(bin.x, bin.y, bin.z, 0.0f)); } NormalBuffer.mData = normals; NormalBuffer.mDimension = 3; MeshData.VBuffer TangentBuffer; bool hasTangent = mesh.Buffers.TryGetValue(ESemantic.Tangent, out TangentBuffer); if (!hasTangent) { TangentBuffer = new MeshData.VBuffer(); mesh.Buffers.Add(ESemantic.Tangent, TangentBuffer); } TangentBuffer.mData = tangents; TangentBuffer.mDimension = 3; MeshData.VBuffer BiNormalBuffer; bool hasBiNormal = mesh.Buffers.TryGetValue(ESemantic.BNormal, out BiNormalBuffer); if (!hasBiNormal) { BiNormalBuffer = new MeshData.VBuffer(); mesh.Buffers.Add(ESemantic.BNormal, BiNormalBuffer); } BiNormalBuffer.mData = binormals; BiNormalBuffer.mDimension = 3; } //Position MeshData.VBuffer posBuf; if (!mesh.Buffers.TryGetValue(ESemantic.Position, out posBuf)) { return; } for (int i = 0; i < posBuf.mData.Count; i++) { Vector4 pos = posBuf.mData[i]; pos.y += 2; pos.w = 1; posBuf.mData[i] = pos; } if (mUseSkeleton) { UseSkeletonData(ref mesh); } mesh.Buffers.Remove(ESemantic.Coord1); mesh.Buffers.Remove(ESemantic.Coord2); mesh.Buffers.Remove(ESemantic.Coord3); mesh.Buffers.Remove(ESemantic.Coord4); }
public void OnGUI() { if (mMeshData == null) { return; } if (mStyles == null) { mStyles = new Styles(); } int vertexNum = mMeshData.GetVertexCount(); float height = this.position.height - 20; float width = 40; int viewItemNum = Mathf.FloorToInt(height / 18 - 1); //id scroll bar current = (int)GUI.VerticalScrollbar(new Rect(0, 0, 20, height + 3), current, viewItemNum, 0, vertexNum); int end = Mathf.Min(current + viewItemNum, vertexNum); int start = Mathf.Max(0, end - viewItemNum); EditorGUILayout.BeginHorizontal(); { GUILayout.Space(20); //draw id EditorGUILayout.BeginVertical(mStyles.mPreviewBox, GUILayout.Width(width), GUILayout.Height(height)); { EditorGUILayout.BeginHorizontal(mStyles.mOLTitle); { EditorGUILayout.LabelField(" id", EditorStyles.boldLabel, GUILayout.Width(width)); } EditorGUILayout.EndHorizontal(); for (int i = start; i < end; i++) { EditorGUILayout.LabelField(i.ToString(), EditorStyles.boldLabel, GUILayout.Width(width)); } } EditorGUILayout.EndVertical(); GUILayout.Space(1); DataPanelScroll = EditorGUILayout.BeginScrollView(DataPanelScroll); EditorGUILayout.BeginHorizontal(); { foreach (var pair in mMeshData.Buffers) { ESemantic s = pair.Key; MeshData.VBuffer buff = pair.Value; width = buff.mDimension * 100; EditorGUILayout.BeginVertical(mStyles.mPreviewBox, GUILayout.Width(width), GUILayout.Height(height)); { EditorGUILayout.BeginHorizontal(mStyles.mOLTitle); { EditorGUILayout.LabelField(" " + s.ToString(), EditorStyles.boldLabel, GUILayout.Width(width)); } EditorGUILayout.EndHorizontal(); for (int i = start; i < end; i++) { EditorGUILayout.BeginHorizontal(); for (int j = 0; j < buff.mDimension; j++) { EditorGUILayout.LabelField(buff.mData[i][j].ToString(), GUILayout.Width(90)); } EditorGUILayout.EndHorizontal(); } } EditorGUILayout.EndVertical(); } } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndScrollView(); } GUILayout.Label(mMeshData.GetInfo()); EditorGUILayout.EndHorizontal(); }
private static void MeshFBX(MeshData m_Mesh, string MeshID, StringBuilder ob) { int vertexNum = m_Mesh.GetVertexCount(); if (vertexNum > 0)//general failsafe { //StatusStripUpdate("Writing Geometry: " + m_Mesh.m_Name); ob.AppendFormat("\n\tGeometry: 3{0}, \"Geometry::\", \"Mesh\" {{", MeshID); ob.Append("\n\t\tProperties70: {"); var randomColor = RandomColorGenerator(m_Mesh.Name); ob.AppendFormat("\n\t\t\tP: \"Color\", \"ColorRGB\", \"Color\", \"\",{0},{1},{2}", ((float)randomColor[0] / 255), ((float)randomColor[1] / 255), ((float)randomColor[2] / 255)); ob.Append("\n\t\t}"); #region Vertices MeshData.VBuffer buff = m_Mesh.Buffers[ESemantic.Position]; ob.AppendFormat("\n\t\tVertices: *{0} {{\n\t\t\ta: ", vertexNum * 3); int lineSplit = ob.Length; for (int v = 0; v < vertexNum; v++) { ob.AppendFormat("{0},{1},{2},", buff.mData[v].x, buff.mData[v].y, buff.mData[v].z); if (ob.Length - lineSplit > 2000) { ob.Append("\n"); lineSplit = ob.Length; } } ob.Length--;//remove last comma ob.Append("\n\t\t}"); #endregion #region Indices int indexNum = m_Mesh.GetIndexCount(); //in order to test topology for triangles/quads we need to store submeshes and write each one as geometry, then link to Mesh Node ob.AppendFormat("\n\t\tPolygonVertexIndex: *{0} {{\n\t\t\ta: ", indexNum); lineSplit = ob.Length; for (int f = 0; f < indexNum / 3; f++) { ob.AppendFormat("{0},{1},{2},", m_Mesh.Trangles[f * 3], m_Mesh.Trangles[f * 3 + 1], (-m_Mesh.Trangles[f * 3 + 2] - 1)); if (ob.Length - lineSplit > 2000) { ob.Append("\n"); lineSplit = ob.Length; } } ob.Length--;//remove last comma ob.Append("\n\t\t}"); ob.Append("\n\t\tGeometryVersion: 124"); #endregion #region Normals if (m_Mesh.Buffers.TryGetValue(ESemantic.Normal, out buff)) { ob.Append("\n\t\tLayerElementNormal: 0 {"); ob.Append("\n\t\t\tVersion: 101"); ob.Append("\n\t\t\tName: \"\""); ob.Append("\n\t\t\tMappingInformationType: \"ByVertice\""); ob.Append("\n\t\t\tReferenceInformationType: \"Direct\""); ob.AppendFormat("\n\t\t\tNormals: *{0} {{\n\t\t\ta: ", (vertexNum * 3)); lineSplit = ob.Length; for (int v = 0; v < vertexNum; v++) { ob.AppendFormat("{0},{1},{2},", buff.mData[v].x, buff.mData[v].y, buff.mData[v].z); if (ob.Length - lineSplit > 2000) { ob.Append("\n"); lineSplit = ob.Length; } } ob.Length--;//remove last comma ob.Append("\n\t\t\t}\n\t\t}"); } #endregion #region Binormals if (m_Mesh.Buffers.TryGetValue(ESemantic.BNormal, out buff)) { ob.Append("\n\t\tLayerElementBinormal: 0 {"); ob.Append("\n\t\t\tVersion: 101"); ob.Append("\n\t\t\tName: \"\""); ob.Append("\n\t\t\tMappingInformationType: \"ByVertice\""); ob.Append("\n\t\t\tReferenceInformationType: \"Direct\""); ob.AppendFormat("\n\t\t\tBinormals: *{0} {{\n\t\t\ta: ", (vertexNum * 3)); lineSplit = ob.Length; for (int v = 0; v < vertexNum; v++) { if (buff.mDimension == 3) { ob.AppendFormat("{0},{1},{2},", buff.mData[v].x, buff.mData[v].y, buff.mData[v].z); } else { ob.AppendFormat("{0},{1},{2},", buff.mData[v].x * buff.mData[v].w, buff.mData[v].y * buff.mData[v].w, buff.mData[v].z * buff.mData[v].w); } if (ob.Length - lineSplit > 2000) { ob.Append("\n"); lineSplit = ob.Length; } } ob.Length--;//remove last comma ob.Append("\n\t\t\t}\n\t\t}"); } #endregion #region Tangents if (m_Mesh.Buffers.TryGetValue(ESemantic.Tangent, out buff)) { ob.Append("\n\t\tLayerElementTangent: 0 {"); ob.Append("\n\t\t\tVersion: 101"); ob.Append("\n\t\t\tName: \"\""); ob.Append("\n\t\t\tMappingInformationType: \"ByVertice\""); ob.Append("\n\t\t\tReferenceInformationType: \"Direct\""); ob.AppendFormat("\n\t\t\tTangents: *{0} {{\n\t\t\ta: ", (vertexNum * 3)); lineSplit = ob.Length; for (int v = 0; v < vertexNum; v++) { if (buff.mDimension == 3) { ob.AppendFormat("{0},{1},{2},", buff.mData[v].x, buff.mData[v].y, buff.mData[v].z); } else { ob.AppendFormat("{0},{1},{2},", buff.mData[v].x * buff.mData[v].w, buff.mData[v].y * buff.mData[v].w, buff.mData[v].z * buff.mData[v].w); } if (ob.Length - lineSplit > 2000) { ob.Append("\n"); lineSplit = ob.Length; } } ob.Length--;//remove last comma ob.Append("\n\t\t\t}\n\t\t}"); } #endregion #region Colors if (m_Mesh.Buffers.TryGetValue(ESemantic.Color, out buff)) { ob.Append("\n\t\tLayerElementColor: 0 {"); ob.Append("\n\t\t\tVersion: 101"); ob.Append("\n\t\t\tName: \"\""); //ob.Append("\n\t\t\tMappingInformationType: \"ByVertice\""); //ob.Append("\n\t\t\tReferenceInformationType: \"Direct\""); ob.Append("\n\t\t\tMappingInformationType: \"ByPolygonVertex\""); ob.Append("\n\t\t\tReferenceInformationType: \"IndexToDirect\""); ob.AppendFormat("\n\t\t\tColors: *{0} {{\n\t\t\ta: ", vertexNum * 4); //ob.Append(string.Join(",", m_Mesh.m_Colors)); lineSplit = ob.Length; if (buff.mDimension == 3) { for (int i = 0; i < vertexNum; i++) { ob.AppendFormat("{0},{1},{2},{3},", buff.mData[i].x, buff.mData[i].y, buff.mData[i].z, 1.0f); if (ob.Length - lineSplit > 2000) { ob.Append("\n"); lineSplit = ob.Length; } } } else { for (int i = 0; i < vertexNum; i++) { ob.AppendFormat("{0},{1},{2},{3},", buff.mData[i].x, buff.mData[i].y, buff.mData[i].z, buff.mData[i].w); if (ob.Length - lineSplit > 2000) { ob.Append("\n"); lineSplit = ob.Length; } } } ob.Length--;//remove last comma ob.Append("\n\t\t\t}"); ob.AppendFormat("\n\t\t\tColorIndex: *{0} {{\n\t\t\ta: ", indexNum); lineSplit = ob.Length; for (int f = 0; f < indexNum / 3; f++) { ob.AppendFormat("{0},{1},{2},", m_Mesh.Trangles[f * 3], m_Mesh.Trangles[f * 3 + 1], (m_Mesh.Trangles[f * 3 + 2])); if (ob.Length - lineSplit > 2000) { ob.Append("\n"); lineSplit = ob.Length; } } ob.Length--;//remove last comma ob.Append("\n\t\t\t}\n\t\t}"); } #endregion #region UV1 //does FBX support UVW coordinates? if (m_Mesh.Buffers.TryGetValue(ESemantic.Coord0, out buff)) { ob.Append("\n\t\tLayerElementUV: 0 {"); ob.Append("\n\t\t\tVersion: 101"); ob.Append("\n\t\t\tName: \"UVChannel_1\""); ob.Append("\n\t\t\tMappingInformationType: \"ByVertice\""); ob.Append("\n\t\t\tReferenceInformationType: \"Direct\""); ob.AppendFormat("\n\t\t\tUV: *{0} {{\n\t\t\ta: ", vertexNum * buff.mDimension); lineSplit = ob.Length; for (int v = 0; v < vertexNum; v++) { for (int j = 0; j < buff.mDimension; j++) { ob.AppendFormat("{0},", buff.mData[v][j]); } if (ob.Length - lineSplit > 2000) { ob.Append("\n"); lineSplit = ob.Length; } } ob.Length--;//remove last comma ob.Append("\n\t\t\t}\n\t\t}"); } #endregion #region UV2 if (m_Mesh.Buffers.TryGetValue(ESemantic.Coord1, out buff)) { ob.Append("\n\t\tLayerElementUV: 1 {"); ob.Append("\n\t\t\tVersion: 101"); ob.Append("\n\t\t\tName: \"UVChannel_2\""); ob.Append("\n\t\t\tMappingInformationType: \"ByVertice\""); ob.Append("\n\t\t\tReferenceInformationType: \"Direct\""); ob.AppendFormat("\n\t\t\tUV: *{0} {{\n\t\t\ta: ", vertexNum * buff.mDimension); lineSplit = ob.Length; for (int v = 0; v < vertexNum; v++) { for (int j = 0; j < buff.mDimension; j++) { ob.AppendFormat("{0},", buff.mData[v][j]); } if (ob.Length - lineSplit > 2000) { ob.Append("\n"); lineSplit = ob.Length; } } ob.Length--;//remove last comma ob.Append("\n\t\t\t}\n\t\t}"); } #endregion #region UV3 if (m_Mesh.Buffers.TryGetValue(ESemantic.Coord2, out buff)) { ob.Append("\n\t\tLayerElementUV: 2 {"); ob.Append("\n\t\t\tVersion: 101"); ob.Append("\n\t\t\tName: \"UVChannel_3\""); ob.Append("\n\t\t\tMappingInformationType: \"ByVertice\""); ob.Append("\n\t\t\tReferenceInformationType: \"Direct\""); ob.AppendFormat("\n\t\t\tUV: *{0} {{\n\t\t\ta: ", vertexNum * buff.mDimension); lineSplit = ob.Length; for (int v = 0; v < vertexNum; v++) { for (int j = 0; j < buff.mDimension; j++) { ob.AppendFormat("{0},", buff.mData[v][j]); } if (ob.Length - lineSplit > 2000) { ob.Append("\n"); lineSplit = ob.Length; } } ob.Length--;//remove last comma ob.Append("\n\t\t\t}\n\t\t}"); } #endregion #region UV4 if (m_Mesh.Buffers.TryGetValue(ESemantic.Coord3, out buff)) { ob.Append("\n\t\tLayerElementUV: 3 {"); ob.Append("\n\t\t\tVersion: 101"); ob.Append("\n\t\t\tName: \"UVChannel_4\""); ob.Append("\n\t\t\tMappingInformationType: \"ByVertice\""); ob.Append("\n\t\t\tReferenceInformationType: \"Direct\""); ob.AppendFormat("\n\t\t\tUV: *{0} {{\n\t\t\ta: ", vertexNum * buff.mDimension); lineSplit = ob.Length; for (int v = 0; v < vertexNum; v++) { for (int j = 0; j < buff.mDimension; j++) { ob.AppendFormat("{0},", buff.mData[v][j]); } if (ob.Length - lineSplit > 2000) { ob.Append("\n"); lineSplit = ob.Length; } } ob.Length--;//remove last comma ob.Append("\n\t\t\t}\n\t\t}"); } #endregion #region Material ob.Append("\n\t\tLayerElementMaterial: 0 {"); ob.Append("\n\t\t\tVersion: 101"); ob.Append("\n\t\t\tName: \"\""); ob.Append("\n\t\t\tMappingInformationType: \""); //if (m_Mesh.m_SubMeshes.Count == 1) { ob.Append("AllSame\""); } //else { ob.Append("ByPolygon\""); } ob.Append("\n\t\t\tReferenceInformationType: \"IndexToDirect\""); ob.AppendFormat("\n\t\t\tMaterials: *{0} {{", 0); ob.Append("\n\t\t\t\t"); //if (m_Mesh.m_SubMeshes.Count == 1) { ob.Append("0"); } //else //{ // lineSplit = ob.Length; // for (int i = 0; i < m_Mesh.m_materialIDs.Count; i++) // { // ob.AppendFormat("{0},", m_Mesh.m_materialIDs[i]); // if (ob.Length - lineSplit > 2000) // { // ob.Append("\n"); // lineSplit = ob.Length; // } // } // ob.Length--;//remove last comma //} ob.Append("\n\t\t\t}\n\t\t}"); #endregion #region Layers ob.Append("\n\t\tLayer: 0 {"); ob.Append("\n\t\t\tVersion: 100"); if (m_Mesh.Buffers.ContainsKey(ESemantic.Normal)) { ob.Append("\n\t\t\tLayerElement: {"); ob.Append("\n\t\t\t\tType: \"LayerElementNormal\""); ob.Append("\n\t\t\t\tTypedIndex: 0"); ob.Append("\n\t\t\t}"); } if (m_Mesh.Buffers.ContainsKey(ESemantic.BNormal)) { ob.Append("\n\t\t\tLayerElement: {"); ob.Append("\n\t\t\t\tType: \"LayerElementBinormal\""); ob.Append("\n\t\t\t\tTypedIndex: 0"); ob.Append("\n\t\t\t}"); } if (m_Mesh.Buffers.ContainsKey(ESemantic.Tangent)) { ob.Append("\n\t\t\tLayerElement: {"); ob.Append("\n\t\t\t\tType: \"LayerElementTangent\""); ob.Append("\n\t\t\t\tTypedIndex: 0"); ob.Append("\n\t\t\t}"); } ob.Append("\n\t\t\tLayerElement: {"); ob.Append("\n\t\t\t\tType: \"LayerElementMaterial\""); ob.Append("\n\t\t\t\tTypedIndex: 0"); ob.Append("\n\t\t\t}"); // /*ob.Append("\n\t\t\tLayerElement: {"); * ob.Append("\n\t\t\t\tType: \"LayerElementTexture\""); * ob.Append("\n\t\t\t\tTypedIndex: 0"); * ob.Append("\n\t\t\t}"); * ob.Append("\n\t\t\tLayerElement: {"); * ob.Append("\n\t\t\t\tType: \"LayerElementBumpTextures\""); * ob.Append("\n\t\t\t\tTypedIndex: 0"); * ob.Append("\n\t\t\t}");*/ if (m_Mesh.Buffers.ContainsKey(ESemantic.Color)) { ob.Append("\n\t\t\tLayerElement: {"); ob.Append("\n\t\t\t\tType: \"LayerElementColor\""); ob.Append("\n\t\t\t\tTypedIndex: 0"); ob.Append("\n\t\t\t}"); } if (m_Mesh.Buffers.ContainsKey(ESemantic.Coord0)) { ob.Append("\n\t\t\tLayerElement: {"); ob.Append("\n\t\t\t\tType: \"LayerElementUV\""); ob.Append("\n\t\t\t\tTypedIndex: 0"); ob.Append("\n\t\t\t}"); } ob.Append("\n\t\t}"); //Layer 0 end if (m_Mesh.Buffers.ContainsKey(ESemantic.Coord1)) { ob.Append("\n\t\tLayer: 1 {"); ob.Append("\n\t\t\tVersion: 100"); ob.Append("\n\t\t\tLayerElement: {"); ob.Append("\n\t\t\t\tType: \"LayerElementUV\""); ob.Append("\n\t\t\t\tTypedIndex: 1"); ob.Append("\n\t\t\t}"); ob.Append("\n\t\t}"); //Layer 1 end } if (m_Mesh.Buffers.ContainsKey(ESemantic.Coord2)) { ob.Append("\n\t\tLayer: 2 {"); ob.Append("\n\t\t\tVersion: 100"); ob.Append("\n\t\t\tLayerElement: {"); ob.Append("\n\t\t\t\tType: \"LayerElementUV\""); ob.Append("\n\t\t\t\tTypedIndex: 2"); ob.Append("\n\t\t\t}"); ob.Append("\n\t\t}"); //Layer 2 end } if (m_Mesh.Buffers.ContainsKey(ESemantic.Coord3)) { ob.Append("\n\t\tLayer: 3 {"); ob.Append("\n\t\t\tVersion: 100"); ob.Append("\n\t\t\tLayerElement: {"); ob.Append("\n\t\t\t\tType: \"LayerElementUV\""); ob.Append("\n\t\t\t\tTypedIndex: 3"); ob.Append("\n\t\t\t}"); ob.Append("\n\t\t}"); //Layer 3 end } #endregion ob.Append("\n\t}"); //Geometry end } }