Beispiel #1
0
        public override SceneComponent GetObjectData()
        {
            var sceneData = new SceneTerrain();

            sceneData.type            = "Terrain";
            sceneData.heightmapHeight = heightmapHeight;
            sceneData.heightmapWidth  = heightmapWidth;
            sceneData.size            = size;
            sceneData.alphamapWidth   = alphamapWidth;
            sceneData.alphamapHeight  = alphamapHeight;
            sceneData.alphamapLayers  = alphamapLayers;

            sceneData.heightmapTexture                 = new SceneTexture();
            sceneData.heightmapTexture.base64PNG       = base64Height;
            sceneData.heightmapTexture.base64PNGLength = base64HeightLength;

            sceneData.alphamapTexture                 = new SceneTexture();
            sceneData.alphamapTexture.base64PNG       = base64Alpha;
            sceneData.alphamapTexture.base64PNGLength = base64AlphaLength;

            if (textures.Count > 0)
            {
                sceneData.textureIDs = new int[textures.Count];
                for (int i = 0; i < textures.Count; i++)
                {
                    sceneData.textureIDs[i] = textures[i].uniqueID;
                }

                sceneData.textureTilingOffsets = new Vector4[textures.Count];
                for (int i = 0; i < textures.Count; i++)
                {
                    Vector2 sc = textureSizes[i], of = textureOffsets[i];
                    sceneData.textureTilingOffsets[i] = new Vector4(sc.x, sc.y, of.x, of.y);
                }
            }

            if (terrain != null)
            {
                sceneData.lightmapIndex        = terrain.lightmapIndex;
                sceneData.lightmapTilingOffset = terrain.lightmapScaleOffset;
            }
            return(sceneData);
        }
Beispiel #2
0
        public static string ExportTerrain(ref SceneData sceneData, ref SceneTerrain st, string spaces)
        {
            string osgData = spaces + "Size " + st.size.x + " " + st.size.y + " " + st.size.z + "\n";

            // Handle heightmap data
            osgData += spaces + "HeightMap " + st.heightmapWidth + " " + st.heightmapHeight + " {\n";
            byte[] heightData            = System.Convert.FromBase64String(st.heightmapTexture.base64PNG);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int y = 0; y < st.heightmapHeight; y++)
            {
                sb.Append(spaces + "  ");
                for (int x = 0; x < st.heightmapWidth; x++)
                {
                    int index = (y * st.heightmapWidth) + x;
                    sb.Append(System.BitConverter.ToSingle(heightData, index * 4) + " ");
                }
                sb.Append("\n");
            }
            osgData += sb.ToString() + spaces + "}\n";

            // Handle alphamap layers
            osgData += spaces + "AlphaMap " + st.alphamapWidth + " " + st.alphamapHeight
                       + " " + st.alphamapLayers + " {\n";
            byte[] alphaData = System.Convert.FromBase64String(st.alphamapTexture.base64PNG);
            System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
            for (int i = 0; i < st.alphamapLayers; i++)
            {
                sb2.Append(spaces + "  Layer " + i + " {\n");
                for (int y = 0; y < st.alphamapHeight; y++)
                {
                    sb2.Append(spaces + "    ");
                    for (int x = 0; x < st.alphamapWidth; x++)
                    {
                        int index = i * (st.alphamapHeight * st.alphamapWidth) + (y * st.alphamapWidth) + x;
                        sb2.Append(System.BitConverter.ToSingle(alphaData, index * 4) + " ");
                    }
                    sb2.Append("\n");
                }
                sb2.Append(spaces + "  }\n");
            }
            osgData += sb2.ToString() + spaces + "}\n";

            // Handle all splat textures
            for (int i = 0; i < st.textureIDs.Length; i++)
            {
                int          texID   = st.textureIDs[i];
                SceneTexture texture = sceneData.resources.GetTexture(texID, false);
                if (texture == null)
                {
                    continue;
                }

                Vector4 off = st.textureTilingOffsets[i];
                osgData += spaces + "Splat" + i + " \"" + texture.name + "\""
                           + " \"" + texture.path + "\"\n";
                osgData += spaces + "SplatTilingOffset" + i + " "
                           + off.x + " " + off.y + " " + off.z + " " + off.w + "\n";
            }

            // Handle lightmaps
            if (st.lightmapIndex >= 0)
            {
                SceneTexture texture = sceneData.resources.lightmaps[st.lightmapIndex];
                if (texture != null)
                {
                    osgData += spaces + "Lightmap \"" + texture.name + "\""
                               + " \"" + texture.path + "\"\n";
                    osgData += spaces + "LightmapTilingOffset " + st.lightmapTilingOffset.x + " "
                               + st.lightmapTilingOffset.y + " " + st.lightmapTilingOffset.z + " "
                               + st.lightmapTilingOffset.w + "\n";
                }
            }
            return(osgData);
        }
Beispiel #3
0
        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);
        }