public void SetVoxel(Vector3i index, Voxel voxel, VoxelEntity entity = null) { if (Data.SetVoxel(index, voxel)) { if (_voxelEntities.ContainsKey(index)) { var oldEntity = _voxelEntities[index]; GameObject.RemoveChild(oldEntity); GameObject.CurrentScene.RemoveGameObject(oldEntity); _voxelEntities.Remove(index); } if (entity != null) { if (entity.Space != null) { throw new Exception("Tried setting a VoxelEntity which is already in a VoxelSpace"); } entity.Space = this; entity.Index = index; entity.Voxel = voxel; var gameObject = new GameObject(); gameObject.AddComponent(entity); gameObject.Transform.Position = index * Data.VoxelSize + Vector3.One * Data.VoxelSize / 2f; gameObject.Transform.Orientation = voxel.Orientation.GetQuaternion(); GameObject.AddChild(gameObject); _voxelEntities[index] = gameObject; } } }
public void SetVoxel(Vector3i index, Voxel voxel, VoxelEntity entity = null) { var gridsIndex = new Vector3i( (int)MathF.Floor((float)index.X / GridSize.X), (int)MathF.Floor((float)index.Y / GridSize.Y), (int)MathF.Floor((float)index.Z / GridSize.Z)); var voxelIndex = new Vector3i( index.X - gridsIndex.X * GridSize.X, index.Y - gridsIndex.Y * GridSize.Y, index.Z - gridsIndex.Z * GridSize.Z); var grid = this[gridsIndex]; grid?.SetVoxel(voxelIndex, voxel, entity); }