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 QuadTree(float3 bounds, Entity entity) { this.bounds = bounds; int maxSprites = SpriteSheetCache.GetLength("emoji"); if (entity == Entity.Null) { var color = UnityEngine.Random.ColorHSV(.35f, .85f); List <IComponentData> components = new List <IComponentData> { new Position2D { Value = bounds.xy }, new Scale { Value = bounds.z }, new SpriteIndex { Value = UnityEngine.Random.Range(0, maxSprites) }, new SpriteSheetAnimation { frameCount = maxSprites, playMode = PlayMode.Loop, framesPerSecond = 10 }, new SpriteSheetColor { color = new float4(color.r, color.g, color.b, color.a) } }; this.entity = SpriteSheetManager.Instantiate(FractalInitializer.archetype, components, "emoji"); } else { this.entity = entity; UpdateEntityMatrix(); } }
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); }
void Update() { if (Input.GetKeyDown(KeyCode.Space)) { int maxSprites = SpriteSheetCache.GetLength(spriteSheetName); var color = UnityEngine.Random.ColorHSV(.35f, .85f); // 3) Populate components List <IComponentData> components = new List <IComponentData> { new Position2D { Value = UnityEngine.Random.insideUnitCircle * 7 }, new Scale { Value = UnityEngine.Random.Range(0, 3f) }, new SpriteIndex { Value = UnityEngine.Random.Range(0, maxSprites) }, new SpriteSheetAnimation { frameCount = maxSprites, playMode = PlayMode.Loop, framesPerSecond = 10 }, new SpriteSheetColor { color = new float4(color.r, color.g, color.b, color.a) }, new LifeTime { Value = UnityEngine.Random.Range(5, 15) } }; SpriteSheetManager.Instantiate(archetype, components, spriteSheetName); } }
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 }); }
void Update() { if (Input.GetKeyDown(KeyCode.Space)) { int maxSprites = SpriteSheetCache.GetLength("emoji"); var color = UnityEngine.Random.ColorHSV(.35f, .85f); // 3) Populate components List <IComponentData> components = new List <IComponentData> { new Position2D { Value = UnityEngine.Random.insideUnitCircle * 7 }, new Scale { Value = UnityEngine.Random.Range(0, 3f) }, new SpriteIndex { Value = UnityEngine.Random.Range(0, maxSprites) }, new SpriteSheetAnimation { maxSprites = maxSprites, play = true, repetition = SpriteSheetAnimation.RepetitionType.Loop, samples = 10 }, new SpriteSheetColor { color = new float4(color.r, color.g, color.b, color.a) }, new LifeTime { Value = UnityEngine.Random.Range(5, 15) } }; SpriteSheetManager.Instantiate(archetype, components, "emoji"); } }
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); } }
int UpdateBuffers(int renderIndex) { SpriteSheetManager.ReleaseBuffer(renderIndex); RenderInformation renderInformation = SpriteSheetManager.renderInformation[renderIndex]; int instanceCount = EntityManager.GetBuffer <SpriteIndexBuffer>(renderInformation.bufferEntity).Length; if (instanceCount > 0) { //TODO: deve moltiplicare il numero di sprites per questa animazione int stride = instanceCount >= 16 ? 16 : 16 * SpriteSheetCache.GetLenght(renderInformation.material); renderInformation.uvBuffer = new ComputeBuffer(instanceCount, stride); renderInformation.uvBuffer.SetData(EntityManager.GetBuffer <UvBuffer>(renderInformation.bufferEntity).Reinterpret <float4>().AsNativeArray()); renderInformation.material.SetBuffer("uvBuffer", renderInformation.uvBuffer); renderInformation.indexBuffer = new ComputeBuffer(instanceCount, sizeof(int)); renderInformation.indexBuffer.SetData(EntityManager.GetBuffer <SpriteIndexBuffer>(renderInformation.bufferEntity).Reinterpret <int>().AsNativeArray()); renderInformation.material.SetBuffer("indexBuffer", renderInformation.indexBuffer); renderInformation.matrixBuffer = new ComputeBuffer(instanceCount, 16); renderInformation.matrixBuffer.SetData(EntityManager.GetBuffer <MatrixBuffer>(renderInformation.bufferEntity).Reinterpret <float4>().AsNativeArray()); renderInformation.material.SetBuffer("matrixBuffer", renderInformation.matrixBuffer); renderInformation.args[1] = (uint)instanceCount; renderInformation.argsBuffer.SetData(renderInformation.args); renderInformation.colorsBuffer = new ComputeBuffer(instanceCount, 16); renderInformation.colorsBuffer.SetData(EntityManager.GetBuffer <SpriteColorBuffer>(renderInformation.bufferEntity).Reinterpret <float4>().AsNativeArray()); renderInformation.material.SetBuffer("colorsBuffer", renderInformation.colorsBuffer); } return(instanceCount); }
public static void DestroyEntity(Entity e, string materialName) { Material material = SpriteSheetCache.GetMaterial(materialName); int bufferID = EntityManager.GetComponentData <BufferHook>(e).bufferID; DynamicBufferManager.RemoveBuffer(material, bufferID); EntityManager.DestroyEntity(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); } }
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 RenderInformation(Material material, Entity bufferEntity) { this.material = material; spriteCount = SpriteSheetCache.GetLenght(material); this.bufferEntity = bufferEntity; args = new uint[5] { 0, 0, 0, 0, 0 }; argsBuffer = new ComputeBuffer(1, args.Length * sizeof(uint), ComputeBufferType.IndirectArguments); //thoose args are always the same since we always use the same mesh args[0] = (uint)6; args[2] = args[3] = (uint)0; }
public RenderInformation(Material mat, Entity bufEntity) { material = mat; material.enableInstancing = true; spriteCount = SpriteSheetCache.GetLenght(material); bufferEntity = bufEntity; // Arguments for drawing mesh args = new uint[5] { 0, 0, 0, 0, 0 }; argsBuffer = new ComputeBuffer(1, args.Length * sizeof(uint), ComputeBufferType.IndirectArguments); // 0 == number of triangle indices, 1 == population, others are only relevant if drawing submeshes args[0] = (uint)6; args[2] = args[3] = (uint)0; updateUvs = true; }
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); }
public void Convert(Entity entity, EntityManager eManager, GameObjectConversionSystem conversionSystem) { // 1) Create Archetype EntityArchetype archetype = eManager.CreateArchetype( typeof(Position2D), typeof(Rotation2D), typeof(Scale), //required params typeof(SpriteIndex), typeof(SpriteSheetAnimation), typeof(SpriteSheetMaterial), typeof(SpriteSheetColor), typeof(SpriteMatrix), typeof(BufferHook) ); // 2) Record and bake this spritesheet(only once) SpriteSheetManager.RecordSpriteSheet(sprites, spriteSheetName); int maxSprites = SpriteSheetCache.GetLength(spriteSheetName); var color = UnityEngine.Random.ColorHSV(.35f, .85f); // 3) Populate components List <IComponentData> components = new List <IComponentData> { new Position2D { Value = float2.zero }, new Scale { Value = 15 }, new SpriteIndex { Value = UnityEngine.Random.Range(0, maxSprites) }, new SpriteSheetAnimation { frameCount = maxSprites, playMode = PlayMode.Loop, framesPerSecond = 10 }, new SpriteSheetColor { color = new float4(color.r, color.g, color.b, color.a) } }; // 4) Instantiate the entity _ = SpriteSheetManager.Instantiate(archetype, components, spriteSheetName); }
public static void Play(Entity e, string animationName = "") { SpriteSheetAnimator animator = SpriteSheetCache.GetAnimator(e); if (animationName == "") { animationName = animator.animations[animator.currentAnimationIndex].name; } int i = 0; foreach (SpriteSheetAnimationData animation in animator.animations) { if (animation.animationName == animationName) { SpriteSheetManager.SetAnimation(e, animation, animator.animations[animator.currentAnimationIndex].name); animator.currentAnimationIndex = i; } i++; } }
public static void Play(EntityCommandBuffer buffer, Entity e, BufferHook hook, string animationName = "") { SpriteSheetAnimator animator = SpriteSheetCache.GetAnimator(e); if (animationName == "") { animationName = animator.animations[animator.currentAnimationIndex].name; } int i = 0; foreach (SpriteSheetAnimationData animation in animator.animations) { if (animation.animationName == animationName) { SpriteSheetManager.SetAnimation(buffer, e, animation, hook); animator.currentAnimationIndex = i; return; } i++; } }
//we should only update the index of the changed datas for index buffer,matrixbuffer and color buffer inside a burst job to avoid overhead int UpdateBuffers(int renderIndex) { SpriteSheetManager.ReleaseBuffer(renderIndex); RenderInformation renderInformation = SpriteSheetManager.renderInformation[renderIndex]; int instanceCount = EntityManager.GetBuffer <SpriteIndexBuffer>(renderInformation.bufferEntity).Length; if (instanceCount > 0) { int stride = instanceCount >= 16 ? 16 : 16 * SpriteSheetCache.GetLenght(renderInformation.material); if (renderInformation.updateUvs) { SpriteSheetManager.ReleaseUvBuffer(renderIndex); renderInformation.uvBuffer = new ComputeBuffer(instanceCount, stride); renderInformation.uvBuffer.SetData(EntityManager.GetBuffer <UvBuffer>(renderInformation.bufferEntity).Reinterpret <float4>().AsNativeArray()); renderInformation.material.SetBuffer("uvBuffer", renderInformation.uvBuffer); renderInformation.updateUvs = false; } renderInformation.indexBuffer = new ComputeBuffer(instanceCount, sizeof(int)); renderInformation.indexBuffer.SetData(EntityManager.GetBuffer <SpriteIndexBuffer>(renderInformation.bufferEntity).Reinterpret <int>().AsNativeArray()); renderInformation.material.SetBuffer("indexBuffer", renderInformation.indexBuffer); renderInformation.layerBuffer = new ComputeBuffer(instanceCount, sizeof(int)); renderInformation.layerBuffer.SetData(EntityManager.GetBuffer <SpriteLayerBuffer>(renderInformation.bufferEntity).Reinterpret <int>().AsNativeArray()); renderInformation.material.SetBuffer("layerBuffer", renderInformation.layerBuffer); renderInformation.matrixBuffer = new ComputeBuffer(instanceCount, 16); renderInformation.matrixBuffer.SetData(EntityManager.GetBuffer <MatrixBuffer>(renderInformation.bufferEntity).Reinterpret <float4>().AsNativeArray()); renderInformation.material.SetBuffer("matrixBuffer", renderInformation.matrixBuffer); renderInformation.args[1] = (uint)instanceCount; renderInformation.argsBuffer.SetData(renderInformation.args); renderInformation.colorsBuffer = new ComputeBuffer(instanceCount, 16); renderInformation.colorsBuffer.SetData(EntityManager.GetBuffer <SpriteColorBuffer>(renderInformation.bufferEntity).Reinterpret <float4>().AsNativeArray()); renderInformation.material.SetBuffer("colorsBuffer", renderInformation.colorsBuffer); } return(instanceCount); }
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]); } } }