예제 #1
0
        protected override void OnLoad(EventArgs e)
        {
            // Initialize GL State
            GPUCapabilities.Initialize();
            GL.ClearColor(0.25f, 0.25f, 0.25f, 1.0f);

            // Set up PackageManager
            PackageManager.BasePath = "../../Assets/";

            // Initialize camera. Rest is in OnResize()
            orthoCamera = new Camera(this);
            orthoCamera.SetAsCurrent();

            // Load shaders
            ShaderManager.LoadCollection("Shaders/collection.xml");

            // Initialize batch and atlas manager
            spriteBatch = new SpriteBatch();
            sam = new SpriteAtlasManager();

            // Temporary example until editor gets sprite support
            // Uncomment to regenerate sprite file
            //GenerateSpriteFile();

            // Get the sprite atlas
            spaceShips = sam.Get("Sprites/SpaceShips.spr", true);

            // Get sprite by ID (slow operation, cache result on load)
            spaceShip = spaceShips["PlayerBlue1"];
        }
예제 #2
0
    public static void DrawShadowsAndMask(LightingBuffer2D buffer, int layer)
    {
        buffer.CalculateCoords();

        drawMask = false;
        if (buffer.lightSource.layerSetting[layer].type != LightingLayerType.ShadowOnly)
        {
            drawMask = true;
        }

        drawShadows = false;
        if (buffer.lightSource.layerSetting[layer].type != LightingLayerType.MaskOnly)
        {
            drawShadows = true;
        }

        GL.PushMatrix();

        if (LightingManager2D.Get().lightingSpriteAtlas&& SpriteAtlasManager.Get().atlasTexture != null)
        {
            DrawWithAtlas(buffer, layer);
        }
        else
        {
            DrawWithoutAtlas(buffer, layer);
        }

        GL.PopMatrix();
    }
예제 #3
0
    public Material GetAtlasMaterial()
    {
        if (atlasMaterial == null)
        {
            atlasMaterial = new Material(Shader.Find(Max2D.shaderPath + "Particles/Alpha Blended"));
        }

        atlasMaterial.mainTexture = SpriteAtlasManager.Get().atlasTexture.texture;

        return(atlasMaterial);
    }
예제 #4
0
    public void Initialize()
    {
        instance = this;

        transform.position = Vector3.zero;

        mainBuffer = LightingMainBuffer2D.Get();

        fboManager = FBOManager.Get();

        spriteAtlas = SpriteAtlasManager.Get();

        meshRendererManager = MeshRendererManager.Get();

        version = VERSION;
    }
예제 #5
0
    List <Sprite> ScanGameObject(GameObject obj)
    {
        List <Sprite> single = new List <Sprite>();

        if (obj != null)
        {
            SpriteReferenceInfo.ScanGameObjectSprite(obj, (o, s, p) => {
                string key = GetSpriteKey(s);

                List <SpriteReferenceInfo.GameObjectInfo> _refs;
                if (!refInfo.TryGetValue(key, out _refs))
                {
                    _refs        = new List <SpriteReferenceInfo.GameObjectInfo>();
                    refInfo[key] = _refs;
                    var oi       = _refs.Find((i) => i.obj == obj);
                    if (oi == null)
                    {
                        var info = new SpriteReferenceInfo.GameObjectInfo(obj);
                        info.paths.Add(p);
                        _refs.Add(info);
                    }
                    else
                    {
                        oi.paths.Add(p);
                    }
                }

                SpriteAtlasData a = SpriteAtlasManager.Get(s.texture);
                if (a != null)
                {
                    if (!atlas.Contains(a))
                    {
                        atlas.Add(a);
                    }
                }
                else
                {
                    if (!single.Contains(s))
                    {
                        single.Add(s);
                    }
                }
            });
        }
        return(single);
    }
