コード例 #1
0
ファイル: BundleLightmap.cs プロジェクト: yxred/UnityToOSG
        // TODO: we also want to copy the exr data in case user wants it
        new public static void Preprocess()
        {
            LightmapData[] lightmaps = LightmapSettings.lightmaps;
            for (int i = 0; i < lightmaps.Length; i++)
            {
                var lightmap = lightmaps[i].lightmapFar;
                if (lightmap == null)
                {
                    lightmap = lightmaps[i].lightmapNear;
                }

                string path = AssetDatabase.GetAssetPath(lightmap);
                if (path == "")
                {
                    string   currentScenePath = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().path;
                    string[] parts            = currentScenePath.Split('/', '\\');
                    string   sceneName        = parts[parts.Length - 1].Split('.')[0];
                    string   lightmapPath     = Path.GetDirectoryName(currentScenePath) + "/" + sceneName + "/";
                    path = lightmapPath + "Lightmap-" + i + "_comp_light.exr";
                }

                /*
                 * TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
                 * TextureImporterSettings settings = new TextureImporterSettings();
                 * textureImporter.ReadTextureSettings( settings );
                 *
                 * bool setReadable = false;
                 * if ( !settings.readable ) setReadable = true;
                 * settings.readable = true;
                 * settings.lightmap = true;
                 * textureImporter.SetTextureSettings( settings );
                 * AssetDatabase.ImportAsset( path, ImportAssetOptions.ForceUpdate );
                 *
                 * var pixels = lightmap.GetPixels32();
                 * Texture2D ntexture = new Texture2D(lightmap.width, lightmap.height, TextureFormat.ARGB32, false);
                 * ntexture.SetPixels32( pixels );
                 * ntexture.Apply();
                 *
                 * var bytes = ntexture.EncodeToPNG();
                 * UnityEngine.Object.DestroyImmediate( ntexture );
                 * if ( setReadable ) settings.readable = false;
                 *
                 * settings.lightmap = true;
                 * textureImporter.SetTextureSettings(settings);
                 * AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
                 */
                SceneTexture lm = new SceneTexture();
                lm.name     = BundleScene.sceneName + "_Lightmap_" + i;
                lm.path     = path;
                lm.uniqueID = lightmap.GetInstanceID();
                //lm.base64PNGLength = bytes.Length;
                //lm.base64PNG =  System.Convert.ToBase64String(bytes, 0, bytes.Length);
                allLightmaps.Add(lm);
            }
        }
コード例 #2
0
ファイル: BundleTexture.cs プロジェクト: yxred/UnityToOSG
        public new SceneTexture GetObjectData()
        {
            var sceneData = new SceneTexture();

            sceneData.uniqueID = uniqueID;
            sceneData.name     = name;
            sceneData.path     = path;
            //sceneData.base64PNG = base64PNG;
            //sceneData.base64PNGLength = base64PNGLength;
            return(sceneData);
        }
コード例 #3
0
ファイル: BundleLightmap.cs プロジェクト: yxred/UnityToOSG
        public static List <SceneTexture> GenerateObjectList()
        {
            List <SceneTexture> lightmaps = new List <SceneTexture>();

            for (int i = 0; i < allLightmaps.Count; i++)
            {
                SceneTexture lightmap  = allLightmaps[i];
                SceneTexture jlightmap = new SceneTexture();
                jlightmap.name            = lightmap.name;
                jlightmap.path            = lightmap.path;
                jlightmap.uniqueID        = lightmap.uniqueID;
                jlightmap.base64PNG       = lightmap.base64PNG;
                jlightmap.base64PNGLength = lightmap.base64PNGLength;
                lightmaps.Add(jlightmap);
            }
            return(lightmaps);
        }
コード例 #4
0
ファイル: ExportMeterial.cs プロジェクト: yxred/UnityToOSG
        public static string ExportTextureAttr(ref SceneTexture texture, string spaces)
        {
            string osgData = spaces + "  DataVariance STATIC\n"
                             + spaces + "  name \"" + texture.name + "\"\n"
                             + spaces + "  file \"" + texture.path + "\"\n"
                             + spaces + "  wrap_s REPEAT\n"
                             + spaces + "  wrap_t REPEAT\n"
                             + spaces + "  wrap_r REPEAT\n"
                             + spaces + "  min_filter LINEAR_MIPMAP_LINEAR\n"
                             + spaces + "  mag_filter LINEAR\n"
                             + spaces + "  maxAnisotropy 1\n"
                             + spaces + "  borderColor 0 0 0 0\n"
                             + spaces + "  borderWidth 0\n"
                             + spaces + "  useHardwareMipMapGeneration TRUE\n"
                             + spaces + "  unRefImageDataAfterApply TRUE\n"
                             + spaces + "  internalFormatMode USE_IMAGE_DATA_FORMAT\n"
                             + spaces + "  resizeNonPowerOfTwo TRUE\n";

            return(osgData);
        }
