public void ClearChunk(ColorChunk chunk)
 {
     for (int i = 0; i < chunk.colors.Length; i++)
     {
         chunk.colors[i] = this.clearColor;
     }
 }
Esempio n. 2
0
		void Optimize(ColorChunk chunk)
		{
			bool empty = true;
			Color32 clearColor32 = this.clearColor;
			foreach (var c in chunk.colors)
			{
				if (c.r != clearColor32.r ||
					c.g != clearColor32.g ||
					c.b != clearColor32.b ||
					c.a != clearColor32.a)
				{
					empty = false;
					break;
				}
			}
			
			if (empty)
				chunk.colors = new Color32[0];
		}
Esempio n. 3
0
        public static void BuildForChunk(tk2dTileMap tileMap, SpriteChunk chunk, ColorChunk colorChunk, bool useColor, bool skipPrefabs, int baseX, int baseY)
        {
            List <Vector3> meshVertices = new List <Vector3>();
            List <Color>   meshColors   = new List <Color>();
            List <Vector2> meshUvs      = new List <Vector2>();

            //List<int> meshIndices = new List<int>();

            int[]   spriteIds   = chunk.spriteIds;
            Vector3 tileSize    = tileMap.data.tileSize;
            int     spriteCount = tileMap.SpriteCollectionInst.spriteDefinitions.Length;

            Object[] tilePrefabs = tileMap.data.tilePrefabs;

            Color32 clearColor = (useColor && tileMap.ColorChannel != null)?tileMap.ColorChannel.clearColor:Color.white;

            // revert to no color mode (i.e. fill with clear color) when there isn't a color channel, or it is empty
            if (colorChunk == null || colorChunk.colors.Length == 0)
            {
                useColor = false;
            }

            int x0, x1, dx;
            int y0, y1, dy;

            BuilderUtil.GetLoopOrder(tileMap.data.sortMethod,
                                     tileMap.partitionSizeX, tileMap.partitionSizeY,
                                     out x0, out x1, out dx,
                                     out y0, out y1, out dy);

            float xOffsetMult = 0.0f, yOffsetMult = 0.0f;

            tileMap.data.GetTileOffset(out xOffsetMult, out yOffsetMult);

            List <int>[] meshIndices = new List <int> [tileMap.SpriteCollectionInst.materials.Length];
            for (int j = 0; j < meshIndices.Length; ++j)
            {
                meshIndices[j] = new List <int>();
            }

            int colorChunkSize = tileMap.partitionSizeX + 1;

            for (int y = y0; y != y1; y += dy)
            {
                float xOffset = ((baseY + y) & 1) * xOffsetMult;
                for (int x = x0; x != x1; x += dx)
                {
                    int     tile       = spriteIds[y * tileMap.partitionSizeX + x];
                    Vector3 currentPos = new Vector3(tileSize.x * (x + xOffset), tileSize.y * y, 0);

                    if (tile < 0 || tile >= spriteCount)
                    {
                        continue;
                    }

                    if (skipPrefabs && tilePrefabs[tile])
                    {
                        continue;
                    }

                    var sprite = tileMap.SpriteCollectionInst.spriteDefinitions[tile];

                    int baseVertex = meshVertices.Count;
                    for (int v = 0; v < sprite.positions.Length; ++v)
                    {
                        if (useColor)
                        {
                            Color tileColorx0y0 = colorChunk.colors[y * colorChunkSize + x];
                            Color tileColorx1y0 = colorChunk.colors[y * colorChunkSize + x + 1];
                            Color tileColorx0y1 = colorChunk.colors[(y + 1) * colorChunkSize + x];
                            Color tileColorx1y1 = colorChunk.colors[(y + 1) * colorChunkSize + (x + 1)];

                            Vector3 centeredSpriteVertex = sprite.positions[v] - sprite.untrimmedBoundsData[0];
                            Vector3 alignedSpriteVertex  = centeredSpriteVertex + tileMap.data.tileSize * 0.5f;
                            float   tileColorX           = Mathf.Clamp01(alignedSpriteVertex.x / tileMap.data.tileSize.x);
                            float   tileColorY           = Mathf.Clamp01(alignedSpriteVertex.y / tileMap.data.tileSize.y);

                            Color color = Color.Lerp(
                                Color.Lerp(tileColorx0y0, tileColorx1y0, tileColorX),
                                Color.Lerp(tileColorx0y1, tileColorx1y1, tileColorX),
                                tileColorY);
                            meshColors.Add(color);
                        }
                        else
                        {
                            meshColors.Add(clearColor);
                        }

                        meshVertices.Add(currentPos + sprite.positions[v]);
                        meshUvs.Add(sprite.uvs[v]);
                    }

                    List <int> indices = meshIndices[sprite.materialId];
                    for (int i = 0; i < sprite.indices.Length; ++i)
                    {
                        indices.Add(baseVertex + sprite.indices[i]);
                    }
                }
            }

            if (chunk.mesh == null)
            {
                chunk.mesh = tileMap.GetOrCreateMesh();
            }

            chunk.mesh.vertices = meshVertices.ToArray();
            chunk.mesh.uv       = meshUvs.ToArray();
            chunk.mesh.colors   = meshColors.ToArray();

            List <Material> materials    = new List <Material>();
            int             materialId   = 0;
            int             subMeshCount = 0;

            foreach (var indices in meshIndices)
            {
                if (indices.Count > 0)
                {
                    materials.Add(tileMap.SpriteCollectionInst.materials[materialId]);
                    subMeshCount++;
                }
                materialId++;
            }
            if (subMeshCount > 0)
            {
                chunk.mesh.subMeshCount             = subMeshCount;
                chunk.gameObject.renderer.materials = materials.ToArray();
                int subMeshId = 0;
                foreach (var indices in meshIndices)
                {
                    if (indices.Count > 0)
                    {
                        chunk.mesh.SetTriangles(indices.ToArray(), subMeshId);
                        subMeshId++;
                    }
                }
            }

            chunk.mesh.RecalculateBounds();

            if (tileMap.serializeRenderData)
            {
                chunk.mesh.RecalculateNormals();
            }

            var meshFilter = chunk.gameObject.GetComponent <MeshFilter>();

            meshFilter.sharedMesh = chunk.mesh;
        }
