// Token: 0x06000009 RID: 9 RVA: 0x00002404 File Offset: 0x00000604
        public static tk2dSpriteDefinition ConstructDefinition(Texture2D texture)
        {
            RuntimeAtlasSegment runtimeAtlasSegment = ETGMod.Assets.Packer.Pack(texture, false);
            Material            material            = new Material(ShaderCache.Acquire(PlayerController.DefaultShaderName));

            material.mainTexture = runtimeAtlasSegment.texture;
            int   width  = texture.width;
            int   height = texture.height;
            float num    = 0f;
            float num2   = 0f;
            float num3   = (float)width / 16f;
            float num4   = (float)height / 16f;
            tk2dSpriteDefinition tk2dSpriteDefinition = new tk2dSpriteDefinition
            {
                normals = new Vector3[]
                {
                    new Vector3(0f, 0f, -1f),
                    new Vector3(0f, 0f, -1f),
                    new Vector3(0f, 0f, -1f),
                    new Vector3(0f, 0f, -1f)
                },
                tangents = new Vector4[]
                {
                    new Vector4(1f, 0f, 0f, 1f),
                    new Vector4(1f, 0f, 0f, 1f),
                    new Vector4(1f, 0f, 0f, 1f),
                    new Vector4(1f, 0f, 0f, 1f)
                },
                texelSize                  = new Vector2(0.0625f, 0.0625f),
                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(num, num2, 0f),
                position1                  = new Vector3(num + num3, num2, 0f),
                position2                  = new Vector3(num, num2 + num4, 0f),
                position3                  = new Vector3(num + num3, num2 + num4, 0f),
                material                   = material,
                materialInst               = material,
                materialId                 = 0,
                uvs                        = runtimeAtlasSegment.uvs,
                boundsDataCenter           = new Vector3(num3 / 2f, num4 / 2f, 0f),
                boundsDataExtents          = new Vector3(num3, num4, 0f),
                untrimmedBoundsDataCenter  = new Vector3(num3 / 2f, num4 / 2f, 0f),
                untrimmedBoundsDataExtents = new Vector3(num3, num4, 0f)
            };

            tk2dSpriteDefinition.name = texture.name;
            return(tk2dSpriteDefinition);
        }
예제 #2
0
 public static void ReplaceTexture(tk2dSpriteDefinition frame, Texture2D replacement, bool pack = true)
 {
     frame.flipped       = tk2dSpriteDefinition.FlipMode.None;
     frame.materialInst  = new Material(frame.material);
     frame.texelSize     = replacement.texelSize;
     frame.extractRegion = pack;
     if (pack)
     {
         RuntimeAtlasSegment segment = Packer.Pack(replacement);
         frame.materialInst.mainTexture = segment.texture;
         frame.uvs = segment.uvs;
     }
     else
     {
         frame.materialInst.mainTexture = replacement;
         frame.uvs = _DefaultUVs;
     }
 }
예제 #3
0
        public static tk2dSpriteCollectionData ReplaceDungeonCollection(tk2dSpriteCollectionData sourceCollection, Texture2D spriteSheet = null, List <string> spriteList = null)
        {
            if (sourceCollection == null)
            {
                return(null);
            }
            tk2dSpriteCollectionData collectionData = UnityEngine.Object.Instantiate(sourceCollection);

            tk2dSpriteDefinition[] spriteDefinietions = new tk2dSpriteDefinition[collectionData.spriteDefinitions.Length];
            for (int i = 0; i < collectionData.spriteDefinitions.Length; i++)
            {
                spriteDefinietions[i] = collectionData.spriteDefinitions[i].Copy();
            }
            collectionData.spriteDefinitions = spriteDefinietions;
            if (spriteSheet != null)
            {
                Material[] materials    = sourceCollection.materials;
                Material[] newMaterials = new Material[materials.Length];
                if (materials != null)
                {
                    for (int i = 0; i < materials.Length; i++)
                    {
                        newMaterials[i] = materials[i].Copy(spriteSheet);
                    }
                    collectionData.materials = newMaterials;
                    foreach (Material material2 in collectionData.materials)
                    {
                        foreach (tk2dSpriteDefinition spriteDefinition in collectionData.spriteDefinitions)
                        {
                            bool flag3 = material2 != null && spriteDefinition.material.name.Equals(material2.name);
                            if (flag3)
                            {
                                spriteDefinition.material     = material2;
                                spriteDefinition.materialInst = new Material(material2);
                            }
                        }
                    }
                }
            }
            else if (spriteList != null)
            {
                RuntimeAtlasPage runtimeAtlasPage = new RuntimeAtlasPage(0, 0, TextureFormat.RGBA32, 2);
                for (int i = 0; i < spriteList.Count; i++)
                {
                    Texture2D texture2D = ResourceExtractor.GetTextureFromResource(spriteList[i]);
                    if (!texture2D)
                    {
                        Debug.Log("[BuildDungeonCollection] Null Texture found at index: " + i);
                        goto IL_EXIT;
                    }
                    float X = (texture2D.width / 16f);
                    float Y = (texture2D.height / 16f);
                    // tk2dSpriteDefinition spriteData = collectionData.GetSpriteDefinition(i.ToString());
                    tk2dSpriteDefinition spriteData = collectionData.spriteDefinitions[i];
                    if (spriteData != null)
                    {
                        if (spriteData.boundsDataCenter != Vector3.zero)
                        {
                            try
                            {
                                // Debug.Log("[BuildDungeonCollection] Pack Existing Atlas Element at index: " + i);
                                RuntimeAtlasSegment runtimeAtlasSegment = runtimeAtlasPage.Pack(texture2D, false);
                                spriteData.materialInst.mainTexture = runtimeAtlasSegment.texture;
                                spriteData.uvs                        = runtimeAtlasSegment.uvs;
                                spriteData.extractRegion              = true;
                                spriteData.position0                  = Vector3.zero;
                                spriteData.position1                  = new Vector3(X, 0, 0);
                                spriteData.position2                  = new Vector3(0, Y, 0);
                                spriteData.position3                  = new Vector3(X, Y, 0);
                                spriteData.boundsDataCenter           = new Vector2((X / 2), (Y / 2));
                                spriteData.untrimmedBoundsDataCenter  = spriteData.boundsDataCenter;
                                spriteData.boundsDataExtents          = new Vector2(X, Y);
                                spriteData.untrimmedBoundsDataExtents = spriteData.boundsDataExtents;
                            }
                            catch (Exception)
                            {
                                Debug.Log("[BuildDungeonCollection] Exception caught at index: " + i);
                            }
                        }
                        else
                        {
                            // Debug.Log("Test 3. Replace Existing Atlas Element at index: " + i);
                            try
                            {
                                ETGMod.ReplaceTexture(spriteData, texture2D, true);
                            }
                            catch (Exception)
                            {
                                Debug.Log("[BuildDungeonCollection] Exception caught at index: " + i);
                            }
                        }
                    }
                    else
                    {
                        Debug.Log("[BuildDungeonCollection] SpriteData is null at index: " + i);
                    }
                    IL_EXIT :;
                }
                runtimeAtlasPage.Apply();
            }
            else
            {
                Debug.Log("[BuildDungeonCollection] SpriteList is null!");
            }
            return(collectionData);
        }
예제 #4
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 = ETGMod.Assets.Packer.Pack(texture); //pack your resources beforehand or the outlines will turn out weird

            Material material = new Material(ShaderCache.Acquire(PlayerController.DefaultShaderName));

            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);
        }
예제 #5
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);
    }