Search in sources :

Example 1 with VoxelTexture

use of io.xol.chunkstories.api.voxel.textures.VoxelTexture 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;
}
Also used : VoxelBakerHighPoly(io.xol.chunkstories.api.rendering.voxel.VoxelBakerHighPoly) Voxel(io.xol.chunkstories.api.voxel.Voxel) World(io.xol.chunkstories.api.world.World) VoxelTexture(io.xol.chunkstories.api.voxel.textures.VoxelTexture)

Example 2 with VoxelTexture

use of io.xol.chunkstories.api.voxel.textures.VoxelTexture in project chunkstories by Hugobros3.

the class FarTerrainMeshRenderer method getBlocksTexturesSummary.

/**
 * Dirty and stupid
 */
public Texture1D getBlocksTexturesSummary() {
    if (!blocksTexturesSummaryDone) {
        int size = 512;
        ByteBuffer bb = ByteBuffer.allocateDirect(size * 4);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        int counter = 0;
        Iterator<VoxelTexture> i = world.getContent().voxels().textures().all();
        while (i.hasNext() && counter < size) {
            VoxelTextureAtlased voxelTexture = (VoxelTextureAtlased) i.next();
            bb.put((byte) (voxelTexture.getColor().x() * 255));
            bb.put((byte) (voxelTexture.getColor().y() * 255));
            bb.put((byte) (voxelTexture.getColor().z() * 255));
            bb.put((byte) (voxelTexture.getColor().w() * 255));
            voxelTexture.positionInColorIndex = counter;
            counter++;
        }
        // Padding
        while (counter < size) {
            bb.put((byte) (0));
            bb.put((byte) (0));
            bb.put((byte) (0));
            bb.put((byte) (0));
            counter++;
        }
        bb.flip();
        blockTexturesSummary.uploadTextureData(size, bb);
        blockTexturesSummary.setLinearFiltering(false);
        blocksTexturesSummaryDone = true;
    }
    return blockTexturesSummary;
}
Also used : VoxelTextureAtlased(io.xol.chunkstories.voxel.VoxelTextureAtlased) VoxelTexture(io.xol.chunkstories.api.voxel.textures.VoxelTexture) ByteBuffer(java.nio.ByteBuffer)

Example 3 with VoxelTexture

use of io.xol.chunkstories.api.voxel.textures.VoxelTexture in project chunkstories by Hugobros3.

the class CustomVoxelModel method renderInto.

