Esempio n. 1
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;
            }
        }
Esempio n. 2
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;
        }
    }