Пример #1
0
        private AssetImage TryAddTexture(Texture2D tex)
        {
            List <AssetImage> images = room.AssetImages;
            AssetImage        image  = images.FirstOrDefault(c => c.Texture == tex);

            if (image == null)
            {
                return(AddTexture(tex));
            }
            return(image);
        }
Пример #2
0
 private void UpdateTextureFormat(ExportTextureFormat format, AssetImage asset, string extension)
 {
     if (string.IsNullOrEmpty(extension))
     {
         asset.src = asset.src + JanusUtil.GetImageExtension(format);
     }
     else
     {
         asset.src = asset.src + extension;
     }
 }
Пример #3
0
        private AssetImage AddTexture(Texture2D tex)
        {
            AssetImage image = new AssetImage();

            image.id      = tex.name;
            image.src     = tex.name;
            image.Texture = tex;
            image.Created = true;
            room.AddAssetImage(image);

            return(image);
        }
Пример #4
0
        private AssetImage AddFakeTexture(string name)
        {
            name = "SkyBox" + name;

            AssetImage image = new AssetImage();

            image.id      = name;
            image.src     = name;
            image.Created = true;
            room.AddAssetImage(image);

            return(image);
        }
Пример #5
0
        public void ProcessTextures()
        {
            LightmapExportType    lightmapExportType    = room.LightmapType;
            LightmapTextureFormat lightmapTextureFormat = room.LightmapTextureFormat;
            ExportTextureFormat   format = room.TextureFormat;

            // export everything that has already been loaded
            List <AssetImage> assetImages = room.AssetImages;

            for (int i = 0; i < assetImages.Count; i++)
            {
                AssetImage image = assetImages[i];
                if (image.Texture || (room.ExportOnlyHtml && image.Created))
                {
                    if (image.Texture)
                    {
                        EditorUtility.DisplayProgressBar("Exporting main textures...", image.Texture.name, i / (float)assetImages.Count);
                    }
                    ExportTexture(image.Texture, format, image, true);
                }
            }

            switch (lightmapExportType)
            {
            case LightmapExportType.BakedMaterial:
                ProcessLightmapsBaked();
                break;

            case LightmapExportType.Packed:
            {
                switch (lightmapTextureFormat)
                {
                case LightmapTextureFormat.EXR:
                    ProcessLightmapsPackedSourceEXR();
                    break;

                default:
                    ProcessLightmapsPacked();
                    break;
                }
            }
            break;

            case LightmapExportType.Unpacked:
                ProcessLightmapsUnpacked();
                break;
            }
        }
Пример #6
0
        private AssetImage GenerateCmftIrrad(int size, Cubemap cubemap, string forceName = "")
        {
            string path = AssetDatabase.GetAssetPath(cubemap);

            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }

            // remove assets
            path = path.Remove(0, path.IndexOf('/'));

            string appPath  = Application.dataPath;
            string fullPath = appPath + path;
            string name     = forceName;

            if (string.IsNullOrEmpty(name))
            {
                name = cubemap.name + "_irradiance";
            }

            AssetImage data = new AssetImage();

            data.id  = name;
            data.src = name + ".dds";
            room.AddAssetImage(data);

            if (room.ExportOnlyHtml)
            {
                return(data);
            }

            string exportPath = room.RootFolder;
            string irradPath  = Path.Combine(exportPath, name);

            string cmd = "--input \"" + fullPath
                         + "\" --srcFaceSize " + cubemap.width + " --dstFaceSize" + size
                         + " --filter irradiance --outputNum 1 --output0 \""
                         + irradPath + "\" --output0params dds,bgra8,cubemap";

            CMFT.CmftInterop.DoExecute(cmd);

            return(data);
        }