public int renderInto(VoxelBakerHighPoly baker, ChunkRenderContext bakingContext, CellData info, Chunk chunk, int x, int y, int z) {
    // int lightLevelSun = chunk.getSunLight(x, y, z);
    // int lightLevelVoxel = chunk.getBlockLight(x, y, z);
    // Obtains the light amount (sun/voxel) for this model
    // TODO interpolation w/ neighbors
    // float[] lightColors = bakeLightColors(lightLevelVoxel, lightLevelVoxel, lightLevelVoxel, lightLevelVoxel, lightLevelSun, lightLevelSun, lightLevelSun, lightLevelSun);
    // We have an array of textures and we jump to the next based on baked offsets and vertice counting
    int modelTextureIndex = 0;
    VoxelTexture currentVoxelTexture = null;
    // Selects an appropriate texture
    currentVoxelTexture = selectsTextureFromIndex(info, modelTextureIndex);
    baker.usingTexture(currentVoxelTexture);
    int maxVertexIndexToUseThisTextureFor = this.texturesOffsets[modelTextureIndex];
    // Actual coordinates in the Atlas
    // int textureS = currentVoxelTexture.getAtlasS();
    // int textureT = currentVoxelTexture.getAtlasT();
    // We look the 6 adjacent faces to determine wether or not we should consider them culled
    boolean[] cullingCache = new boolean[6];
    for (int j = 0; j < 6; j++) {
        CellData adj = info.getNeightbor(j);
        // If it is, don't draw it.
        cullingCache[j] = adj.getVoxel().getDefinition().isOpaque() || adj.getVoxel().isFaceOpaque(VoxelSide.values()[j], adj.getMetaData()) || info.getVoxel().getDefinition().isSelfOpaque() && adj.getVoxel().sameKind(info.getVoxel()) && adj.getMetaData() == info.getMetaData();
    }
    // Generate some jitter if it is enabled
    float dx = 0f, dy = 0f, dz = 0f;
    if (this.jitterX != 0.0f)
        dx = (float) ((Math.random() * 2.0 - 1.0) * this.jitterX);
    if (this.jitterY != 0.0f)
        dy = (float) ((Math.random() * 2.0 - 1.0) * this.jitterY);
    if (this.jitterZ != 0.0f)
        dz = (float) ((Math.random() * 2.0 - 1.0) * this.jitterZ);
    // int drewVertices = 0;
    drawVertex: for (int i_currentVertex = 0; i_currentVertex < this.vertices.length / 3; i_currentVertex++) {
        if (i_currentVertex >= maxVertexIndexToUseThisTextureFor) {
            modelTextureIndex++;
            // Selects an appropriate texture
            currentVoxelTexture = selectsTextureFromIndex(info, modelTextureIndex);
            maxVertexIndexToUseThisTextureFor = this.texturesOffsets[modelTextureIndex];
            baker.usingTexture(currentVoxelTexture);
        // textureS = currentVoxelTexture.getAtlasS();// +mod(sx,texture.textureScale)*offset;
        // textureT = currentVoxelTexture.getAtlasT();// +mod(sz,texture.textureScale)*offset;
        }
        /*
			 * How culling works :
			 * culling[][] array contains [vertices.len/3][faces (6)] booleans
			 * for each triangle (vert/3) it checks for i 0 -> 6 that either ![v][i] or [v][i] && info.neightbours[i] is solid
			 * if any cull condition fails then it doesn't render this triangle.<
			 */
        int cullIndex = i_currentVertex / 3;
        // boolean drawFace = true;
        for (int j = 0; j < 6; j++) {
            // Is culling enabled on this face, on this vertex ?
            if (this.culling[cullIndex][j]) {
                // TODO possible optimization : skip to the next culling spec label
                if (cullingCache[j]) {
                    i_currentVertex += 2;
                    continue drawVertex;
                }
            }
        }
        float vertX = this.vertices[i_currentVertex * 3 + 0];
        float vertY = this.vertices[i_currentVertex * 3 + 1];
        float vertZ = this.vertices[i_currentVertex * 3 + 2];
        baker.beginVertex(vertX + x + dx, vertY + y + dy, vertZ + z + dz);
        // renderByteBuffer.addVerticeFloat(vertX + x + dx, vertY + y + dy, vertZ + z + dz);
        baker.setTextureCoordinates(this.texCoords[i_currentVertex * 2 + 0], this.texCoords[i_currentVertex * 2 + 1]);
        // renderByteBuffer.addTexCoordInt((int) (textureS + this.texCoords[i_currentVertex*2+0] * currentVoxelTexture.getAtlasOffset()), (int) (textureT + this.texCoords[i_currentVertex*2+1] * currentVoxelTexture.getAtlasOffset()));
        byte sunLight, blockLight, ao;
        sunLight = bakingContext.getCurrentVoxelLighter().getSunlightLevelInterpolated(vertX, vertY, vertZ);
        blockLight = bakingContext.getCurrentVoxelLighter().getBlocklightLevelInterpolated(vertX, vertY, vertZ);
        ao = bakingContext.getCurrentVoxelLighter().getAoLevelInterpolated(vertX, vertY, vertZ);
        baker.setVoxelLight(sunLight, blockLight, ao);
        // renderByteBuffer.addColors(sunLight, blockLight, ao);
        baker.setNormal(normals[i_currentVertex * 3 + 0], normals[i_currentVertex * 3 + 1], normals[i_currentVertex * 3 + 2]);
        baker.setWavyFlag(this.extra[i_currentVertex] != 0);
        // renderByteBuffer.addNormalsInt(intifyNormal(this.normals[i_currentVertex*3+0]), intifyNormal(this.normals[i_currentVertex*3+1]), intifyNormal(this.normals[i_currentVertex*3+2]), this.extra[i_currentVertex]);
        baker.endVertex();
    // drewVertices++;
    }
    return this.vertices.length;
}
Also used : VoxelTexture(io.xol.chunkstories.api.voxel.textures.VoxelTexture) CellData(io.xol.chunkstories.api.world.cell.CellData)

Aggregations

VoxelTexture (io.xol.chunkstories.api.voxel.textures.VoxelTexture)3 VoxelBakerHighPoly (io.xol.chunkstories.api.rendering.voxel.VoxelBakerHighPoly)1 Voxel (io.xol.chunkstories.api.voxel.Voxel)1 World (io.xol.chunkstories.api.world.World)1 CellData (io.xol.chunkstories.api.world.cell.CellData)1 VoxelTextureAtlased (io.xol.chunkstories.voxel.VoxelTextureAtlased)1 ByteBuffer (java.nio.ByteBuffer)1