use of io.xol.chunkstories.api.rendering.voxel.VoxelBakerHighPoly in project chunkstories-core by Hugobros3.
the class SmoothStepVoxelRenderer method bakeInto.
@Override
public int bakeInto(ChunkRenderer chunkRenderer, ChunkRenderContext bakingContext, Chunk chunk, CellData voxelInformations) {
int x = bakingContext.getRenderedVoxelPositionInChunkX();
int y = bakingContext.getRenderedVoxelPositionInChunkY();
int z = bakingContext.getRenderedVoxelPositionInChunkZ();
// Check we have 8 neighbours
World world = voxelInformations.getWorld();
int wx = x + chunk.getChunkX() * 32;
int wy = y + chunk.getChunkY() * 32;
int wz = z + chunk.getChunkZ() * 32;
// bakingContext.getCurrentVoxelLighter().getSunlightLevelInterpolated(x + 0.5f, y + 0.5f, z + 0.5f);
byte sunlight = 0;
// bakingContext.getCurrentVoxelLighter().getBlocklightLevelInterpolated(x + 0.5f, y + 0.5f, z + 0.5f);
byte blockLight = 15;
byte ao = bakingContext.getCurrentVoxelLighter().getAoLevelInterpolated(x + 0.5f, y + 0.5f, z + 0.5f);
VoxelBakerHighPoly baker = chunkRenderer.getHighpolyBakerFor(LodLevel.ANY, ShadingType.OPAQUE);
/*for(int a = wx - 1; a <= wx + 1; a++)
for(int b = wz - 1; b <= wz + 1; b ++) {
//If a single one is fucky
if(VoxelFormat.id(world.getVoxelData(a, wy, b)) != voxel.getId())
return old(voxelInformations).renderInto(chunkRenderer, bakingContext, chunk, voxelInformations);
}*/
// if(true)
// return old(voxelInformations).renderInto(chunkRenderer, bakingContext, chunk, voxelInformations);
VoxelTexture texture = voxel.getVoxelTexture(VoxelSide.TOP, voxelInformations);
baker.usingTexture(texture);
// int offset = texture.getAtlasOffset() / texture.getTextureScale();
// int textureS = texture.getAtlasS() + (x % texture.getTextureScale()) * offset;
// int textureT = texture.getAtlasT() + (z % texture.getTextureScale()) * offset;
// final int max_step = 8;
float[] height = new float[9];
for (int a = wx - 1; a <= wx + 1; a++) bLoop: for (int b = wz - 1; b <= wz + 1; b++) {
for (int h = wy + 1; h >= wy - 1; h--) {
Voxel peek = world.peekSimple(a, h, b);
int raw = world.peekRaw(a, h, b);
if (voxel.sameKind(peek)) {
height[(a - wx + 1) * 3 + b - wz + 1] = h - chunk.getChunkY() * 32 + 1 / 8f + VoxelFormat.meta(raw) / 8f;
continue bLoop;
}
}
return old(voxelInformations).bakeInto(chunkRenderer, bakingContext, chunk, voxelInformations);
}
// X --->
// 036
// 147
// 258
float corner00 = (height[0] + height[3] + height[1] + height[4]) / 4f;
float corner10 = (height[3] + height[4] + height[7] + height[6]) / 4f;
float corner01 = (height[1] + height[4] + height[2] + height[5]) / 4f;
float corner11 = (height[4] + height[7] + height[5] + height[8]) / 4f;
/*float corner00, corner01, corner10, corner11;
int data00, data01, data10, data11;
data00 = world.getVoxelData(wx, wy, wz);
corner00 = 1/8f + y + VoxelFormat.meta(data00) / 8f;
//Merge with left/top upper lip
int dataN0p = world.getVoxelData(wx - 1, wy + 1, wz);
int data0Np = world.getVoxelData(wx, wy + 1, wz - 1);
int dataNNp = world.getVoxelData(wx - 1, wy + 1, wz - 1);
if(VoxelFormat.id(dataN0p) == voxel.getId()) {
int meta = VoxelFormat.meta(dataN0p);
if(meta < max_step)
corner00 = Math.max(0, 1/8f + y + 1 + meta / 8f);
}
else if(VoxelFormat.id(data0Np) == voxel.getId()) {
int meta = VoxelFormat.meta(data0Np);
if(meta < max_step)
corner00 = Math.max(0, 1/8f + y + 1 + meta / 8f);
}
else if(VoxelFormat.id(dataNNp) == voxel.getId()) {
int meta = VoxelFormat.meta(dataNNp);
if(meta < max_step)
corner00 = Math.max(0, 1/8f + y + 1 + meta / 8f);
}
int data01p = world.getVoxelData(wx, wy + 1, wz + 1);
if(VoxelFormat.id(data01p) == voxel.getId()) {
corner01 = 1/8f + y + 1 +VoxelFormat.meta(data01p) / 8f;
}
else {
data01 = world.getVoxelData(wx, wy, wz + 1);
if(VoxelFormat.id(data01) == voxel.getId()) {
corner01 = 1/8f + y +VoxelFormat.meta(data01) / 8f;
}
else {
corner01 = corner00;
}
}
if(VoxelFormat.id(dataN0p) == voxel.getId()) {
int meta = VoxelFormat.meta(dataN0p);
if(meta < max_step)
corner01 = Math.max(0, 1/8f + y + 1 + meta / 8f);
}
int data10p = world.getVoxelData(wx + 1, wy + 1, wz);
if(VoxelFormat.id(data10p) == voxel.getId()) {
corner10 = 1/8f + y + 1 +VoxelFormat.meta(data10p) / 8f;
}
else {
data10 = world.getVoxelData(wx + 1, wy, wz);
if(VoxelFormat.id(data10) == voxel.getId()) {
corner10 = 1/8f + y +VoxelFormat.meta(data10) / 8f;
}
else {
corner10 = corner00;
}
}
if(VoxelFormat.id(data0Np) == voxel.getId()) {
int meta = VoxelFormat.meta(data0Np);
if(meta < max_step)
corner10 = Math.max(0, 1/8f + y + 1 + meta / 8f);
}
int data11p = world.getVoxelData(wx + 1, wy + 1, wz + 1);
if(VoxelFormat.id(data11p) == voxel.getId()) {
corner11 = 1/8f + y + 1 +VoxelFormat.meta(data11p) / 8f;
}
else {
data11 = world.getVoxelData(wx + 1, wy, wz + 1);
if(VoxelFormat.id(data11) == voxel.getId()) {
corner11 = 1/8f + y +VoxelFormat.meta(data11) / 8f;
}
else {
corner11 = corner00;
}
}
/*float corner00 = 1/8f + y + VoxelFormat.meta(world.getVoxelData(wx, wy, wz)) / 8f;
int dataN0p = world.getVoxelData(wx - 1, wy + 1, wz);
int data0Np = world.getVoxelData(wx, wy + 1, wz - 1);
int dataNNp = world.getVoxelData(wx - 1, wy + 1, wz - 1);
if(VoxelFormat.id(dataN0p) == voxel.getId()) {
int meta = VoxelFormat.meta(dataN0p);
if(meta < max_step)
corner00 = Math.max(corner00, 1/8f + y + 1 + meta / 8f);
}
else if(VoxelFormat.id(data0Np) == voxel.getId()) {
int meta = VoxelFormat.meta(data0Np);
if(meta < max_step)
corner00 = Math.max(corner00, 1/8f + y + 1 + meta / 8f);
}
else if(VoxelFormat.id(dataNNp) == voxel.getId()) {
int meta = VoxelFormat.meta(dataNNp);
if(meta < max_step)
corner00 = Math.max(corner00, 1/8f + y + 1 + meta / 8f);
}
int data01 = world.getVoxelData(wx, wy, wz + 1);
float corner01;
if(VoxelFormat.id(data01) == voxel.getId()) {
corner01 = 1/8f + y + VoxelFormat.meta(data01) / 8f;
}
else {
corner01 = corner00;
int data01p = world.getVoxelData(wx, wy + 1, wz + 1);
if(VoxelFormat.id(data01p) == voxel.getId()) {
int meta = VoxelFormat.meta(data01p);
if(meta < max_step)
corner01 = 1/8f + y + 1 + meta / 8f;
}
}
int dataN1p = world.getVoxelData(wx - 1, wy + 1, wz);
if(VoxelFormat.id(dataN1p) == voxel.getId()) {
int meta = VoxelFormat.meta(dataN1p);
if(meta < max_step)
corner01 = Math.max(corner01, 1/8f + y + 1 + meta / 8f);
}
//float corner01 = VoxelFormat.id(data01) == voxel.getId() ? 1/8f + y + VoxelFormat.meta(data01) / 8f : corner00;
int data10 = world.getVoxelData(wx + 1, wy, wz);
float corner10;
if(VoxelFormat.id(data10) == voxel.getId()) {
corner10 = 1/8f + y + VoxelFormat.meta(data10) / 8f;
}
else {
corner10 = corner00;
int data10p = world.getVoxelData(wx + 1, wy + 1, wz);
if(VoxelFormat.id(data10p) == voxel.getId()) {
int meta = VoxelFormat.meta(data10p);
if(meta < max_step)
corner10 = 1/8f + y + 1 + meta / 8f;
}
}
//float corner10 = VoxelFormat.id(data10) == voxel.getId() ? 1/8f + y + VoxelFormat.meta(data10) / 8f : corner00;
int data1Np = world.getVoxelData(wx, wy + 1, wz - 1);
if(VoxelFormat.id(data1Np) == voxel.getId()) {
int meta = VoxelFormat.meta(data1Np);
if(meta < max_step)
corner10 = Math.max(corner10, 1/8f + y + 1 + meta / 8f);
}
int data11 = world.getVoxelData(wx + 1, wy, wz + 1);
float corner11;
if(VoxelFormat.id(data11) == voxel.getId()) {
corner11 = 1/8f + y + VoxelFormat.meta(data11) / 8f;
}
else {
corner11 = Math.max(corner10, corner01);
int data11p = world.getVoxelData(wx + 1, wy + 1, wz + 1);
if(VoxelFormat.id(data11p) == voxel.getId()) {
int meta = VoxelFormat.meta(data11p);
if(meta < max_step)
corner11 = 1/8f + y + 1 + meta / 8f;
}
}*/
// float corner11 = VoxelFormat.id(data11) == voxel.getId() ? 1/8f + y + VoxelFormat.meta(data11) / 8f : Math.min(corner10, corner01);
baker.setNormal(0f, 1f, 0f);
baker.beginVertex(x, corner00, z);
baker.setTextureCoordinates(0, 0);
baker.setVoxelLight(sunlight, blockLight, ao);
// baker.addNormalsInt(511, 1023, 511, (byte)0);
baker.endVertex();
baker.beginVertex(x + 1, corner11, z + 1);
baker.setTextureCoordinates(0 + 1, 0 + 1);
baker.setVoxelLight(sunlight, blockLight, ao);
// baker.addNormalsInt(511, 1023, 511, (byte)0);
baker.endVertex();
baker.beginVertex(x + 1, corner10, z);
baker.setTextureCoordinates(0 + 1, 0);
baker.setVoxelLight(sunlight, blockLight, ao);
// baker.addNormalsInt(511, 1023, 511, (byte)0);
baker.endVertex();
// <- ------------------- ->
baker.beginVertex(x, corner01, z + 1);
baker.setTextureCoordinates(0, 0 + 1);
baker.setVoxelLight(sunlight, blockLight, ao);
// baker.addNormalsInt(511, 1023, 511, (byte)0);
baker.endVertex();
baker.beginVertex(x + 1, corner11, z + 1);
baker.setTextureCoordinates(0 + 1, 0 + 1);
baker.setVoxelLight(sunlight, blockLight, ao);
// baker.addNormalsInt(511, 1023, 511, (byte)0);
baker.endVertex();
baker.beginVertex(x, corner00, z);
baker.setTextureCoordinates(0, 0);
baker.setVoxelLight(sunlight, blockLight, ao);
// baker.addNormalsInt(511, 1023, 511, (byte)0);
baker.endVertex();
return 6;
}
use of io.xol.chunkstories.api.rendering.voxel.VoxelBakerHighPoly 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;
}
Aggregations