Example #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);
        }
Example #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}");
                    }
                }