Exemplo n.º 1
0
        public static void BakeStaticShadow(Camera camera, GameObject fieldObj, Model model, Studio studio)
        {
            if (model == null)
            {
                return;
            }

            camera.CopyFrom(Camera.main);
            camera.targetDisplay = 1;

            camera.targetTexture = new RenderTexture(camera.pixelWidth, camera.pixelHeight, 24, RenderTextureFormat.ARGB32);

            string dirPath        = Path.Combine(Application.dataPath, EngineGlobal.PROJECT_PATH_NAME + "/Shadow");
            int    assetRootIndex = dirPath.IndexOf("Assets");

            if (assetRootIndex < 0)
            {
                Debug.LogError(string.Format("{0} is out of Assets folder.", dirPath));
                return;
            }
            dirPath = dirPath.Substring(assetRootIndex);

            camera.transform.localPosition = new Vector3(0, 500, 0);
            camera.transform.localRotation = Quaternion.Euler(90f, 0f, 0f);
            CameraHelper.LookAtModel(camera.transform, model);

            fieldObj.SetActive(false);
            Texture2D rawTex = CapturingHelper.CaptureModelInCamera(model, camera, studio, true);

            fieldObj.SetActive(true);

            string filePath = TextureHelper.SaveTexture(dirPath, EditorGlobal.STATIC_SHADOW_NAME, rawTex);

            AssetDatabase.ImportAsset(filePath);

            fieldObj.transform.localPosition = Vector3.zero;
            fieldObj.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);
            ScaleShadowField(camera, fieldObj);

            camera.targetTexture = null;
            camera.targetDisplay = 2;
        }
Exemplo n.º 2
0
        private string SaveAtlasAndSetMetaData(string fileName, ref Texture2D atlasTexture, int atlasLength, Rect[] atlasRects, Texture2D[] textures, IntegerVector[] pivots, string[] spriteNames, bool isNormalMap = false)
        {
            if (isNormalMap)
            {
                fileName += "_normal";
            }

            string filePath = TextureHelper.SaveTexture(folderPath, fileName, atlasTexture);

            AssetDatabase.ImportAsset(filePath);

            TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(filePath);

            if (textureImporter != null)
            {
                textureImporter.textureType      = (isNormalMap ? TextureImporterType.NormalMap : TextureImporterType.Sprite);
                textureImporter.spriteImportMode = SpriteImportMode.Multiple;
                textureImporter.maxTextureSize   = atlasLength;

                SpriteMetaData[] metaData = new SpriteMetaData[textures.Length];
                for (int i = 0; i < textures.Length; i++)
                {
                    string name = spriteNames[i].PadLeft((spriteNames.Length - 1).ToString().Length, '0');
                    metaData[i].name      = name;
                    metaData[i].rect      = atlasRects[i];
                    metaData[i].alignment = (int)SpriteAlignment.Custom;
                    metaData[i].pivot     = new Vector2((float)pivots[i].x / (float)textures[i].width,
                                                        (float)pivots[i].y / (float)textures[i].height);
                }
                textureImporter.spritesheet = metaData;

                AssetDatabase.ImportAsset(filePath);

                atlasTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(filePath);
            }

            return(filePath);
        }
