Fix cell rendering (blessed Grondag for helping immensely)

This commit is contained in:
Prospector 2019-07-30 23:34:51 -07:00
parent 07db922127
commit 78b72795c6
22 changed files with 116 additions and 20 deletions

View file

@ -33,6 +33,7 @@ import net.minecraft.client.render.model.ModelLoader;
import net.minecraft.client.render.model.UnbakedModel;
import net.minecraft.client.texture.Sprite;
import net.minecraft.client.texture.SpriteAtlasTexture;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.util.Identifier;
import techreborn.client.render.DynamicCellBakedModel;
@ -47,13 +48,20 @@ public class TechRebornClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
ClientSpriteRegistryCallback.event(SpriteAtlasTexture.BLOCK_ATLAS_TEX)
.register((spriteAtlasTexture, registry) -> registry.register(new Identifier("techreborn:item/cell_base")));
.register((spriteAtlasTexture, registry) -> registry.register(new Identifier("techreborn:item/cell_base")));
ModelLoadingRegistry.INSTANCE.registerAppender((manager, out) -> {
out.accept(new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "cell_base"), "inventory"));
out.accept(new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "cell_fluid"), "inventory"));
out.accept(new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "cell_background"), "inventory"));
out.accept(new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "cell_glass"), "inventory"));
});
ModelLoadingRegistry.INSTANCE.registerVariantProvider(resourceManager -> (modelIdentifier, modelProviderContext) -> {
if(!modelIdentifier.getNamespace().equals("techreborn")){
if (!modelIdentifier.getNamespace().equals("techreborn")) {
return null;
}
if(!modelIdentifier.getPath().equals("cell")){
if (!modelIdentifier.getPath().equals("cell")) {
return null;
}
return new UnbakedModel() {
@ -77,5 +85,4 @@ public class TechRebornClient implements ClientModInitializer {
}
}

View file

@ -35,13 +35,16 @@ import net.fabricmc.fabric.api.renderer.v1.mesh.MutableQuadView;
import net.fabricmc.fabric.api.renderer.v1.mesh.QuadEmitter;
import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel;
import net.fabricmc.fabric.api.renderer.v1.render.RenderContext;
import net.fabricmc.indigo.renderer.helper.GeometryHelper;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.BakedModelManager;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.render.model.json.ModelItemPropertyOverrideList;
import net.minecraft.client.render.model.json.ModelTransformation;
import net.minecraft.client.texture.Sprite;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.entity.LivingEntity;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
@ -52,6 +55,7 @@ import net.minecraft.util.math.Direction;
import net.minecraft.world.ExtendedBlockView;
import net.minecraft.world.World;
import reborncore.common.fluid.container.ItemFluidInfo;
import techreborn.TechReborn;
import javax.annotation.Nullable;
import java.awt.*;
@ -62,7 +66,12 @@ import java.util.function.Supplier;
public class DynamicCellBakedModel implements BakedModel, FabricBakedModel {
Sprite base;
private final Sprite base;
private static final ModelIdentifier CELL_BASE = new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "cell_base"), "inventory");
private static final ModelIdentifier CELL_BACKGROUND = new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "cell_background"), "inventory");
private static final ModelIdentifier CELL_FLUID = new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "cell_fluid"), "inventory");
private static final ModelIdentifier CELL_GLASS = new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "cell_glass"), "inventory");
public DynamicCellBakedModel() {
base = MinecraftClient.getInstance().getSpriteAtlas().getSprite(new Identifier("techreborn:item/cell_base"));
@ -76,12 +85,36 @@ public class DynamicCellBakedModel implements BakedModel, FabricBakedModel {
@Override
public void emitItemQuads(ItemStack stack, Supplier<Random> randomSupplier, RenderContext context) {
Fluid fluid = Fluids.EMPTY;
if(stack.getItem() instanceof ItemFluidInfo){
if (stack.getItem() instanceof ItemFluidInfo) {
ItemFluidInfo fluidInfo = (ItemFluidInfo) stack.getItem();
fluid = fluidInfo.getFluid(stack);
}
context.meshConsumer().accept(getMesh(fluid));
BakedModelManager bakedModelManager = MinecraftClient.getInstance().getBakedModelManager();
context.fallbackConsumer().accept(bakedModelManager.getModel(CELL_BASE));
context.fallbackConsumer().accept(bakedModelManager.getModel(CELL_BACKGROUND));
if (fluid != Fluids.EMPTY) {
FluidRenderHandler fluidRenderHandler = FluidRenderHandlerRegistry.INSTANCE.get(fluid);
BakedModel fluidModel = bakedModelManager.getModel(CELL_FLUID);
int fluidColor = fluidRenderHandler.getFluidColor(MinecraftClient.getInstance().world, MinecraftClient.getInstance().player.getBlockPos(), fluid.getDefaultState());
Sprite fluidSprite = fluidRenderHandler.getFluidSprites(MinecraftClient.getInstance().world, BlockPos.ORIGIN, fluid.getDefaultState())[0];
int color = new Color((float) (fluidColor >> 16 & 255) / 255.0F, (float) (fluidColor >> 8 & 255) / 255.0F, (float) (fluidColor & 255) / 255.0F).getRGB();
context.pushTransform(quad -> {
quad.nominalFace(GeometryHelper.lightFace(quad));
quad.spriteColor(0, color, color, color, color);
quad.spriteBake(0, fluidSprite, MutableQuadView.BAKE_LOCK_UV);
return true;
});
final QuadEmitter emitter = context.getEmitter();
fluidModel.getQuads(null, null, randomSupplier.get()).forEach(q -> {
emitter.fromVanilla(q.getVertexData(), 0, false);
emitter.emit();
});
context.popTransform();
}
context.fallbackConsumer().accept(bakedModelManager.getModel(CELL_GLASS));
}
@Override
@ -90,7 +123,7 @@ public class DynamicCellBakedModel implements BakedModel, FabricBakedModel {
}
//I have no idea what im doing, this works good enough for me, if you want to make it nicer a PR or tips would be appreciated, thanks.
private Mesh getMesh(Fluid fluid){
private Mesh getMesh(Fluid fluid) {
Renderer renderer = RendererAccess.INSTANCE.getRenderer();
MeshBuilder builder = renderer.meshBuilder();
QuadEmitter emitter = builder.getEmitter();
@ -99,22 +132,22 @@ public class DynamicCellBakedModel implements BakedModel, FabricBakedModel {
//TODO make the base texture 3d somehow
emitter.square(Direction.SOUTH, 0, 0, 1, 1, 0)
.material(mat)
.spriteColor(0, -1, -1, -1, -1)
.spriteBake(0, base, MutableQuadView.BAKE_LOCK_UV).emit();
.material(mat)
.spriteColor(0, -1, -1, -1, -1)
.spriteBake(0, base, MutableQuadView.BAKE_LOCK_UV).emit();
if(fluid != Fluids.EMPTY){
if (fluid != Fluids.EMPTY) {
FluidRenderHandler fluidRenderHandler = FluidRenderHandlerRegistry.INSTANCE.get(fluid);
if(fluidRenderHandler != null){
if (fluidRenderHandler != null) {
int color = fluidRenderHandler.getFluidColor(MinecraftClient.getInstance().world, MinecraftClient.getInstance().player.getBlockPos(), fluid.getDefaultState());
//Does maths that works
color = new Color((float)(color >> 16 & 255) / 255.0F, (float)(color >> 8 & 255) / 255.0F,(float)(color & 255) / 255.0F).getRGB();
color = new Color((float) (color >> 16 & 255) / 255.0F, (float) (color >> 8 & 255) / 255.0F, (float) (color & 255) / 255.0F).getRGB();
Sprite fluidSprite = fluidRenderHandler.getFluidSprites(MinecraftClient.getInstance().world, BlockPos.ORIGIN, fluid.getDefaultState())[0];
emitter.square(Direction.SOUTH, 0.4F, 0.25F, 0.6F, 0.75F, -0.0001F)
.material(mat)
.spriteColor(0, color, color, color, color)
.spriteBake(0, fluidSprite, MutableQuadView.BAKE_LOCK_UV).emit();
.material(mat)
.spriteColor(0, color, color, color, color)
.spriteBake(0, fluidSprite, MutableQuadView.BAKE_LOCK_UV).emit();
}
}
@ -134,7 +167,7 @@ public class DynamicCellBakedModel implements BakedModel, FabricBakedModel {
@Override
public boolean hasDepthInGui() {
return true;
return false;
}
@Override
@ -149,7 +182,7 @@ public class DynamicCellBakedModel implements BakedModel, FabricBakedModel {
@Override
public ModelTransformation getTransformation() {
return ModelHelper.HANDHELD_ITEM_TRANSFORMS;
return ModelHelper.DEFAULT_ITEM_TRANSFORMS;
}
protected class ItemProxy extends ModelItemPropertyOverrideList {

View file

@ -0,0 +1,6 @@
{
"parent": "techreborn:item/cell_fluid_template",
"textures": {
"texture": "techreborn:item/cell_background"
}
}

View file

@ -1,6 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "techreborn:item/cell_empty"
"layer0": "techreborn:item/cell_base"
}
}

View file

@ -0,0 +1,6 @@
{
"parent": "techreborn:item/cell_fluid_template",
"textures": {
"texture": "techreborn:item/cell_background"
}
}

View file

@ -0,0 +1,38 @@
{
"elements": [
{
"from": [7, 4, 7.5],
"to": [10, 12, 8.5],
"faces": {
"north": {"uv": [7, 4, 10, 12], "texture": "#texture"},
"south": {"uv": [7, 4, 10, 12], "texture": "#texture"}
}
}
],
"display": {
"ground": {
"rotation": [ 0, 0, 0 ],
"translation": [ 0, 2, 0],
"scale":[ 0.5, 0.5, 0.5 ]
},
"head": {
"rotation": [ 0, 180, 0 ],
"translation": [ 0, 13, 7],
"scale":[ 1, 1, 1]
},
"thirdperson_righthand": {
"rotation": [ 0, 0, 0 ],
"translation": [ 0, 3, 1 ],
"scale": [ 0.55, 0.55, 0.55 ]
},
"firstperson_righthand": {
"rotation": [ 0, -90, 25 ],
"translation": [ 1.13, 3.2, 1.13],
"scale": [ 0.68, 0.68, 0.68 ]
},
"fixed": {
"rotation": [ 0, 180, 0 ],
"scale": [ 1, 1, 1 ]
}
}
}

View file

@ -0,0 +1,6 @@
{
"parent": "techreborn:item/cell_fluid_template",
"textures": {
"texture": "techreborn:item/cell_glass"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 505 B

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 446 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 506 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 321 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 643 B

After

Width:  |  Height:  |  Size: 2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 832 B

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 664 B

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 634 B

After

Width:  |  Height:  |  Size: 2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 826 B

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 664 B

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 659 B

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 627 B

After

Width:  |  Height:  |  Size: 2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 823 B

After

Width:  |  Height:  |  Size: 1.8 KiB