Пример #7
0
        private void ExportTexture(Texture2D texture, ExportTextureFormat format, AssetImage asset, bool canCopy)
        {
            // force PNG if alpha??
            string sourcePath      = AssetDatabase.GetAssetPath(asset.Texture);
            string sourceExtension = Path.GetExtension(sourcePath);
            bool   forceConversion = room.TextureForceReExport && !JanusUtil.IgnoreReExport(sourcePath);

            string rootDir = room.RootFolder;

            if (!forceConversion && canCopy && JanusUtil.SupportsImageFormat(sourceExtension))
            {
                asset.src = asset.src + sourceExtension;
                if (room.ExportOnlyHtml)
                {
                    return;
                }
                string texPath = Path.Combine(rootDir, asset.src);
                File.Copy(sourcePath, texPath, true);
            }
            else
            {
                try
                {
                    asset.src = asset.src + JanusUtil.GetImageExtension(format);
                    if (room.ExportOnlyHtml)
                    {
                        return;
                    }
                    string texPath = Path.Combine(rootDir, asset.src);

                    using (Stream output = File.OpenWrite(texPath))
                    {
                        TempTextureData data = TextureUtil.LockTexture(texture, sourcePath);
                        TextureUtil.ExportTexture(texture, output, format, room.TextureData, false);
                        TextureUtil.UnlockTexture(data);
                    }
                }
                catch
                {
                    Debug.LogError("Failure exporting texture " + asset.id);
                }
            }
        }
Пример #8
0
        private void ProcessLightmapsPackedSourceEXR()
        {
            if (room.ExportOnlyHtml)
            {
                foreach (var lightPair in lightmapped)
                {
                    int    id    = lightPair.Key;
                    string imgId = "Lightmap" + id;
                    UpdateTextureFormat(ExportTextureFormat.PNG, room.GetTexture(imgId), ".exr");
                }
                return;
            }

            string lightMapsFolder = UnityUtil.GetLightmapsFolder();

            foreach (var lightPair in lightmapped)
            {
                int id = lightPair.Key;
                List <RoomObject> toRender  = lightPair.Value;
                AssetImage        lmapImage = room.TryGetTexture("Lightmap" + id);

                // get the path to the lightmap file
                string    lightMapFile = Path.Combine(lightMapsFolder, "Lightmap-" + id + "_comp_light.exr");
                Texture2D texture      = AssetDatabase.LoadAssetAtPath <Texture2D>(lightMapFile);
                if (texture == null)
                {
                    continue;
                }

                string sourcePath      = AssetDatabase.GetAssetPath(texture);
                string sourceExtension = Path.GetExtension(sourcePath);
                string rootDir         = room.RootFolder;

                lmapImage.src = lmapImage.src + sourceExtension;
                string texPath = Path.Combine(rootDir, lmapImage.src);
                File.Copy(sourcePath, texPath, true);
            }
        }
Пример #9
0
        private AssetImage GenerateCmftRad(int res, Cubemap cubemap, string forceName = "")
        {
            string path = AssetDatabase.GetAssetPath(cubemap);

            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }

            // remove assets
            path = path.Remove(0, path.IndexOf('/'));

            string appPath  = Application.dataPath;
            string fullPath = appPath + path;
            string name     = forceName;

            if (string.IsNullOrEmpty(name))
            {
                name = cubemap.name + "_radiance";
            }

            // we know the format were exporting, so we set it now
            AssetImage data = new AssetImage();

            data.id  = name;
            data.src = name + ".dds";
            room.AddAssetImage(data);

            if (room.ExportOnlyHtml)
            {
                return(data);
            }

            string exportPath = room.RootFolder;
            string radPath    = Path.Combine(exportPath, name);

            string dstFaceSize             = res.ToString(CultureInfo.InvariantCulture);// "256";
            string excludeBase             = "false";
            string mipCount                = "9";
            string glossScale              = "10";
            string glossBias               = "1";
            string lightingModel           = "phongbrdf";
            string inputGammaNumerator     = "1.0";
            string inputGammaDenominator   = "1.0";
            string outputGammaNumerator    = "1.0";
            string outputGammaDenominator  = "1.0";
            string generateMipChain        = "false";
            string numCpuProcessingThreads = "1";


            StringBuilder builder = new StringBuilder();

            builder.Append("--input \"" + fullPath + "\"");
            builder.Append(" --srcFaceSize " + cubemap.width);
            builder.Append(" --filter radiance");
            builder.Append(" --dstFaceSize " + dstFaceSize);
            builder.Append(" --excludeBase " + excludeBase);
            builder.Append(" --mipCount " + mipCount);
            builder.Append(" --glossBias " + glossBias);
            builder.Append(" --glossScale " + glossScale);
            builder.Append(" --lightingModel " + lightingModel);
            builder.Append(" --numCpuProcessingThreads " + numCpuProcessingThreads);
            builder.Append(" --inputGammaNumerator " + inputGammaNumerator);
            builder.Append(" --inputGammaDenominator " + inputGammaDenominator);
            builder.Append(" --outputGammaNumerator " + outputGammaNumerator);
            builder.Append(" --outputGammaDenominator " + outputGammaDenominator);
            builder.Append(" --generateMipChain " + generateMipChain);
            builder.Append(" --outputNum 1");
            builder.Append(" --output0 \"" + radPath + "\"");
            builder.Append(" --output0params dds,bgra8,cubemap");
            string cmd = builder.ToString();

            // we refer by namespace so Unity never really imports CMFT on Unity 5.0
            CMFT.CmftInterop.DoExecute(cmd);


            return(data);
        }
