Skip to content

Commit ef4d8e7

Browse files
committed
Rename BlockModel to Model, move renderer property from models into blockstates and add entitystates and entity-renderers
1 parent 180c9fc commit ef4d8e7

File tree

30 files changed

+823
-283
lines changed

30 files changed

+823
-283
lines changed

core/src/main/java/de/bluecolored/bluemap/core/map/hires/HiresModelRenderer.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
*/
2525
package de.bluecolored.bluemap.core.map.hires;
2626

27+
import com.flowpowered.math.vector.Vector3d;
2728
import com.flowpowered.math.vector.Vector3i;
2829
import de.bluecolored.bluemap.core.map.TextureGallery;
2930
import de.bluecolored.bluemap.core.map.TileMetaConsumer;
3031
import de.bluecolored.bluemap.core.map.hires.block.BlockStateModelRenderer;
32+
import de.bluecolored.bluemap.core.map.hires.entity.EntityModelRenderer;
3133
import de.bluecolored.bluemap.core.resources.pack.resourcepack.ResourcePack;
3234
import de.bluecolored.bluemap.core.util.math.Color;
3335
import de.bluecolored.bluemap.core.world.Chunk;
@@ -41,12 +43,14 @@ public class HiresModelRenderer {
4143
private final RenderSettings renderSettings;
4244

4345
private final ThreadLocal<BlockStateModelRenderer> threadLocalBlockRenderer;
46+
private final ThreadLocal<EntityModelRenderer> threadLocalEntityRenderer;
4447

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

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

5256
public void render(World world, Vector3i modelMin, Vector3i modelMax, TileModel model) {
@@ -60,12 +64,13 @@ public void render(World world, Vector3i modelMin, Vector3i modelMax, TileModel
6064

6165
// render blocks
6266
BlockStateModelRenderer blockRenderer = threadLocalBlockRenderer.get();
67+
EntityModelRenderer entityRenderer = threadLocalEntityRenderer.get();
6368

6469
int maxHeight, minY, maxY;
6570
double topBlockLight;
6671
Color columnColor = new Color(), blockColor = new Color();
6772
BlockNeighborhood block = new BlockNeighborhood(new Block(world, 0, 0, 0), resourcePack, renderSettings, world.getDimensionType());
68-
TileModelView blockModel = new TileModelView(tileModel);
73+
TileModelView tileModelView = new TileModelView(tileModel);
6974

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

8489
for (y = maxY; y >= minY; y--) {
90+
if (x == -1743 && y == 64 && z == 1393)
91+
System.out.println();
92+
8593
block.set(x, y, z);
8694
if (!block.isInsideRenderBounds()) continue;
8795

88-
blockModel.initialize();
96+
tileModelView.initialize();
8997

90-
blockRenderer.render(block, blockModel, blockColor);
98+
blockRenderer.render(block, tileModelView, blockColor);
9199

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

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

98106
//update color and height (only if not 100% translucent)
99107
if (blockColor.a > 0) {
@@ -114,7 +122,11 @@ public void render(World world, Vector3i modelMin, Vector3i modelMax, TileModel
114122
}
115123

116124
// render entities
117-
world.iterateEntities(min.getX(), min.getZ(), max.getX(), max.getZ(), entity -> {});
125+
world.iterateEntities(min.getX(), min.getZ(), max.getX(), max.getZ(), entity -> {
126+
Vector3d pos = entity.getPos();
127+
block.set(pos.getFloorX(), pos.getFloorY(), pos.getFloorZ());
128+
entityRenderer.render(entity, block, tileModelView);
129+
});
118130

119131
}
120132
}

core/src/main/java/de/bluecolored/bluemap/core/map/hires/block/BlockRenderer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public interface BlockRenderer {
4343
* </p>
4444
* @param block The block information that should be rendered.
4545
* @param variant The block-state variant that should be rendered.
46-
* @param blockModel The model(-view) where the block should be rendered to.
46+
* @param tileModel The model(-view) where the block should be rendered to.
4747
* @param blockColor The color that should be set to the color that represents the rendered block.
4848
*/
49-
void render(BlockNeighborhood block, Variant variant, TileModelView blockModel, Color blockColor);
49+
void render(BlockNeighborhood block, Variant variant, TileModelView tileModel, Color blockColor);
5050

5151
}

core/src/main/java/de/bluecolored/bluemap/core/map/hires/block/BlockStateModelRenderer.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import de.bluecolored.bluemap.core.map.hires.RenderSettings;
3131
import de.bluecolored.bluemap.core.map.hires.TileModelView;
3232
import de.bluecolored.bluemap.core.resources.pack.resourcepack.ResourcePack;
33-
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockmodel.BlockModel;
3433
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockstate.Variant;
3534
import de.bluecolored.bluemap.core.util.math.Color;
3635
import de.bluecolored.bluemap.core.world.BlockState;
@@ -57,30 +56,30 @@ public void render(BlockNeighborhood block, TileModelView blockModel, Color bloc
5756
}
5857

5958
private final Color waterloggedColor = new Color();
60-
public void render(BlockNeighborhood block, BlockState blockState, TileModelView blockModel, Color blockColor) {
59+
public void render(BlockNeighborhood block, BlockState blockState, TileModelView tileModel, Color blockColor) {
6160
blockColor.set(0, 0, 0, 0, true);
6261

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

66-
int modelStart = blockModel.getStart();
65+
int modelStart = tileModel.getStart();
6766

6867
// render block
69-
renderModel(block, blockState, blockModel.initialize(), blockColor);
68+
renderModel(block, blockState, tileModel.initialize(), blockColor);
7069

7170
// add water if block is waterlogged
7271
if (blockState.isWaterlogged() || block.getProperties().isAlwaysWaterlogged()) {
7372
waterloggedColor.set(0f, 0f, 0f, 0f, true);
74-
renderModel(block, WATERLOGGED_BLOCKSTATE, blockModel.initialize(), waterloggedColor);
73+
renderModel(block, WATERLOGGED_BLOCKSTATE, tileModel.initialize(), waterloggedColor);
7574
blockColor.set(waterloggedColor.overlay(blockColor.premultiplied()));
7675
}
7776

78-
blockModel.initialize(modelStart);
77+
tileModel.initialize(modelStart);
7978
}
8079

8180
private final Color variantColor = new Color();
82-
private void renderModel(BlockNeighborhood block, BlockState blockState, TileModelView blockModel, Color blockColor) {
83-
int modelStart = blockModel.getStart();
81+
private void renderModel(BlockNeighborhood block, BlockState blockState, TileModelView tileModel, Color blockColor) {
82+
int modelStart = tileModel.getStart();
8483

8584
var stateResource = resourcePack.getBlockState(blockState);
8685
if (stateResource == null) return;
@@ -91,19 +90,14 @@ private void renderModel(BlockNeighborhood block, BlockState blockState, TileMod
9190

9291
//noinspection ForLoopReplaceableByForEach
9392
for (int i = 0; i < variants.size(); i++) {
94-
Variant variant = variants.get(i);
95-
96-
BlockModel modelResource = variant.getModel().getResource(resourcePack::getBlockModel);
97-
if (modelResource == null) continue;
98-
9993
variantColor.set(0f, 0f, 0f, 0f, true);
10094

101-
blockRenderers.get(modelResource.getRenderer())
102-
.render(block, variant, blockModel.initialize(), variantColor);
95+
Variant variant = variants.get(i);
96+
blockRenderers.get(variant.getRenderer())
97+
.render(block, variant, tileModel.initialize(), variantColor);
10398

10499
if (variantColor.a > blockColorOpacity)
105100
blockColorOpacity = variantColor.a;
106-
107101
blockColor.add(variantColor.premultiplied());
108102
}
109103

@@ -112,7 +106,7 @@ private void renderModel(BlockNeighborhood block, BlockState blockState, TileMod
112106
blockColor.a = blockColorOpacity;
113107
}
114108

115-
blockModel.initialize(modelStart);
109+
tileModel.initialize(modelStart);
116110
}
117111

118112
private final static BlockState WATERLOGGED_BLOCKSTATE = new BlockState("minecraft:water");

core/src/main/java/de/bluecolored/bluemap/core/map/hires/block/LiquidModelRenderer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
import de.bluecolored.bluemap.core.resources.BlockColorCalculatorFactory;
3434
import de.bluecolored.bluemap.core.resources.ResourcePath;
3535
import de.bluecolored.bluemap.core.resources.pack.resourcepack.ResourcePack;
36-
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockmodel.BlockModel;
37-
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockmodel.TextureVariable;
36+
import de.bluecolored.bluemap.core.resources.pack.resourcepack.model.Model;
37+
import de.bluecolored.bluemap.core.resources.pack.resourcepack.model.TextureVariable;
3838
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockstate.Variant;
3939
import de.bluecolored.bluemap.core.resources.pack.resourcepack.texture.Texture;
4040
import de.bluecolored.bluemap.core.util.Direction;
@@ -69,7 +69,7 @@ public class LiquidModelRenderer implements BlockRenderer {
6969
private BlockNeighborhood block;
7070
private BlockState blockState;
7171
private boolean isWaterlogged, isWaterLike;
72-
private BlockModel modelResource;
72+
private Model modelResource;
7373
private TileModelView blockModel;
7474
private Color blockColor;
7575

@@ -98,10 +98,12 @@ public void render(BlockNeighborhood block, Variant variant, TileModelView block
9898
this.blockState = block.getBlockState();
9999
this.isWaterlogged = blockState.isWaterlogged() || block.getProperties().isAlwaysWaterlogged();
100100
this.isWaterLike = blockState.isWater() || isWaterlogged;
101-
this.modelResource = variant.getModel().getResource();
101+
this.modelResource = variant.getModel().getResource(resourcePack::getModel);
102102
this.blockModel = blockModel;
103103
this.blockColor = color;
104104

105+
if (this.modelResource == null) return;
106+
105107
build();
106108
}
107109

core/src/main/java/de/bluecolored/bluemap/core/map/hires/block/ResourceModelRenderer.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
import de.bluecolored.bluemap.core.resources.BlockColorCalculatorFactory;
3636
import de.bluecolored.bluemap.core.resources.ResourcePath;
3737
import de.bluecolored.bluemap.core.resources.pack.resourcepack.ResourcePack;
38-
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockmodel.BlockModel;
39-
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockmodel.Element;
40-
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockmodel.Face;
4138
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockstate.Variant;
39+
import de.bluecolored.bluemap.core.resources.pack.resourcepack.model.Element;
40+
import de.bluecolored.bluemap.core.resources.pack.resourcepack.model.Face;
41+
import de.bluecolored.bluemap.core.resources.pack.resourcepack.model.Model;
4242
import de.bluecolored.bluemap.core.resources.pack.resourcepack.texture.Texture;
4343
import de.bluecolored.bluemap.core.util.Direction;
4444
import de.bluecolored.bluemap.core.util.math.Color;
@@ -70,7 +70,7 @@ public class ResourceModelRenderer implements BlockRenderer {
7070

7171
private BlockNeighborhood block;
7272
private Variant variant;
73-
private BlockModel modelResource;
73+
private Model modelResource;
7474
private TileModelView blockModel;
7575
private Color blockColor;
7676
private float blockColorOpacity;
@@ -85,14 +85,15 @@ public ResourceModelRenderer(ResourcePack resourcePack, TextureGallery textureGa
8585
for (int i = 0; i < rawUvs.length; i++) rawUvs[i] = new VectorM2f(0, 0);
8686
}
8787

88-
private final MatrixM4f modelTransform = new MatrixM4f();
8988
public void render(BlockNeighborhood block, Variant variant, TileModelView blockModel, Color color) {
9089
this.block = block;
9190
this.blockModel = blockModel;
9291
this.blockColor = color;
9392
this.blockColorOpacity = 0f;
9493
this.variant = variant;
95-
this.modelResource = variant.getModel().getResource();
94+
this.modelResource = variant.getModel().getResource(resourcePack::getModel);
95+
96+
if (this.modelResource == null) return;
9697

9798
this.tintColor.set(0, 0, 0, -1, true);
9899

@@ -113,14 +114,9 @@ public void render(BlockNeighborhood block, Variant variant, TileModelView block
113114

114115
blockModel.initialize(modelStart);
115116

116-
// apply model-rotation
117-
if (variant.isRotated()) {
118-
blockModel.transform(modelTransform.identity()
119-
.translate(-0.5f, -0.5f, -0.5f)
120-
.multiplyTo(variant.getRotationMatrix())
121-
.translate(0.5f, 0.5f, 0.5f)
122-
);
123-
}
117+
// apply model-transform
118+
if (variant.isTransformed())
119+
blockModel.transform(variant.getTransformMatrix());
124120

125121
//random offset
126122
if (block.getProperties().isRandomOffset()){
@@ -258,7 +254,7 @@ private void createElementFace(Element element, Direction faceDir, VectorM3f c0,
258254

259255
// UV-Lock counter-rotation
260256
float uvRotation = 0f;
261-
if (variant.isUvlock() && variant.isRotated()) {
257+
if (variant.isUvlock() && variant.isTransformed()) {
262258
float xRotSin = TrigMath.sin(variant.getX() * TrigMath.DEG_TO_RAD);
263259
float xRotCos = TrigMath.cos(variant.getX() * TrigMath.DEG_TO_RAD);
264260

@@ -300,8 +296,8 @@ private void createElementFace(Element element, Direction faceDir, VectorM3f c0,
300296
tileModel.setColor(face1, tintColor.r, tintColor.g, tintColor.b);
301297
tileModel.setColor(face2, tintColor.r, tintColor.g, tintColor.b);
302298
} else {
303-
tileModel.setColor(face1, 1, 1, 1);
304-
tileModel.setColor(face2, 1, 1, 1);
299+
tileModel.setColor(face1, 1f, 1f, 1f);
300+
tileModel.setColor(face2, 1f, 1f, 1f);
305301
}
306302

307303
// ####### blocklight
@@ -375,8 +371,8 @@ private ExtendedBlock getRotationRelativeBlock(int dx, int dy, int dz){
375371
}
376372

377373
private void makeRotationRelative(VectorM3f direction){
378-
if (variant.isRotated())
379-
direction.transform(variant.getRotationMatrix());
374+
if (variant.isTransformed())
375+
direction.rotateAndScale(variant.getTransformMatrix());
380376
}
381377

382378
private float testAo(VectorM3f vertex, Direction dir){
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package de.bluecolored.bluemap.core.map.hires.entity;
2+
3+
import com.github.benmanes.caffeine.cache.Caffeine;
4+
import com.github.benmanes.caffeine.cache.LoadingCache;
5+
import de.bluecolored.bluemap.core.map.TextureGallery;
6+
import de.bluecolored.bluemap.core.map.hires.RenderSettings;
7+
import de.bluecolored.bluemap.core.map.hires.TileModelView;
8+
import de.bluecolored.bluemap.core.resources.pack.resourcepack.ResourcePack;
9+
import de.bluecolored.bluemap.core.resources.pack.resourcepack.entitystate.EntityState;
10+
import de.bluecolored.bluemap.core.resources.pack.resourcepack.entitystate.Part;
11+
import de.bluecolored.bluemap.core.world.Entity;
12+
import de.bluecolored.bluemap.core.world.block.BlockNeighborhood;
13+
14+
public class EntityModelRenderer {
15+
16+
private final ResourcePack resourcePack;
17+
private final LoadingCache<EntityRendererType, EntityRenderer> entityRenderers;
18+
19+
public EntityModelRenderer(ResourcePack resourcePack, TextureGallery textureGallery, RenderSettings renderSettings) {
20+
this.resourcePack = resourcePack;
21+
this.entityRenderers = Caffeine.newBuilder()
22+
.build(type -> type.create(resourcePack, textureGallery, renderSettings));
23+
}
24+
25+
public void render(Entity entity, BlockNeighborhood block, TileModelView tileModel) {
26+
int modelStart = tileModel.getStart();
27+
28+
EntityState stateResource = resourcePack.getEntityState(entity.getId());
29+
if (stateResource == null) return;
30+
31+
Part[] parts = stateResource.getParts();
32+
33+
//noinspection ForLoopReplaceableByForEach
34+
for (int i = 0; i < parts.length; i++) {
35+
Part part = parts[i];
36+
entityRenderers.get(part.getRenderer())
37+
.render(entity, block, part, tileModel.initialize());
38+
}
39+
40+
tileModel.initialize(modelStart);
41+
}
42+
43+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of BlueMap, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package de.bluecolored.bluemap.core.map.hires.entity;
26+
27+
import de.bluecolored.bluemap.core.map.hires.TileModelView;
28+
import de.bluecolored.bluemap.core.resources.pack.resourcepack.blockstate.Variant;
29+
import de.bluecolored.bluemap.core.resources.pack.resourcepack.entitystate.Part;
30+
import de.bluecolored.bluemap.core.util.math.Color;
31+
import de.bluecolored.bluemap.core.world.Entity;
32+
import de.bluecolored.bluemap.core.world.block.BlockAccess;
33+
import de.bluecolored.bluemap.core.world.block.BlockNeighborhood;
34+
35+
public interface EntityRenderer {
36+
37+
/**
38+
* Renders the given entities part into the given model, and sets the given blockColor to the
39+
* color that represents the rendered block.
40+
* <p>
41+
* <b>Implementation Note:</b><br>
42+
* This method is guaranteed to be called only on <b>one thread per BlockRenderer instance</b>, so you can use this
43+
* for optimizations.<br>
44+
* Keep in mind this method will be called once for every block that is being rendered, so be very careful
45+
* about performance and instance-creations.
46+
* </p>
47+
* @param entity The entity information that should be rendered.
48+
* @param block the block-position the entity lives at.
49+
* @param part The entity part that should be rendered.
50+
* @param tileModel The model(-view) where the block should be rendered to.
51+
*/
52+
void render(Entity entity, BlockNeighborhood block, Part part, TileModelView tileModel);
53+
54+
}

0 commit comments

Comments
 (0)