public static void SetAnimation(Entity e, SpriteSheetAnimationData animation) { int bufferEnityID = EntityManager.GetComponentData <BufferHook>(e).bufferEnityID; int bufferID = EntityManager.GetComponentData <BufferHook>(e).bufferID; Material oldMaterial = DynamicBufferManager.GetMaterial(bufferEnityID); string oldAnimation = SpriteSheetCache.GetMaterialName(oldMaterial); if (animation.animationName != oldAnimation) { Material material = SpriteSheetCache.GetMaterial(animation.animationName); var spriteSheetMaterial = new SpriteSheetMaterial { material = material }; DynamicBufferManager.RemoveBuffer(oldMaterial, bufferID); //use new buffer bufferID = DynamicBufferManager.AddDynamicBuffers(DynamicBufferManager.GetEntityBuffer(material), material); BufferHook bh = new BufferHook { bufferID = bufferID, bufferEnityID = DynamicBufferManager.GetEntityBufferID(spriteSheetMaterial) }; EntityManager.SetSharedComponentData(e, spriteSheetMaterial); EntityManager.SetComponentData(e, bh); } EntityManager.SetComponentData(e, new SpriteSheetAnimation { maxSprites = animation.sprites.Length, play = animation.playOnStart, samples = animation.samples, repetition = animation.repetition, elapsedFrames = 0 }); EntityManager.SetComponentData(e, new SpriteIndex { Value = animation.startIndex }); MarkDirty <SpriteSheetColor>(e); MarkDirty <SpriteIndex>(e); MarkDirty <SpriteMatrix>(e); }
public void Convert(Entity entity, EntityManager eManager, GameObjectConversionSystem conversionSystem) { instance = this; archetype = eManager.CreateArchetype( typeof(Position2D), typeof(Rotation2D), typeof(Scale), //required params typeof(SpriteIndex), typeof(SpriteSheetAnimation), typeof(SpriteSheetMaterial), typeof(SpriteSheetColor), typeof(SpriteMatrix), typeof(BufferHook) ); NativeArray <Entity> entities = new NativeArray <Entity>(spriteCount, Allocator.Temp); eManager.CreateEntity(archetype, entities); //only needed for first tiem material baking KeyValuePair <Material, float4[]> atlasData = SpriteSheetCache.BakeSprites(sprites, "emoji"); SpriteSheetMaterial material = new SpriteSheetMaterial { material = atlasData.Key }; DynamicBufferManager.manager = eManager; DynamicBufferManager.GenerateBuffers(material, entities.Length); DynamicBufferManager.BakeUvBuffer(material, atlasData); Rect area = GetSpawnArea(); Random rand = new Random((uint)UnityEngine.Random.Range(0, int.MaxValue)); int cellCount = atlasData.Value.Length; for (int i = 0; i < entities.Length; i++) { Entity e = entities[i]; eManager.SetComponentData(e, new SpriteIndex { Value = rand.NextInt(0, cellCount) }); eManager.SetComponentData(e, new Scale { Value = 10 }); eManager.SetComponentData(e, new Position2D { Value = rand.NextFloat2(area.min, area.max) }); eManager.SetComponentData(e, new SpriteSheetAnimation { maxSprites = cellCount, play = true, repetition = SpriteSheetAnimation.RepetitionType.Loop, samples = 10 }); var color = UnityEngine.Random.ColorHSV(.15f, .75f); SpriteSheetColor col = new SpriteSheetColor { color = new float4(color.r, color.g, color.b, color.a) }; eManager.SetComponentData(e, col); eManager.SetComponentData(e, new BufferHook { bufferID = i, bufferEnityID = DynamicBufferManager.GetEntityBufferID(material) }); eManager.SetSharedComponentData(e, material); } }
public static void SetAnimation(EntityCommandBuffer commandBuffer, Entity e, SpriteSheetAnimationData animation, BufferHook hook) { Material oldMaterial = DynamicBufferManager.GetMaterial(hook.bufferEnityID); string oldAnimation = SpriteSheetCache.GetMaterialName(oldMaterial); if (animation.animationName != oldAnimation) { Material material = SpriteSheetCache.GetMaterial(animation.animationName); var spriteSheetMaterial = new SpriteSheetMaterial { material = material }; //clean old buffer DynamicBufferManager.RemoveBuffer(oldMaterial, hook.bufferID); //use new buffer int bufferID = DynamicBufferManager.AddDynamicBuffers(DynamicBufferManager.GetEntityBuffer(material), material); BufferHook bh = new BufferHook { bufferID = bufferID, bufferEnityID = DynamicBufferManager.GetEntityBufferID(spriteSheetMaterial) }; commandBuffer.SetSharedComponent(e, spriteSheetMaterial); commandBuffer.SetComponent(e, bh); } commandBuffer.SetComponent(e, new SpriteSheetAnimation { maxSprites = animation.sprites.Length, play = animation.playOnStart, samples = animation.samples, repetition = animation.repetition, elapsedFrames = 0 }); commandBuffer.SetComponent(e, new SpriteIndex { Value = animation.startIndex }); }
public static Entity Instantiate(EntityArchetype archetype, SpriteSheetAnimator animator) { Entity e = EntityManager.CreateEntity(archetype); animator.currentAnimationIndex = animator.defaultAnimationIndex; SpriteSheetAnimationData startAnim = animator.animations[animator.defaultAnimationIndex]; int maxSprites = startAnim.sprites.Length; Material material = SpriteSheetCache.GetMaterial(animator.animations[animator.defaultAnimationIndex].animationName); int bufferID = DynamicBufferManager.AddDynamicBuffers(DynamicBufferManager.GetEntityBuffer(material), material); var spriteSheetMaterial = new SpriteSheetMaterial { material = material }; BufferHook bh = new BufferHook { bufferID = bufferID, bufferEnityID = DynamicBufferManager.GetEntityBufferID(spriteSheetMaterial) }; EntityManager.SetComponentData(e, bh); EntityManager.SetComponentData(e, new SpriteSheetAnimation { maxSprites = maxSprites, play = startAnim.playOnStart, samples = startAnim.samples, repetition = startAnim.repetition }); EntityManager.SetComponentData(e, new SpriteIndex { Value = startAnim.startIndex }); EntityManager.SetSharedComponentData(e, spriteSheetMaterial); animator.managedEntity = e; SpriteSheetCache.entityAnimator.Add(e, animator); return(e); }
public void Convert(Entity entity, EntityManager eManager, GameObjectConversionSystem conversionSystem) { EntityArchetype archetype = eManager.CreateArchetype( typeof(Position2D), typeof(Rotation2D), typeof(Scale), //required params typeof(SpriteIndex), typeof(SpriteSheetAnimation), typeof(SpriteSheetMaterial), typeof(SpriteSheetColor), typeof(SpriteMatrix), typeof(BufferHook) ); NativeArray <Entity> entities = new NativeArray <Entity>(spriteCount, Allocator.Temp); eManager.CreateEntity(archetype, entities); //only needed for the first time to bake the material and create the uv map SpriteSheetManager.RecordSpriteSheet(sprites, spriteSheetName, entities.Length); Rect area = GetSpawnArea(); Random rand = new Random((uint)UnityEngine.Random.Range(0, int.MaxValue)); int cellCount = SpriteSheetCache.GetLength(spriteSheetName); SpriteSheetMaterial material = new SpriteSheetMaterial { material = SpriteSheetCache.GetMaterial(spriteSheetName) }; for (int i = 0; i < entities.Length; i++) { Entity e = entities[i]; eManager.SetComponentData(e, new SpriteIndex { Value = rand.NextInt(0, cellCount) }); eManager.SetComponentData(e, new Scale { Value = 10 }); eManager.SetComponentData(e, new Position2D { Value = rand.NextFloat2(area.min, area.max) }); eManager.SetComponentData(e, new SpriteSheetAnimation { frameCount = cellCount, playMode = PlayMode.Loop, framesPerSecond = 10 }); var color = UnityEngine.Random.ColorHSV(.15f, .75f); SpriteSheetColor col = new SpriteSheetColor { color = new float4(color.r, color.g, color.b, color.a) }; eManager.SetComponentData(e, col); eManager.SetComponentData(e, new BufferHook { bufferID = i, bufferEntityID = DynamicBufferManager.GetEntityBufferID(material) }); eManager.SetSharedComponentData(e, material); } }
//only use this when you didn't bake the uv yet public static void BakeUvBuffer(SpriteSheetMaterial spriteSheetMaterial, KeyValuePair <Material, float4[]> atlasData) { Entity entity = GetEntityBuffer(spriteSheetMaterial.material); var buffer = EntityManager.GetBuffer <UvBuffer>(entity); for (int j = 0; j < atlasData.Value.Length; j++) { buffer.Add(atlasData.Value[j]); } }
void BuildBuffersForKey(SpriteSheetMaterial key) { List <BufferState> states = materialBufferMap_.GetOrCreateValueList(key); states.Clear(); for (int i = 0; i < bufferSignatures_.Count; ++i) { states.Add(new BufferState(bufferSignatures_[i].shaderName, bufferSignatures_[i].stride)); } }
void WriteUVs(SpriteSheetMaterial mat) { ComputeBuffer b; if (!uvBuffers_.TryGetValue(mat, out b)) { b = CachedUVData.GetUVBuffer(mat.material); mat.material.SetBuffer("uvBuffer", b); uvBuffers_[mat] = b; } }
public static void RecordSpriteSheet(Sprite[] sprites, string spriteSheetName, int spriteCount = 0) { KeyValuePair <Material, float4[]> atlasData = SpriteSheetCache.BakeSprites(sprites, spriteSheetName); SpriteSheetMaterial material = new SpriteSheetMaterial { material = atlasData.Key }; DynamicBufferManager.GenerateBuffers(material, spriteCount); DynamicBufferManager.BakeUvBuffer(material, atlasData); renderInformation.Add(new RenderInformation(material.material, DynamicBufferManager.GetEntityBuffer(material.material))); }
public static void RecordAnimator(SpriteSheetAnimator animator) { foreach (SpriteSheetAnimationData animation in animator.animations) { KeyValuePair <Material, float4[]> atlasData = SpriteSheetCache.BakeSprites(animation.sprites, animation.animationName); SpriteSheetMaterial material = new SpriteSheetMaterial { material = atlasData.Key }; DynamicBufferManager.GenerateBuffers(material); DynamicBufferManager.BakeUvBuffer(material, atlasData); renderInformation.Add(new RenderInformation(material.material, DynamicBufferManager.GetEntityBuffer(material.material))); } }
public static void GenerateBuffers(SpriteSheetMaterial material, int entityCount = 0) { if (!materialEntityBufferID.ContainsKey(material.material)) { CreateBuffersContainer(material); availableEntityID.Add(material.material, new List <int>()); for (int i = 0; i < entityCount; i++) { availableEntityID[material.material].Add(i); } MassAddBuffers(bufferEntities.Last(), entityCount); } }
//use this when it's the first time you are using that material //use this just to generate the buffers container public static void CreateBuffersContainer(SpriteSheetMaterial material) { var archetype = EntityManager.CreateArchetype( typeof(SpriteIndexBuffer), typeof(MatrixBuffer), typeof(SpriteColorBuffer), typeof(SpriteSheetMaterial), typeof(UvBuffer) ); Entity e = EntityManager.CreateEntity(archetype); bufferEntities.Add(e); EntityManager.SetSharedComponentData(e, material); materialEntityBufferID.Add(material.material, materialEntityBufferID.Count); }
public static Entity Instantiate(EntityArchetype archetype, string spriteSheetName) { Entity e = EntityManager.CreateEntity(archetype); Material material = SpriteSheetCache.GetMaterial(spriteSheetName); int bufferID = DynamicBufferManager.AddDynamicBuffers(DynamicBufferManager.GetEntityBuffer(material), material); var spriteSheetMaterial = new SpriteSheetMaterial { material = material }; BufferHook bh = new BufferHook { bufferID = bufferID, bufferEnityID = DynamicBufferManager.GetEntityBufferID(spriteSheetMaterial) }; EntityManager.SetComponentData(e, bh); EntityManager.SetSharedComponentData(e, spriteSheetMaterial); return(e); }
int UpdateBuffers(int renderIndex) { ClearDataBuffers(renderIndex); SpriteSheetMaterial material = new SpriteSheetMaterial { material = renderInfos[renderIndex].material }; uvBufferQuery.SetFilter(material); matrixBufferQuery.SetFilter(material); indexBufferQuery.SetFilter(material); colorBufferQuery.SetFilter(material); var entities = matrixBufferQuery.ToEntityArray(Allocator.TempJob); var bufferEntity = entities[0]; entities.Dispose(); int instanceCount = EntityManager.GetBuffer <SpriteIndexBuffer>(bufferEntity).Length; if (instanceCount > 0) { renderInfos[renderIndex].uvBuffer = new ComputeBuffer(instanceCount, 16); renderInfos[renderIndex].uvBuffer.SetData(EntityManager.GetBuffer <UvBuffer>(bufferEntity).Reinterpret <float4>().AsNativeArray()); renderInfos[renderIndex].material.SetBuffer("uvBuffer", renderInfos[renderIndex].uvBuffer); renderInfos[renderIndex].indexBuffer = new ComputeBuffer(instanceCount, sizeof(int)); renderInfos[renderIndex].indexBuffer.SetData(EntityManager.GetBuffer <SpriteIndexBuffer>(bufferEntity).Reinterpret <int>().AsNativeArray()); renderInfos[renderIndex].material.SetBuffer("indexBuffer", renderInfos[renderIndex].indexBuffer); renderInfos[renderIndex].matrixBuffer = new ComputeBuffer(instanceCount, 16); renderInfos[renderIndex].matrixBuffer.SetData(EntityManager.GetBuffer <MatrixBuffer>(bufferEntity).Reinterpret <float4>().AsNativeArray()); renderInfos[renderIndex].material.SetBuffer("matrixBuffer", renderInfos[renderIndex].matrixBuffer); renderInfos[renderIndex].args[1] = (uint)instanceCount; renderInfos[renderIndex].argsBuffer.SetData(renderInfos[renderIndex].args); renderInfos[renderIndex].colorsBuffer = new ComputeBuffer(instanceCount, 16); renderInfos[renderIndex].colorsBuffer.SetData(EntityManager.GetBuffer <SpriteColorBuffer>(bufferEntity).Reinterpret <float4>().AsNativeArray()); renderInfos[renderIndex].material.SetBuffer("colorsBuffer", renderInfos[renderIndex].colorsBuffer); } return(instanceCount); }
public static Entity SpawnEntity(EntityCommandBuffer eManager, string materialName) { var e = eManager.CreateEntity(instance.archetype); //only needed for first tiem material baking Material material = SpriteSheetCache.materialNameMaterial[materialName]; int bufferID = DynamicBufferManager.AddDynamicBuffers(DynamicBufferManager.GetEntityBuffer(material), material); Entity uvBuffer = DynamicBufferManager.GetEntityBuffer(material); int maxSprites = DynamicBufferManager.manager.GetBuffer <UvBuffer>(uvBuffer).Length; Rect area = instance.GetSpawnArea(); Random rand = new Random((uint)UnityEngine.Random.Range(0, int.MaxValue)); eManager.SetComponent(e, new SpriteIndex { Value = 0 }); eManager.SetComponent(e, new Scale { Value = 20 }); eManager.SetComponent(e, new Position2D { Value = rand.NextFloat2(area.min, area.max) }); eManager.SetComponent(e, new SpriteSheetAnimation { maxSprites = maxSprites, play = true, repetition = SpriteSheetAnimation.RepetitionType.Loop, samples = 10 }); var color = UnityEngine.Random.ColorHSV(.15f, .75f); SpriteSheetColor col = new SpriteSheetColor { color = new float4(color.r, color.g, color.b, color.a) }; eManager.SetComponent(e, col); var spriteSheetMaterial = new SpriteSheetMaterial { material = material }; eManager.SetComponent(e, new BufferHook { bufferID = bufferID, bufferEnityID = DynamicBufferManager.GetEntityBufferID(spriteSheetMaterial) }); eManager.SetSharedComponent(e, spriteSheetMaterial); return(e); }
public void Render(SpriteSheetMaterial key, int count) { if (count == 0) { return; } int oldCount; instanceCounts_.TryGetValue(key, out oldCount); if (count != oldCount) { instanceCounts_[key] = count; args[1] = (uint)count; argsBuffer.SetData(args); } WriteUVs(key); var bounds = new Bounds(Vector3.zero, Vector3.one * 5000); Graphics.DrawMeshInstancedIndirect(mesh_, 0, key.material, bounds, argsBuffer); }
public BufferState GetState(SpriteSheetMaterial key, int index) { return(materialBufferMap_[key][index]); }
public static int GetEntityBufferID(SpriteSheetMaterial material) { return(materialEntityBufferID[material.material]); }
public static BufferHook GetBufferHook(SpriteSheetMaterial material) { return(new BufferHook { bufferEnityID = materialEntityBufferID[material.material], bufferID = NextIDForEntity(material.material) }); }
public void Convert(Entity entity, EntityManager eManager, GameObjectConversionSystem conversionSystem) { var archetype = eManager.CreateArchetype( typeof(Position2D), typeof(Rotation2D), typeof(Scale), //typeof(Bound2D), typeof(SpriteSheet), typeof(SpriteSheetAnimation), typeof(SpriteSheetMaterial), typeof(UvBuffer), typeof(SpriteSheetColor), typeof(RenderData) ); NativeArray <Entity> entities = new NativeArray <Entity>(spriteCount, Allocator.Temp); eManager.CreateEntity(archetype, entities); KeyValuePair <Material, float4[]> atlasData = SpriteSheetCache.BakeSprites(sprites); int cellCount = atlasData.Value.Length; Random rand = new Random((uint)UnityEngine.Random.Range(0, int.MaxValue)); Rect area = GetSpawnArea(); SpriteSheetMaterial material = new SpriteSheetMaterial { material = atlasData.Key }; for (int i = 0; i < entities.Length; i++) { Entity e = entities[i]; SpriteSheet sheet = new SpriteSheet { spriteIndex = rand.NextInt(0, cellCount), maxSprites = cellCount }; Scale scale = new Scale { Value = rand.NextFloat(minScale, maxScale) }; Position2D pos = new Position2D { Value = rand.NextFloat2(area.min, area.max) }; SpriteSheetAnimation anim = new SpriteSheetAnimation { play = true, repetition = SpriteSheetAnimation.RepetitionType.Loop, samples = 10 }; var color = UnityEngine.Random.ColorHSV(.15f, .75f); SpriteSheetColor col = new SpriteSheetColor { value = new float4(color.r, color.g, color.b, color.a) }; eManager.SetComponentData(e, sheet); eManager.SetComponentData(e, scale); eManager.SetComponentData(e, pos); eManager.SetComponentData(e, anim); eManager.SetComponentData(e, col); eManager.SetSharedComponentData(e, material); // Fill uv buffer var buffer = eManager.GetBuffer <UvBuffer>(entities[i]); for (int j = 0; j < atlasData.Value.Length; j++) { buffer.Add(atlasData.Value[j]); } } }
void DoSpawn(SpriteSpawnerTest.SpawnData spawner, Material mat) { ComponentType[] type = new ComponentType[] { typeof(Position2D), typeof(Rotation2D), typeof(Scale), typeof(SpriteSheetAnimation), typeof(SpriteSheetMaterial), typeof(SpriteSheetColor), typeof(UVCell) }; var em = EntityManager; var archetype = em.CreateArchetype(type); NativeArray <Entity> entities = new NativeArray <Entity>(spawner.spriteCount, Allocator.Temp); em.CreateEntity(archetype, entities); int cellCount = CachedUVData.GetCellCount(mat); Random rand = new Random((uint)UnityEngine.Random.Range(0, int.MaxValue)); Rect area = spawner.GetSpawnArea(); SpriteSheetMaterial material = new SpriteSheetMaterial { material = mat }; var maxFrames = CachedUVData.GetCellCount(mat); //Debug.Log("Spawning entities and setting components"); for (int i = 0; i < entities.Length; i++) { Entity e = entities[i]; Scale scale = new Scale { Value = rand.NextFloat(spawner.minScale, spawner.maxScale) }; float2 p = rand.NextFloat2(area.min, area.max); Position2D pos = spawner.origin.xy + p; Rotation2D rot = new Rotation2D { angle = spawner.rotation }; int numFrames = rand.NextInt(3, maxFrames / 2); int minFrame = rand.NextInt(0, maxFrames - numFrames); SpriteSheetAnimation anim = new SpriteSheetAnimation { play = true, repetition = SpriteSheetAnimation.RepetitionType.Loop, fps = rand.NextFloat(spawner.minFPS_, spawner.maxFPS_), frameMin = minFrame, frameMax = minFrame + numFrames }; SpriteSheetColor color = UnityEngine.Random.ColorHSV(.35f, .75f); UVCell cell = new UVCell { value = rand.NextInt(0, maxFrames) }; em.SetComponentData(e, scale); em.SetComponentData(e, pos); em.SetComponentData(e, anim); em.SetComponentData(e, color); em.SetComponentData(e, cell); em.SetComponentData(e, rot); em.SetSharedComponentData(e, material); } }