예제 #6
0
    static public void DrawShadowsAndMask(LightingBuffer2D buffer, int layer)
    {
        buffer.CalculateCoords();

        offset.x = -buffer.lightSource.transform.position.x;
        offset.y = -buffer.lightSource.transform.position.y;

        manager = LightingManager2D.Get();

        layerSettings = buffer.lightSource.layerSetting[layer];

        z = buffer.transform.position.z;
        lightSizeSquared = Mathf.Sqrt(buffer.lightSource.lightSize * buffer.lightSource.lightSize + buffer.lightSource.lightSize * buffer.lightSource.lightSize);

        SortObjects(buffer, layer);

        drawMask = false;
        if (buffer.lightSource.layerSetting[layer].type != LightingLayerType.ShadowOnly)
        {
            drawMask = true;
        }

        drawShadows = false;
        if (buffer.lightSource.layerSetting[layer].type != LightingLayerType.MaskOnly)
        {
            drawShadows = true;
        }

        GL.PushMatrix();

        if (manager.lightingSpriteAtlas && SpriteAtlasManager.Get().atlasTexture != null)
        {
            DrawWithAtlas(buffer, layer);
        }
        else
        {
            DrawWithoutAtlas(buffer, layer);
        }

        GL.PopMatrix();
    }
예제 #7
0
    static public void DrawWithAtlas(Vector2D offset, float z)
    {
        material = manager.materials.GetAdditive();
        material.SetColor("_TintColor", Color.white);
        material.mainTexture = SpriteAtlasManager.Get().atlasTexture.texture;

        material.SetPass(0);

        GL.Begin(GL.TRIANGLES);

        for (int i = 0; i < spriteRendererList.Count; i++)
        {
            id = spriteRendererList[i];

            if (id.type != LightingSpriteRenderer2D.Type.Particle)
            {
                continue;
            }

            if (id.GetSprite() == null)
            {
                continue;
            }

            if (id.InCamera() == false)
            {
                continue;
            }

            position = id.transform.position;

            scale    = id.transform.lossyScale;
            scale.x *= id.offsetScale.x;
            scale.y *= id.offsetScale.y;

            rot = id.offsetRotation;
            if (id.applyTransformRotation)
            {
                rot += id.transform.rotation.eulerAngles.z;
            }

            color   = id.color;
            color.a = id.alpha;

            GL.Color(color);

            spriteRenderer.sprite = SpriteAtlasManager.RequestSprite(id.GetSprite(), SpriteRequest.Type.Normal);

            Max2D.DrawSpriteBatched_Tris_Day(spriteRenderer, offset.ToVector2() + position + id.offsetPosition, scale, rot, z);

            LightingDebug.SpriteRenderersDrawn++;
        }

        GL.End();

        material = manager.materials.GetAtlasMaterial();
        material.SetPass(0);

        GL.Begin(GL.TRIANGLES);

        for (int i = 0; i < spriteRendererList.Count; i++)
        {
            id = spriteRendererList[i];

            if (id.type == LightingSpriteRenderer2D.Type.Particle)
            {
                continue;
            }

            if (id.GetSprite() == null)
            {
                continue;
            }

            if (id.InCamera() == false)
            {
                continue;
            }

            position = id.transform.position;

            scale    = id.transform.lossyScale;
            scale.x *= id.offsetScale.x;
            scale.y *= id.offsetScale.y;

            rot = id.offsetRotation;
            if (id.applyTransformRotation)
            {
                rot += id.transform.rotation.eulerAngles.z;
            }

            switch (id.type)
            {
            case LightingSpriteRenderer2D.Type.WhiteMask:
                GL.Color(Color.white);

                spriteRenderer.sprite = SpriteAtlasManager.RequestSprite(id.GetSprite(), SpriteRequest.Type.WhiteMask);

                Max2D.DrawSpriteBatched_Tris_Day(spriteRenderer, offset.ToVector2() + position + id.offsetPosition, scale, rot, z);
                break;

            case LightingSpriteRenderer2D.Type.BlackMask:
                GL.Color(Color.black);

                spriteRenderer.sprite = SpriteAtlasManager.RequestSprite(id.GetSprite(), SpriteRequest.Type.WhiteMask);

                Max2D.DrawSpriteBatched_Tris_Day(spriteRenderer, offset.ToVector2() + position + id.offsetPosition, scale, rot, z);

                break;
            }

            LightingDebug.SpriteRenderersDrawn++;
        }

        GL.End();
    }
