示例#1
0
        public RuntimeAtlasSegment Pack(Texture2D tex, bool apply = false)
        {
            texRect.Set(Padding, Padding, tex.width + Padding, tex.height + Padding);
            bool fit = _Rects.Count == 0;

            if (right)
            {
                if (!fit)
                {
                    for (int i = _Rects.Count - 1; 0 <= i; i--)
                    {
                        Rect existing = _Rects[i];

                        texRect.x = existing.xMax + Padding;
                        texRect.y = existing.y;
                        if (_Feasible(texRect, i))
                        {
                            fit   = true;
                            right = true;
                            break;
                        }
                    }
                }

                if (!fit)
                {
                    for (int i = 0; i < _Rects.Count; i++)
                    {
                        Rect existing = _Rects[i];

                        texRect.x = existing.x;
                        texRect.y = existing.yMax + Padding;
                        if (_Feasible(texRect, i))
                        {
                            fit   = true;
                            right = false;
                            break;
                        }
                    }
                }
            }
            else
            {
                if (!fit)
                {
                    for (int i = 0; i < _Rects.Count; i++)
                    {
                        Rect existing = _Rects[i];

                        texRect.x = existing.x;
                        texRect.y = existing.yMax + Padding;
                        if (_Feasible(texRect, i))
                        {
                            fit   = true;
                            right = false;
                            break;
                        }
                    }
                }

                if (!fit)
                {
                    for (int i = _Rects.Count - 1; 0 <= i; i--)
                    {
                        Rect existing = _Rects[i];

                        texRect.x = existing.xMax + Padding;
                        texRect.y = existing.y;
                        if (_Feasible(texRect, i))
                        {
                            fit   = true;
                            right = true;
                            break;
                        }
                    }
                }
            }

            if (!fit)
            {
                return(null);
            }

            _Rects.Add(texRect);

            RuntimeAtlasSegment segment = new RuntimeAtlasSegment()
            {
                texture = Texture,
                x       = Mathf.RoundToInt(texRect.x),
                y       = Mathf.RoundToInt(texRect.y),
                width   = tex.width,
                height  = tex.height
            };

            Segments.Add(segment);

            Texture.SetPixels(segment.x, segment.y, segment.width, segment.height, tex.GetPixels());

            ++_Changes;
            if (apply)
            {
                Apply();
            }
            return(segment);
        }
示例#2
0
        /// <summary>
        /// Constructs a new tk2dSpriteDefinition with the given texture
        /// </summary>
        /// <returns>A new sprite definition with the given texture</returns>
        public static tk2dSpriteDefinition ConstructDefinition(Texture2D texture)
        {
            RuntimeAtlasSegment ras = Packer.Pack(texture); //pack your resources beforehand or the outlines will turn out weird

            Material material = new Material(Shader.Find("tk2d/BlendVertexColor"));

            material.mainTexture = ras.texture;
            //material.mainTexture = texture;

            var width  = texture.width;
            var height = texture.height;

            var x = 0f;
            var y = 0f;

            var w = width / 16f;
            var h = height / 16f;

            var def = new tk2dSpriteDefinition
            {
                normals = new Vector3[] {
                    new Vector3(0.0f, 0.0f, -1.0f),
                    new Vector3(0.0f, 0.0f, -1.0f),
                    new Vector3(0.0f, 0.0f, -1.0f),
                    new Vector3(0.0f, 0.0f, -1.0f),
                },
                tangents = new Vector4[] {
                    new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
                    new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
                    new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
                    new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
                },
                texelSize       = new Vector2(1 / 16f, 1 / 16f),
                extractRegion   = false,
                regionX         = 0,
                regionY         = 0,
                regionW         = 0,
                regionH         = 0,
                flipped         = tk2dSpriteDefinition.FlipMode.None,
                complexGeometry = false,
                physicsEngine   = tk2dSpriteDefinition.PhysicsEngine.Physics3D,
                colliderType    = tk2dSpriteDefinition.ColliderType.None,
                collisionLayer  = CollisionLayer.HighObstacle,
                position0       = new Vector3(x, y, 0f),
                position1       = new Vector3(x + w, y, 0f),
                position2       = new Vector3(x, y + h, 0f),
                position3       = new Vector3(x + w, y + h, 0f),
                material        = material,
                materialInst    = material,
                materialId      = 0,
                //uvs = ETGMod.Assets.GenerateUVs(texture, 0, 0, width, height), //uv machine broke
                uvs = ras.uvs,
                boundsDataCenter           = new Vector3(w / 2f, h / 2f, 0f),
                boundsDataExtents          = new Vector3(w, h, 0f),
                untrimmedBoundsDataCenter  = new Vector3(w / 2f, h / 2f, 0f),
                untrimmedBoundsDataExtents = new Vector3(w, h, 0f),
            };

            def.name = texture.name;
            return(def);
        }