Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16]Help with treads with other blocks in your custom model


Recommended Posts

 

public class ConnectedTexturesModel implements IModelGeometry<ConnectedTexturesModel> {
	ConnectedTextures[] faces;

	public ConnectedTexturesModel(ConnectedTextures[] faces) {
		this.faces = faces;
	}

	@Override
	public IBakedModel bake(IModelConfiguration owner, ModelBakery bakery, Function<RenderMaterial, TextureAtlasSprite> spriteGetter, IModelTransform modelTransform, ItemOverrideList overrides, ResourceLocation modelLocation) {
		ExpansionMod.LOGGER.debug("baketexture");
		return new BakedModel(faces, modelTransform);
	}

	@Override
	public Collection<RenderMaterial> getTextures(IModelConfiguration owner, Function<ResourceLocation, IUnbakedModel> modelGetter, Set<Pair<String, String>> missingTextureErrors) {
		List<RenderMaterial> materials = new ArrayList<>();
		for (ConnectedTextures face : faces) {
			switch (face.type) {
				case NORMAL:
					for (ResourceLocation texture : ((List<ResourceLocation>) face.contents)) {
						materials.add(new RenderMaterial(AtlasTexture.LOCATION_BLOCKS_TEXTURE, texture));
					}

					break;
				case TILES:
					for (TilesConnection.Tile tile : ((List<TilesConnection.Tile>) face.contents)) {
						for (ResourceLocation texture : tile.textures) {
							materials.add(new RenderMaterial(AtlasTexture.LOCATION_BLOCKS_TEXTURE, texture));
						}
					}
					break;
				//for (ConnectedTextures.Tiles tiles : ((List<ConnectedTextures.Tiles>) face.contents)) {
				//}
			}
		}
		return materials;
	}

	public static final class BakedModel implements IBakedModel {
		private IModelTransform modelTransform;
		ConnectedTextures[] faces;
		private List<TextureAtlasSprite> sprites;

		private static final ModelProperty<ConnectedTexturesModelData> CONNECTED_TEXTURES_DATA = new ModelProperty<>();

		public BakedModel(ConnectedTextures[] faces, IModelTransform modelTransform) {
			this.faces = faces;
			this.modelTransform = modelTransform;
			sprites = new ArrayList<>(
				Arrays.asList(
					null, null, null, null, null, null
					//new RenderMaterial(AtlasTexture.LOCATION_BLOCKS_TEXTURE, new ResourceLocation("block/missing")).getSprite(),
				)
			);
		}

		@Override
		public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, @Nonnull Random rand, @Nonnull IModelData extraData) {
			for (int i = 0; i < faces.length; i++) {
				ConnectedTextures face = faces[i];
				switch (face.type) {
					case NORMAL:
						ResourceLocation texture = ((List<ResourceLocation>) face.contents).get(0);
						sprites.set(i, new RenderMaterial(AtlasTexture.LOCATION_BLOCKS_TEXTURE, texture).getSprite());
						break;
					case TILES:
						ResourceLocation textureTile = ((List<TilesConnection.Tile>) face.contents).get(0).textures.get(0);
						sprites.set(i, new RenderMaterial(AtlasTexture.LOCATION_BLOCKS_TEXTURE, textureTile).getSprite());
						break;
					//for (ConnectedTextures.Tiles tiles : ((List<ConnectedTextures.Tiles>) face.contents)) {
					//}
				}
			}
			if (extraData.hasProperty(CONNECTED_TEXTURES_DATA)) {
				ExpansionMod.LOGGER.debug(extraData.getData(CONNECTED_TEXTURES_DATA).pos + "extraPos");
			}
			List<BakedQuad> quads = new ArrayList<>();
			TextureAtlasSprite down = sprites.get(0);
			TextureAtlasSprite up = sprites.get(1);
			TextureAtlasSprite north = sprites.get(2);
			TextureAtlasSprite south = sprites.get(3);
			TextureAtlasSprite west = sprites.get(4);
			TextureAtlasSprite east = sprites.get(5);
			quads.add(buildQuad(Direction.NORTH, north,
				1, 1, 0, north.getMinU(), north.getMinV(),
				1, 0, 0, north.getMinU(), north.getMaxV(),
				0, 0, 0, north.getMaxU(), north.getMaxV(),
				0, 1, 0, north.getMaxU(), north.getMinV()
			));
			quads.add(buildQuad(Direction.SOUTH, south,
				0, 1, 1, south.getMinU(), south.getMinV(),
				0, 0, 1, south.getMinU(), south.getMaxV(),
				1, 0, 1, south.getMaxU(), south.getMaxV(),
				1, 1, 1, south.getMaxU(), south.getMinV()
			));

			return quads;
		}

