Jump to content

Recommended Posts

Posted
public class ConnectedTexturesModel implements IModelGeometry<ConnectedTexturesModel> {
	IConnection[] connections;

	public ConnectedTexturesModel(IConnection[] connections) {
		this.connections = connections;
	}

	@Override
	public IBakedModel bake(IModelConfiguration owner, ModelBakery bakery, Function<RenderMaterial, TextureAtlasSprite> spriteGetter, IModelTransform modelTransform, ItemOverrideList overrides, ResourceLocation modelLocation) {
		return new BakedModel(connections, modelTransform);
	}

	@Override
	public Collection<RenderMaterial> getTextures(IModelConfiguration owner, Function<ResourceLocation, IUnbakedModel> modelGetter, Set<Pair<String, String>> missingTextureErrors) {
		// return new ArrayList<>(Arrays.asList(new RenderMaterial(AtlasTexture.LOCATION_BLOCKS_TEXTURE, new ResourceLocation("expansionmod", "block/plaster_half_timber_none"))));
		List<RenderMaterial> textures = new ArrayList();
		Arrays.stream(connections).forEach(i -> textures.addAll(i.getTextures()));
		return textures;
	}

	public static final class BakedModel implements IBakedModel {
		IConnection[] connections;
		private IModelTransform modelTransform;
		private final List<TextureAtlasSprite> sprites = new ArrayList<TextureAtlasSprite>(Arrays.asList(
			null, null, null, null, null, null
		));

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

		public BakedModel(IConnection[] connections, IModelTransform modelTransform) {
			this.connections = connections;
			this.modelTransform = modelTransform;
		}

		@Override
		public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, @Nonnull Random rand, @Nonnull IModelData extraData) {
			for (int i = 0; i < connections.length; i++) {
				IConnection connection = connections[i];

				if (extraData.hasProperty(CONNECTED_TEXTURES_DATA)) {
					sprites.set(i, connection.makeTexture(
						extraData.getData(CONNECTED_TEXTURES_DATA).world,
						extraData.getData(CONNECTED_TEXTURES_DATA).pos,
						state,
						Direction.byIndex(i))
					);
					//ExpansionMod.LOGGER.debug(sprites.toString());
					//ExpansionMod.LOGGER.debug(connections[2].getTextures().toString());

				} else {
					sprites.set(i, new RenderMaterial(AtlasTexture.LOCATION_BLOCKS_TEXTURE, MissingTextureSprite.getLocation()).getSprite());
				}
			}

			//ExpansionMod.LOGGER.debug(sprites.get(2).toString());
			//ExpansionMod.LOGGER.debug(new RenderMaterial(AtlasTexture.LOCATION_BLOCKS_TEXTURE, new ResourceLocation("expansionmod", "block/plaster_half_timber_cross")).getSprite().toString());

			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);

			if (side == Direction.DOWN) {
				quads.add(buildQuad(Direction.DOWN, down,
					0, 0, 1, down.getMinU(), down.getMinV(),
					0, 0, 0, down.getMinU(), down.getMaxV(),
					1, 0, 0, down.getMaxU(), down.getMaxV(),
					1, 0, 1, down.getMaxU(), down.getMinV()
				));
			}
			if (side == Direction.UP) {
				quads.add(buildQuad(Direction.UP, up,
					0, 1, 0, up.getMinU(), up.getMinV(),
					0, 1, 1, up.getMinU(), up.getMaxV(),
					1, 1, 1, up.getMaxU(), up.getMaxV(),
					1, 1, 0, up.getMaxU(), up.getMinV()
				));
			}
			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()
				));
			}
			if (side == Direction.SOUTH) {
				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()
				));
			}
			if (side == Direction.WEST) {
				quads.add(buildQuad(Direction.WEST, west,
					0, 1, 0, west.getMinU(), west.getMinV(),
					0, 0, 0, west.getMinU(), west.getMaxV(),
					0, 0, 1, west.getMaxU(), west.getMaxV(),
					0, 1, 1, west.getMaxU(), west.getMinV()
				));
			}
			if (side == Direction.EAST) {
				quads.add(buildQuad(Direction.EAST, east,
					1, 1, 1, east.getMinU(), east.getMinV(),
					1, 0, 1, east.getMinU(), east.getMaxV(),
					1, 0, 0, east.getMaxU(), east.getMaxV(),
					1, 1, 0, east.getMaxU(), east.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<IConnection> connections = new ArrayList<>(Arrays.asList(
				NormalConnection.missing(),
				NormalConnection.missing(),
				NormalConnection.missing(),
				NormalConnection.missing(),
				NormalConnection.missing(),
				NormalConnection.missing()
			));

			if (modelContents.has("faces")) {
				JsonObject facesJson = modelContents.get("faces").getAsJsonObject();

				if (facesJson.has("all")) {
					for (Direction face : Direction.values()) {
						IConnection connection = ExpansionModModelLoaderRegistry.ExpandedBlockModelDeserializer.INSTANCE.fromJson(facesJson.get("all"), IConnection.class);
						connections.set(face.getIndex(), connection);
					}
				}
				if (facesJson.has("end")) {
					IConnection connection = ExpansionModModelLoaderRegistry.ExpandedBlockModelDeserializer.INSTANCE.fromJson(facesJson.get("end"), IConnection.class);
					connections.set(Direction.DOWN.getIndex(), connection);
					connections.set(Direction.UP.getIndex(), connection);
				}
				if (facesJson.has("side")) {
					IConnection connection = ExpansionModModelLoaderRegistry.ExpandedBlockModelDeserializer.INSTANCE.fromJson(facesJson.get("side"), IConnection.class);
					connections.set(Direction.NORTH.getIndex(), connection);
					connections.set(Direction.SOUTH.getIndex(), connection);
					connections.set(Direction.WEST.getIndex(), connection);
					connections.set(Direction.EAST.getIndex(), connection);
				}
				for (Direction face : Direction.values()) {
					IConnection connection = ExpansionModModelLoaderRegistry.ExpandedBlockModelDeserializer.INSTANCE.fromJson(facesJson.get(face.toString()), IConnection.class);
					if (modelContents.has(face.toString())) {
						connections.set(face.getIndex(), connection);
					}
				}
			}

			return new ConnectedTexturesModel(connections.toArray(new IConnection[connections.size()]));
		}
	}
}

I created a custom model like this and it's working fine. Now, in models / item / custom_item.json, when you specify the json of this custom model for parent, how to draw it like a normal cube block? If my perception is correct, I first need to check if the model is a block or an item and generate a cubic item model. If this is correct

1. How to check if the block is being modeled or the item is being modeled now
2. Cube item model class

I would like to know.

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.