protected override void OnUpdate() { var volumeEntities = volumesGroup.GetEntityArray(); var assets = volumesGroup.GetComponentDataArray <VolumeAsset>(); var count = volumeEntities.Length; for (int index = 0; index < count; index++) { var entity = volumeEntities[index]; var asset = registry.GetAsset(assets[index].Id); if (!EntityManager.HasComponent <VolumePosition>(entity)) { var pos = EntityManager.HasComponent <Position>(entity) ? new int3(EntityManager.GetComponentData <Position>(entity).Value * 32) : new int3(0); PostUpdateCommands.AddComponent(entity, new VolumePosition { Value = pos }); } PostUpdateCommands.AddComponent(entity, new VolumeSize { Value = asset.VolumeSize }); PostUpdateCommands.AddComponent(entity, new VolumePivot { Value = asset.VolumePivot }); if (!EntityManager.HasComponent <VolumeRotate>(entity)) { PostUpdateCommands.AddComponent(entity, new VolumeRotate()); } PostUpdateCommands.AddComponent(entity, new VolumeBounds()); if (!EntityManager.HasComponent <VolumeChanged>(entity)) { PostUpdateCommands.AddComponent(entity, new VolumeChanged()); } } }
protected override void OnUpdate() { Debug.Log("Rendering queue: " + renderingQueue.CalculateLength()); var chunks = renderingQueue.ToEntityArray(Allocator.TempJob); var count = chunks.Length; for (int index = 0; index < count; index++) { var chunk = chunks[index]; var hash = EntityManager.GetComponentData <ChunkVolumesHash>(chunk).VolumesHash; var position = EntityManager.GetComponentData <ChunkPosition>(chunk).Value; #if FALSE if (hash == 0) { // Empty buffer if (EntityManager.HasComponent <ChunkView>(chunk)) { RemoveChunkView(chunk); } if (EntityManager.HasComponent <RenderedChunk>(chunk)) { PostUpdateCommands.RemoveComponent <RenderedChunk>(chunk); } } else { Material bakeMaterial; if (!EntityManager.HasComponent <ChunkView>(chunk)) { bakeMaterial = CreateChunkView(chunk, position); } else { bakeMaterial = GetChunkView(chunk); } var volumes = EntityManager.GetBuffer <ChunkVolumes>(chunk); if (!EntityManager.HasComponent <RenderedChunk>(chunk)) { PostUpdateCommands.AddComponent(chunk, new RenderedChunk()); } VoxelRaymarching.VolumeAsset[] assets = new VoxelRaymarching.VolumeAsset[volumes.Length]; int3[] assetPositions = new int3[volumes.Length]; int3[] assetRotations = new int3[volumes.Length]; for (int volumeIndex = 0; volumeIndex < volumes.Length; volumeIndex++) { var assetId = EntityManager.GetComponentData <VolumeAsset>(volumes[volumeIndex].VolumeEntity).Id; var assetPosition = EntityManager .GetComponentData <VolumePosition>(volumes[volumeIndex].VolumeEntity).Value; assets[volumeIndex] = registry.GetAsset(assetId); assetPositions[volumeIndex] = assetPosition; assetRotations[volumeIndex] = EntityManager .GetComponentData <VolumeRotate>(volumes[volumeIndex].VolumeEntity).Value; } Debug.LogFormat("Write to {0}", bakeMaterial.GetInt(VOLUME_BUFFER_PTR)); var task = registry.BufferBaker.CreateTask(cb, bakeMaterial.GetInt(VOLUME_BUFFER_PTR)); for (var id = 0; id < assets.Length; id++) { var volumeAsset = assets[id]; if (volumeAsset.VolumeTexture == null) { Debug.LogErrorFormat("Unavailable volume texture in {0}", volumeAsset.name); } task.AddLayer(new ChunkVolumeLayer { BlendingMode = ChunkVolumeLayer.LayerBlendingMode.Normal, FlipHorizontal = false, FlipVertical = false, LayerVolume = volumeAsset.VolumeTexture, Offset = assetPositions[id] - new int3(position.x, position.y, position.z) * 32, VolumePivot = volumeAsset.VolumePivot, VolumeSize = volumeAsset.VolumeSize, Rotate = assetRotations[id] }); } registry.BufferBaker.DeployTask(task); // registry.RenderTexturesBaker.Bake(assets.Select((asset, id) => new ChunkVolumeLayer // { // BlendingMode = ChunkVolumeLayer.LayerBlendingMode.Normal, // FlipHorizontal = false, // FlipVertical = false, // LayerVolume = asset.VolumeTexture, // Offset = assetPositions[id] - new int3(position.x, position.y, position.z) * 32, // VolumePivot = asset.VolumePivot, // VolumeSize = asset.VolumeSize, // VolumeTRS = Matrix4x4.identity // }).ToArray(), texture); } #endif PostUpdateCommands.RemoveComponent <UpdateRenderCommand>(chunk); } chunks.Dispose(); }
protected override void OnUpdate() { Debug.Log("Rendering queue: " + renderingQueue.CalculateLength()); var chunks = renderingQueue.ToEntityArray(Allocator.TempJob); var count = chunks.Length; for (int index = 0; index < count; index++) { var chunk = chunks[index]; var hash = EntityManager.GetComponentData <ChunkVolumesHash>(chunk).VolumesHash; var position = EntityManager.GetComponentData <ChunkPosition>(chunk).Value; if (hash == 0) { // Empty buffer if (EntityManager.HasComponent <ChunkBufferPtr>(chunk)) { var ptr = EntityManager.GetComponentData <ChunkBufferPtr>(chunk).Ptr; pooledIndexes.Push(ptr); var allocationIndex = ChunkPositionToAllocationIndex(position); chunkIndexes[allocationIndex] = 0; AllocationMap.SetData(chunkIndexes); PostUpdateCommands.RemoveComponent <ChunkBufferPtr>(chunk); } if (EntityManager.HasComponent <RenderedChunk>(chunk)) { PostUpdateCommands.RemoveComponent <RenderedChunk>(chunk); } } else { uint ptr; if (!EntityManager.HasComponent <ChunkBufferPtr>(chunk)) { ptr = AllocChunk(); var allocationIndex = ChunkPositionToAllocationIndex(position); chunkIndexes[allocationIndex] = ptr; AllocationMap.SetData(chunkIndexes); PostUpdateCommands.AddComponent(chunk, new ChunkBufferPtr { Ptr = ptr }); } else { ptr = EntityManager.GetComponentData <ChunkBufferPtr>(chunk).Ptr; } var volumes = EntityManager.GetBuffer <ChunkVolumes>(chunk); if (!EntityManager.HasComponent <RenderedChunk>(chunk)) { PostUpdateCommands.AddComponent(chunk, new RenderedChunk()); } VoxelRaymarching.VolumeAsset[] assets = new VoxelRaymarching.VolumeAsset[volumes.Length]; int3[] assetPositions = new int3[volumes.Length]; int3[] assetRotations = new int3[volumes.Length]; for (int volumeIndex = 0; volumeIndex < volumes.Length; volumeIndex++) { var assetId = EntityManager.GetComponentData <VolumeAsset>(volumes[volumeIndex].VolumeEntity).Id; var assetPosition = EntityManager .GetComponentData <VolumePosition>(volumes[volumeIndex].VolumeEntity).Value; if (registry == null) { throw new NullReferenceException("WTF?"); } assets[volumeIndex] = registry.GetAsset(assetId); assetPositions[volumeIndex] = assetPosition; assetRotations[volumeIndex] = EntityManager .GetComponentData <VolumeRotate>(volumes[volumeIndex].VolumeEntity).Value; } var task = registry.BufferBaker.CreateTask(ChunksBuffer, ptr); for (var id = 0; id < assets.Length; id++) { var volumeAsset = assets[id]; if (volumeAsset.VolumeTexture == null) { Debug.LogErrorFormat("Unavailable volume texture in {0}", volumeAsset.name); } task.AddLayer(new ChunkVolumeLayer { BlendingMode = ChunkVolumeLayer.LayerBlendingMode.Normal, FlipHorizontal = false, FlipVertical = false, LayerVolume = volumeAsset.VolumeTexture, Offset = assetPositions[id] - new int3(position.x, position.y, position.z) * 32, VolumePivot = volumeAsset.VolumePivot, VolumeSize = volumeAsset.VolumeSize, Rotate = assetRotations[id] }); } registry.BufferBaker.DeployTask(task); } PostUpdateCommands.RemoveComponent <UpdateRenderCommand>(chunk); } chunks.Dispose(); }