Example #1
0
    void DrawSpriteRenderer(LightSprite2D script)
    {
        if (script.spriteMode == LightSprite2D.SpriteMode.Custom)
        {
            bool foldout0 = GUIFoldout.Draw("Sprite Renderer", script);

            if (foldout0)
            {
                EditorGUI.indentLevel++;

                sprite.objectReferenceValue = (Sprite)EditorGUILayout.ObjectField("Sprite", sprite.objectReferenceValue, typeof(Sprite), true);

                DrawColor();

                EditorGUILayout.PropertyField(flipX, new GUIContent("Flip X"));
                EditorGUILayout.PropertyField(flipY, new GUIContent("Flip Y"));

                EditorGUI.indentLevel--;
            }
        }
        else
        {
            DrawColor();
        }
    }
Example #2
0
    public bool IsRendered()
    {
        string type = owner.GetType().ToString();

        switch (type)
        {
        case "Light2D":
            Light2D light = (Light2D)owner;
            if (light)
            {
                return(light.meshMode.enable && light.isActiveAndEnabled && light.InAnyCamera());
            }

            break;

        case "LightSprite2D":
            LightSprite2D sprite = (LightSprite2D)owner;
            if (sprite)
            {
                return(sprite.meshMode.enable && sprite.isActiveAndEnabled);
            }

            break;
        }

        return(false);
    }
  static void CreateLightSpriteRenderer()
  {
      GameObject newGameObject = new GameObject("Light Sprite 2D");

      LightSprite2D spriteRenderer2D = newGameObject.AddComponent <LightSprite2D>();

      spriteRenderer2D.sprite = Resources.Load <Sprite>("Sprites/gfx_light");

      newGameObject.transform.position = GetCameraPoint();
  }
Example #4
0
        static public void Draw(Pass pass)
        {
            for (int id = 0; id < pass.sortList.count; id++)
            {
                Sorting.SortObject sortObject = pass.sortList.list[id];

                switch (sortObject.type)
                {
                case Sorting.SortObject.Type.TilemapRoom:
                    LightTilemapRoom2D tilemapRoom = (LightTilemapRoom2D)sortObject.lightObject;

                    if (tilemapRoom != null)
                    {
                        TilemapRoom.Draw(tilemapRoom, pass.camera);
                    }

                    break;

                case Sorting.SortObject.Type.Room:
                    LightRoom2D room = (LightRoom2D)sortObject.lightObject;

                    if (room != null)
                    {
                        Room.Draw(room, pass.camera);
                    }

                    break;

                case Sorting.SortObject.Type.LightSprite:
                    LightSprite2D lightSprite = (LightSprite2D)sortObject.lightObject;

                    if (lightSprite != null)
                    {
                        LightSprite.Draw(lightSprite, pass.camera);
                    }

                    break;

                case Sorting.SortObject.Type.Light:
                    Light2D light = (Light2D)sortObject.lightObject;

                    if (light != null)
                    {
                        LightSource.Draw(light, pass.camera);
                    }

                    break;
                }
            }
        }
Example #5
0
    void DrawTransform(LightSprite2D script)
    {
        bool foldout = GUIFoldout.Draw("Transform", script.lightSpriteTransform);

        if (foldout)
        {
            EditorGUI.indentLevel++;

            EditorGUILayout.PropertyField(transform_position, new GUIContent("Position"));
            EditorGUILayout.PropertyField(transform_scale, new GUIContent("Scale"));
            EditorGUILayout.PropertyField(transform_rotation, new GUIContent("Rotation"));
            EditorGUILayout.PropertyField(transform_applyRotation, new GUIContent("Apply Rotation"));

            EditorGUI.indentLevel--;
        }
    }
Example #6
0
        private static void DrawLightSprites(Pass pass)
        {
            List <LightSprite2D> spriteRendererList = LightSprite2D.List;
            int spriteRendererCount = spriteRendererList.Count;

            if (spriteRendererCount < 1)
            {
                return;
            }

            for (int i = 0; i < spriteRendererCount; i++)
            {
                LightSprite2D id = spriteRendererList[i];

                if (id.nightLayer != pass.layerId)
                {
                    continue;
                }

                LightSprite.Draw(id, pass.camera);
            }
        }