Esempio n. 4
0
        public static void BuildForChunk(tk2dTileMap tileMap, SpriteChunk chunk, ColorChunk colorChunk, bool useColor, bool skipPrefabs, int baseX, int baseY)
        {
            List <Vector3> meshVertices = new List <Vector3>();
            List <Color>   meshColors   = new List <Color>();
            List <Vector2> meshUvs      = new List <Vector2>();
            List <Vector2> meshUv2s     = new List <Vector2>();

            //List<int> meshIndices = new List<int>();

            int[]   spriteIds   = chunk.spriteIds;
            Vector3 tileSize    = tileMap.data.tileSize;
            int     spriteCount = tileMap.SpriteCollectionInst.spriteDefinitions.Length;

            Object[]             tilePrefabs = tileMap.data.tilePrefabs;
            tk2dSpriteDefinition firstSprite = tileMap.SpriteCollectionInst.FirstValidDefinition;
            bool buildNormals = (firstSprite != null && firstSprite.normals != null && firstSprite.normals.Length > 0);

            bool generateUv2 = tileMap.data.generateUv2;
            var  colorMode   = tileMap.data.colorMode;

            Color32 clearColor = (useColor && tileMap.ColorChannel != null)?tileMap.ColorChannel.clearColor:Color.white;

            // revert to no color mode (i.e. fill with clear color) when there isn't a color channel, or it is empty
            if (colorChunk == null || colorChunk.colors.Length == 0)
            {
                useColor = false;
            }

            int x0, x1, dx;
            int y0, y1, dy;

            BuilderUtil.GetLoopOrder(tileMap.data.sortMethod,
                                     tileMap.partitionSizeX, tileMap.partitionSizeY,
                                     out x0, out x1, out dx,
                                     out y0, out y1, out dy);

            float xOffsetMult = 0.0f, yOffsetMult = 0.0f;

            tileMap.data.GetTileOffset(out xOffsetMult, out yOffsetMult);

            List <int>[] meshIndices = new List <int> [tileMap.SpriteCollectionInst.materials.Length];
            for (int j = 0; j < meshIndices.Length; ++j)
            {
                meshIndices[j] = new List <int>();
            }

            int colorChunkSize = tileMap.partitionSizeX + 1;

            for (int y = y0; y != y1; y += dy)
            {
                float xOffset = ((baseY + y) & 1) * xOffsetMult;
                for (int x = x0; x != x1; x += dx)
                {
                    int  spriteId = spriteIds[y * tileMap.partitionSizeX + x];
                    int  tile     = BuilderUtil.GetTileFromRawTile(spriteId);
                    bool flipH    = BuilderUtil.IsRawTileFlagSet(spriteId, tk2dTileFlags.FlipX);
                    bool flipV    = BuilderUtil.IsRawTileFlagSet(spriteId, tk2dTileFlags.FlipY);
                    bool rot90    = BuilderUtil.IsRawTileFlagSet(spriteId, tk2dTileFlags.Rot90);

                    Vector3 currentPos = new Vector3(tileSize.x * (x + xOffset), tileSize.y * y, 0);

                    if (tile < 0 || tile >= spriteCount)
                    {
                        continue;
                    }

                    if (skipPrefabs && tilePrefabs[tile])
                    {
                        continue;
                    }

                    var sprite = tileMap.SpriteCollectionInst.spriteDefinitions[tile];

                    int baseVertex = meshVertices.Count;
                    for (int v = 0; v < sprite.positions.Length; ++v)
                    {
                        Vector3 flippedPos = BuilderUtil.ApplySpriteVertexTileFlags(tileMap, sprite, sprite.positions[v], flipH, flipV, rot90);

                        if (useColor && colorChunk != null)
                        {
                            Color tileColorx0y0 = colorChunk.colors[y * colorChunkSize + x];
                            Color tileColorx1y0 = colorChunk.colors[y * colorChunkSize + x + 1];
                            Color tileColorx0y1 = colorChunk.colors[(y + 1) * colorChunkSize + x];
                            Color tileColorx1y1 = colorChunk.colors[(y + 1) * colorChunkSize + (x + 1)];

                            switch (colorMode)
                            {
                            case tk2dTileMapData.ColorMode.Interpolate:
                            {
                                Vector3 centeredSpriteVertex = flippedPos - sprite.untrimmedBoundsData[0];
                                Vector3 alignedSpriteVertex  = centeredSpriteVertex + tileMap.data.tileSize * 0.5f;
                                float   tileColorX           = Mathf.Clamp01(alignedSpriteVertex.x / tileMap.data.tileSize.x);
                                float   tileColorY           = Mathf.Clamp01(alignedSpriteVertex.y / tileMap.data.tileSize.y);

                                Color color = Color.Lerp(
                                    Color.Lerp(tileColorx0y0, tileColorx1y0, tileColorX),
                                    Color.Lerp(tileColorx0y1, tileColorx1y1, tileColorX),
                                    tileColorY);
                                meshColors.Add(color);
                                break;
                            }

                            case tk2dTileMapData.ColorMode.Solid:
                            {
                                meshColors.Add(tileColorx0y0);
                                break;
                            }
                            }
                        }
                        else
                        {
                            meshColors.Add(clearColor);
                        }

                        if (generateUv2)
                        {
                            if (sprite.normalizedUvs.Length == 0)
                            {
                                meshUv2s.Add(Vector2.zero);
                            }
                            else
                            {
                                meshUv2s.Add(sprite.normalizedUvs[v]);
                            }
                        }

                        meshVertices.Add(currentPos + flippedPos);
                        meshUvs.Add(sprite.uvs[v]);
                    }

                    bool reverseIndices = false;                     // flipped?
                    if (flipH)
                    {
                        reverseIndices = !reverseIndices;
                    }
                    if (flipV)
                    {
                        reverseIndices = !reverseIndices;
                    }

                    List <int> indices = meshIndices[sprite.materialId];
                    for (int i = 0; i < sprite.indices.Length; ++i)
                    {
                        int j = reverseIndices ? (sprite.indices.Length - 1 - i) : i;
                        indices.Add(baseVertex + sprite.indices[j]);
                    }
                }
            }

            if (chunk.mesh == null)
            {
                chunk.mesh = tk2dUtil.CreateMesh();
            }

            chunk.mesh.Clear();

            chunk.mesh.vertices = meshVertices.ToArray();
            chunk.mesh.uv       = meshUvs.ToArray();
            if (generateUv2)
            {
                chunk.mesh.uv2 = meshUv2s.ToArray();
            }
            chunk.mesh.colors = meshColors.ToArray();

            List <Material> materials    = new List <Material>();
            int             materialId   = 0;
            int             subMeshCount = 0;

            foreach (var indices in meshIndices)
            {
                if (indices.Count > 0)
                {
                    materials.Add(tileMap.SpriteCollectionInst.materialInsts[materialId]);
                    subMeshCount++;
                }
                materialId++;
            }
            if (subMeshCount > 0)
            {
                chunk.mesh.subMeshCount = subMeshCount;
                chunk.gameObject.GetComponent <Renderer>().materials = materials.ToArray();
                int subMeshId = 0;
                foreach (var indices in meshIndices)
                {
                    if (indices.Count > 0)
                    {
                        chunk.mesh.SetTriangles(indices.ToArray(), subMeshId);
                        subMeshId++;
                    }
                }
            }

            chunk.mesh.RecalculateBounds();
            if (buildNormals)
            {
                chunk.mesh.RecalculateNormals();
            }

            var meshFilter = chunk.gameObject.GetComponent <MeshFilter>();

            meshFilter.sharedMesh = chunk.mesh;
        }
