Dynamic bucket textures

This commit is contained in:
Prospector 2019-07-31 16:13:03 -07:00
parent 779dea4bf8
commit 78cbf9a2ac
10 changed files with 295 additions and 73 deletions

View file

@ -32,10 +32,14 @@ import net.minecraft.client.render.model.ModelLoader;
import net.minecraft.client.render.model.UnbakedModel;
import net.minecraft.client.texture.Sprite;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import reborncore.client.gui.builder.GuiBase;
import reborncore.client.hud.StackInfoHUD;
import techreborn.client.render.DynamicBucketBakedModel;
import techreborn.client.render.DynamicCellBakedModel;
import techreborn.events.StackToolTipHandler;
import techreborn.init.TRContent;
@ -43,7 +47,6 @@ import techreborn.items.ItemDynamicCell;
import techreborn.items.ItemFrequencyTransmitter;
import techreborn.utils.StackWIPHandler;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
@ -58,32 +61,53 @@ public class TechRebornClient implements ClientModInitializer {
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"));
out.accept(new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "bucket_base"), "inventory"));
out.accept(new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "bucket_fluid"), "inventory"));
out.accept(new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "bucket_background"), "inventory"));
});
ModelLoadingRegistry.INSTANCE.registerVariantProvider(resourceManager -> (modelIdentifier, modelProviderContext) -> {
if (!modelIdentifier.getNamespace().equals("techreborn")) {
return null;
}
if (!modelIdentifier.getPath().equals("cell")) {
return null;
}
return new UnbakedModel() {
@Override
public Collection<Identifier> getModelDependencies() {
return Collections.emptyList();
}
if (modelIdentifier.getNamespace().equals(TechReborn.MOD_ID)) {
if (modelIdentifier.getPath().equals("cell")) {
return new UnbakedModel() {
@Override
public Collection<Identifier> getModelDependencies() {
return Collections.emptyList();
}
@Override
public Collection<Identifier> getTextureDependencies(Function<Identifier, UnbakedModel> function, Set<String> set) {
return Collections.emptyList();
}
@Override
public Collection<Identifier> getTextureDependencies(Function<Identifier, UnbakedModel> function, Set<String> set) {
return Collections.emptyList();
}
@Nullable
@Override
public BakedModel bake(ModelLoader modelLoader, Function<Identifier, Sprite> function, ModelBakeSettings modelBakeSettings) {
return new DynamicCellBakedModel();
@Override
public BakedModel bake(ModelLoader modelLoader, Function<Identifier, Sprite> function, ModelBakeSettings modelBakeSettings) {
return new DynamicCellBakedModel();
}
};
}
};
Fluid fluid = Registry.FLUID.get(new Identifier(TechReborn.MOD_ID, modelIdentifier.getPath().split("_bucket")[0]));
if (modelIdentifier.getPath().endsWith("_bucket") && fluid != Fluids.EMPTY) {
return new UnbakedModel() {
@Override
public Collection<Identifier> getModelDependencies() {
return Collections.emptyList();
}
@Override
public Collection<Identifier> getTextureDependencies(Function<Identifier, UnbakedModel> function, Set<String> set) {
return Collections.emptyList();
}
@Override
public BakedModel bake(ModelLoader modelLoader, Function<Identifier, Sprite> function, ModelBakeSettings modelBakeSettings) {
return new DynamicBucketBakedModel(fluid);
}
};
}
}
return null;
});
StackToolTipHandler.setup();

View file

@ -0,0 +1,162 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.client.render;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandler;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandlerRegistry;
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;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
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.*;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.function.Supplier;
public class DynamicBucketBakedModel implements BakedModel, FabricBakedModel {
private final Fluid fluid;
private static final ModelIdentifier BUCKET_BASE = new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "bucket_base"), "inventory");
private static final ModelIdentifier BUCKET_BACKGROUND = new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "bucket_background"), "inventory");
private static final ModelIdentifier BUCKET_FLUID = new ModelIdentifier(new Identifier(TechReborn.MOD_ID, "bucket_fluid"), "inventory");
public DynamicBucketBakedModel(Fluid fluid) {
this.fluid = fluid;
}
@Override
public void emitBlockQuads(ExtendedBlockView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context) {
}
@Override
public void emitItemQuads(ItemStack stack, Supplier<Random> randomSupplier, RenderContext context) {
Fluid fluid = Fluids.EMPTY;
if (stack.getItem() instanceof ItemFluidInfo) {
ItemFluidInfo fluidInfo = (ItemFluidInfo) stack.getItem();
fluid = fluidInfo.getFluid(stack);
}
BakedModelManager bakedModelManager = MinecraftClient.getInstance().getBakedModelManager();
context.fallbackConsumer().accept(bakedModelManager.getModel(BUCKET_BASE));
context.fallbackConsumer().accept(bakedModelManager.getModel(BUCKET_BACKGROUND));
if (fluid != Fluids.EMPTY) {
FluidRenderHandler fluidRenderHandler = FluidRenderHandlerRegistry.INSTANCE.get(fluid);
BakedModel fluidModel = bakedModelManager.getModel(BUCKET_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();
}
}
@Override
public List<BakedQuad> getQuads(@Nullable BlockState blockState, @Nullable Direction direction, Random random) {
return Collections.emptyList();
}
@Override
public boolean isVanillaAdapter() {
return false;
}
@Override
public boolean useAmbientOcclusion() {
return true;
}
@Override
public boolean hasDepthInGui() {
return false;
}
@Override
public boolean isBuiltin() {
return false;
}
@Override
public Sprite getSprite() {
return MinecraftClient.getInstance().getSpriteAtlas().getSprite(new Identifier("minecraft:item/bucket"));
}
@Override
public ModelTransformation getTransformation() {
return ModelHelper.DEFAULT_ITEM_TRANSFORMS;
}
protected class ItemProxy extends ModelItemPropertyOverrideList {
public ItemProxy() {
super(null, null, null, Collections.emptyList());
}
@Override
public BakedModel apply(BakedModel bakedModel, ItemStack itemStack, World world, LivingEntity livingEntity) {
return DynamicBucketBakedModel.this;
}
}
@Override
public ModelItemPropertyOverrideList getItemPropertyOverrides() {
return new ItemProxy();
}
}