コード例 #5
0
ファイル: ExportParticle.cs プロジェクト: yxred/UnityToOSG
        public static string ExportParticle(ref SceneData sceneData, ref SceneParticleSystem sps, string spaces)
        {
            string osgData = spaces + "Duration " + sps.duration + "\n"
                             + spaces + "Playing " + sps.playingSpeed + " " + (sps.isLooping ? 1 : 0)
                             + " " + (sps.isAutoStarted ? 1 : 0) + "\n"
                             + spaces + "MaxParticles " + sps.maxParticles + "\n"
                             + spaces + "Gravity " + sps.gravity.x + " " + sps.gravity.y + " " + sps.gravity.z + "\n"
                             + spaces + "Rotation " + sps.rotation.x + " " + sps.rotation.y + " " + sps.rotation.z + "\n"
                             + spaces + "StartAttributes " + sps.startAttributes.x + " " + sps.startAttributes.y + " "
                             + sps.startAttributes.z + " " + sps.startAttributes.w + "\n"
                             + spaces + "StartColor " + sps.startColor.r + " " + sps.startColor.g + " "
                             + sps.startColor.b + " " + sps.startColor.a + "\n";

            for (int i = 0; i < sps.enabledModules.Length; ++i)
            {
                string moduleName = sps.enabledModules[i];
                osgData += spaces + moduleName + " {\n";
                if (moduleName == "Emission")
                {
                    osgData += spaces + "  Type " + sps.emissionType + "\n"
                               + spaces + "  Rate " + sps.emissionRate.Length + " {\n";
                    for (int j = 0; j < sps.emissionRate.Length; ++j)
                    {
                        Vector4 v = sps.emissionRate[j];
                        osgData += spaces + "    " + v.x + " " + v.y + " " + v.z + " " + v.w + "\n";
                    }
                    osgData += spaces + "  }\n";
                }
                else if (moduleName == "TextureSheetAnimation")
                {
                    osgData += spaces + "  Type " + sps.tsaAnimationType + "\n"
                               + spaces + "  Tiles " + sps.tsaNumTiles.x + " " + sps.tsaNumTiles.y + "\n"
                               + spaces + "  CycleCount " + sps.tsaCycleCount + "\n"
                               + spaces + "  FrameOverTime " + sps.tsaFrameOverTime.Length + " {\n";
                    for (int j = 0; j < sps.tsaFrameOverTime.Length; ++j)
                    {
                        Vector4 v = sps.tsaFrameOverTime[j];
                        osgData += spaces + "    " + v.x + " " + v.y + " " + v.z + " " + v.w + "\n";
                    }
                    osgData += spaces + "  }\n";
                }
                else if (moduleName == "Renderer")
                {
                    osgData += spaces + "  ShapeMode " + sps.renderShapeMode + "\n"
                               + spaces + "  SortMode " + sps.renderSortMode + "\n"
                               + spaces + "  Attributes " + sps.renderAttributes.x + " " + sps.renderAttributes.y + " "
                               + sps.renderAttributes.z + " " + sps.renderAttributes.w + "\n";

                    SceneMaterial material = sceneData.resources.GetMaterial(sps.renderMaterial);
                    osgData += spaces + "  Material " + material.textureIDs.Length + " {\n";
                    for (int j = 0; j < material.textureIDs.Length; ++j)
                    {
                        SceneTexture texture = sceneData.resources.GetTexture(material.textureIDs[j], false);
                        if (texture == null)
                        {
                            continue;
                        }

                        Vector4 off = material.textureTilingOffsets[j];
                        osgData += spaces + "    Texture" + j + " \"" + texture.name + "\""
                                   + " \"" + texture.path + "\"\n"
                                   + spaces + "    TilingOffset" + j + " " + off.x + " " + off.y
                                   + " " + off.z + " " + off.w + "\n";
                    }
                    osgData += spaces + "  }\n";
                }
                osgData += spaces + "}\n";
            }
            return(osgData);
        }
コード例 #6
0
ファイル: ExportTerrain.cs プロジェクト: yxred/UnityToOSG
        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);
        }
コード例 #7
0
ファイル: ExportMeterial.cs プロジェクト: yxred/UnityToOSG
        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);
        }