Пример #1
0
        static bool VoxelRaycast(RaycastInput input, out Unity.Physics.RaycastHit hitinfo,
                                 out VoxelRaycastHit voxelInfo)
        {
            var physicsSystem  = World.Active.GetOrCreateSystem <BuildPhysicsWorld>();
            var collisionWorld = physicsSystem.PhysicsWorld.CollisionWorld;

            if (collisionWorld.CastRay(input, out hitinfo))
            {
                var voxSpace = UnivoxUtil.ToVoxelSpace(hitinfo.Position, hitinfo.SurfaceNormal);
                UnivoxUtil.SplitPosition(voxSpace, out var cPos, out var bPos);
                var bIndex = UnivoxUtil.GetIndex(bPos);

                if (GameManager.Universe.TryGetValue(0, out var world))
                {
                    if (world.TryGetValue(cPos, out var chunkRecord))
                    {
//                    var chunk = chunkRecord.Chunk;
//                    var block = chunk.GetAccessor(bIndex);

                        voxelInfo = new VoxelRaycastHit()
                        {
                            World = world,
//                        Chunk = chunk,
//                        Block = block,
                            BlockPosition = bPos,
                            BlockIndex    = bIndex,
                            ChunkPosition = cPos,
                            ChunkEntity   = chunkRecord
                        };
                        return(true);
                    }
                }
            }

            voxelInfo = default;
            return(false);
        }