Esempio n. 5
0
        public static void Build(tk2dTileMap tileMap, bool editMode, bool forceBuild)
        {
            bool skipPrefabs = editMode?false:true;
            bool incremental = !forceBuild;
            int  numLayers   = tileMap.data.NumLayers;

            for (int layerId = 0; layerId < numLayers; ++layerId)
            {
                var layer = tileMap.Layers[layerId];
                if (layer.IsEmpty)
                {
                    continue;
                }

                var  layerData = tileMap.data.Layers[layerId];
                bool useColor  = !tileMap.ColorChannel.IsEmpty && tileMap.data.Layers[layerId].useColor;
#if !(UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2)
                bool useSortingLayer = tileMap.data.useSortingLayers;
#endif

                for (int cellY = 0; cellY < layer.numRows; ++cellY)
                {
                    int baseY = cellY * layer.divY;
                    for (int cellX = 0; cellX < layer.numColumns; ++cellX)
                    {
                        int baseX = cellX * layer.divX;
                        var chunk = layer.GetChunk(cellX, cellY);

                        ColorChunk colorChunk = tileMap.ColorChannel.GetChunk(cellX, cellY);

                        bool colorChunkDirty = (colorChunk != null) && colorChunk.Dirty;
                        if (incremental && !colorChunkDirty && !chunk.Dirty)
                        {
                            continue;
                        }

                        if (chunk.mesh != null)
                        {
                            chunk.mesh.Clear();
                        }

                        if (chunk.IsEmpty)
                        {
                            continue;
                        }

                        if (editMode ||
                            (!editMode && !layerData.skipMeshGeneration))
                        {
                            BuildForChunk(tileMap, chunk, colorChunk, useColor, skipPrefabs, baseX, baseY);

#if !(UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2)
                            if (chunk.gameObject != null && useSortingLayer)
                            {
                                Renderer r = chunk.gameObject.GetComponent <Renderer>();
                                if (r != null)
                                {
                                    r.sortingLayerName = layerData.sortingLayerName;
                                    r.sortingOrder     = layerData.sortingOrder;
                                }
                            }
#endif
                        }

                        if (chunk.mesh != null)
                        {
                            tileMap.TouchMesh(chunk.mesh);
                        }
                    }
                }
            }
        }
		public static void BuildForChunk(tk2dTileMap tileMap, SpriteChunk chunk, ColorChunk colorChunk, bool useColor, bool skipPrefabs, int baseX, int baseY)
		{
			List<Vector3> meshVertices = new List<Vector3>();
			List<Color> meshColors = new List<Color>();
			List<Vector2> meshUvs = new List<Vector2>();
			//List<int> meshIndices = new List<int>();
			
			int[] spriteIds = chunk.spriteIds;
			Vector3 tileSize = tileMap.data.tileSize;
			int spriteCount = tileMap.SpriteCollectionInst.spriteDefinitions.Length;
			Object[] tilePrefabs = tileMap.data.tilePrefabs;
			tk2dSpriteDefinition firstSprite = tileMap.SpriteCollectionInst.FirstValidDefinition;
			bool buildNormals = (firstSprite != null && firstSprite.normals != null && firstSprite.normals.Length > 0);
			
			Color32 clearColor = (useColor && tileMap.ColorChannel != null)?tileMap.ColorChannel.clearColor:Color.white;
					
			// revert to no color mode (i.e. fill with clear color) when there isn't a color channel, or it is empty
			if (colorChunk == null || colorChunk.colors.Length == 0)
				useColor = false;
			
			int x0, x1, dx;
			int y0, y1, dy;
			BuilderUtil.GetLoopOrder(tileMap.data.sortMethod, 
				tileMap.partitionSizeX, tileMap.partitionSizeY, 
				out x0, out x1, out dx,
				out y0, out y1, out dy);
			
			float xOffsetMult = 0.0f, yOffsetMult = 0.0f;
			tileMap.data.GetTileOffset(out xOffsetMult, out yOffsetMult);
			
			List<int>[] meshIndices = new List<int>[tileMap.SpriteCollectionInst.materials.Length];
			for (int j = 0; j < meshIndices.Length; ++j)
				meshIndices[j] = new List<int>();
			
			int colorChunkSize = tileMap.partitionSizeX + 1;
			for (int y = y0; y != y1; y += dy)
			{
				float xOffset = ((baseY + y) & 1) * xOffsetMult;
				for (int x = x0; x != x1; x += dx)
				{
					int spriteId = spriteIds[y * tileMap.partitionSizeX + x];
					int tile = BuilderUtil.GetTileFromRawTile(spriteId);
					bool flipH = BuilderUtil.IsRawTileFlagSet(spriteId, tk2dTileFlags.FlipX);
					bool flipV = BuilderUtil.IsRawTileFlagSet(spriteId, tk2dTileFlags.FlipY);
					bool rot90 = BuilderUtil.IsRawTileFlagSet(spriteId, tk2dTileFlags.Rot90);

					Vector3 currentPos = new Vector3(tileSize.x * (x + xOffset), tileSize.y * y, 0);
	
					if (tile < 0 || tile >= spriteCount) 
						continue;
					
					if (skipPrefabs && tilePrefabs[tile])
						continue;
					
					var sprite = tileMap.SpriteCollectionInst.spriteDefinitions[tile];
					
					int baseVertex = meshVertices.Count;
					for (int v = 0; v < sprite.positions.Length; ++v)
					{
						Vector3 flippedPos = BuilderUtil.ApplySpriteVertexTileFlags(tileMap, sprite, sprite.positions[v], flipH, flipV, rot90);

						if (useColor)
						{
							Color tileColorx0y0 = colorChunk.colors[y * colorChunkSize + x];
							Color tileColorx1y0 = colorChunk.colors[y * colorChunkSize + x + 1];
							Color tileColorx0y1 = colorChunk.colors[(y + 1) * colorChunkSize + x];
							Color tileColorx1y1 = colorChunk.colors[(y + 1) * colorChunkSize + (x + 1)];
							
							Vector3 centeredSpriteVertex = flippedPos - sprite.untrimmedBoundsData[0];
							Vector3 alignedSpriteVertex = centeredSpriteVertex + tileMap.data.tileSize * 0.5f;
							float tileColorX = Mathf.Clamp01(alignedSpriteVertex.x / tileMap.data.tileSize.x);
							float tileColorY = Mathf.Clamp01(alignedSpriteVertex.y / tileMap.data.tileSize.y);
							
							Color color = Color.Lerp(
										  Color.Lerp(tileColorx0y0, tileColorx1y0, tileColorX),
										  Color.Lerp(tileColorx0y1, tileColorx1y1, tileColorX),
										  tileColorY);
							meshColors.Add(color);
						}
						else
						{
							meshColors.Add(clearColor);
						}

						meshVertices.Add(currentPos + flippedPos);
						meshUvs.Add(sprite.uvs[v]);
					}

					bool reverseIndices = false; // flipped?
					if (flipH) reverseIndices = !reverseIndices;
					if (flipV) reverseIndices = !reverseIndices;
					
					List<int> indices = meshIndices[sprite.materialId];
					for (int i = 0; i < sprite.indices.Length; ++i) {
						int j = reverseIndices ? (sprite.indices.Length - 1 - i) : i;
						indices.Add(baseVertex + sprite.indices[j]);
					}
					
				}
			}
			
			if (chunk.mesh == null)
				chunk.mesh = tk2dUtil.CreateMesh();

			chunk.mesh.vertices = meshVertices.ToArray();
			chunk.mesh.uv = meshUvs.ToArray();
			chunk.mesh.colors = meshColors.ToArray();

			List<Material> materials = new List<Material>();
			int materialId = 0;
			int subMeshCount = 0;
			foreach (var indices in meshIndices)
			{
				if (indices.Count > 0)
				{
					materials.Add(tileMap.SpriteCollectionInst.materialInsts[materialId]);
					subMeshCount++;
				}
				materialId++;
			}
			if (subMeshCount > 0)
			{
				chunk.mesh.subMeshCount = subMeshCount;
				chunk.gameObject.renderer.materials = materials.ToArray();
				int subMeshId = 0;
				foreach (var indices in meshIndices)
				{
					if (indices.Count > 0)
					{
						chunk.mesh.SetTriangles(indices.ToArray(), subMeshId);
						subMeshId++;
					}
				}
			}
			
			chunk.mesh.RecalculateBounds();
			if (buildNormals) {
				chunk.mesh.RecalculateNormals();
			}

			var meshFilter = chunk.gameObject.GetComponent<MeshFilter>();
			meshFilter.sharedMesh = chunk.mesh;
		}
 private void Optimize(ColorChunk chunk)
 {
     bool flag = true;
     Color32 clearColor = this.clearColor;
     foreach (Color32 color2 in chunk.colors)
     {
         if (((color2.r != clearColor.r) || (color2.g != clearColor.g)) || ((color2.b != clearColor.b) || (color2.a != clearColor.a)))
         {
             flag = false;
             break;
         }
     }
     if (flag)
     {
         chunk.colors = new Color32[0];
     }
 }
 private void InitChunk(ColorChunk chunk)
 {
     if (chunk.colors.Length == 0)
     {
         chunk.colors = new Color32[(this.divX + 1) * (this.divY + 1)];
         for (int i = 0; i < chunk.colors.Length; i++)
         {
             chunk.colors[i] = this.clearColor;
         }
     }
 }
 public static unsafe void BuildForChunk(tk2dTileMap tileMap, SpriteChunk chunk, ColorChunk colorChunk, bool useColor, bool skipPrefabs, int baseX, int baseY)
 {
     int num2;
     int num3;
     int num4;
     int num5;
     int num6;
     int num7;
     List<Vector3> list = new List<Vector3>();
     List<Color> list2 = new List<Color>();
     List<Vector2> list3 = new List<Vector2>();
     int[] spriteIds = chunk.spriteIds;
     Vector3 tileSize = tileMap.data.tileSize;
     int length = tileMap.SpriteCollectionInst.spriteDefinitions.Length;
     UnityEngine.Object[] tilePrefabs = tileMap.data.tilePrefabs;
     tk2dSpriteDefinition firstValidDefinition = tileMap.SpriteCollectionInst.FirstValidDefinition;
     bool flag = ((firstValidDefinition != null) && (firstValidDefinition.normals != null)) && (firstValidDefinition.normals.Length > 0);
     Color32 color = (!useColor || (tileMap.ColorChannel == null)) ? Color.white : tileMap.ColorChannel.clearColor;
     if ((colorChunk == null) || (colorChunk.colors.Length == 0))
     {
         useColor = false;
     }
     BuilderUtil.GetLoopOrder(tileMap.data.sortMethod, tileMap.partitionSizeX, tileMap.partitionSizeY, out num2, out num3, out num4, out num5, out num6, out num7);
     float x = 0f;
     float y = 0f;
     tileMap.data.GetTileOffset(out x, out y);
     List<int>[] listArray = new List<int>[tileMap.SpriteCollectionInst.materials.Length];
     for (int i = 0; i < listArray.Length; i++)
     {
         listArray[i] = new List<int>();
     }
     int num11 = tileMap.partitionSizeX + 1;
     for (int j = num5; j != num6; j += num7)
     {
         float num13 = ((baseY + j) & 1) * x;
         for (int k = num2; k != num3; k += num4)
         {
             int rawTile = spriteIds[(j * tileMap.partitionSizeX) + k];
             int tileFromRawTile = BuilderUtil.GetTileFromRawTile(rawTile);
             bool flipH = BuilderUtil.IsRawTileFlagSet(rawTile, tk2dTileFlags.FlipX);
             bool flipV = BuilderUtil.IsRawTileFlagSet(rawTile, tk2dTileFlags.FlipY);
             bool flag4 = BuilderUtil.IsRawTileFlagSet(rawTile, tk2dTileFlags.Rot90);
             Vector3 vector2 = new Vector3(tileSize.x * (k + num13), tileSize.y * j, 0f);
             if (((tileFromRawTile >= 0) && (tileFromRawTile < length)) && (!skipPrefabs || (tilePrefabs[tileFromRawTile] == null)))
             {
                 tk2dSpriteDefinition spriteDef = tileMap.SpriteCollectionInst.spriteDefinitions[tileFromRawTile];
                 int count = list.Count;
                 for (int m = 0; m < spriteDef.positions.Length; m++)
                 {
                     Vector3 vector3 = BuilderUtil.ApplySpriteVertexTileFlags(tileMap, spriteDef, spriteDef.positions[m], flipH, flipV, flag4);
                     if (useColor)
                     {
                         Color a = *((Color*) &(colorChunk.colors[(j * num11) + k]));
                         Color b = *((Color*) &(colorChunk.colors[((j * num11) + k) + 1]));
                         Color color4 = *((Color*) &(colorChunk.colors[((j + 1) * num11) + k]));
                         Color color5 = *((Color*) &(colorChunk.colors[((j + 1) * num11) + (k + 1)]));
                         Vector3 vector4 = vector3 - spriteDef.untrimmedBoundsData[0];
                         Vector3 vector5 = vector4 + ((Vector3) (tileMap.data.tileSize * 0.5f));
                         float t = Mathf.Clamp01(vector5.x / tileMap.data.tileSize.x);
                         float num20 = Mathf.Clamp01(vector5.y / tileMap.data.tileSize.y);
                         Color item = Color.Lerp(Color.Lerp(a, b, t), Color.Lerp(color4, color5, t), num20);
                         list2.Add(item);
                     }
                     else
                     {
                         list2.Add((Color) color);
                     }
                     list.Add(vector2 + vector3);
                     list3.Add(spriteDef.uvs[m]);
                 }
                 bool flag5 = false;
                 if (flipH)
                 {
                     flag5 = !flag5;
                 }
                 if (flipV)
                 {
                     flag5 = !flag5;
                 }
                 List<int> list4 = listArray[spriteDef.materialId];
                 for (int n = 0; n < spriteDef.indices.Length; n++)
                 {
                     int num22 = !flag5 ? n : ((spriteDef.indices.Length - 1) - n);
                     list4.Add(count + spriteDef.indices[num22]);
                 }
             }
         }
     }
     if (chunk.mesh == null)
     {
         chunk.mesh = tk2dUtil.CreateMesh();
     }
     chunk.mesh.vertices = list.ToArray();
     chunk.mesh.uv = list3.ToArray();
     chunk.mesh.colors = list2.ToArray();
     List<Material> list5 = new List<Material>();
     int index = 0;
     int num24 = 0;
     foreach (List<int> list6 in listArray)
     {
         if (list6.Count > 0)
         {
             list5.Add(tileMap.SpriteCollectionInst.materialInsts[index]);
             num24++;
         }
         index++;
     }
     if (num24 > 0)
     {
         chunk.mesh.subMeshCount = num24;
         chunk.gameObject.renderer.materials = list5.ToArray();
         int submesh = 0;
         foreach (List<int> list7 in listArray)
         {
             if (list7.Count > 0)
             {
                 chunk.mesh.SetTriangles(list7.ToArray(), submesh);
                 submesh++;
             }
         }
     }
     chunk.mesh.RecalculateBounds();
     if (flag)
     {
         chunk.mesh.RecalculateNormals();
     }
     chunk.gameObject.GetComponent<MeshFilter>().sharedMesh = chunk.mesh;
 }