Example #7
0
        public void SortObjects()
        {
            sortList.Reset();

            List <Light2D> lightList = Light2D.List;

            for (int id = 0; id < lightList.Count; id++)
            {
                Light2D light = lightList[id];

                if ((int)light.nightLayer != layerId)
                {
                    continue;
                }

                switch (layer.sorting)
                {
                case LayerSorting.ZAxisLower:
                    sortList.Add((object)light, Sorting.SortObject.Type.Light, -light.transform.position.z);
                    break;

                case LayerSorting.ZAxisHigher:
                    sortList.Add((object)light, Sorting.SortObject.Type.Light, light.transform.position.z);
                    break;
                }
            }

            List <LightRoom2D> roomList = LightRoom2D.List;

            for (int id = 0; id < roomList.Count; id++)
            {
                LightRoom2D room = roomList[id];

                if ((int)room.nightLayer != layerId)
                {
                    continue;
                }

                switch (layer.sorting)
                {
                case LayerSorting.ZAxisLower:
                    sortList.Add((object)room, Sorting.SortObject.Type.Room, -room.transform.position.z);
                    break;

                case LayerSorting.ZAxisHigher:
                    sortList.Add((object)room, Sorting.SortObject.Type.Room, room.transform.position.z);
                    break;
                }
            }

            List <LightTilemapRoom2D> roomTilemapList = LightTilemapRoom2D.List;

            for (int id = 0; id < roomTilemapList.Count; id++)
            {
                LightTilemapRoom2D tilemapRoom = roomTilemapList[id];

                if ((int)tilemapRoom.nightLayer != layerId)
                {
                    continue;
                }

                switch (layer.sorting)
                {
                case LayerSorting.ZAxisLower:
                    sortList.Add((object)tilemapRoom, Sorting.SortObject.Type.TilemapRoom, -tilemapRoom.transform.position.z);
                    break;

                case LayerSorting.ZAxisHigher:
                    sortList.Add((object)tilemapRoom, Sorting.SortObject.Type.TilemapRoom, tilemapRoom.transform.position.z);
                    break;
                }
            }

            List <LightSprite2D> spriteList = LightSprite2D.List;

            for (int id = 0; id < spriteList.Count; id++)
            {
                LightSprite2D lightSprite = spriteList[id];

                if ((int)lightSprite.nightLayer != layerId)
                {
                    continue;
                }

                switch (layer.sorting)
                {
                case LayerSorting.ZAxisLower:
                    sortList.Add((object)lightSprite, Sorting.SortObject.Type.LightSprite, -lightSprite.transform.position.z);
                    break;

                case LayerSorting.ZAxisHigher:
                    sortList.Add((object)lightSprite, Sorting.SortObject.Type.LightSprite, lightSprite.transform.position.z);
                    break;
                }
            }

            sortList.Sort();
        }
Example #8
0
    private void OnEnable()
    {
        lightSprite2D = target as LightSprite2D;

        InitProperties();
    }
Example #9
0
        static public void Draw(LightSprite2D id, Camera camera)
        {
            Material material = null;

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

            if (id.InCamera(camera) == false)
            {
                return;
            }

            Vector2 offset = -camera.transform.position;

            Vector2 position = id.transform.position;

            Vector2 scale = id.transform.lossyScale;

            scale.x *= id.lightSpriteTransform.scale.x;
            scale.y *= id.lightSpriteTransform.scale.y;

            float rot = id.lightSpriteTransform.rotation;

            if (id.lightSpriteTransform.applyRotation)
            {
                rot += id.transform.rotation.eulerAngles.z;
            }

            Color color = id.color;

            Sprite    sprite  = id.GetSprite();
            Texture2D texture = sprite.texture;
            float     ratio   = (float)texture.width / (float)texture.height;

            //float ratio = sprite.rect.width / sprite.rect.height;

            if (id.glowMode.enable)
            {
                switch (id.type)
                {
                case LightSprite2D.Type.Light:
                    material = Lighting2D.materials.GetBlurLight();

                    material.SetColor("_Color", color);
                    material.SetFloat("_Radius", id.glowMode.glowRadius);
                    material.SetFloat("_Ratio", ratio);

                    material.mainTexture = texture;

                    Universal.Sprite.FullRect.Simple.Draw(id.spriteMeshObject, material, id.spriteRenderer, offset + position + id.lightSpriteTransform.position, scale, rot);

                    material.mainTexture = null;

                    break;

                case LightSprite2D.Type.Mask:

                    material = Lighting2D.materials.GetBlurMask();

                    material.SetColor("_Color", color);
                    material.SetFloat("_Radius", id.glowMode.glowRadius);
                    material.SetFloat("_Ratio", ratio);

                    material.mainTexture = texture;

                    Universal.Sprite.FullRect.Simple.Draw(id.spriteMeshObject, material, id.spriteRenderer, offset + position + id.lightSpriteTransform.position, scale, rot);

                    material.mainTexture = null;

                    break;
                }
            }
            else
            {
                switch (id.type)
                {
                case LightSprite2D.Type.Light:

                    material = Lighting2D.materials.GetAdditive();
                    material.SetColor("_TintColor", color);

                    material.mainTexture = texture;

                    Universal.Sprite.FullRect.Simple.Draw(id.spriteMeshObject, material, id.spriteRenderer, offset + position + id.lightSpriteTransform.position, scale, rot);

                    material.mainTexture = null;

                    break;

                case LightSprite2D.Type.Mask:

                    material       = Lighting2D.materials.GetMask();
                    material.color = color;

                    material.mainTexture = texture;

                    Universal.Sprite.FullRect.Simple.Draw(id.spriteMeshObject, material, id.spriteRenderer, offset + position + id.lightSpriteTransform.position, scale, rot);

                    material.mainTexture = null;


                    break;
                }
            }

            if (material != null)
            {
                material.color = Color.white;
            }
        }
