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.



×
×
  • Create New...

Important Information

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