		@Override
		public IModelData getModelData(@Nonnull IBlockDisplayReader world, @Nonnull BlockPos pos, @Nonnull BlockState state, @Nonnull IModelData tileData) {
			if (tileData == EmptyModelData.INSTANCE) {
				tileData = new ModelDataMap.Builder().withProperty(CONNECTED_TEXTURES_DATA).build();
			}
			tileData.setData(CONNECTED_TEXTURES_DATA, new ConnectedTexturesModelData(world, pos));
			return tileData;
		}

		private BakedQuad buildQuad(Direction side, TextureAtlasSprite sprite,
		                            float x0, float y0, float z0, float u0, float v0,
		                            float x1, float y1, float z1, float u1, float v1,
		                            float x2, float y2, float z2, float u2, float v2,
		                            float x3, float y3, float z3, float u3, float v3) {
			BakedQuadBuilder builder = new BakedQuadBuilder(sprite);
			builder.setApplyDiffuseLighting(true);
			builder.setContractUVs(true);
			boolean hasTransform = this.modelTransform.getRotation().isIdentity();
			IVertexConsumer consumer = hasTransform ? new TRSRTransformer(builder, this.modelTransform.getRotation()) : builder;

			builder.setQuadOrientation(side);

			putVertex(consumer, side, x0, y0, z0, u0, v0);
			putVertex(consumer, side, x1, y1, z1, u1, v1);
			putVertex(consumer, side, x2, y2, z2, u2, v2);
			putVertex(consumer, side, x3, y3, z3, u3, v3);

			return builder.build();
		}

		private static void putVertex(IVertexConsumer consumer, Direction side, float x, float y, float z, float u, float v) {
			ImmutableList<VertexFormatElement> elements = consumer.getVertexFormat().getElements();
			for (int e = 0; e <= elements.size() - 1; e++) {
				VertexFormatElement element = elements.get(e);
				switch (element.getUsage()) {
					case POSITION:
						consumer.put(e, x, y, z, 1.0f);
						break;
					case COLOR:
						consumer.put(e, 1.0f, 1.0f, 1.0f, 1.0f);
						break;
					case NORMAL:
						float offX = (float) side.getXOffset();
						float offY = (float) side.getYOffset();
						float offZ = (float) side.getZOffset();
						consumer.put(e, offX, offY, offZ, 0.0f);
						break;
					case UV:
						if (element.getIndex() == 0) {
							consumer.put(e, u, v, 0f, 1f);
							break;
						}
					default:
						consumer.put(e);
						break;
				}
			}
		}

		@Override
		public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) {
			throw new AssertionError("IBakedModel::getQuads should never be called, only IForgeBakedModel::getQuads");
		}

		@Override
		public boolean isAmbientOcclusion() {
			return true;
		}

		@Override
		public boolean isGui3d() {
			return true;
		}

		@Override
		public boolean isSideLit() {
			return false;
		}

		@Override
		public boolean isBuiltInRenderer() {
			return false;
		}

		@Override
		public TextureAtlasSprite getParticleTexture() {
			return sprites.get(2);
		}

		@Override
		public ItemOverrideList getOverrides() {
			return null;
		}
	}

	public static class Loader implements IModelLoader<ConnectedTexturesModel> {
		public static Loader INSTANCE = new Loader();

		@Override
		public void onResourceManagerReload(IResourceManager resourceManager) {

		}

		@Override
		public ConnectedTexturesModel read(JsonDeserializationContext deserializationContext, JsonObject modelContents) {
			//List<ConnectedTextures> faces = new ArrayList<>();
			ConnectedTextures[] faces = new ConnectedTextures[6];
			if (modelContents.has("faces")) {
				JsonObject facesJson = modelContents.get("faces").getAsJsonObject();

				if (facesJson.has("all")) {
					for (int i = 0; i < Direction.values().length; i++) {
						faces[i] = ExpansionModModelLoaderRegistry.ExpandedBlockModelDeserializer.INSTANCE.fromJson(facesJson.get("all"), ConnectedTextures.class);
					}
				}
				if (facesJson.has("end")) {
					for (int i = 0; i < 2; i++) {
						faces[i] = ExpansionModModelLoaderRegistry.ExpandedBlockModelDeserializer.INSTANCE.fromJson(facesJson.get("end"), ConnectedTextures.class);
					}
				}
				if (facesJson.has("side")) {
					for (int i = 2; i < 6; i++) {
						faces[i] = ExpansionModModelLoaderRegistry.ExpandedBlockModelDeserializer.INSTANCE.fromJson(facesJson.get("side"), ConnectedTextures.class);
					}
				}
				for (Direction face : Direction.values()) {
					if (modelContents.has(face.toString())) {
						faces[face.getIndex()] = ExpansionModModelLoaderRegistry.ExpandedBlockModelDeserializer.INSTANCE.fromJson(facesJson.get(face.toString()), ConnectedTextures.class);
					}
				}
			}
			return new ConnectedTexturesModel(faces);
		}
	}
}

125108514_(71).thumb.png.9e26de7082f8e1187a2afc32d8b36c6a.png

