public void EntityPatcher_ApplyChanges_WithChunkData_ManagedComponents() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var guid = CreateEntityGuid(); var entity = SrcEntityManager.CreateEntity(); Entity dstRootEntity; // Chunk component is added but no values are copied // Because chunks are generally caches and thus must be rebuildable automatically. // They are also likely a totally different set of chunks. // Diff & Patch is generally working against entities not on chunk level { SrcEntityManager.AddComponentData(entity, guid); SrcEntityManager.AddComponentData(entity, new EcsTestData(1)); SrcEntityManager.AddChunkComponentData <EcsTestData2>(entity); SrcEntityManager.SetChunkComponentData(SrcEntityManager.GetChunk(entity), new EcsTestData2(3)); SrcEntityManager.AddChunkComponentData <EcsTestManagedComponent>(entity); SrcEntityManager.SetChunkComponentData(SrcEntityManager.GetChunk(entity), new EcsTestManagedComponent() { value = "SomeString" }); PushChanges(differ, DstEntityManager); dstRootEntity = GetEntity(DstEntityManager, guid); Assert.AreEqual(1, DstEntityManager.GetComponentData <EcsTestData>(dstRootEntity).value); Assert.IsTrue(DstEntityManager.HasChunkComponent <EcsTestData2>(dstRootEntity)); Assert.IsTrue(DstEntityManager.HasChunkComponent <EcsTestManagedComponent>(dstRootEntity)); Assert.AreEqual(0, DstEntityManager.GetChunkComponentData <EcsTestData2>(dstRootEntity).value0); Assert.AreEqual(null, DstEntityManager.GetChunkComponentData <EcsTestManagedComponent>(dstRootEntity)); Assert.AreEqual(1, DstEntityManager.CreateEntityQuery(typeof(ChunkHeader)).CalculateEntityCount()); } // Changing Chunk component creates no diff { SrcEntityManager.SetChunkComponentData(SrcEntityManager.GetChunk(entity), new EcsTestData2(7)); SrcEntityManager.SetChunkComponentData(SrcEntityManager.GetChunk(entity), new EcsTestManagedComponent() { value = "SomeOtherString" }); using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.IsFalse(changes.AnyChanges); } } // Removing chunk component, removes chunk component again { SrcEntityManager.RemoveChunkComponent <EcsTestData2>(entity); SrcEntityManager.RemoveChunkComponent <EcsTestManagedComponent>(entity); PushChanges(differ, DstEntityManager); Assert.IsFalse(DstEntityManager.HasChunkComponent <EcsTestData2>(dstRootEntity)); Assert.IsFalse(DstEntityManager.HasChunkComponent <EcsTestManagedComponent>(dstRootEntity)); Assert.AreEqual(0, DstEntityManager.CreateEntityQuery(typeof(ChunkHeader)).CalculateEntityCount()); } } }
bool TryGetAssetEntity <T>(T queryFilter, out Entity entity) where T : struct, ISharedComponentData { entity = default; var assetQuery = DstEntityManager.CreateEntityQuery(ComponentType.ReadOnly <T>()); assetQuery.SetFilter(queryFilter); if (assetQuery.CalculateEntityCount() == 0) { return(false); } using (var assetEntityArray = assetQuery.ToEntityArray(Allocator.TempJob)) entity = assetEntityArray[0]; return(true); }
protected override void OnUpdate() { var sceneBounds = MinMaxAABB.Empty; Entities.ForEach((UnityEngine.SpriteRenderer spriteRenderer) => ProcessSpriteRender(sceneBounds, spriteRenderer)); using (var boundingVolume = DstEntityManager.CreateEntityQuery(typeof(SceneBoundingVolume))) { if (!boundingVolume.IsEmptyIgnoreFilter) { var bounds = boundingVolume.GetSingleton <SceneBoundingVolume>(); bounds.Value.Encapsulate(sceneBounds); boundingVolume.SetSingleton(bounds); } } }
protected override void OnUpdate() { var bakedAnimationsQuery = DstEntityManager.CreateEntityQuery(new EntityQueryDesc { All = new[] { ComponentType.ReadOnly <BakedAnimationClip>() }, Options = EntityQueryOptions.IncludeDisabled | EntityQueryOptions.IncludePrefab }); var commandBuffer = new EntityCommandBuffer(Allocator.TempJob); var bakedAnimationEntities = bakedAnimationsQuery.ToEntityArray(Allocator.TempJob); for (int entityIndex = 0; entityIndex < bakedAnimationEntities.Length; ++entityIndex) { commandBuffer.DestroyEntity(bakedAnimationEntities[entityIndex]); } var animationBindingNameQuery = DstEntityManager.CreateEntityQuery(new EntityQueryDesc { All = new[] { ComponentType.ReadOnly <AnimationBindingName>() }, Options = EntityQueryOptions.IncludeDisabled | EntityQueryOptions.IncludePrefab }); commandBuffer.RemoveComponent <AnimationBindingName>(animationBindingNameQuery); commandBuffer.Playback(DstEntityManager); commandBuffer.Dispose(); bakedAnimationEntities.Dispose(); TinyAnimationConversionState.Clear(); }
private void ConvertImage(Image image) { var assetQuery = DstEntityManager.CreateEntityQuery(ComponentType.ReadOnly <SpriteAsset>()); var entity = GetPrimaryEntity(image); var sprite = image.sprite ?? m_DefaultSprite; var asset = new SpriteAsset() { Value = sprite, }; assetQuery.SetFilter(asset); Entity assetEntity; if (assetQuery.CalculateEntityCount() == 0) { assetEntity = DstEntityManager.CreateEntity(typeof(SpriteAsset), typeof(SpriteVertexData)); DstEntityManager.SetSharedComponentData(assetEntity, new SpriteAsset { Value = sprite }); } else { using (var assetEntityArray = assetQuery.ToEntityArray(Allocator.TempJob)) assetEntity = assetEntityArray[0]; } SpriteImage spriteImage = new SpriteImage { Asset = assetEntity }; DstEntityManager.AddComponentData(entity, spriteImage); DstEntityManager.AddBuffer <ControlVertexData>(entity); DstEntityManager.AddBuffer <ControlVertexIndex>(entity); DstEntityManager.AddComponent(entity, typeof(ElementVertexPointerInMesh)); DstEntityManager.AddComponent(entity, typeof(RebuildElementMeshFlag)); }
void ResolveFloatBindings(EntityCommandBuffer ecb) { var query = DstEntityManager.CreateEntityQuery( new EntityQueryDesc { All = new[] { ComponentType.ReadWrite <AnimationBindingRetarget>(), ComponentType.ReadOnly <AnimationBindingName>(), ComponentType.ReadWrite <AnimationBinding>() } }); using (var animatedEntities = query.ToEntityArray(Allocator.TempJob)) { for (int entityIndex = 0; entityIndex < animatedEntities.Length; ++entityIndex) { var entity = animatedEntities[entityIndex]; var bindingRetargetBuffer = DstEntityManager.GetBuffer <AnimationBindingRetarget>(entity); var bindingNameBuffer = DstEntityManager.GetBuffer <AnimationBindingName>(entity); var bindingBuffer = DstEntityManager.GetBuffer <AnimationBinding>(entity); for (int i = bindingNameBuffer.Length - 1; i >= 0; --i) { var bindingName = bindingNameBuffer[i]; var propertyPath = bindingName.Value; var discardEntry = true; // A 0-length property path had no ECS equivalent at build time if (!propertyPath.IsEmpty) { var targetEntity = bindingBuffer[i].TargetEntity; var result = BindingUtils.GetBindingInfo(DstEntityManager, targetEntity, propertyPath); if (result.Success) { var retarget = bindingRetargetBuffer[i]; retarget.StableTypeHash = result.StableTypeHash; bindingRetargetBuffer[i] = retarget; var binding = bindingBuffer[i]; binding.FieldOffset = result.FieldOffset; binding.FieldSize = result.FieldSize; bindingBuffer[i] = binding; discardEntry = false; } else { WarnDiscardedBinding(bindingName.Value.ToString(), bindingName.TargetGameObjectName.ToString(), bindingName.ClipName.ToString(), bindingName.SourceGameObjectName.ToString()); } } if (discardEntry) { bindingBuffer.RemoveAt(i); bindingRetargetBuffer.RemoveAt(i); } } // Cleanup bindingNameBuffer.Clear(); ecb.RemoveComponent <AnimationBindingName>(entity); if (bindingBuffer.Length == 0) { // Nothing remains to animate ecb.RemoveComponent <AnimationBinding>(entity); ecb.RemoveComponent <AnimationBindingRetarget>(entity); } else { bindingBuffer.TrimExcess(); bindingRetargetBuffer.TrimExcess(); } } } }
protected override void OnUpdate() { // fixme if (DstEntityManager.CreateEntityQuery(typeof(QuadTreeData)).CalculateEntityCount() > 0) { return; } var table = Object.FindObjectOfType <TableBehavior>().Table; foreach (var playable in table.Playables) { playable.Init(table); } // index hittables var hitObjects = new List <HitObject>(); var id = 0; foreach (var item in table.Hittables) { foreach (var hitObject in item.GetHitShapes()) { hitObject.ItemIndex = item.Index; hitObject.ItemVersion = item.Version; hitObject.Id = id++; hitObject.CalcHitBBox(); hitObjects.Add(hitObject); } } // construct quad tree var quadTree = new HitQuadTree(hitObjects, table.Data.BoundingBox); var quadTreeBlobAssetRef = QuadTreeBlob.CreateBlobAssetReference( quadTree, table.GeneratePlayfieldHit(), // todo use `null` if separate playfield mesh exists table.GenerateGlassHit() ); // playfield and glass need special treatment, since not part of the quad tree var playfieldHitObject = table.GeneratePlayfieldHit(); var glassHitObject = table.GenerateGlassHit(); playfieldHitObject.Id = id++; glassHitObject.Id = id; hitObjects.Add(playfieldHitObject); hitObjects.Add(glassHitObject); // construct collider blob var colliderBlob = ColliderBlob.CreateBlobAssetReference(hitObjects, playfieldHitObject.Id, glassHitObject.Id); // save it to entity var collEntity = DstEntityManager.CreateEntity(ComponentType.ReadOnly <QuadTreeData>(), ComponentType.ReadOnly <ColliderData>()); //DstEntityManager.SetName(collEntity, "Collision Data Holder"); DstEntityManager.SetComponentData(collEntity, new QuadTreeData { Value = quadTreeBlobAssetRef }); DstEntityManager.SetComponentData(collEntity, new ColliderData { Value = colliderBlob }); Logger.Info("Static QuadTree initialized."); }
protected override void OnUpdate() { var sceneBounds = MinMaxAABB.Empty; var materials = new List <Material>(10); Entities.ForEach((MeshRenderer meshRenderer, MeshFilter meshFilter) => { var entity = GetPrimaryEntity(meshRenderer); var dst = new RenderMesh(); dst.mesh = meshFilter.sharedMesh; dst.castShadows = meshRenderer.shadowCastingMode; dst.receiveShadows = meshRenderer.receiveShadows; dst.layer = meshRenderer.gameObject.layer; //@TODO: Transform system should handle RenderMeshFlippedWindingTag automatically. This should not be the responsibility of the conversion system. float4x4 localToWorld = meshRenderer.transform.localToWorldMatrix; var flipWinding = math.determinant(localToWorld) < 0.0; meshRenderer.GetSharedMaterials(materials); var materialCount = materials.Count; if (materialCount == 1 && AttachToPrimaryEntityForSingleMaterial) { dst.material = materials[0]; dst.subMesh = 0; DstEntityManager.AddSharedComponentData(entity, dst); DstEntityManager.AddComponentData(entity, new PerInstanceCullingTag()); if (flipWinding) { DstEntityManager.AddComponent(entity, ComponentType.ReadWrite <RenderMeshFlippedWindingTag>()); } } else { for (var m = 0; m != materialCount; m++) { var meshEntity = CreateAdditionalEntity(meshRenderer); dst.material = materials[m]; dst.subMesh = m; DstEntityManager.AddSharedComponentData(meshEntity, dst); DstEntityManager.AddComponentData(meshEntity, new PerInstanceCullingTag()); DstEntityManager.AddComponentData(meshEntity, new LocalToWorld { Value = localToWorld }); if (!DstEntityManager.HasComponent <Static>(meshEntity)) { DstEntityManager.AddComponentData(meshEntity, new Parent { Value = entity }); DstEntityManager.AddComponentData(meshEntity, new LocalToParent { Value = float4x4.identity }); } if (flipWinding) { DstEntityManager.AddComponent(meshEntity, ComponentType.ReadWrite <RenderMeshFlippedWindingTag>()); } } } sceneBounds.Encapsulate(meshRenderer.bounds.ToAABB()); }); using (var boundingVolume = DstEntityManager.CreateEntityQuery(typeof(SceneBoundingVolume))) { if (!boundingVolume.IsEmptyIgnoreFilter) { var bounds = boundingVolume.GetSingleton <SceneBoundingVolume>(); bounds.Value.Encapsulate(sceneBounds); boundingVolume.SetSingleton(bounds); } } }