Пример #2
0
        void Update()
        {
//            if (Input.GetKeyDown(KeyCode.UpArrow))
//            {
//                id++;
//
//
//                id %= idLimit;
//            }
//            else if (Input.GetKeyDown(KeyCode.DownArrow))
//            {
//                id--;
//                id += idLimit;
//
//
//                id %= idLimit;
//            }

            if (Input.GetMouseButtonDown(0))
            {
                if (EventSystem.current.IsPointerOverGameObject())
                {
                    return;
                }
                const float distance  = UnivoxDefine.AxisSize * 8; //Raycast at least 8 chunks away
                var         camRay    = _camera.ScreenPointToRay(Input.mousePosition);
                var         start     = camRay.origin;
                var         direction = camRay.direction;

                var input = new RaycastInput()
                {
                    Start  = start,
                    End    = start + direction * distance,
                    Filter = CollisionFilter.Default
                };
                _lastRay = input;
                if (mode == ClickMode.Alter)
                {
                    _lastRay = input;
                    if (VoxelRaycast(input, out var hit, out var voxelInfo))
                    {
                        var em = voxelInfo.World.EntityManager;

                        var blockIdentityArray = em.GetBuffer <BlockIdentityComponent>(voxelInfo.ChunkEntity);
//                        em.DirtyComponent<BlockActiveComponent.Version>(entity);
                        em.DirtyComponent <BlockIdentityComponent.Version>(voxelInfo.ChunkEntity);


                        blockIdentityArray[voxelInfo.BlockIndex] = new BlockIdentity()
                        {
                            Mod = 0, Block = id
                        };

//                    accessorInfo.Version.Dirty();
//                    accessorRender.Version.Dirty();
//                    BlockChanged.NotifyEntity(voxelInfo.ChunkEntity, voxelInfo.World.EntityManager,
//                        (short) voxelInfo.BlockIndex);

                        _lastVoxel = voxelInfo.WorldPosition;
                        _hitPoint  = hit.Position;

//                    Debug.Log($"Hit Alter : {voxelInfo.BlockPosition}");
                    }
                    else
                    {
                        Debug.Log($"Missed Alter : {hit.Position} -> {hit.SurfaceNormal}");
                    }
                }
                else if (mode == ClickMode.Place)
                {
                    if (VoxelRaycast(input, out var hit, out var voxelInfo))
                    {
                        var em = voxelInfo.World.EntityManager;

                        var blockPos   = voxelInfo.BlockPosition + new int3(hit.SurfaceNormal);
                        var blockIndex = UnivoxUtil.GetIndex(blockPos);

                        if (UnivoxUtil.IsPositionValid(blockPos))
                        {
                            var blockActiveArray   = em.GetBuffer <BlockActiveComponent>(voxelInfo.ChunkEntity);
                            var blockIdentityArray = em.GetBuffer <BlockIdentityComponent>(voxelInfo.ChunkEntity);
                            em.DirtyComponent <BlockIdentityComponent.Version>(voxelInfo.ChunkEntity);
                            em.DirtyComponent <BlockActiveComponent.Version>(voxelInfo.ChunkEntity);


                            blockActiveArray[blockIndex] = new BlockActiveComponent()
                            {
                                Value = true
                            };
                            blockIdentityArray[blockIndex] = new BlockIdentity()
                            {
                                Mod = 0, Block = id
                            };

                            _lastVoxel = UnivoxUtil.ToWorldPosition(voxelInfo.ChunkPosition, blockPos);
                            _hitPoint  = hit.Position;
                        }
                        else
                        {
                            Debug.Log($"OOB Create : {hit.Position} -> {blockPos} -> {hit.SurfaceNormal}");
                        }
                    }
                    else
                    {
                        Debug.Log($"Missed Create : {hit.Position} -> {hit.SurfaceNormal}");
                    }
                }
Пример #3
0
        bool SetupChunk(ChunkIdentity chunkIdentity)
        {
            var world       = GameManager.Universe[chunkIdentity.WorldId];
            var entityWorld = world.EntityWorld.GetOrCreateSystem <InitializationSystemGroup>();


            var chunkPos = chunkIdentity.ChunkId;

            if (!world.ContainsKey(chunkPos))
            {
                return(false);
            }

            var blockReg = GameManager.Registry.Blocks;


            if (!blockReg.TryGetIdentity(BaseGameMod.GrassBlock, out var grass))
            {
                throw new AssetNotFoundException(BaseGameMod.GrassBlock.ToString());
            }
            if (!blockReg.TryGetIdentity(BaseGameMod.DirtBlock, out var dirt))
            {
                throw new AssetNotFoundException(BaseGameMod.DirtBlock.ToString());
            }
            if (!blockReg.TryGetIdentity(BaseGameMod.StoneBlock, out var stone))
            {
                throw new AssetNotFoundException(BaseGameMod.StoneBlock.ToString());
            }
            if (!blockReg.TryGetIdentity(BaseGameMod.SandBlock, out var sand))
            {
                throw new AssetNotFoundException(BaseGameMod.SandBlock.ToString());
            }

            var em = world.EntityManager;
            var entityArchetype = world.EntityManager.CreateArchetype(
                typeof(ChunkIdComponent),
                typeof(BlockActiveComponent), typeof(BlockIdentityComponent),
                typeof(BlockShapeComponent), typeof(BlockMaterialIdentityComponent),
                typeof(BlockSubMaterialIdentityComponent), typeof(BlockCulledFacesComponent)
                );

            var entity = world.GetOrCreate(chunkPos, entityArchetype);


            var activeArray     = em.GetBuffer <BlockActiveComponent>(entity);
            var blockIdentities = em.GetBuffer <BlockIdentityComponent>(entity);

            em.DirtyComponent <BlockActiveComponent.Version>(entity);
            em.DirtyComponent <BlockIdentityComponent.Version>(entity);


            for (var i = 0; i < UnivoxDefine.CubeSize; i++)
            {
                var pos = UnivoxUtil.GetPosition3(i);

                var xTop = (pos.x == UnivoxDefine.AxisSize - 1);
                var yTop = (pos.y == UnivoxDefine.AxisSize - 1);
                var zTop = (pos.z == UnivoxDefine.AxisSize - 1);


                activeArray[i] = true;

                if (!yTop)
                {
                    if (xTop && !zTop)
                    {
                        blockIdentities[i] = stone;
                    }
                    else if (!xTop && zTop)
                    {
                        blockIdentities[i] = sand;
                    }
                    else
                    {
                        blockIdentities[i] = dirt;
                    }
                }

                else
                {
                    blockIdentities[i] = grass;
                }
            }

            return(true);
        }