In the above code, the sprite of the surface in contact with other blocks is drawn like the image. How can I prevent this from being drawn? Also, although not directly related to that, is there a way to get from the blockstate whether a sprite of a particular face is drawn (in contact with another block) or the sprite itself?

Link to post
Share on other sites

I think part of problem is that in getQuads, you are returning north and south faces and ignoring the quads for the requested side (method argument).

The rendering code calls Block.shouldSideBeRendered to check if quads for a given side are drawn or skipped.

Link to post
Share on other sites

Thank you.

31 minutes ago, lupicus said:

I think part of problem is that in getQuads, you are returning north and south faces and ignoring the quads for the requested side (method argument).

I think this is the side argument of type Direction, but what does that mean, for example, side == Direction.NORTH?

Link to post
Share on other sites

It is asking for quads for the NORTH side. It will call getQuads for all 6 sides and one extra for general quads (when side == null) not for a specific side. So by returning all the quads for each call then you are rendering the sides multiple times.

Edited by lupicus
Link to post
Share on other sites

Could be up to 7 times. You currently pass side to buildQuad. Do something like this for each add call you make:

if (side == Direction.NORTH)
    quads.add(buildQuad(Direction.NORTH, north,
                        1, 1, 0, north.getMinU(), north.getMinV(),
                        1, 0, 0, north.getMinU(), north.getMaxV(),
                        0, 0, 0, north.getMaxU(), north.getMaxV(),
                        0, 1, 0, north.getMaxU(), north.getMinV() ));

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • I'm currently trying to make a new cauldron-type block, and I'm having difficulty implementing blockstates. Here's the relevant part of the alchemical cauldron's code:   public class AlchemicalCauldronBlock extends Block { public static final IntegerProperty LEVEL = IntegerProperty.create("level", 1, 3); private static final VoxelShape INSIDE = box(2.0D, 4.0D, 2.0D, 14.0D, 16.0D, 14.0D); protected static final VoxelShape SHAPE = VoxelShapes.join(VoxelShapes.block(), VoxelShapes.or(box(0.0D, 0.0D, 4.0D, 16.0D, 3.0D, 12.0D), box(4.0D, 0.0D, 0.0D, 12.0D, 3.0D, 16.0D), box(2.0D, 0.0D, 2.0D, 14.0D, 3.0D, 14.0D), INSIDE), IBooleanFunction.ONLY_FIRST); public AlchemicalCauldronBlock() { super(Properties.of(Material.METAL).sound(SoundType.METAL).harvestLevel(3).strength(2F,2F)); // this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, 0)); // this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, Integer.valueOf(0))); } ... When I try to run this with either of the two attempts at registerDefaultState uncommented, I get this error: https://pastebin.com/vPf3UryC. For convenience, I believe this part is most relevant: How can I avoid this? My registry classes look like this, if it helps: public class registry { public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, AlchemyPlus.MODID); public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, AlchemyPlus.MODID); public static void register() { IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); BLOCKS.register(modEventBus); ITEMS.register(modEventBus); registerItems.register(); registerBlocks.register(); } } public class registerBlocks { public static final RegistryObject<Block> ALCHEMICAL_CAULDRON = register("alchemical_cauldron", AlchemicalCauldronBlock::new); static void register() {} private static <T extends Block> RegistryObject<T> registerNoItem(String name, Supplier<T> block) { return registry.BLOCKS.register(name, block); } private static <T extends Block> RegistryObject<T> register(String name, Supplier<T> block) { RegistryObject<T> ret = registerNoItem(name, block); registry.ITEMS.register(name, () -> new BlockItem(ret.get(), new Item.Properties().tab(ItemGroup.TAB_BREWING))); return ret; } } Full GitHub repository available here.   (Other minor problem(?):  I copied the methods getShape and getInteractionShape from net.minecraft.block.CauldronBlock and I'm getting these weird warnings. What's up with that? Do I need to worry about it?)  
    • Personally I use IForgeRegistryEntry#getRegistryName and then ResourceLocation#getNameSpace to get the id of the object.
    • you can get a block form a ResourceLocation, take a look at NBTUtil#readBlockState, do somthing like that
    • I am running forge version 1.16.5 36.1.31 on a server with a lot of mods and when a player refreshes the server list, it sends a long error in the server console. Additionally, the server list displays "Can't connect to server", despite refreshing and actually being able to connect to the server. here's the links to the logs: https://gist.github.com/WaffleTraits/83885b1539d5711fabff73a91924e665 https://gist.github.com/WaffleTraits/5ae78e6eaa330422bbc9f7226faf60ce Here's what's displayed in console when a player refreshes server listing: https://hastebin.com/ativexeyoy.properties server is running on ubuntu 20.04 and is using java 8 for this forge instance.
    • I kept getting this error whenever I tried joining my modded server I want to find a solution to this I checked every mod and it all matched Client : https://mclo.gs/Q39SSu5 server : https://mclo.gs/UO2kbDz
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.