Beispiel #1
0
        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);
        }
Beispiel #2
0
        public override SceneComponent GetObjectData()
        {
            var sceneData = new SceneMeshRenderer();

            sceneData.type                 = "MeshRenderer";
            sceneData.mesh                 = mesh.name;
            sceneData.enabled              = unityMeshRenderer.enabled;
            sceneData.castShadows          = (unityMeshRenderer.shadowCastingMode != UnityEngine.Rendering.ShadowCastingMode.Off);
            sceneData.receiveShadows       = unityMeshRenderer.receiveShadows;
            sceneData.lightmapIndex        = unityMeshRenderer.lightmapIndex;
            sceneData.lightmapTilingOffset = unityMeshRenderer.lightmapScaleOffset;

            sceneData.materials = new string[materials.Count];
            for (int i = 0; i < materials.Count; i++)
            {
                sceneData.materials[i] = materials[i].name;
            }
            return(sceneData);
        }
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);
        }
Beispiel #4
0
        public static string ExportStateSet(ref SceneData sceneData, ref SceneMeshRenderer smr, string spaces)
        {
            string osgData = spaces + "StateSet {\n" + ExportStateSetAttr(false, spaces);

            for (int i = 0; i < smr.materials.Length; ++i)
            {
                SceneMaterial material = sceneData.resources.GetMaterial(smr.materials[i]);
                if (material.textureIDs == null)
                {
                    continue;
                }

                string shaderData = spaces + "  nwTools::ShaderData {\n"
                                    + spaces + "    ShaderName \"" + material.shader + "\"\n";
                for (int j = 0; j < material.textureIDs.Length; ++j)
                {
                    int          texID = material.textureIDs[j], unit = material.textureUnits[j];
                    SceneTexture texture = sceneData.resources.GetTexture(texID, false);
                    if (texture == null || unit < 0)
                    {
                        continue;
                    }

                    shaderData += spaces + "    Texture" + unit + " \"" + texture.name + "\""
                                  + " \"" + texture.path + "\"\n";
                    if (i > 0)
                    {
                        continue;     // For multi-material case, record more materials to ShaderData
                    }
                    // Handle texture tiling and offset
                    osgData += spaces + "  textureUnit " + unit + " {\n"
                               + spaces + "    GL_TEXTURE_2D ON\n";
                    if (material.textureTilingOffsets[j] != indentityTilingOffsetVector)
                    {
                        Vector4   off = material.textureTilingOffsets[j];
                        Matrix4x4 m   = Matrix4x4.TRS(new Vector3(off.z, off.w, 0.0f), Quaternion.identity,
                                                      new Vector3(off.x, off.y, 1.0f));
                        osgData += spaces + "    TexMat {\n"
                                   + 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";
                    }

                    // Handle texture
                    if (sharedTextureNames.ContainsKey(texID))
                    {
                        osgData += spaces + "    Use " + sharedTextureNames[texID] + "\n";
                    }
                    else
                    {
                        sharedTextureNames[texID] = "Texture_" + texID;
                        osgData += spaces + "    Texture2D {\n"
                                   + spaces + "      UniqueID Texture_" + texID + "\n"
                                   + ExportTextureAttr(ref texture, spaces + "    ")
                                   + spaces + "    }\n";
                    }
                    osgData += spaces + "  }\n";
                }

                // Save shader data for use
                if (material.shaderKeywords != null)
                {
                    shaderData += spaces + "    Keywords ";
                    for (int k = 0; k < material.shaderKeywords.Length; ++k)
                    {
                        shaderData += material.shaderKeywords[k]
                                      + ((k < material.shaderKeywords.Length - 1) ? " " : "\n");
                    }
                }
                osgData += shaderData + spaces + "  }\n";
            }

            // Handle lightmaps
            if (smr.lightmapIndex >= 0)
            {
                SceneTexture texture = sceneData.resources.lightmaps[smr.lightmapIndex];
                if (texture != null)
                {
                    osgData += spaces + "  textureUnit 1 {\n" // FIXME: always 1?
                               + spaces + "    GL_TEXTURE_2D ON\n";

                    // Handle lightmap tiling and offset
                    if (smr.lightmapTilingOffset != indentityTilingOffsetVector)
                    {
                        Vector4   off = smr.lightmapTilingOffset;
                        Matrix4x4 m   = Matrix4x4.TRS(new Vector3(off.z, off.w, 0.0f), Quaternion.identity,
                                                      new Vector3(off.x, off.y, 1.0f));
                        osgData += spaces + "    TexMat {\n"
                                   + 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";
                    }

                    // Handle texture
                    if (sharedTextureNames.ContainsKey(texture.uniqueID))
                    {
                        osgData += spaces + "    Use " + sharedTextureNames[texture.uniqueID] + "\n";
                    }
                    else
                    {
                        sharedTextureNames[texture.uniqueID] = "Texture_" + texture.uniqueID;
                        osgData += spaces + "    Texture2D {\n"
                                   + spaces + "      UniqueID Texture_" + texture.uniqueID + "\n"
                                   + ExportTextureAttr(ref texture, spaces + "    ")
                                   + spaces + "    }\n";
                    }
                    osgData += spaces + "  }\n";
                }
            }
            osgData += spaces + "}\n";
            return(osgData);
        }