Пример #10
0
        private void ProcessLightmapsPacked()
        {
            ExportTextureFormat format = room.TextureFormat;

            if (room.ExportOnlyHtml)
            {
                foreach (var lightPair in lightmapped)
                {
                    int    id    = lightPair.Key;
                    string imgId = "Lightmap" + id;
                    UpdateTextureFormat(format, room.GetTexture(imgId), "");
                }
                return;
            }

            string lightMapsFolder = UnityUtil.GetLightmapsFolder();

            Shader exposureShader = Shader.Find("Hidden/ExposureShader");

            JanusUtil.AssertShader(exposureShader);

            Material exposureMat = new Material(exposureShader);

            exposureMat.SetPass(0);
            exposureMat.SetFloat("_RelFStops", room.LightmapRelFStops);
            exposureMat.SetFloat("_IsLinear", PlayerSettings.colorSpace == ColorSpace.Linear ? 1 : 0);

            foreach (var lightPair in lightmapped)
            {
                int        id        = lightPair.Key;
                AssetImage lmapImage = room.TryGetTexture("Lightmap" + id);
                if (lmapImage == null)
                {
                    continue;
                }

                // get the path to the lightmap file
                string    lightMapFile = Path.Combine(lightMapsFolder, "Lightmap-" + id + "_comp_light.exr");
                Texture2D texture      = AssetDatabase.LoadAssetAtPath <Texture2D>(lightMapFile);
                if (texture == null)
                {
                    continue;
                }

                exposureMat.SetTexture("_InputTex", texture);

                // We need to access unity_Lightmap_HDR to decode the lightmap,
                // but we can't, so we have to render everything to a custom RenderTexture!
                Texture2D decTex = new Texture2D(texture.width, texture.height);
                decTex.name = "Lightmap" + id;
                //texturesExported.Add(decTex);

                RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height);
                Graphics.SetRenderTarget(renderTexture);
                GL.Clear(true, true, new Color(0, 0, 0, 0)); // clear to transparent

                exposureMat.SetPass(0);
                Graphics.DrawMeshNow(JanusResources.PlaneMesh, Matrix4x4.identity);

                decTex.ReadPixels(new Rect(0, 0, decTex.width, decTex.height), 0, 0);

                Graphics.SetRenderTarget(null);
                RenderTexture.ReleaseTemporary(renderTexture);

                // save the lightmap file
                ExportTexture(decTex, format, lmapImage, false);
                UObject.DestroyImmediate(decTex);
            }
            UObject.DestroyImmediate(exposureMat);
        }