View file

@ -26,11 +26,6 @@ package techreborn.client.render;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandler;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandlerRegistry;
import net.fabricmc.fabric.api.renderer.v1.Renderer;
import net.fabricmc.fabric.api.renderer.v1.RendererAccess;
import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;
import net.fabricmc.fabric.api.renderer.v1.mesh.Mesh;
import net.fabricmc.fabric.api.renderer.v1.mesh.MeshBuilder;
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;
@ -66,17 +61,11 @@ import java.util.function.Supplier;
public class DynamicCellBakedModel implements BakedModel, FabricBakedModel {
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"));
}
@Override
public void emitBlockQuads(ExtendedBlockView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context) {
@ -122,39 +111,6 @@ public class DynamicCellBakedModel implements BakedModel, FabricBakedModel {
return Collections.emptyList();
}
//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) {
Renderer renderer = RendererAccess.INSTANCE.getRenderer();
MeshBuilder builder = renderer.meshBuilder();
QuadEmitter emitter = builder.getEmitter();
RenderMaterial mat = renderer.materialFinder().disableDiffuse(0, true).find();
//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();
if (fluid != Fluids.EMPTY) {
FluidRenderHandler fluidRenderHandler = FluidRenderHandlerRegistry.INSTANCE.get(fluid);
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();
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();
}
}
return builder.build();
}
@Override
public boolean isVanillaAdapter() {
return false;
@ -177,7 +133,7 @@ public class DynamicCellBakedModel implements BakedModel, FabricBakedModel {
@Override
public Sprite getSprite() {
return base;
return MinecraftClient.getInstance().getSpriteAtlas().getSprite(new Identifier("techreborn:item/cell_base"));
}
@Override

View file

@ -27,6 +27,7 @@ package techreborn.init;
import net.fabricmc.fabric.api.block.FabricBlockSettings;
import net.minecraft.block.Material;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import reborncore.common.fluid.*;
@ -76,28 +77,30 @@ public enum ModFluids {
private final Identifier identifier;
ModFluids() {
this.identifier = new Identifier("techreborn", this.name().replace("_", "").toLowerCase());
this.identifier = new Identifier(TechReborn.MOD_ID, this.name().replace("_", "").toLowerCase());
FluidSettings fluidSettings = FluidSettings.create();
Identifier texture = new Identifier("techreborn", "block/fluids/" + this.name().replace("_", "").toLowerCase() + "_flowing");
Identifier texture = new Identifier(TechReborn.MOD_ID, "block/fluids/" + this.name().replace("_", "").toLowerCase() + "_flowing");
fluidSettings.setStillTexture(texture);
fluidSettings.setFlowingTexture(texture);
stillFluid = new RebornFluid(true, fluidSettings, () -> block, () -> bucket, () -> flowingFluid, () -> stillFluid){};
flowingFluid = new RebornFluid(false, fluidSettings, () -> block, () -> bucket, () -> flowingFluid, () -> stillFluid){};
stillFluid = new RebornFluid(true, fluidSettings, () -> block, () -> bucket, () -> flowingFluid, () -> stillFluid) {
};
flowingFluid = new RebornFluid(false, fluidSettings, () -> block, () -> bucket, () -> flowingFluid, () -> stillFluid) {
};
block = new RebornFluidBlock(stillFluid, FabricBlockSettings.of(Material.WATER).noCollision().hardness(100.0F).dropsNothing().build());
bucket = new RebornBucketItem(stillFluid, new Item.Settings().group(TechReborn.ITEMGROUP));
bucket = new RebornBucketItem(stillFluid, new Item.Settings().group(TechReborn.ITEMGROUP).recipeRemainder(Items.BUCKET).maxCount(1));
}
public void register() {
RebornFluidManager.register(stillFluid, identifier);
RebornFluidManager.register(flowingFluid, new Identifier("techreborn", identifier.getPath() + "_flowing"));
RebornFluidManager.register(flowingFluid, new Identifier(TechReborn.MOD_ID, identifier.getPath() + "_flowing"));
Registry.register(Registry.BLOCK, identifier, block);
Registry.register(Registry.ITEM, identifier, bucket);
Registry.register(Registry.ITEM, new Identifier(TechReborn.MOD_ID, identifier.getPath() + "_bucket"), bucket);
}
public RebornFluid getFluid() {

View file

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

View file

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

View file

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

View file

@ -0,0 +1,59 @@
{
"elements": [
{
"from": [4, 11, 7.5],
"to": [12, 13, 8.5],
"faces": {
"north": {"uv": [4, 3, 12, 5], "texture": "#texture"},
"south": {"uv": [4, 3, 12, 5], "texture": "#texture"}
}
},
{
"from": [5, 10, 7.5],
"to": [11, 11, 8.5],
"faces": {
"north": {"uv": [5, 5, 11, 6], "texture": "#texture"},
"south": {"uv": [5, 5, 11, 6], "texture": "#texture"}
}
},
{
"from": [3, 11, 7.5],
"to": [4, 12, 8.5],
"faces": {
"north": {"uv": [12, 4, 13, 5], "texture": "#texture"},
"south": {"uv": [12, 4, 13, 5], "texture": "#texture"}
}
},
{
"from": [12, 11, 7.5],
"to": [13, 12, 8.5],
"rotation": {"angle": 0, "axis": "y", "origin": [17, 8, 8]},
"faces": {
"north": {"uv": [3, 4, 4, 5], "texture": "#texture"},
"south": {"uv": [3, 4, 4, 5], "texture": "#texture"}
}
}
],
"display": {
"thirdperson_righthand": {
"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]
},
"ground": {
"translation": [0, 2, 0],
"scale": [0.5, 0.5, 0.5]
},
"head": {
"rotation": [0, 180, 0],
"translation": [0, 13, 7]
},
"fixed": {
"rotation": [0, 180, 0]
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 335 B