Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity rendering pipeline #645

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/bluemap.base.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ version = gitVersion()

repositories {
maven ("https://repo.bluecolored.de/releases") {
content { includeGroupByRegex ("de\\.bluecolored\\..*") }
content { includeGroupByRegex ("de\\.bluecolored.*") }
}
maven ("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") {
content { includeGroup ("org.spigotmc") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
import de.bluecolored.bluemap.core.storage.MapStorage;
import de.bluecolored.bluemap.core.storage.Storage;
import de.bluecolored.bluemap.core.util.Grid;
import de.bluecolored.bluemap.core.world.BlockEntity;
import de.bluecolored.bluemap.core.world.Chunk;
import de.bluecolored.bluemap.core.world.ChunkConsumer;
import de.bluecolored.bluemap.core.world.World;
import de.bluecolored.bluemap.core.world.block.Block;
import de.bluecolored.bluemap.core.world.block.entity.BlockEntity;

import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -594,8 +594,8 @@ public int debugBlockCommand(CommandContext<S> context) {
new Thread(() -> {
// collect and output debug info
Vector3i blockPos = position.floor().toInt();
Block<?> block = new Block<>(world, blockPos.getX(), blockPos.getY(), blockPos.getZ());
Block<?> blockBelow = new Block<>(world, blockPos.getX(), blockPos.getY() - 1, blockPos.getZ());
Block block = new Block(world, blockPos.getX(), blockPos.getY(), blockPos.getZ());
Block blockBelow = new Block(world, blockPos.getX(), blockPos.getY() - 1, blockPos.getZ());

source.sendMessages(Arrays.asList(
Text.of(TextColor.GOLD, "Block at you: \n", formatBlock(block)),
Expand All @@ -606,7 +606,7 @@ public int debugBlockCommand(CommandContext<S> context) {
return 1;
}

private Text formatBlock(Block<?> block) {
private Text formatBlock(Block block) {
World world = block.getWorld();
Chunk chunk = block.getChunk();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
*/
package de.bluecolored.bluemap.core.map.hires;

import com.flowpowered.math.vector.Vector3d;
import com.flowpowered.math.vector.Vector3i;
import de.bluecolored.bluemap.core.map.TextureGallery;
import de.bluecolored.bluemap.core.map.TileMetaConsumer;
import de.bluecolored.bluemap.core.map.hires.blockmodel.BlockStateModelRenderer;
import de.bluecolored.bluemap.core.map.hires.block.BlockStateModelRenderer;
import de.bluecolored.bluemap.core.map.hires.entity.EntityModelRenderer;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.ResourcePack;
import de.bluecolored.bluemap.core.util.math.Color;
import de.bluecolored.bluemap.core.world.Chunk;
import de.bluecolored.bluemap.core.world.World;
import de.bluecolored.bluemap.core.world.block.Block;
import de.bluecolored.bluemap.core.world.block.BlockNeighborhood;

public class HiresModelRenderer {
Expand All @@ -40,12 +43,14 @@ public class HiresModelRenderer {
private final RenderSettings renderSettings;

private final ThreadLocal<BlockStateModelRenderer> threadLocalBlockRenderer;
private final ThreadLocal<EntityModelRenderer> threadLocalEntityRenderer;

public HiresModelRenderer(ResourcePack resourcePack, TextureGallery textureGallery, RenderSettings renderSettings) {
this.resourcePack = resourcePack;
this.renderSettings = renderSettings;

this.threadLocalBlockRenderer = ThreadLocal.withInitial(() -> new BlockStateModelRenderer(resourcePack, textureGallery, renderSettings));
this.threadLocalEntityRenderer = ThreadLocal.withInitial(() -> new EntityModelRenderer(resourcePack, textureGallery, renderSettings));
}

public void render(World world, Vector3i modelMin, Vector3i modelMax, TileModel model) {
Expand All @@ -57,13 +62,15 @@ public void render(World world, Vector3i modelMin, Vector3i modelMax, TileModel
Vector3i max = modelMax.min(renderSettings.getMaxPos());
Vector3i modelAnchor = new Vector3i(modelMin.getX(), 0, modelMin.getZ());

// render blocks
BlockStateModelRenderer blockRenderer = threadLocalBlockRenderer.get();
EntityModelRenderer entityRenderer = threadLocalEntityRenderer.get();

int maxHeight, minY, maxY;
double topBlockLight;
Color columnColor = new Color(), blockColor = new Color();
BlockNeighborhood<?> block = new BlockNeighborhood<>(resourcePack, renderSettings, world, 0, 0, 0);
TileModelView blockModel = new TileModelView(tileModel);
BlockNeighborhood block = new BlockNeighborhood(new Block(world, 0, 0, 0), resourcePack, renderSettings, world.getDimensionType());
TileModelView tileModelView = new TileModelView(tileModel);

int x, y, z;
for (x = modelMin.getX(); x <= modelMax.getX(); x++){
Expand All @@ -80,18 +87,21 @@ public void render(World world, Vector3i modelMin, Vector3i modelMax, TileModel
maxY = Math.min(max.getY(), chunk.getMaxY(x, z));

for (y = maxY; y >= minY; y--) {
if (x == -1743 && y == 64 && z == 1393)
System.out.println();

block.set(x, y, z);
if (!block.isInsideRenderBounds()) continue;

blockModel.initialize();
tileModelView.initialize();

blockRenderer.render(block, blockModel, blockColor);
blockRenderer.render(block, tileModelView, blockColor);

//update topBlockLight
topBlockLight = Math.max(topBlockLight, block.getBlockLightLevel() * (1 - columnColor.a));

// move block-model to correct position
blockModel.translate(x - modelAnchor.getX(), y - modelAnchor.getY(), z - modelAnchor.getZ());
tileModelView.translate(x - modelAnchor.getX(), y - modelAnchor.getY(), z - modelAnchor.getZ());

//update color and height (only if not 100% translucent)
if (blockColor.a > 0) {
Expand All @@ -110,5 +120,18 @@ public void render(World world, Vector3i modelMin, Vector3i modelMax, TileModel
tileMetaConsumer.set(x, z, columnColor, maxHeight, (int) topBlockLight);
}
}

// render entities
world.iterateEntities(min.getX(), min.getZ(), max.getX(), max.getZ(), entity -> {
Vector3d pos = entity.getPos();
block.set(pos.getFloorX(), pos.getFloorY(), pos.getFloorZ());
entityRenderer.render(entity, block, tileModelView.initialize());
tileModelView.translate(
(float) pos.getX() - modelAnchor.getX(),
(float) pos.getY() - modelAnchor.getY(),
(float) pos.getZ() - modelAnchor.getZ()
);
});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.map.hires.blockmodel;
package de.bluecolored.bluemap.core.map.hires.block;

import de.bluecolored.bluemap.core.map.hires.TileModelView;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockstate.Variant;
Expand All @@ -43,9 +43,9 @@ public interface BlockRenderer {
* </p>
* @param block The block information that should be rendered.
* @param variant The block-state variant that should be rendered.
* @param blockModel The model(-view) where the block should be rendered to.
* @param tileModel The model(-view) where the block should be rendered to.
* @param blockColor The color that should be set to the color that represents the rendered block.
*/
void render(BlockNeighborhood<?> block, Variant variant, TileModelView blockModel, Color blockColor);
void render(BlockNeighborhood block, Variant variant, TileModelView tileModel, Color blockColor);

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.map.hires.blockmodel;
package de.bluecolored.bluemap.core.map.hires.block;

import de.bluecolored.bluemap.core.map.TextureGallery;
import de.bluecolored.bluemap.core.map.hires.RenderSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.map.hires.blockmodel;
package de.bluecolored.bluemap.core.map.hires.block;

import de.bluecolored.bluemap.core.map.TextureGallery;
import de.bluecolored.bluemap.core.map.hires.RenderSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.map.hires.blockmodel;
package de.bluecolored.bluemap.core.map.hires.block;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import de.bluecolored.bluemap.core.map.TextureGallery;
import de.bluecolored.bluemap.core.map.hires.TileModelView;
import de.bluecolored.bluemap.core.map.hires.RenderSettings;
import de.bluecolored.bluemap.core.map.hires.TileModelView;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.ResourcePack;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockmodel.BlockModel;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockstate.Variant;
import de.bluecolored.bluemap.core.util.math.Color;
import de.bluecolored.bluemap.core.world.block.BlockNeighborhood;
import de.bluecolored.bluemap.core.world.BlockState;
import de.bluecolored.bluemap.core.world.block.BlockNeighborhood;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -52,35 +51,35 @@ public BlockStateModelRenderer(ResourcePack resourcePack, TextureGallery texture
.build(type -> type.create(resourcePack, textureGallery, renderSettings));
}

public void render(BlockNeighborhood<?> block, TileModelView blockModel, Color blockColor) {
public void render(BlockNeighborhood block, TileModelView blockModel, Color blockColor) {
render(block, block.getBlockState(), blockModel, blockColor);
}

private final Color waterloggedColor = new Color();
public void render(BlockNeighborhood<?> block, BlockState blockState, TileModelView blockModel, Color blockColor) {
public void render(BlockNeighborhood block, BlockState blockState, TileModelView tileModel, Color blockColor) {
blockColor.set(0, 0, 0, 0, true);

//shortcut for air
if (blockState.isAir()) return;

int modelStart = blockModel.getStart();
int modelStart = tileModel.getStart();

// render block
renderModel(block, blockState, blockModel.initialize(), blockColor);
renderModel(block, blockState, tileModel.initialize(), blockColor);

// add water if block is waterlogged
if (blockState.isWaterlogged() || block.getProperties().isAlwaysWaterlogged()) {
waterloggedColor.set(0f, 0f, 0f, 0f, true);
renderModel(block, WATERLOGGED_BLOCKSTATE, blockModel.initialize(), waterloggedColor);
renderModel(block, WATERLOGGED_BLOCKSTATE, tileModel.initialize(), waterloggedColor);
blockColor.set(waterloggedColor.overlay(blockColor.premultiplied()));
}

blockModel.initialize(modelStart);
tileModel.initialize(modelStart);
}

private final Color variantColor = new Color();
private void renderModel(BlockNeighborhood<?> block, BlockState blockState, TileModelView blockModel, Color blockColor) {
int modelStart = blockModel.getStart();
private void renderModel(BlockNeighborhood block, BlockState blockState, TileModelView tileModel, Color blockColor) {
int modelStart = tileModel.getStart();

var stateResource = resourcePack.getBlockState(blockState);
if (stateResource == null) return;
Expand All @@ -91,19 +90,14 @@ private void renderModel(BlockNeighborhood<?> block, BlockState blockState, Tile

//noinspection ForLoopReplaceableByForEach
for (int i = 0; i < variants.size(); i++) {
Variant variant = variants.get(i);

BlockModel modelResource = variant.getModel().getResource(resourcePack::getBlockModel);
if (modelResource == null) continue;

variantColor.set(0f, 0f, 0f, 0f, true);

blockRenderers.get(modelResource.getRenderer())
.render(block, variant, blockModel.initialize(), variantColor);
Variant variant = variants.get(i);
blockRenderers.get(variant.getRenderer())
.render(block, variant, tileModel.initialize(), variantColor);

if (variantColor.a > blockColorOpacity)
blockColorOpacity = variantColor.a;

blockColor.add(variantColor.premultiplied());
}

Expand All @@ -112,7 +106,7 @@ private void renderModel(BlockNeighborhood<?> block, BlockState blockState, Tile
blockColor.a = blockColorOpacity;
}

blockModel.initialize(modelStart);
tileModel.initialize(modelStart);
}

private final static BlockState WATERLOGGED_BLOCKSTATE = new BlockState("minecraft:water");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.map.hires.blockmodel;
package de.bluecolored.bluemap.core.map.hires.block;

import com.flowpowered.math.TrigMath;
import com.flowpowered.math.vector.Vector3i;
import de.bluecolored.bluemap.core.map.TextureGallery;
import de.bluecolored.bluemap.core.map.hires.TileModelView;
import de.bluecolored.bluemap.core.map.hires.TileModel;
import de.bluecolored.bluemap.core.map.hires.RenderSettings;
import de.bluecolored.bluemap.core.map.hires.TileModel;
import de.bluecolored.bluemap.core.map.hires.TileModelView;
import de.bluecolored.bluemap.core.resources.BlockColorCalculatorFactory;
import de.bluecolored.bluemap.core.resources.ResourcePath;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.ResourcePack;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockmodel.BlockModel;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockmodel.TextureVariable;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.model.Model;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.model.TextureVariable;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockstate.Variant;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.texture.Texture;
import de.bluecolored.bluemap.core.util.Direction;
import de.bluecolored.bluemap.core.util.math.Color;
import de.bluecolored.bluemap.core.util.math.MatrixM3f;
import de.bluecolored.bluemap.core.util.math.VectorM2f;
import de.bluecolored.bluemap.core.util.math.VectorM3f;
import de.bluecolored.bluemap.core.world.block.BlockNeighborhood;
import de.bluecolored.bluemap.core.world.BlockState;
import de.bluecolored.bluemap.core.world.block.BlockNeighborhood;
import de.bluecolored.bluemap.core.world.block.ExtendedBlock;

/**
Expand All @@ -66,10 +66,10 @@ public class LiquidModelRenderer implements BlockRenderer {
private final VectorM3f[] corners;
private final VectorM2f[] uvs = new VectorM2f[4];

private BlockNeighborhood<?> block;
private BlockNeighborhood block;
private BlockState blockState;
private boolean isWaterlogged, isWaterLike;
private BlockModel modelResource;
private Model modelResource;
private TileModelView blockModel;
private Color blockColor;

Expand All @@ -93,15 +93,17 @@ public LiquidModelRenderer(ResourcePack resourcePack, TextureGallery textureGall
for (int i = 0; i < uvs.length; i++) uvs[i] = new VectorM2f(0, 0);
}

public void render(BlockNeighborhood<?> block, Variant variant, TileModelView blockModel, Color color) {
public void render(BlockNeighborhood block, Variant variant, TileModelView blockModel, Color color) {
this.block = block;
this.blockState = block.getBlockState();
this.isWaterlogged = blockState.isWaterlogged() || block.getProperties().isAlwaysWaterlogged();
this.isWaterLike = blockState.isWater() || isWaterlogged;
this.modelResource = variant.getModel().getResource();
this.modelResource = variant.getModel().getResource(resourcePack::getModel);
this.blockModel = blockModel;
this.blockColor = color;

if (this.modelResource == null) return;

build();
}

Expand Down Expand Up @@ -191,7 +193,7 @@ private float getLiquidCornerHeight(int x, int z){

float sumHeight = 0f;
int count = 0;
ExtendedBlock<?> neighbor;
ExtendedBlock neighbor;
BlockState neighborBlockState;

for (ix = x; ix <= x+1; ix++){
Expand Down Expand Up @@ -223,7 +225,7 @@ private boolean isLiquidBlockingBlock(BlockState blockState){
}

@SuppressWarnings("StringEquality")
private boolean isSameLiquid(ExtendedBlock<?> block){
private boolean isSameLiquid(ExtendedBlock block){
BlockState blockState = block.getBlockState();

if (this.isWaterlogged)
Expand All @@ -245,7 +247,7 @@ private boolean createElementFace(Direction faceDir, VectorM3f c0, VectorM3f c1,
Vector3i faceDirVector = faceDir.toVector();

//face culling
ExtendedBlock<?> bl = block.getNeighborBlock(
ExtendedBlock bl = block.getNeighborBlock(
faceDirVector.getX(),
faceDirVector.getY(),
faceDirVector.getZ()
Expand Down Expand Up @@ -369,7 +371,7 @@ private int getFlowingAngle() {
}

private float compareLiquidHeights(float ownHeight, int dx, int dz) {
ExtendedBlock<?> neighbor = block.getNeighborBlock(dx, 0, dz);
ExtendedBlock neighbor = block.getNeighborBlock(dx, 0, dz);
if (neighbor.getBlockState().isAir()) return 0;
if (!isSameLiquid(neighbor)) return 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.map.hires.blockmodel;
package de.bluecolored.bluemap.core.map.hires.block;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
Expand Down Expand Up @@ -56,7 +56,7 @@ public MissingModelRenderer(ResourcePack resourcePack, TextureGallery textureGal
}

@Override
public void render(BlockNeighborhood<?> block, Variant variant, TileModelView blockModel, Color blockColor) {
public void render(BlockNeighborhood block, Variant variant, TileModelView blockModel, Color blockColor) {
blockRenderers.get(BLOCK_RENDERER_TYPES.get(block.getBlockState()))
.render(block, variant, blockModel, blockColor);
}
Expand Down
Loading
Loading