use of org.joml.Vector3dc in project chunkstories-api by Hugobros3.
the class WorldEnvironment method setupShadowColors.
/**
* Set-ups the shader constants used to render shadows
*/
public default void setupShadowColors(RenderingInterface renderer, Shader shader) {
Vector3dc cameraPosition = renderer.getCamera().getCameraPosition();
shader.setUniform1f("shadowStrength", 1.0f);
shader.setUniform3f("sunColor", getSunlightColor(cameraPosition));
shader.setUniform3f("shadowColor", getShadowColor(cameraPosition));
}
use of org.joml.Vector3dc in project chunkstories by Hugobros3.
the class TaskBakeChunk method task.
@Override
protected boolean task(TaskExecutor taskExecutor) {
if (!(taskExecutor instanceof BakeChunkTaskExecutor))
throw new UnexecutableTaskException(this, "This class requires to be executed by a BakeChunkTaskExecutor");
this.cmd = ((BakeChunkTaskExecutor) taskExecutor).getBuffers();
if (chunk == null) {
throw new RuntimeException("Fuck off no");
}
Vector3dc camera = ((WorldClient) chunk.getWorld()).getWorldRenderer().getRenderingInterface().getCamera().getCameraPosition();
// Check we aren't too far from the camera, and thus that our request hasn't been yet cancelled
int vx = Math2.floor(camera.x() / 32);
int vy = Math2.floor(camera.y() / 32);
int vz = Math2.floor(camera.z() / 32);
int dx = LoopingMathHelper.moduloDistance(chunk.getChunkX(), vx, chunk.getWorld().getSizeInChunks());
int dz = LoopingMathHelper.moduloDistance(chunk.getChunkZ(), vz, chunk.getWorld().getSizeInChunks());
int dy = Math.abs(chunk.getChunkY() - vy);
int chunksViewDistance = (int) (world.getClient().getConfiguration().getIntOption("client.rendering.viewDistance") / 32);
if (dx > chunksViewDistance || dz > chunksViewDistance || dy > 2) {
// logger.info("unscheduled chunk mesh render task for it being too far to be rendered anyway");
return true;
}
// Require the chunk and nearby ones to be already loaded in the world
ChunkRenderable chunkWithinWorld = (ChunkRenderable) world.getChunk(chunk.getChunkX(), chunk.getChunkY(), chunk.getChunkZ());
if (chunkWithinWorld != null) {
// Require the chunks ARROUND it to be already loaded in the world
int nearChunks = 0;
if (world.isChunkLoaded(chunk.getChunkX() + 1, chunk.getChunkY(), chunk.getChunkZ()))
nearChunks++;
if (world.isChunkLoaded(chunk.getChunkX() - 1, chunk.getChunkY(), chunk.getChunkZ()))
nearChunks++;
if (world.isChunkLoaded(chunk.getChunkX(), chunk.getChunkY(), chunk.getChunkZ() + 1))
nearChunks++;
if (world.isChunkLoaded(chunk.getChunkX(), chunk.getChunkY(), chunk.getChunkZ() - 1))
nearChunks++;
if (world.isChunkLoaded(chunk.getChunkX(), chunk.getChunkY() + 1, chunk.getChunkZ()) || chunk.getChunkY() == world.getWorldInfo().getSize().heightInChunks - 1)
nearChunks++;
if (world.isChunkLoaded(chunk.getChunkX(), chunk.getChunkY() - 1, chunk.getChunkZ()) || chunk.getChunkY() == 0)
nearChunks++;
if (nearChunks != 6) {
// We wait until that's the case
return false;
}
} else {
// We wait until the chunk is loaded in the world ( or destroyed, then the task is cancelled )
return false;
}
// If the chunk has pending light updates, wait until THOSE are done
if (chunk.lightBaker.pendingUpdates() > 0) {
chunk.lightBaker.spawnUpdateTaskIfNeeded();
return false;
}
int updatesToConsider = chunk.chunkRenderData.unbakedUpdates.get();
// Don't waste time rendering void chunks m8
if (chunk.isAirChunk())
i = 32;
int cx = chunk.getChunkX();
int cy = chunk.getChunkY();
int cz = chunk.getChunkZ();
// Fill chunk caches ( saves much time avoiding slow-ass world->regions hashmap->chunk holder access for each vert )
for (int relx = -1; relx <= 1; relx++) for (int rely = -1; rely <= 1; rely++) for (int relz = -1; relz <= 1; relz++) {
CubicChunk chunk2 = (CubicChunk) world.getChunk(cx + relx, cy + rely, cz + relz);
if (chunk2 != null)
cmd.cache[((relx + 1) * 3 + (rely + 1)) * 3 + (relz + 1)] = chunk2.chunkVoxelData;
else
cmd.cache[((relx + 1) * 3 + (rely + 1)) * 3 + (relz + 1)] = null;
}
// Make sure we clear each sub-buffer type.
for (int i = 0; i < ChunkMeshDataSubtypes.VertexLayout.values().length; i++) {
for (int j = 0; j < ChunkMeshDataSubtypes.LodLevel.values().length; j++) {
for (int k = 0; k < ChunkMeshDataSubtypes.ShadingType.values().length; k++) {
cmd.byteBuffers[i][j][k].clear();
}
}
}
// Creates wrapper/interfaces for all the elements
ChunkRenderer chunkRendererOutput = new ChunkRenderer() {
@Override
public VoxelBakerHighPoly getHighpolyBakerFor(LodLevel lodLevel, ShadingType renderPass) {
return (VoxelBakerHighPoly) cmd.byteBuffersWrappers[VertexLayout.INTRICATE.ordinal()][lodLevel.ordinal()][renderPass.ordinal()];
}
@Override
public VoxelBakerCubic getLowpolyBakerFor(LodLevel lodLevel, ShadingType renderPass) {
return (VoxelBakerCubic) cmd.byteBuffersWrappers[VertexLayout.WHOLE_BLOCKS.ordinal()][lodLevel.ordinal()][renderPass.ordinal()];
}
};
ChunkBakerRenderContext chunkRenderingContext = new ChunkBakerRenderContext(chunk, cx, cy, cz);
bakedBlockId = -1;
Map<Voxel, DynamicallyRenderedVoxelType> dynamicVoxels = new HashMap<>();
BakeChunkScratchCell cell = new BakeChunkScratchCell(world);
// Render the fucking thing!
for (i = 0; i < 32; i++) {
for (j = 0; j < 32; j++) {
for (k = 0; k < 32; k++) {
peek(i, k, j, cell);
if (cell.voxel.isAir())
continue;
// Fill near-blocks info
// chunkRenderingContext.prepareVoxelLight(); // lol nope
VoxelRenderer voxelRenderer = cell.getVoxelRenderer();
if (voxelRenderer == null)
voxelRenderer = world.getContent().voxels().getDefaultVoxelRenderer();
// Run the VoxelRenderer
voxelRenderer.bakeInto(chunkRendererOutput, chunkRenderingContext, chunk, cell);
// We handle voxels with a dynamic renderer here too - we just add them to a list !
if (voxelRenderer instanceof VoxelDynamicRenderer) {
DynamicallyRenderedVoxelType drvt = dynamicVoxels.get(cell.voxel);
if (drvt == null) {
drvt = new DynamicallyRenderedVoxelType((VoxelDynamicRenderer) voxelRenderer, cell.voxel);
dynamicVoxels.put(cell.voxel, drvt);
}
drvt.indexes.add(i * 1024 + k * 32 + j);
}
bakedBlockId++;
}
}
}
// Parse output neatly
int[][][] sizes = new int[ChunkMeshDataSubtypes.VertexLayout.values().length][ChunkMeshDataSubtypes.LodLevel.values().length][ChunkMeshDataSubtypes.ShadingType.values().length];
;
int[][][] offsets = new int[ChunkMeshDataSubtypes.VertexLayout.values().length][ChunkMeshDataSubtypes.LodLevel.values().length][ChunkMeshDataSubtypes.ShadingType.values().length];
;
int currentOffset = 0;
// Compute total size to create final bytebuffer
int sizeInBytes = 0;
for (VertexLayout vertexLayout : VertexLayout.values()) for (LodLevel lodLevel : LodLevel.values()) for (ShadingType renderPass : ShadingType.values()) {
int vertexLayoutIndex = vertexLayout.ordinal();
int lodLevelIndex = lodLevel.ordinal();
int renderPassIndex = renderPass.ordinal();
final ByteBuffer relevantByteBuffer = cmd.byteBuffers[vertexLayoutIndex][lodLevelIndex][renderPassIndex];
// / vertexLayout.bytesPerVertex;
sizeInBytes += relevantByteBuffer.position();
}
ByteBuffer finalData = MemoryUtil.memAlloc(sizeInBytes);
MemFreeByteBuffer wrappedBuffer = new MemFreeByteBuffer(finalData);
// For EACH section, make offset and shite
for (VertexLayout vertexLayout : VertexLayout.values()) for (LodLevel lodLevel : LodLevel.values()) for (ShadingType renderPass : ShadingType.values()) {
int vertexLayoutIndex = vertexLayout.ordinal();
int lodLevelIndex = lodLevel.ordinal();
int renderPassIndex = renderPass.ordinal();
// Else it gets really long for no reason
final ByteBuffer relevantByteBuffer = cmd.byteBuffers[vertexLayoutIndex][lodLevelIndex][renderPassIndex];
offsets[vertexLayoutIndex][lodLevelIndex][renderPassIndex] = currentOffset;
sizes[vertexLayoutIndex][lodLevelIndex][renderPassIndex] = relevantByteBuffer.position() / vertexLayout.bytesPerVertex;
// Move the offset accordingly
currentOffset += relevantByteBuffer.position();
// Limit the temporary byte buffer and fill the main buffer with it
relevantByteBuffer.limit(relevantByteBuffer.position());
relevantByteBuffer.position(0);
finalData.put(relevantByteBuffer);
}
finalData.flip();
ChunkMeshDataSections newRenderData = new ChunkMeshDataSections(wrappedBuffer, sizes, offsets);
DynamicallyRenderedVoxelType[] bakedDrvt = new DynamicallyRenderedVoxelType[dynamicVoxels.size()];
Iterator<DynamicallyRenderedVoxelType> i = dynamicVoxels.values().iterator();
for (int j = 0; j < dynamicVoxels.size(); j++) {
if (i.hasNext())
bakedDrvt[j] = i.next();
else {
logger.error("while baking dynamicVoxelTypes array the iterator returned less than dynamicVoxels.size() elements");
logger.error("cancelling");
bakedDrvt = null;
break;
}
}
newRenderData.dynamicVoxelTypes = bakedDrvt;
chunk.getChunkRenderData().setData(newRenderData);
chunk.chunkRenderData.unbakedUpdates.addAndGet(-updatesToConsider);
return true;
}
use of org.joml.Vector3dc in project chunkstories by Hugobros3.
the class FarTerrainMeshRenderer method renderTerrain.
public void renderTerrain(RenderingInterface renderer, ReadyVoxelMeshesMask mask) {
// Check for world updates
Vector3dc cameraPosition = renderer.getCamera().getCameraPosition();
int xCoordinates = ((int) (double) cameraPosition.x());
int zCoordinates = ((int) (double) cameraPosition.z());
xCoordinates %= world.getWorldSize();
zCoordinates %= world.getWorldSize();
if (xCoordinates < 0)
xCoordinates += world.getWorldSize();
if (zCoordinates < 0)
zCoordinates += world.getWorldSize();
int chunkCoordinatesX = xCoordinates / 32;
int chunkCoordinatesZ = zCoordinates / 32;
if (centerChunkX != chunkCoordinatesX || centerChunkZ != chunkCoordinatesZ) {
centerChunkX = chunkCoordinatesX;
centerChunkZ = chunkCoordinatesZ;
this.markFarTerrainMeshDirty();
}
// Setup shader etc
Shader terrainShader = renderer.useShader("terrain");
renderer.setBlendMode(BlendMode.DISABLED);
renderer.getCamera().setupShader(terrainShader);
worldRenderer.getSkyRenderer().setupShader(terrainShader);
terrainShader.setUniform1f("viewDistance", world.getClient().getConfiguration().getIntOption("client.rendering.viewDistance"));
Texture2D waterTexture = renderer.textures().getTexture("./textures/water/shallow.png");
waterTexture.setLinearFiltering(true);
waterTexture.setMipMapping(true);
// renderer.bindCubemap("environmentCubemap", worldRenderer.renderBuffers.rbEnvironmentMap);
renderer.bindTexture2D("blockLightmap", TexturesHandler.getTexture("./textures/environement/light.png"));
Texture2D lightColors = TexturesHandler.getTexture("./textures/environement/lightcolors.png");
renderer.bindTexture2D("lightColors", lightColors);
renderer.bindTexture2D("normalTexture", waterTexture);
world.getGenerator().getEnvironment().setupShadowColors(renderer, terrainShader);
// worldRenderer.setupShadowColors(terrainShader);
renderer.bindTexture2D("vegetationColorTexture", world.getGenerator().getEnvironment().getGrassTexture(renderer));
// renderingContext.bindTexture2D("vegetationColorTexture", worldRenderer.getGrassTexture());
terrainShader.setUniform1f("mapSize", world.getSizeInChunks() * 32);
// TODO hidden inputs ?
if (renderer.getClient().getInputsManager().getInputByName("wireframeFarTerrain").isPressed() && ClientLimitations.isDebugAllowed)
renderer.setPolygonFillMode(PolygonFillMode.WIREFRAME);
if (!renderer.getClient().getInputsManager().getInputByName("hideFarTerrain").isPressed() && ClientLimitations.isDebugAllowed)
drawTerrainBits(renderer, mask, terrainShader);
renderer.setPolygonFillMode(PolygonFillMode.FILL);
}
use of org.joml.Vector3dc in project chunkstories by Hugobros3.
the class DefaultWorldCollisionsManager method raytraceSolid.
private Location raytraceSolid(Vector3dc initialPosition, Vector3dc directionIn, double limit, boolean outer, boolean selectable) {
Vector3d direction = new Vector3d();
directionIn.normalize(direction);
// direction.scale(0.02);
// float distance = 0f;
CellData cell;
// Voxel vox;
int x, y, z;
x = (int) Math.floor(initialPosition.x());
y = (int) Math.floor(initialPosition.y());
z = (int) Math.floor(initialPosition.z());
// DDA algorithm
// It requires double arrays because it works using loops over each dimension
double[] rayOrigin = new double[3];
double[] rayDirection = new double[3];
rayOrigin[0] = initialPosition.x();
rayOrigin[1] = initialPosition.y();
rayOrigin[2] = initialPosition.z();
rayDirection[0] = direction.x();
rayDirection[1] = direction.y();
rayDirection[2] = direction.z();
int[] voxelCoords = new int[] { x, y, z };
int[] voxelDelta = new int[] { 0, 0, 0 };
double[] deltaDist = new double[3];
double[] next = new double[3];
int[] step = new int[3];
int side = 0;
// Prepare distances
for (int i = 0; i < 3; ++i) {
double deltaX = rayDirection[0] / rayDirection[i];
double deltaY = rayDirection[1] / rayDirection[i];
double deltaZ = rayDirection[2] / rayDirection[i];
deltaDist[i] = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
if (rayDirection[i] < 0.f) {
step[i] = -1;
next[i] = (rayOrigin[i] - voxelCoords[i]) * deltaDist[i];
} else {
step[i] = 1;
next[i] = (voxelCoords[i] + 1.f - rayOrigin[i]) * deltaDist[i];
}
}
do {
// DDA steps
side = 0;
for (int i = 1; i < 3; ++i) {
if (next[side] > next[i]) {
side = i;
}
}
next[side] += deltaDist[side];
voxelCoords[side] += step[side];
voxelDelta[side] += step[side];
x = voxelCoords[0];
y = voxelCoords[1];
z = voxelCoords[2];
cell = world.peekSafely(x, y, z);
if (cell.getVoxel().getDefinition().isSolid() || (selectable && cell.getVoxel().getDefinition().isSelectable())) {
boolean collides = false;
for (CollisionBox box : cell.getTranslatedCollisionBoxes()) {
// System.out.println(box);
Vector3dc collisionPoint = box.lineIntersection(initialPosition, direction);
if (collisionPoint != null) {
collides = true;
// System.out.println("collides @ "+collisionPoint);
}
}
if (collides) {
if (!outer)
return new Location(world, x, y, z);
else {
// Back off a bit
switch(side) {
case 0:
x -= step[side];
break;
case 1:
y -= step[side];
break;
case 2:
z -= step[side];
break;
}
return new Location(world, x, y, z);
}
}
}
// distance += deltaDist[side];
} while (voxelDelta[0] * voxelDelta[0] + voxelDelta[1] * voxelDelta[1] + voxelDelta[2] * voxelDelta[2] < limit * limit);
return null;
}
use of org.joml.Vector3dc in project chunkstories-core by Hugobros3.
the class EntityLivingImplementation method tick.
@Override
public void tick() {
if (getWorld() == null)
return;
// Despawn counter is strictly a client matter
if (getWorld() instanceof WorldMaster) {
if (isDead()) {
deathDespawnTimer--;
if (deathDespawnTimer < 0) {
world.removeEntity(this);
return;
}
}
// Fall damage
if (isOnGround()) {
if (!wasStandingLastTick && !Double.isNaN(lastStandingHeight)) {
double fallDistance = lastStandingHeight - this.positionComponent.getLocation().y();
if (fallDistance > 0) {
// System.out.println("Fell "+fallDistance+" meters");
if (fallDistance > 5) {
float fallDamage = (float) (fallDistance * fallDistance / 2);
System.out.println(this + "Took " + fallDamage + " hp of fall damage");
this.damage(DAMAGE_CAUSE_FALL, fallDamage);
}
}
}
lastStandingHeight = this.positionComponent.getLocation().y();
}
this.wasStandingLastTick = isOnGround();
}
boolean shouldDoTick = false;
if (this instanceof EntityControllable) {
Controller controller = ((EntityControllable) this).getControllerComponent().getController();
if (controller == null)
shouldDoTick = (getWorld() instanceof WorldMaster);
else if (getWorld() instanceof WorldClient && ((WorldClient) getWorld()).getClient().getPlayer().equals(controller))
shouldDoTick = true;
} else
shouldDoTick = (getWorld() instanceof WorldMaster);
if (shouldDoTick) {
Vector3dc ogVelocity = getVelocityComponent().getVelocity();
Vector3d velocity = new Vector3d(ogVelocity);
Vector2f headRotationVelocity = this.getEntityRotationComponent().tickInpulse();
getEntityRotationComponent().addRotation(headRotationVelocity.x(), headRotationVelocity.y());
// voxelIn = VoxelsStore.get().getVoxelById(VoxelFormat.id(world.getVoxelData(positionComponent.getLocation())));
// voxelIn.getType().isLiquid();
boolean inWater = isInWater();
// Gravity
if (!(this instanceof EntityFlying && ((EntityFlying) this).getFlyingComponent().get())) {
double terminalVelocity = inWater ? -0.05 : -0.5;
if (velocity.y() > terminalVelocity)
velocity.y = (velocity.y() - 0.008);
if (velocity.y() < terminalVelocity)
velocity.y = (terminalVelocity);
// Water limits your overall movement
double targetSpeedInWater = 0.02;
if (inWater) {
if (velocity.length() > targetSpeedInWater) {
double decelerationThen = Math.pow((velocity.length() - targetSpeedInWater), 1.0);
// System.out.println(decelerationThen);
double maxDeceleration = 0.006;
if (decelerationThen > maxDeceleration)
decelerationThen = maxDeceleration;
// System.out.println(decelerationThen);
acceleration.add(new Vector3d(velocity).normalize().negate().mul(decelerationThen));
// acceleration.add(0.0, decelerationThen * (velocity.y() > 0.0 ? 1.0 : -1.0), 0.0);
}
}
}
// Acceleration
velocity.x = (velocity.x() + acceleration.x());
velocity.y = (velocity.y() + acceleration.y());
velocity.z = (velocity.z() + acceleration.z());
// TODO ugly
if (!world.isChunkLoaded((int) (double) positionComponent.getLocation().x() / 32, (int) (double) positionComponent.getLocation().y() / 32, (int) (double) positionComponent.getLocation().z() / 32)) {
velocity.set(0d, 0d, 0d);
}
// Eventually moves
Vector3dc remainingToMove = moveWithCollisionRestrain(velocity.x(), velocity.y(), velocity.z());
Vector2d remaining2d = new Vector2d(remainingToMove.x(), remainingToMove.z());
// Auto-step logic
if (remaining2d.length() > 0.001 && isOnGround()) {
// Cap max speed we can get through the bump ?
if (remaining2d.length() > 0.20d) {
System.out.println("Too fast, capping");
remaining2d.normalize();
remaining2d.mul(0.20);
}
// Get whatever we are colliding with
// Test if setting yourself on top would be ok
// Do it if possible
// TODO remake proper
Vector3d blockedMomentum = new Vector3d(remaining2d.x(), 0, remaining2d.y());
for (double d = 0.25; d < 0.5; d += 0.05) {
// I don't want any of this to reflect on the object, because it causes ugly jumps in the animation
Vector3dc canMoveUp = this.canMoveWithCollisionRestrain(new Vector3d(0.0, d, 0.0));
// It can go up that bit
if (canMoveUp.length() == 0.0f) {
// Would it help with being stuck ?
Vector3d tryFromHigher = new Vector3d(this.getLocation());
tryFromHigher.add(new Vector3d(0.0, d, 0.0));
Vector3dc blockedMomentumRemaining = this.canMoveWithCollisionRestrain(tryFromHigher, blockedMomentum);
// If length of remaining momentum < of what we requested it to do, that means it *did* go a bit further away
if (blockedMomentumRemaining.length() < blockedMomentum.length()) {
// Where would this land ?
Vector3d afterJump = new Vector3d(tryFromHigher);
afterJump.add(blockedMomentum);
afterJump.sub(blockedMomentumRemaining);
// land distance = whatever is left of our -0.55 delta when it hits the ground
Vector3dc landDistance = this.canMoveWithCollisionRestrain(afterJump, new Vector3d(0.0, -d, 0.0));
afterJump.add(new Vector3d(0.0, -d, 0.0));
afterJump.sub(landDistance);
this.setLocation(new Location(world, afterJump));
remaining2d = new Vector2d(blockedMomentumRemaining.x(), blockedMomentumRemaining.z());
break;
}
}
}
}
// Collisions, snap to axises
if (Math.abs(remaining2d.x()) >= 0.001d)
velocity.x = (0d);
if (Math.abs(remaining2d.y()) >= 0.001d)
velocity.z = (0d);
// Stap it
if (isOnGround() && velocity.y() < 0)
velocity.y = (0d);
else if (isOnGround())
velocity.y = (0d);
getVelocityComponent().setVelocity(velocity);
}
}
Aggregations