public new SceneMesh GetObjectData() { var sceneData = new SceneMesh(); sceneData.name = name; // submeshes sceneData.subMeshCount = unityMesh.subMeshCount; sceneData.triangles = new int[unityMesh.subMeshCount][]; for (int i = 0; i < unityMesh.subMeshCount; i++) { sceneData.triangles[i] = unityMesh.GetTriangles(i); } // Vertices sceneData.vertexCount = unityMesh.vertexCount; sceneData.vertexPositions = unityMesh.vertices; sceneData.vertexUV = unityMesh.uv; sceneData.vertexUV2 = unityMesh.uv2; sceneData.vertexColors = unityMesh.colors; sceneData.vertexNormals = unityMesh.normals; sceneData.vertexTangents = unityMesh.tangents; sceneData.bindPoses = unityMesh.bindposes; sceneData.boneWeights = new SceneBoneWeight[unityMesh.boneWeights.Length]; sceneData.rootBone = rootBone; for (int i = 0; i < unityMesh.boneWeights.Length; i++) { var bw = unityMesh.boneWeights[i]; var jbw = new SceneBoneWeight(); jbw.indexes[0] = bw.boneIndex0; jbw.indexes[1] = bw.boneIndex1; jbw.indexes[2] = bw.boneIndex2; jbw.indexes[3] = bw.boneIndex3; jbw.weights[0] = bw.weight0; jbw.weights[1] = bw.weight1; jbw.weights[2] = bw.weight2; jbw.weights[3] = bw.weight3; sceneData.boneWeights[i] = jbw; } if (bones != null) { SceneTransform[] jbones = new SceneTransform[bones.Length]; for (int i = 0; i < bones.Length; i++) { SceneTransform jt = new SceneTransform(); jt.localPosition = bones[i].localPosition; jt.localRotation = bones[i].localRotation; jt.localScale = bones[i].localScale; jt.name = bones[i].gameObject.name; jt.parentName = (bones[i].parent == null) ? "" : bones[i].parent.gameObject.name; jt.type = "Bone"; jbones[i] = jt; } sceneData.bones = jbones; } return(sceneData); }
public static string ExportGeometry(ref SceneData sceneData, ref SceneMeshRenderer smr, ref SceneMesh mesh, string spaces) { string osgData = spaces + "useDisplayList TRUE\n" + spaces + "useVertexBufferObjects FALSE\n"; if (smr != null) { osgData += MaterialExporter.ExportStateSet(ref sceneData, ref smr, spaces); } osgData += ExportGeometryData(ref sceneData, ref mesh, spaces); return(osgData); }
private static string ExportGeometryData(ref SceneData sceneData, ref SceneMesh mesh, string spaces) { // Add all primitive sets string osgData = spaces + "PrimitiveSets " + mesh.subMeshCount + " {\n"; for (int i = 0; i < mesh.subMeshCount; ++i) { int numElements = mesh.triangles[i].Length; osgData += spaces + " DrawElements" + (numElements < 65535 ? "UShort" : "UInt") + " TRIANGLES " + numElements + " {\n"; for (int j = 0; j < numElements; j += 3) { osgData += spaces + " " + mesh.triangles[i][j] + " " + mesh.triangles[i][j + 1] + " " + mesh.triangles[i][j + 2] + "\n"; } osgData += spaces + " }\n"; } osgData += spaces + "}\n"; // Add all vertices osgData += spaces + "VertexArray Vec3Array " + mesh.vertexCount + " {\n"; for (int i = 0; i < mesh.vertexCount; ++i) { Vector3 v = mesh.vertexPositions[i]; osgData += spaces + " " + v.x + " " + v.y + " " + v.z + "\n"; } osgData += spaces + "}\n"; // Add all normals if (mesh.vertexNormals.Length > 0) { osgData += spaces + "NormalBinding PER_VERTEX\n" + spaces + "NormalArray Vec3Array " + mesh.vertexCount + " {\n"; for (int i = 0; i < mesh.vertexCount; ++i) { Vector3 v = mesh.vertexNormals[i]; osgData += spaces + " " + v.x + " " + v.y + " " + v.z + "\n"; } osgData += spaces + "}\n"; } // Add all UVs if (mesh.vertexUV.Length > 0) { osgData += spaces + "TexCoordArray 0 Vec2Array " + mesh.vertexCount + " {\n"; for (int i = 0; i < mesh.vertexCount; ++i) { Vector2 v = mesh.vertexUV[i]; osgData += spaces + " " + v.x + " " + v.y + "\n"; } osgData += spaces + "}\n"; } if (mesh.vertexUV2.Length > 0) { osgData += spaces + "TexCoordArray 1 Vec2Array " + mesh.vertexCount + " {\n"; for (int i = 0; i < mesh.vertexCount; ++i) { Vector2 v = mesh.vertexUV2[i]; osgData += spaces + " " + v.x + " " + v.y + "\n"; } osgData += spaces + "}\n"; } // TODO: tangents & bones return(osgData); }
private static string ExportHierarchy(ref SceneData sceneData, SceneGameObject gameObj, int indent, float progressStart, float progressAll) { int needGlobalNodeType = -1; if (gameObj.components.Count <= 0) { return(""); } string osgData = "", osgSubData = "", spaces = ""; for (int i = 0; i < indent; ++i) { spaces += " "; } // Check the main component type as the node type SceneComponent mainComponent = gameObj.components[0]; if (mainComponent.type == "Transform") { SceneTransform st = (SceneTransform)mainComponent; osgData = spaces + "MatrixTransform {\n" + spaces + " referenceFrame RELATIVE\n" + spaces + " Matrix {\n"; needGlobalNodeType = 0; // FIXME: hould convert left-handed to right-handed coordinates Matrix4x4 m = Matrix4x4.TRS(st.localPosition, st.localRotation, st.localScale); osgData += spaces + " " + m[0, 0] + " " + m[1, 0] + " " + m[2, 0] + " " + m[3, 0] + "\n" + spaces + " " + m[0, 1] + " " + m[1, 1] + " " + m[2, 1] + " " + m[3, 1] + "\n" + spaces + " " + m[0, 2] + " " + m[1, 2] + " " + m[2, 2] + " " + m[3, 2] + "\n" + spaces + " " + m[0, 3] + " " + m[1, 3] + " " + m[2, 3] + " " + m[3, 3] + "\n" + spaces + " }\n"; } else { Debug.LogWarning("[UnityToSceneBundle] Unknown main component type: " + mainComponent.type); } if (needGlobalNodeType < 0) { osgData = spaces + "Node {\n"; } osgData += ExportCommonAttr(gameObj.name, spaces, true) + spaces + " num_children "; // Traverse all components to add them to main component type string subSpaces = spaces + " "; int numChildren = gameObj.children.Count; for (int i = 1; i < gameObj.components.Count; ++i) { SceneComponent component = gameObj.components[i]; if (component.type == "Light") { SceneLight sl = (SceneLight)component; osgSubData += spaces + " nwTools::LightData {\n" + subSpaces + "Type " + sl.lightType + "\n" + subSpaces + "Color " + sl.color.r + " " + sl.color.g + " " + sl.color.b + "\n" + subSpaces + "Range " + sl.range + "\n" + subSpaces + "Realtime " + (sl.realtime ? 1 : 0) + " " + (sl.castsShadows ? 1 : 0) + "\n" + spaces + " }\n"; numChildren++; } else if (component.type == "Camera") { SceneCamera sc = (SceneCamera)component; osgSubData += spaces + " nwTools::CameraData {\n" // TODO + spaces + " }\n"; numChildren++; } else if (component.type == "BoxCollider") { //SceneBoxCollider sbc = (SceneBoxCollider)component; // TODO } else if (component.type == "MeshCollider") { //SceneMeshCollider smc = (SceneMeshCollider)component; // TODO } else if (component.type == "ParticleSystem") { SceneParticleSystem sps = (SceneParticleSystem)component; osgSubData += spaces + " nwTools::ParticleSystem {\n" + ExportCommonAttr(sps.type, spaces + " ", false) + ParticleExporter.ExportParticle(ref sceneData, ref sps, subSpaces) + spaces + " }\n"; numChildren++; } else if (component.type == "Terrain") { SceneTerrain st = (SceneTerrain)component; osgSubData += spaces + " Geode {\n" + ExportCommonAttr(st.type, spaces + " ", true) + subSpaces + "num_drawables 1\n"; osgSubData += subSpaces + "nwTools::Terrain {\n" + TerrainExporter.ExportTerrain(ref sceneData, ref st, subSpaces + " ") + subSpaces + "}\n"; osgSubData += spaces + " }\n"; numChildren++; } else if (component.type == "MeshRenderer") { SceneMeshRenderer smr = (SceneMeshRenderer)component; osgSubData += spaces + " Geode {\n" + ExportCommonAttr(smr.type, spaces + " ", true) + subSpaces + "num_drawables 1\n"; SceneMesh mesh = sceneData.resources.GetMesh(smr.mesh); osgSubData += subSpaces + "Geometry {\n" + MeshExporter.ExportGeometry(ref sceneData, ref smr, ref mesh, subSpaces + " ") + subSpaces + "}\n"; osgSubData += spaces + " }\n"; numChildren++; } else if (component.type == "SkinnedMeshRenderer") { SceneSkinnedMeshRenderer smr = (SceneSkinnedMeshRenderer)component; osgSubData += spaces + " Geode {\n" + ExportCommonAttr(smr.type, spaces + " ", true) + subSpaces + "num_drawables 1\n"; SceneMesh mesh = sceneData.resources.GetMesh(smr.mesh); osgSubData += subSpaces + "Geometry {\n" + MeshExporter.ExportSkinnedGeometry(ref sceneData, ref smr, ref mesh, subSpaces + " ") + subSpaces + "}\n"; osgSubData += spaces + " }\n"; numChildren++; } else { Debug.LogWarning("[UnityToSceneBundle] Unknown sub-component type: " + component.type); } } osgData += numChildren + "\n" + osgSubData; // Traverse all child objects float numHierarchy = (float)gameObj.children.Count, numDone = 0.0f; foreach (SceneGameObject childObj in gameObj.children) { osgData += ExportHierarchy(ref sceneData, childObj, indent + 2, 0.0f, 0.0f); numDone += 1.0f; if (progressAll > 0.0f) { float progress = (progressStart + numDone / numHierarchy) / progressAll; EditorUtility.DisplayProgressBar("Scene Bundler", "Exporting hierarchy...", progress); } } osgData += spaces + "}\n"; return(osgData); }