partially functional raycasting

This commit is contained in:
James Seibel
2022-11-16 22:35:56 -06:00
parent d31013a680
commit 103a03c90f
4 changed files with 95 additions and 6 deletions
@@ -10,5 +10,6 @@ import com.seibel.lod.api.interfaces.IDhApiUnsafeWrapper;
*/
public interface IDhApiBlockStateWrapper extends IDhApiUnsafeWrapper
{
boolean isAir();
}
@@ -38,8 +38,13 @@ public interface IDhApiLevelWrapper extends IDhApiUnsafeWrapper
boolean hasSkyLight();
/** Returns the max block height of the level(?) */
int getHeight();
/**
* Returns the lowest possible block position for the level. <br>
* For MC versions before 1.19 this will return 0.
*/
default int getMinHeight() { return 0; }
}
@@ -147,6 +147,7 @@ public class Vec3f
return this.x * vector.x + this.y * vector.y + this.z * vector.z;
}
/** Returns true if normalization had to be done */
public boolean normalize()
{
float squaredSum = this.x * this.x + this.y * this.y + this.z * this.z;
@@ -18,12 +18,17 @@ import com.seibel.lod.core.pos.DhSectionPos;
import com.seibel.lod.core.util.BitShiftUtil;
import com.seibel.lod.core.util.ColorUtil;
import com.seibel.lod.core.util.LodUtil;
import com.seibel.lod.core.util.MathUtil;
import com.seibel.lod.core.util.math.Vec3d;
import com.seibel.lod.core.util.math.Vec3f;
import com.seibel.lod.core.util.math.Vec3i;
import com.seibel.lod.core.wrapperInterfaces.block.IBlockStateWrapper;
import com.seibel.lod.core.wrapperInterfaces.minecraft.IMinecraftClientWrapper;
import com.seibel.lod.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper;
import com.seibel.lod.core.wrapperInterfaces.world.IBiomeWrapper;
import com.seibel.lod.core.wrapperInterfaces.world.IClientLevelWrapper;
import com.seibel.lod.core.wrapperInterfaces.world.ILevelWrapper;
import net.minecraft.client.Minecraft;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -47,6 +52,7 @@ public class DhApiTerrainDataRepo implements IDhApiTerrainDataRepo
private static volatile boolean debugThreadRunning = false;
private static String currentDebugBiomeName = "";
private static int currentDebugBlockColorInt = -1;
private static Vec3i currentDebugVec3i = new Vec3i();
@@ -56,6 +62,66 @@ public class DhApiTerrainDataRepo implements IDhApiTerrainDataRepo
}
// TODO: this will need ot use API objects
public DhApiResult<Vec3i> getLodPosFromRay(IDhApiLevelWrapper levelWrapper, DhBlockPos rayOrigin, Vec3f directionVector, byte detailLevel)
{
directionVector.normalize();
int minBlockHeight = levelWrapper.getMinHeight();
int maxBlockHeight = levelWrapper.getHeight();
int maxLength = 50;
// walk through the grid //
int currentLength = 0;
Vec3d exactPos = new Vec3d(rayOrigin.x, rayOrigin.y, rayOrigin.z);
Vec3i blockPos = new Vec3i(rayOrigin.x, rayOrigin.y, rayOrigin.z);
while (blockPos.y >= minBlockHeight && blockPos.y < maxBlockHeight
&& currentLength <= maxLength)
{
// get the LOD at this position
DhApiResult<DhApiTerrainDataPoint[]> result = this.getColumnDataAtBlockPos(levelWrapper, blockPos.x, blockPos.z);
if (!result.success)
{
// if there was an error, stop and return it
return DhApiResult.createFail(result.errorMessage);
}
// is there a LOD at this position?
for (DhApiTerrainDataPoint dataPoint : result.payload)
{
// is this LOD air?
if (dataPoint.blockStateWrapper != null && !dataPoint.blockStateWrapper.isAir())
{
// does this LOD contain the given Y position?
if (dataPoint.bottomYBlockPos <= exactPos.y && exactPos.y <= dataPoint.topYBlockPos)
{
return DhApiResult.createSuccess(blockPos);
}
}
}
exactPos.x += directionVector.x;
exactPos.y += directionVector.y;
exactPos.z += directionVector.z;
blockPos.x = (int) Math.round(exactPos.x);
blockPos.y = (int) Math.round(exactPos.y);
blockPos.z = (int) Math.round(exactPos.z);
// calculate the taxiCab Distance
currentLength = (int) (Math.abs(rayOrigin.x - exactPos.x) + Math.abs(rayOrigin.y - exactPos.y) + Math.abs(rayOrigin.z - exactPos.z));
}
return DhApiResult.createSuccess(null);
}
@Override
public DhApiResult<DhApiTerrainDataPoint> getSingleDataPointAtBlockPos(IDhApiLevelWrapper levelWrapper, int blockPosX, int blockPosY, int blockPosZ)
@@ -158,7 +224,8 @@ public class DhApiTerrainDataRepo implements IDhApiTerrainDataRepo
* If nullableBlockYPos is null: returns every datapoint in the column defined by the DhLodPos. <br>
* If nullableBlockYPos is NOT null: returns a single datapoint in the column defined by the DhLodPos which contains the block Y position. <br><br>
*
* Returns an empty array if no data could be returned.
* If the ApiResult is successful there will be an array of data. <br>
* The returned array will be empty if no data could be retrieved.
*/
private static DhApiResult<DhApiTerrainDataPoint[]> getTerrainDataColumnArray(IDhApiLevelWrapper levelWrapper, DhLodPos requestedColumnPos, Integer nullableBlockYPos)
{
@@ -288,11 +355,26 @@ public class DhApiTerrainDataRepo implements IDhApiTerrainDataRepo
Thread thread = new Thread(() -> {
try
{
DhApiResult<DhApiTerrainDataPoint> single = getTerrainDataAtBlockYPos(levelWrapper, new DhLodPos(LodUtil.BLOCK_DETAIL_LEVEL, blockPosX, blockPosZ), blockPosY);
DhApiResult<DhApiTerrainDataPoint[]> column = getTerrainDataColumnArray(levelWrapper, new DhLodPos(LodUtil.BLOCK_DETAIL_LEVEL, blockPosX, blockPosZ), null);
// DhApiResult<DhApiTerrainDataPoint> single = getTerrainDataAtBlockYPos(levelWrapper, new DhLodPos(LodUtil.BLOCK_DETAIL_LEVEL, blockPosX, blockPosZ), blockPosY);
// DhApiResult<DhApiTerrainDataPoint[]> column = getTerrainDataColumnArray(levelWrapper, new DhLodPos(LodUtil.BLOCK_DETAIL_LEVEL, blockPosX, blockPosZ), null);
// DhLodPos chunkPos = new DhLodPos(LodUtil.BLOCK_DETAIL_LEVEL, blockPosX, blockPosZ).convertUpwardsTo(LodUtil.CHUNK_DETAIL_LEVEL);
// DhApiResult<DhApiTerrainDataPoint[][][]> area = getTerrainDataOverAreaForPositionDetailLevel(levelWrapper, chunkPos);
IMinecraftRenderWrapper MC_RENDER = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class);
DhApiResult<Vec3i> rayCast = INSTANCE.getLodPosFromRay(levelWrapper, MC_RENDER.getCameraBlockPosition(), MC_RENDER.getLookAtVector(), LodUtil.BLOCK_DETAIL_LEVEL);
if (rayCast.payload != null && !rayCast.payload.equals(currentDebugVec3i))
{
currentDebugVec3i = rayCast.payload;
LOGGER.info("raycast: " + currentDebugVec3i);
}
else if (rayCast.payload == null && currentDebugVec3i != null)
{
currentDebugVec3i = null;
LOGGER.info("raycast: [INFINITY]");
}
DhLodPos chunkPos = new DhLodPos(LodUtil.BLOCK_DETAIL_LEVEL, blockPosX, blockPosZ).convertUpwardsTo(LodUtil.CHUNK_DETAIL_LEVEL);
DhApiResult<DhApiTerrainDataPoint[][][]> area = getTerrainDataOverAreaForPositionDetailLevel(levelWrapper, chunkPos);
int debugPoint = 0;
}