예제 #8
0
        protected override DragAndDropVisualMode HandleDragAndDrop(DragAndDropArgs args)
        {
            if (args.parentItem == null)
            {
                return(DragAndDropVisualMode.Rejected);
            }

            // if (args.parentItem.id == -1) return DragAndDropVisualMode.Rejected;

            SpriteAtlasData atlas;

            SpriteAtlasData.SpriteInfo sprite;
            Sprite origin;

            Get(args.parentItem.id, out atlas, out sprite, out origin);

            List <Texture2D> draging_atlas = new List <Texture2D>();

            Object[] objs = DragAndDrop.objectReferences;

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

            foreach (var obj in DragAndDrop.objectReferences)
            {
                Sprite s = null;
                if (obj.GetType() == typeof(Sprite))
                {
                    s = obj as Sprite;
                }
                else if (obj.GetType() == typeof(Texture2D))
                {
                    TextureImporter importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(obj)) as TextureImporter;
                    if (importer.textureType != TextureImporterType.Sprite)
                    {
                        continue;
                    }

                    if (importer.spriteImportMode == SpriteImportMode.Multiple)
                    {
                        draging_atlas.Add(obj as Texture2D);
                        continue;
                    }
                    else
                    {
                        s = AssetDatabase.LoadAssetAtPath <Sprite>(AssetDatabase.GetAssetPath(obj));
                    }
                }

                if (s == null || s.texture == atlas.texture)
                {
                    continue;
                }

                if (atlas.HaveSprite(s.name))
                {
                    continue;
                }

                sprites.Add(s);
            }

            if (atlas == null)
            {
                if (draging_atlas.Count == 0)
                {
                    return(DragAndDropVisualMode.Rejected);
                }
            }
            else
            {
                if (sprites.Count == 0)
                {
                    return(DragAndDropVisualMode.Rejected);
                }
            }

            if (!args.performDrop)
            {
                return(DragAndDropVisualMode.Copy);
            }

            if (atlas == null)
            {
                foreach (Texture2D tex in draging_atlas)
                {
                    SpriteAtlasData spriteAtlasData = SpriteAtlasManager.Get(tex);
                    if (!atlasList.Contains(spriteAtlasData))
                    {
                        atlasList.Add(spriteAtlasData);
                    }
                }
            }
            else
            {
                foreach (Sprite s in sprites)
                {
                    atlas.AddSprite(s);
                }
            }

            DragAndDrop.AcceptDrag();
            Reload();

            return(DragAndDropVisualMode.Copy);
        }