Пример #11
0
        public void PreProcessObject(MeshRenderer renderer, Mesh mesh, AssetObject obj, RoomObject rObj, bool assignLightmapScale)
        {
            LightmapExportType  lightmapExportType = room.LightmapType;
            ExportTextureFormat format             = room.TextureFormat;

            if (lightmapExportType != LightmapExportType.None)
            {
                int lightMap = renderer.lightmapIndex;
                if (lightMap != -1)
                {
                    // Register mesh for lightmap render
                    List <RoomObject> toRender;
                    if (!lightmapped.TryGetValue(lightMap, out toRender))
                    {
                        toRender = new List <RoomObject>();
                        lightmapped.Add(lightMap, toRender);
                    }

                    toRender.Add(rObj);

                    if (assignLightmapScale)
                    {
                        if (lightmapExportType == LightmapExportType.Packed)
                        {
                            Vector4 lmap = renderer.lightmapScaleOffset;
                            lmap.x = Mathf.Clamp(lmap.x, -2, 2);
                            lmap.y = Mathf.Clamp(lmap.y, -2, 2);
                            lmap.z = Mathf.Clamp(lmap.z, -2, 2);
                            lmap.w = Mathf.Clamp(lmap.w, -2, 2);
                            rObj.SetLightmap(lmap);
                        }
                    }

                    if (lightmapExportType != LightmapExportType.BakedMaterial)
                    {
                        // check if we already have the texture
                        string     lmapId    = "Lightmap" + lightMap;
                        AssetImage lmapImage = room.TryGetTexture(lmapId);
                        if (lmapImage == null)
                        {
                            lmapImage     = new AssetImage();
                            lmapImage.id  = lmapId;
                            lmapImage.src = lmapId;
                            room.AddAssetImage(lmapImage);
                        }
                        rObj.lmap_id = lmapImage.id;
                    }
                }
            }

            switch (lightmapExportType)
            {
            case LightmapExportType.BakedMaterial:
            case LightmapExportType.Unpacked:
            {
                AssetImage image = new AssetImage();
                string     imgId = obj.id + "_Baked";

                image.id  = imgId;
                image.src = imgId;

                rObj.image_id = image.id;

                room.AddAssetImage(image);
            }
            break;
            }

            if (lightmapExportType != LightmapExportType.BakedMaterial &&
                room.ExportMaterials)
            {
                // search for textures/color on object
                Texture2D texture  = null;
                Color     objColor = Color.white;

                Material[] mats = renderer.sharedMaterials;
                for (int j = 0; j < mats.Length; j++)
                {
                    Material mat = mats[j];

                    Vector2 sca = mat.mainTextureScale;
                    Vector2 off = mat.mainTextureOffset;
                    if (sca != Vector2.one || off != Vector2.zero)
                    {
                        rObj.tiling = JanusUtil.FormatVector4(new Vector4(sca.x, sca.y, off.x, off.y));
                    }

                    Shader shader = mat.shader;
                    int    props  = ShaderUtil.GetPropertyCount(shader);
                    for (int k = 0; k < props; k++)
                    {
                        string name = ShaderUtil.GetPropertyName(shader, k);

                        ShaderUtil.ShaderPropertyType propType = ShaderUtil.GetPropertyType(shader, k);
                        if (propType == ShaderUtil.ShaderPropertyType.TexEnv)
                        {
                            if (JanusGlobals.SemanticsMainTex.Contains(name.ToLower()))
                            {
                                // main texture texture
                                Texture matTex = mat.GetTexture(name);
                                if (matTex is Texture2D)
                                {
                                    texture = (Texture2D)matTex;
                                }
                            }
                        }
                        else if (propType == ShaderUtil.ShaderPropertyType.Color)
                        {
                            if (JanusGlobals.SemanticsColor.Contains(name.ToLower()))
                            {
                                objColor = mat.GetColor(name);
                            }
                        }
                    }
                }

                rObj.col = JanusUtil.FormatColor(objColor);

                if (room.ExportTextures && texture != null)
                {
                    if (textureNames.Contains(texture.name))
                    {
                        AssetImage img = room.AssetImages.FirstOrDefault(c => c.Texture == texture);
                        if (img != null)
                        {
                            rObj.image_id = img.id;
                        }
                        return;
                    }
                    textureNames.Add(texture.name);

                    AssetImage image = new AssetImage();
                    image.Texture = texture;
                    image.id      = texture.name;
                    image.src     = texture.name;
                    rObj.image_id = image.id;
                    room.AddAssetImage(image);
                }
            }
        }
Пример #12
0
 public void AddAssetImage(AssetImage assetImg)
 {
     AllAssets.Add(assetImg);
     AssetImages.Add(assetImg);
 }
