Jump to content

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


Zemelua

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 comment
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 comment
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 comment
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 comment
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 comment
Share on other sites

  • 11 months later...

i am doing something simular but i am getting a out of bounds
 

java.lang.ArrayIndexOutOfBoundsException: 4
    at net.minecraftforge.client.model.pipeline.BakedQuadBuilder.put(BakedQuadBuilder.java:99) ~[forge-1.16.5-36.2.20_mapped_official_1.16.5-recomp.jar:?] {re:classloading}
    at mightydanp.industrialtech.api.client.helper.BakedModelHelper.putVertex(BakedModelHelper.java:31) ~[main/:?] {re:classloading}
    at mightydanp.industrialtech.api.client.helper.BakedModelHelper.createQuad(BakedModelHelper.java:80) ~[main/:?] {re:classloading}
    at mightydanp.industrialtech.client.rendering.models.CampFireBakedModel.getQuads(CampFireBakedModel.java:51) ~[main/:?] {re:classloading}

https://github.com/mightydanp/IndustrialTech/blob/master/src/main/java/mightydanp/industrialtech/api/client/helper/BakedModelHelper.java

https://github.com/mightydanp/IndustrialTech/blob/master/src/main/java/mightydanp/industrialtech/client/rendering/models/CampFireBakedModel.java

i cant figure out whats causing the out of bounds

Edited by Mightydanp
Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.