예제 #9
0
    public static void DrawWithAtlas(LightingBuffer2D buffer, int layer)
    {
        float lightSizeSquared = Mathf.Sqrt(buffer.lightSource.lightSize * buffer.lightSource.lightSize + buffer.lightSource.lightSize * buffer.lightSource.lightSize);
        float z = buffer.transform.position.z;

        offset.x = -buffer.lightSource.transform.position.x;
        offset.y = -buffer.lightSource.transform.position.y;

        manager      = LightingManager2D.Get();
        colliderList = LightingCollider2D.GetList();

        LayerSetting layerSetting = buffer.lightSource.layerSetting[layer];
        bool         maskEffect   = (layerSetting.effect == LightingLayerEffect.InvisibleBellow);

                #if UNITY_2018_1_OR_NEWER
        tilemapList = LightingTilemapCollider2D.GetList();
                #endif

        // Shadow Fill
        manager.materials.GetAtlasMaterial().SetPass(0);

        GL.Begin(GL.TRIANGLES);

        if (drawShadows)
        {
            for (int id = 0; id < colliderList.Count; id++)
            {
                if ((int)colliderList[id].lightingCollisionLayer != layer)
                {
                    continue;
                }

                // Collider Shadow
                LightingBufferMesh.Shadow(buffer, colliderList[id], lightSizeSquared, z);
                LightingBufferShape.Shadow(buffer, colliderList[id], lightSizeSquared, z, offset);
            }

                        #if UNITY_2018_1_OR_NEWER
            for (int id = 0; id < tilemapList.Count; id++)
            {
                if ((int)tilemapList[id].lightingCollisionLayer != layer)
                {
                    continue;
                }

                // Tilemap Shadow
                LightingBufferTilemapRectangle.Shadow(buffer, tilemapList[id], lightSizeSquared, z);
            }
                        #endif
        }

        if (drawMask)
        {
            if (colliderList.Count > 0)
            {
                for (int id = 0; id < colliderList.Count; id++)
                {
                    if ((int)colliderList[id].lightingMaskLayer != layer)
                    {
                        continue;
                    }

                    // Collider Shape Mask
                    LightingBufferShape.Mask(buffer, colliderList[id], layerSetting, offset, z);

                    // Collider Sprite Mask
                    LightingBufferSprite.MaskWithAtlas(buffer, colliderList[id], offset, z);
                }
            }

                        #if UNITY_2018_1_OR_NEWER
            for (int id = 0; id < tilemapList.Count; id++)
            {
                if ((int)tilemapList[id].lightingMaskLayer != layer)
                {
                    continue;
                }

                // Tilemap Shape Mask
                LightingBufferTilemapRectangle.MaskShape(buffer, tilemapList[id], offset, z);

                // Tilemap Sprite Mask
                LightingBufferTilemapRectangle.MaskSpriteWithAtlas(buffer, tilemapList[id], offset, z);
            }
                        #endif
        }

        GL.End();

        // Partialy Batched (Default Edition)
        if (buffer.partiallyBatchedList_Collider.Count > 0)
        {
            Material materialWhite = manager.materials.GetWhiteSprite();
            Material materialBlack = manager.materials.GetBlackSprite();
            Material material;

            PartiallyBatched_Collider batch;

            materialWhite.mainTexture = SpriteAtlasManager.Get().atlasTexture.texture;
            materialWhite.SetPass(0);

            for (int i = 0; i < buffer.partiallyBatchedList_Collider.Count; i++)
            {
                batch = buffer.partiallyBatchedList_Collider[i];

                material = materialWhite;

                if (maskEffect && colliderList[i].transform.position.y < buffer.lightSource.transform.position.y)
                {
                    material = materialBlack;
                }

                LightingBufferSprite.MaskWithoutAtlas(buffer, batch.collider2D, material, offset, z);
            }

            materialWhite.mainTexture = null;

            buffer.partiallyBatchedList_Collider.Clear();
        }

        if (buffer.partiallyBatchedList_Tilemap.Count > 0)
        {
            Material materialWhite = manager.materials.GetWhiteSprite();
            Material materialBlack = manager.materials.GetBlackSprite();
            PartiallyBatched_Tilemap batch;
            Material material;

            // Draw Each Partialy Batched
            for (int i = 0; i < buffer.partiallyBatchedList_Tilemap.Count; i++)
            {
                batch = buffer.partiallyBatchedList_Tilemap[i];

                material = materialWhite;

                if (maskEffect && batch.polyOffset.y < 0)
                {
                    material = materialBlack;
                }

                material.mainTexture = batch.virtualSpriteRenderer.sprite.texture;

                Max2D.DrawSprite(material, batch.virtualSpriteRenderer, batch.polyOffset, batch.tileSize, 0, z);

                material.mainTexture = null;
            }

            buffer.partiallyBatchedList_Tilemap.Clear();
        }
    }