Пример #13
0
        public override void Export()
        {
            if (!room.SkyboxEnabled)
            {
                return;
            }

            // look for skybox and grab all 6 textures if it's 6-sided
            Material skybox = RenderSettings.skybox;

            if (skybox != null)
            {
                bool     proceed        = true;
                string[] skyboxTexNames = JanusGlobals.SkyboxTexNames;
                for (int i = 0; i < skyboxTexNames.Length; i++)
                {
                    if (!skybox.HasProperty(skyboxTexNames[i]))
                    {
                        proceed = false;
                    }
                }

                if (proceed)
                {
                    Texture2D skyBoxForward = (Texture2D)skybox.GetTexture("_FrontTex");
                    Texture2D skyBoxBack    = (Texture2D)skybox.GetTexture("_BackTex");
                    Texture2D skyBoxLeft    = (Texture2D)skybox.GetTexture("_LeftTex");
                    Texture2D skyBoxRight   = (Texture2D)skybox.GetTexture("_RightTex");
                    Texture2D skyBoxUp      = (Texture2D)skybox.GetTexture("_UpTex");
                    Texture2D skyBoxDown    = (Texture2D)skybox.GetTexture("_DownTex");

                    assetForward = TryAddTexture(skyBoxForward);
                    assetBack    = TryAddTexture(skyBoxBack);
                    assetLeft    = TryAddTexture(skyBoxLeft);
                    assetRight   = TryAddTexture(skyBoxRight);
                    assetUp      = TryAddTexture(skyBoxUp);
                    assetDown    = TryAddTexture(skyBoxDown);

                    renderedSkybox = false;
                }
                else
                {
                    if (room.ExportOnlyHtml)
                    {
                        assetLeft    = AddFakeTexture("Left");
                        assetRight   = AddFakeTexture("Right");
                        assetForward = AddFakeTexture("Forward");
                        assetBack    = AddFakeTexture("Back");
                        assetUp      = AddFakeTexture("Up");
                        assetDown    = AddFakeTexture("Down");
                    }
                    else
                    {
                        // the skybox is not a 6-texture skybox
                        // lets render it to one then
                        GameObject temp = new GameObject("__TempSkyRender");
                        Camera     cam  = temp.AddComponent <Camera>();

                        cam.enabled = false;

                        int           exportSkyboxResolution = room.SkyboxResolution;
                        RenderTexture tex = new RenderTexture(exportSkyboxResolution, exportSkyboxResolution, 0);
                        cam.targetTexture = tex;
                        cam.clearFlags    = CameraClearFlags.Skybox;
                        cam.cullingMask   = 0;
                        cam.orthographic  = true;

                        Texture2D skyBoxLeft    = RenderSkyBoxSide(Vector3.left, "Left", tex, cam);
                        Texture2D skyBoxRight   = RenderSkyBoxSide(Vector3.right, "Right", tex, cam);
                        Texture2D skyBoxForward = RenderSkyBoxSide(Vector3.forward, "Forward", tex, cam);
                        Texture2D skyBoxBack    = RenderSkyBoxSide(Vector3.back, "Back", tex, cam);
                        Texture2D skyBoxUp      = RenderSkyBoxSide(Vector3.up, "Up", tex, cam);
                        Texture2D skyBoxDown    = RenderSkyBoxSide(Vector3.down, "Down", tex, cam);

                        cam.targetTexture = null;
                        Graphics.SetRenderTarget(null);

                        GameObject.DestroyImmediate(tex);
                        GameObject.DestroyImmediate(temp);

                        assetLeft    = AddTexture(skyBoxLeft);
                        assetRight   = AddTexture(skyBoxRight);
                        assetForward = AddTexture(skyBoxForward);
                        assetBack    = AddTexture(skyBoxBack);
                        assetUp      = AddTexture(skyBoxUp);
                        assetDown    = AddTexture(skyBoxDown);
                    }

                    renderedSkybox = true;
                }
            }

            room.SkyboxLeft  = assetLeft;
            room.SkyboxRight = assetRight;
            room.SkyboxFront = assetForward;
            room.SkyboxBack  = assetBack;
            room.SkyboxUp    = assetUp;
            room.SkyboxDown  = assetDown;
        }