Example #10
0
    public void UpdateLightSprite(LightSprite2D id, MeshMode meshMode)
    {
        if (id.GetSprite() == null)
        {
            Free();
            return;
        }

        if (meshModeMaterial != meshMode.materials)
        {
            meshModeMaterial = meshMode.materials;

            ClearMaterial();
        }

        if (meshModeShader != meshMode.shader)
        {
            meshModeShader = meshMode.shader;

            ClearMaterial();
        }


        Material[] material = GetMaterials();

        if (material == null)
        {
            return;
        }


        float rotation = id.lightSpriteTransform.rotation;

        if (id.lightSpriteTransform.applyRotation)
        {
            rotation += id.transform.rotation.eulerAngles.z;
        }

        ////////////////////// Scale
        Vector2 scale = Vector2.zero;

        Sprite sprite = id.GetSprite();

        Rect spriteRect = sprite.textureRect;

        scale.x = (float)sprite.texture.width / spriteRect.width;
        scale.y = (float)sprite.texture.height / spriteRect.height;

        Vector2 size = id.lightSpriteTransform.scale;


        size.x *= 2;
        size.y *= 2;


        size.x /= scale.x;
        size.y /= scale.y;

        size.x *= (float)sprite.texture.width / (sprite.pixelsPerUnit * 2);
        size.y *= (float)sprite.texture.height / (sprite.pixelsPerUnit * 2);

        if (id.spriteRenderer.flipX)
        {
            size.x = -size.x;
        }

        if (id.spriteRenderer.flipY)
        {
            size.y = -size.y;
        }

        ////////////////////// PIVOT
        Rect    rect  = spriteRect;
        Vector2 pivot = sprite.pivot;

        pivot.x /= spriteRect.width;
        pivot.y /= spriteRect.height;
        pivot.x -= 0.5f;
        pivot.y -= 0.5f;


        pivot.x *= size.x;
        pivot.y *= size.y;


        float pivotDist  = Mathf.Sqrt(pivot.x * pivot.x + pivot.y * pivot.y);
        float pivotAngle = Mathf.Atan2(pivot.y, pivot.x);

        float rot = rotation * Mathf.Deg2Rad + Mathf.PI;

        Vector2 position = Vector2.zero;

        // Pivot Pushes Position

        position.x += Mathf.Cos(pivotAngle + rot) * pivotDist * id.transform.lossyScale.x;
        position.y += Mathf.Sin(pivotAngle + rot) * pivotDist * id.transform.lossyScale.y;
        position.x += id.transform.position.x;
        position.y += id.transform.position.y;
        position.x += id.lightSpriteTransform.position.x;
        position.y += id.lightSpriteTransform.position.y;

        Vector3 pos = position;

        pos.z = id.transform.position.z - 0.1f;
        transform.position = pos;

        Vector3 scale2 = id.transform.lossyScale;

        scale2.x *= size.x;
        scale2.y *= size.y;


        scale2.x /= 2;
        scale2.y /= 2;

        scale2.z = 1;

        transform.localScale = scale2;
        transform.rotation   = Quaternion.Euler(0, 0, rotation);

        Rect uvRect = new Rect();

        uvRect.x      = rect.x / sprite.texture.width;
        uvRect.y      = rect.y / sprite.texture.height;
        uvRect.width  = rect.width / sprite.texture.width + uvRect.x;
        uvRect.height = rect.height / sprite.texture.height + uvRect.y;

        if (meshRenderer != null)
        {
            Color lightColor = id.color;
            lightColor.a = id.meshMode.alpha;

            for (int i = 0; i < materials.Length; i++)
            {
                if (materials[i] == null)
                {
                    continue;
                }

                materials[i].SetColor("_TintColor", lightColor);
                materials[i].color       = lightColor;
                materials[i].mainTexture = id.GetSprite().texture;
            }

            id.meshMode.sortingLayer.ApplyToMeshRenderer(meshRenderer);

            meshRenderer.sharedMaterials = materials;

            meshRenderer.enabled = true;

            Mesh mesh = GetMeshSprite();

            Vector2[] uvs = mesh.uv;
            uvs[0].x = uvRect.x;
            uvs[0].y = uvRect.y;

            uvs[1].x = uvRect.width;
            uvs[1].y = uvRect.y;

            uvs[2].x = uvRect.width;
            uvs[2].y = uvRect.height;

            uvs[3].x = uvRect.x;
            uvs[3].y = uvRect.height;

            mesh.uv = uvs;

            meshFilter.mesh = mesh;
        }
    }