Exemplo n.º 3
0
        protected void TrimToUnifiedSize(IntegerVector[] pivots, Texture2D[] modelTextures, Texture2D[] normalMapTextures = null)
        {
            try
            {
                Debug.Assert(studio.trimming.isUnifiedForAllViews);

                for (int i = 0; i < modelTextures.Length; ++i)
                {
                    pivots[i].SubtractWithMargin(unifiedTexBound.min, studio.trimming.margin);

                    modelTextures[i] = TextureHelper.TrimTexture(modelTextures[i], unifiedTexBound, studio.trimming.margin, EngineGlobal.CLEAR_COLOR32);
                    if (studio.output.normalMapMake)
                    {
                        Color32 defaultColor = (studio.output.isGrayscaleMap ? EngineGlobal.CLEAR_COLOR32 : EngineGlobal.NORMALMAP_COLOR32);
                        normalMapTextures[i] = TextureHelper.TrimTexture(normalMapTextures[i], unifiedTexBound, studio.trimming.margin, defaultColor, studio.output.normalMapMake);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 4
0
        public void OnCaptureFrame()
        {
            try
            {
                double deltaTime = EditorApplication.timeSinceStartup - prevTime;
                if (deltaTime < studio.frame.delay)
                {
                    return;
                }
                prevTime = EditorApplication.timeSinceStartup;

                OnCaptureFrame_();

                Texture2D tex = CapturingHelper.CaptureModelManagingShadow(model, studio);

                studio.samplings.Add(new Sampling(tex, frameTime));

                IntegerBound texBound = new IntegerBound();
                if (!TextureHelper.CalcTextureBound(tex, pivot2D, texBound))
                {
                    texBound.min.x = pivot2D.x - 1;
                    texBound.max.x = pivot2D.x + 1;
                    texBound.min.y = pivot2D.y - 1;
                    texBound.max.y = pivot2D.y + 1;
                }

                TextureHelper.MakeUnifiedBound(pivot2D, texBound, unifiedTexBound);

                stateMachine.ChangeState(SamplingState.EndFrame);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Finish();
            }
        }
Exemplo n.º 5
0
        private void TrimToUnifiedSizeAll()
        {
            try
            {
                for (int i = 0; i < bakingDataList.Length; ++i)
                {
                    BakingData bakingData = bakingDataList[i];

                    bakingData.pivot.SubtractWithMargin(unifiedTexBound.min, studio.trimming.margin);

                    bakingData.modelTexture = TextureHelper.TrimTexture(bakingData.modelTexture,
                                                                        unifiedTexBound, studio.trimming.margin, EngineGlobal.CLEAR_COLOR32);
                    if (studio.output.normalMapMake)
                    {
                        bakingData.normalMapTexture = TextureHelper.TrimTexture(bakingData.normalMapTexture,
                                                                                unifiedTexBound, studio.trimming.margin, EngineGlobal.NORMALMAP_COLOR32, true);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 6
0
        public void OnCaptureFrame()
        {
            try
            {
                double deltaTime = EditorApplication.timeSinceStartup - prevTime;
                if (deltaTime < studio.frame.delay)
                {
                    return;
                }
                prevTime = EditorApplication.timeSinceStartup;

                if (studio.shadow.type == ShadowType.TopDown)
                {
                    Camera     camera;
                    GameObject fieldObj;
                    ShadowHelper.GetCameraAndFieldObject(studio.shadow.obj, out camera, out fieldObj);
                    ShadowHelper.BakeStaticShadow(camera, fieldObj, particleModel, studio);
                }

                Texture2D tex = CapturingHelper.CaptureModelManagingShadow(model, studio);

                IntegerBound texBound = new IntegerBound();
                if (!TextureHelper.CalcTextureBound(tex, pivot2D, texBound))
                {
                    texBound.min.x = pivot2D.x - 1;
                    texBound.max.x = pivot2D.x + 1;
                    texBound.min.y = pivot2D.y - 1;
                    texBound.max.y = pivot2D.y + 1;
                }

                if (studio.trimming.on)
                {
                    if (studio.trimming.isUnifiedForAllViews)
                    {
                        TextureHelper.MakeUnifiedBound(pivot2D, texBound, unifiedTexBound);
                    }
                    else
                    {
                        pivot2D.SubtractWithMargin(texBound.min, studio.trimming.margin);
                        tex = TextureHelper.TrimTexture(tex, texBound, studio.trimming.margin, EngineGlobal.CLEAR_COLOR32);
                    }
                }

                if (studio.packing.on || studio.trimming.isUnifiedForAllViews)
                {
                    frameModelTextures[frameIndex] = tex;
                    framePivots[frameIndex]        = pivot2D;
                }
                else // !studio.packing.on && !studio.trim.isUnifiedSize
                {
                    BakeIndividually(tex, pivot2D, viewName, frameIndex);
                }

                stateMachine.ChangeState(BakingState.EndFrame);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Finish();
            }
        }
Exemplo n.º 7
0
        public void OnCaptureFrame()
        {
            try
            {
                double deltaTime = EditorApplication.timeSinceStartup - prevTime;
                if (deltaTime < studio.frame.delay)
                {
                    return;
                }
                prevTime = EditorApplication.timeSinceStartup;

                if (studio.shadow.type == ShadowType.Simple && studio.shadow.simple.isDynamicScale)
                {
                    ShadowHelper.ScaleSimpleShadowDynamically(modelBaseSizeForView, simpleShadowBaseScale, meshModel, studio);
                }

                Texture2D modelTexture     = CapturingHelper.CaptureModelManagingShadow(model, studio);
                Texture2D normalMapTexture = null;
                if (studio.output.normalMapMake)
                {
                    normalMapTexture = CapturingHelper.CaptureModelForNormalMap(model, studio.output.isGrayscaleMap, studio.shadow.obj);
                }

                IntegerBound texBound     = new IntegerBound();
                IntegerBound compactBound = new IntegerBound();
                if (!TextureHelper.CalcTextureBound(modelTexture, pivot2D, texBound, compactBound))
                {
                    texBound.min.x = pivot2D.x - 1;
                    texBound.max.x = pivot2D.x + 1;
                    texBound.min.y = pivot2D.y - 1;
                    texBound.max.y = pivot2D.y + 1;
                }

                if (frameCompactBounds != null)
                {
                    frameCompactBounds[frameIndex] = compactBound;
                }

                if (studio.trimming.on)
                {
                    if (studio.trimming.isUnifiedForAllViews)
                    {
                        TextureHelper.MakeUnifiedBound(pivot2D, texBound, unifiedTexBound);
                    }
                    else
                    {
                        pivot2D.SubtractWithMargin(texBound.min, studio.trimming.margin);

                        modelTexture = TextureHelper.TrimTexture(modelTexture, texBound, studio.trimming.margin, EngineGlobal.CLEAR_COLOR32);
                        if (studio.output.normalMapMake)
                        {
                            Color32 defaultColor = (studio.output.isGrayscaleMap ? EngineGlobal.CLEAR_COLOR32 : EngineGlobal.NORMALMAP_COLOR32);
                            normalMapTexture = TextureHelper.TrimTexture(normalMapTexture, texBound, studio.trimming.margin, defaultColor, true);
                        }

                        if (frameCompactBounds != null)
                        {
                            CalcCompactVector(modelTexture, texBound);
                        }

                        if (locationMappings != null)
                        {
                            foreach (LocationMapping mapping in locationMappings)
                            {
                                mapping.frameLocationPositions[frameIndex].SubtractWithMargin(texBound.min, studio.trimming.margin);
                            }
                        }
                    }
                }

                if (studio.packing.on || studio.trimming.isUnifiedForAllViews)
                {
                    framePivots[frameIndex]        = pivot2D;
                    frameModelTextures[frameIndex] = modelTexture;
                    if (studio.output.normalMapMake)
                    {
                        frameNormalMapTextures[frameIndex] = normalMapTexture;
                    }
                }
                else // !studio.packing.on && !studio.trim.isUnifiedSize
                {
                    BakeIndividually(modelTexture, pivot2D, viewName, frameIndex);
                    if (studio.output.normalMapMake)
                    {
                        BakeIndividually(normalMapTexture, pivot2D, viewName, frameIndex, true);
                    }
                }

                stateMachine.ChangeState(BakingState.EndFrame);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Finish();
            }
        }
Exemplo n.º 8
0
        private static void Combine(bool onlySpriteName)
        {
            if (Selection.objects.Length < 2)
            {
                return;
            }

            List <Sprite>    sprites  = new List <Sprite>();
            List <Texture2D> textures = new List <Texture2D>();

            foreach (Object obj in Selection.objects)
            {
                if (!EditorUtility.IsPersistent(obj))
                {
                    continue;
                }

                string assetPath = AssetDatabase.GetAssetPath(obj);

                TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(assetPath);
                if (importer == null)
                {
                    continue;
                }

                importer.isReadable = true;
                AssetDatabase.ImportAsset(assetPath);

                Texture2D atlasTex = obj as Texture2D;
                if (atlasTex == null)
                {
                    continue;
                }

                Color32[] srcPixels = atlasTex.GetPixels32();

                for (int metai = 0; metai < importer.spritesheet.Length; ++metai)
                {
                    SpriteMetaData metaData = importer.spritesheet[metai];

                    int spriteWidth  = (int)metaData.rect.width;
                    int spriteHeight = (int)metaData.rect.height;

                    Color32[] destPixels = new Color32[spriteWidth * spriteHeight];
                    for (int destIndex = 0; destIndex < spriteWidth * spriteHeight; destIndex++)
                    {
                        int x        = destIndex % spriteWidth;
                        int y        = destIndex / spriteWidth;
                        int srcIndex = ((int)metaData.rect.y + y) * atlasTex.width + ((int)metaData.rect.x + x);
                        if (srcIndex < srcPixels.Length)
                        {
                            destPixels[destIndex] = srcPixels[srcIndex];
                        }
                    }

                    Texture2D spriteTex = new Texture2D(spriteWidth, spriteHeight, TextureFormat.ARGB32, false);
                    spriteTex.SetPixels32(destPixels);
                    spriteTex.Apply();

                    string spriteName = onlySpriteName ? metaData.name : atlasTex.name + "_" + metaData.name;
                    Sprite sprite     = new Sprite(spriteTex, spriteName, metaData.pivot);
                    sprites.Add(sprite);
                    textures.Add(spriteTex);
                }
            }

            Texture2D newAtlasTex = new Texture2D(8192, 8192, TextureFormat.ARGB32, false);

            Rect[] texRects = newAtlasTex.PackTextures(textures.ToArray(), 2, 8192);
            for (int i = 0; i < sprites.Count; i++)
            {
                Texture2D tex  = sprites[i].tex;
                float     newX = texRects[i].x * newAtlasTex.width;
                float     newY = texRects[i].y * newAtlasTex.height;
                texRects[i] = new Rect(newX, newY, (float)tex.width, (float)tex.height);
            }

            string filePath = AssetDatabase.GetAssetPath(Selection.objects[0]);
            string dirPath  = filePath.Remove(filePath.LastIndexOf('/'));
            string fileName = "CombinedSpritesheet_" + PathHelper.MakeDateTimeString();

            filePath = TextureHelper.SaveTexture(dirPath, fileName, newAtlasTex);
            AssetDatabase.ImportAsset(filePath);

            TextureImporter texImporter = (TextureImporter)AssetImporter.GetAtPath(filePath);

            if (texImporter != null)
            {
                texImporter.textureType      = TextureImporterType.Sprite;
                texImporter.spriteImportMode = SpriteImportMode.Multiple;
                texImporter.maxTextureSize   = 4096;

                int texCount = sprites.Count;
                SpriteMetaData[] metaData = new SpriteMetaData[texCount];
                for (int i = 0; i < texCount; i++)
                {
                    metaData[i].name      = sprites[i].name;
                    metaData[i].rect      = texRects[i];
                    metaData[i].alignment = (int)SpriteAlignment.Custom;
                    metaData[i].pivot     = sprites[i].pivot;
                }
                texImporter.spritesheet = metaData;

                AssetDatabase.ImportAsset(filePath);
            }

            foreach (Object obj in Selection.objects)
            {
                if (!EditorUtility.IsPersistent(obj))
                {
                    continue;
                }

                string assetPath = AssetDatabase.GetAssetPath(obj);

                TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(assetPath);
                if (importer == null)
                {
                    continue;
                }

                importer.isReadable = false;
                AssetDatabase.ImportAsset(assetPath);
            }
        }
Exemplo n.º 9
0
        public void OnCaptureFrame()
        {
            try
            {
                double deltaTime = EditorApplication.timeSinceStartup - prevTime;
                if (deltaTime < studio.frame.delay)
                {
                    return;
                }
                prevTime = EditorApplication.timeSinceStartup;

                Texture2D modelTexture     = CapturingHelper.CaptureModelManagingShadow(model, studio);
                Texture2D normalMapTexture = null;
                if (studio.output.normalMapMake)
                {
                    normalMapTexture = CapturingHelper.CaptureModelForNormalMap(model, studio.output.isGrayscaleMap, studio.shadow.obj);
                }

                IntegerBound texBound = new IntegerBound();
                if (!TextureHelper.CalcTextureBound(modelTexture, pivot2D, texBound))
                {
                    texBound.min.x = pivot2D.x - 1;
                    texBound.max.x = pivot2D.x + 1;
                    texBound.min.y = pivot2D.y - 1;
                    texBound.max.y = pivot2D.y + 1;
                }

                if (studio.trimming.on)
                {
                    if (studio.trimming.isUnifiedForAllViews)
                    {
                        TextureHelper.MakeUnifiedBound(pivot2D, texBound, unifiedTexBound);
                    }
                    else
                    {
                        pivot2D.SubtractWithMargin(texBound.min, studio.trimming.margin);

                        modelTexture = TextureHelper.TrimTexture(modelTexture, texBound, studio.trimming.margin, EngineGlobal.CLEAR_COLOR32);
                        if (studio.output.normalMapMake)
                        {
                            Color32 defaultColor = (studio.output.isGrayscaleMap ? EngineGlobal.CLEAR_COLOR32 : EngineGlobal.NORMALMAP_COLOR32);
                            normalMapTexture = TextureHelper.TrimTexture(normalMapTexture, texBound, studio.trimming.margin, defaultColor, true);
                        }
                    }
                }

                string viewName = studio.view.checkedSubViews[viewIndex].name;
                if (studio.packing.on || studio.trimming.isUnifiedForAllViews)
                {
                    bakingDataList[viewIndex] = new BakingData(viewName, pivot2D, modelTexture, normalMapTexture);
                }
                else // !studio.packing.on && !studio.trim.isUnifiedSize
                {
                    BakeIndividually(modelTexture, pivot2D, viewName, "");
                    if (studio.output.normalMapMake)
                    {
                        BakeIndividually(normalMapTexture, pivot2D, viewName, "", true);
                    }
                }

                stateMachine.ChangeState(BakingState.EndView);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Finish();
            }
        }