Esempio n. 10
0
 public void Create()
 {
     chunks = new ColorChunk[numColumns * numRows];
     for (int i = 0; i < chunks.Length; ++i)
         chunks[i] = new ColorChunk();
 }
Esempio n. 11
0
 public void ClearChunk(ColorChunk chunk)
 {
     for (int i = 0; i < chunk.colors.Length; ++i)
         chunk.colors[i] = clearColor;
 }
Esempio n. 12
0
        void Optimize(ColorChunk chunk)
        {
            bool empty = true;
            Color32 clearColor32 = this.clearColor;
            foreach (var c in chunk.colors)
            {
                if (c.r != clearColor32.r ||
                    c.g != clearColor32.g ||
                    c.b != clearColor32.b ||
                    c.a != clearColor32.a)
                {
                    empty = false;
                    break;
                }
            }

            if (empty)
                chunk.colors = new Color32[0];
        }
Esempio n. 13
0
 // create chunk if it doesn't already exist
 void InitChunk(ColorChunk chunk)
 {
     if (chunk.colors.Length == 0)
     {
         chunk.colors = new Color32[(divX + 1) * (divY + 1)];
         for (int i = 0; i < chunk.colors.Length; ++i)
             chunk.colors[i] = clearColor;
     }
 }
Esempio n. 14
0
        public static void BuildForChunk(tk2dTileMap tileMap, SpriteChunk chunk, ColorChunk colorChunk, bool useColor, bool skipPrefabs, int baseX, int baseY)
        {
            List<Vector3> meshVertices = new List<Vector3>();
            List<Color> meshColors = new List<Color>();
            List<Vector2> meshUvs = new List<Vector2>();
            //List<int> meshIndices = new List<int>();

            int[] spriteIds = chunk.spriteIds;
            Vector3 tileSize = tileMap.data.tileSize;
            int spriteCount = tileMap.spriteCollection.spriteDefinitions.Length;
            Object[] tilePrefabs = tileMap.data.tilePrefabs;

            Color32 clearColor = (useColor && tileMap.ColorChannel != null)?tileMap.ColorChannel.clearColor:Color.white;

            // revert to no color mode (i.e. fill with clear color) when there isn't a color channel, or it is empty
            if (colorChunk == null || colorChunk.colors.Length == 0)
                useColor = false;

            int x0, x1, dx;
            int y0, y1, dy;
            BuilderUtil.GetLoopOrder(tileMap.data.sortMethod,
                tileMap.partitionSizeX, tileMap.partitionSizeY,
                out x0, out x1, out dx,
                out y0, out y1, out dy);

            List<int>[] meshIndices = new List<int>[tileMap.spriteCollection.materials.Length];
            for (int j = 0; j < meshIndices.Length; ++j)
                meshIndices[j] = new List<int>();

            int colorChunkSize = tileMap.partitionSizeX + 1;
            for (int y = y0; y != y1; y += dy)
            {
                for (int x = x0; x != x1; x += dx)
                {
                    int tile = spriteIds[y * tileMap.partitionSizeX + x];
                    Vector3 currentPos = new Vector3(tileSize.x * x, tileSize.y * y, 0);

                    if (tile < 0 || tile >= spriteCount)
                        continue;

                    if (skipPrefabs && tilePrefabs[tile])
                        continue;

                    var sprite = tileMap.spriteCollection.spriteDefinitions[tile];

                    int baseVertex = meshVertices.Count;
                    for (int v = 0; v < sprite.positions.Length; ++v)
                    {
                        if (useColor)
                        {
                            Color tileColorx0y0 = colorChunk.colors[y * colorChunkSize + x];
                            Color tileColorx1y0 = colorChunk.colors[y * colorChunkSize + x + 1];
                            Color tileColorx0y1 = colorChunk.colors[(y + 1) * colorChunkSize + x];
                            Color tileColorx1y1 = colorChunk.colors[(y + 1) * colorChunkSize + (x + 1)];

                            Vector3 centeredSpriteVertex = sprite.positions[v] - sprite.untrimmedBoundsData[0];
                            Vector3 alignedSpriteVertex = centeredSpriteVertex + tileMap.data.tileSize * 0.5f;
                            float tileColorX = Mathf.Clamp01(alignedSpriteVertex.x / tileMap.data.tileSize.x);
                            float tileColorY = Mathf.Clamp01(alignedSpriteVertex.y / tileMap.data.tileSize.y);

                            Color color = Color.Lerp(
                                          Color.Lerp(tileColorx0y0, tileColorx1y0, tileColorX),
                                          Color.Lerp(tileColorx0y1, tileColorx1y1, tileColorX),
                                          tileColorY);
                            meshColors.Add(color);
                        }
                        else
                        {
                            meshColors.Add(clearColor);
                        }

                        meshVertices.Add(currentPos + sprite.positions[v]);
                        meshUvs.Add(sprite.uvs[v]);
                    }

                    List<int> indices = meshIndices[sprite.materialId];
                    for (int i = 0; i < sprite.indices.Length; ++i)
                        indices.Add(baseVertex + sprite.indices[i]);

                }
            }

            if (chunk.mesh == null)
                chunk.mesh = tileMap.GetOrCreateMesh();

            chunk.mesh.vertices = meshVertices.ToArray();
            chunk.mesh.uv = meshUvs.ToArray();
            chunk.mesh.colors = meshColors.ToArray();

            List<Material> materials = new List<Material>();
            int materialId = 0;
            int subMeshCount = 0;
            foreach (var indices in meshIndices)
            {
                if (indices.Count > 0)
                {
                    materials.Add(tileMap.spriteCollection.materials[materialId]);
                    subMeshCount++;
                }
                materialId++;
            }
            if (subMeshCount > 0)
            {
                chunk.mesh.subMeshCount = subMeshCount;
                chunk.gameObject.renderer.materials = materials.ToArray();
                int subMeshId = 0;
                foreach (var indices in meshIndices)
                {
                    if (indices.Count > 0)
                    {
                        chunk.mesh.SetTriangles(indices.ToArray(), subMeshId);
                        subMeshId++;
                    }
                }
            }

            chunk.mesh.RecalculateBounds();

            if (tileMap.serializeRenderData)
                chunk.mesh.RecalculateNormals();

            var meshFilter = chunk.gameObject.GetComponent<MeshFilter>();
            meshFilter.sharedMesh = chunk.mesh;
        }
Esempio n. 15
0
		public void Create()
		{
			chunks = new ColorChunk[numColumns * numRows];
			for (int i = 0; i < chunks.Length; ++i)
				chunks[i] = new ColorChunk();
		}
Esempio n. 16
0
		public void ClearChunk(ColorChunk chunk)
		{
			for (int i = 0; i < chunk.colors.Length; ++i)
				chunk.colors[i] = clearColor;
		}