public int Bake(string ID, UnityEngine.Sprite[] spriteSheet, int instanceCapacityOverride = DEFAULT_INSTANCE_CAPACITY) { UnityEngine.Texture textureCache = spriteSheet[0].texture; for (int i = 0; i < spriteSheet.Length; i++) { if (spriteSheet[i].texture != textureCache) { throw new System.ArgumentException("Baking sprites not within same texture is not allowed!"); } } float w = textureCache.width; float h = textureCache.height; float4[] uvs = new float4[spriteSheet.Length]; for (int i = 0; i < spriteSheet.Length; i++) { float tilingX = 1f / (w / spriteSheet[i].rect.width); float tilingY = 1f / (h / spriteSheet[i].rect.height); float OffsetX = tilingX * (spriteSheet[i].rect.x / spriteSheet[i].rect.width); float OffsetY = tilingY * (spriteSheet[i].rect.y / spriteSheet[i].rect.height); uvs[i].x = tilingX; uvs[i].y = tilingY; uvs[i].z = OffsetX; uvs[i].w = OffsetY; } UnityEngine.Material newMat = UnityEngine.Material.Instantiate(BaseMateirial); newMat.mainTexture = textureCache; BakedSpriteSheet newBaked = new BakedSpriteSheet(Baked, spriteSheet.Length, newMat); bakedSpriteSheets[Baked] = newBaked; SpriteSheetPointer newFilter = new SpriteSheetPointer { spriteSheetIndex = Baked }; filters[Baked] = newFilter; BufferStructureChanged.Add(true); spriteSheetUVs[Baked] = new NativeArray <float4>(uvs, Allocator.Persistent); newBaked.UpdateUVBuffers(spriteSheetUVs[Baked]); newBaked.Capacity = (uint)instanceCapacityOverride; sheetInstanceUVPointers[Baked] = new NativeArray <int>(instanceCapacityOverride, Allocator.Persistent); sheetInstanceColors[Baked] = new NativeArray <float4>(instanceCapacityOverride, Allocator.Persistent); sheetInstanceMatrixs[Baked] = new NativeArray <float4>(instanceCapacityOverride, Allocator.Persistent); freeIndexs[Baked] = new NativeList <int>(8, Allocator.Persistent); BakedSpriteSheetMap[ID] = Baked; Baked++; if (Baked >= bakedSpriteSheets.Length) { GrowBuffersForNewSpriteSheet(); } UnityEngine.Debug.Log(string.Format("Baked sprite sheet: {0}! Current baked: {1}", ID, Baked)); return(Baked); }
protected override void OnUpdate() { if (removedSubjectQuery.CalculateEntityCount() > 0) { var removed = removedSubjectQuery.ToEntityArray(Allocator.TempJob); for (int i = 0; i < removed.Length; i++) { SpriteRenderSubject removedSubject = EntityManager.GetComponentData <SpriteRenderSubject>(removed[i]); SpriteSheetPointer removedSubjectPointer = EntityManager.GetSharedComponentData <SpriteSheetPointer>(removed[i]); int sheet = removedSubjectPointer.spriteSheetIndex; int bufferIndex = removedSubject.bufferIndex; // UnityEngine.Debug.Log(string.Format("Removing {0}, which points to {1} and using buffer {2}", removed[i].Index, sheet, bufferIndex)); freeIndexs[sheet].Add(bufferIndex); // UnityEngine.Debug.Log(string.Format("Sheet {0} pooled: {1} ", sheet, freeIndexs[sheet].Length)); sheetInstanceUVPointers[sheet][bufferIndex] = -1; BufferStructureChanged[sheet] = true; } EntityManager.RemoveComponent(removed, typeof(SpriteRenderSubject)); EntityManager.RemoveComponent(removed, typeof(SpriteSheetPointer)); EntityManager.RemoveComponent(removed, typeof(BufferedRenderSubjectTag)); removed.Dispose(); } if (BoundsUpdator != null) { DrawBounds = BoundsUpdator(); } for (int i = 0; i < Baked; i++) { if (bakedSpriteSheets[i].Users > 0) { bakedSpriteSheets[i].UpdateInstanceDataBuffers((int)bakedSpriteSheets[i].Users, sheetInstanceColors[i], sheetInstanceMatrixs[i], sheetInstanceUVPointers[i]); bakedSpriteSheets[i].Material.SetVector("_RenderBounds", new UnityEngine.Vector4(DrawBounds.center.x, DrawBounds.center.y, 0, 0)); UnityEngine.Graphics.DrawMeshInstancedIndirect(mesh, 0, bakedSpriteSheets[i].Material, new UnityEngine.Bounds(DrawBounds.center, UnityEngine.Vector3.one * 100), bakedSpriteSheets[i].ArgsBuffer); } } }