Skip to content

Commit

Permalink
+ render test
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlimiter committed Sep 24, 2023
1 parent 705c8a4 commit c138862
Show file tree
Hide file tree
Showing 33 changed files with 3,477 additions and 114 deletions.
9 changes: 9 additions & 0 deletions src/generated/resources/assets/minecraft/atlases/blocks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"sources": [
{
"type": "minecraft:directory",
"prefix": "",
"source": "shader"
}
]
}
3 changes: 2 additions & 1 deletion src/main/java/committee/nova/mods/avaritia/Avaritia.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package committee.nova.mods.avaritia;

import committee.nova.mods.avaritia.client.AvaritiaClient;
import committee.nova.mods.avaritia.client.shader.AvaritiaShaders;
import committee.nova.mods.avaritia.common.entity.EndestPearlEntity;
import committee.nova.mods.avaritia.init.config.ModConfig;
Expand Down Expand Up @@ -47,7 +48,7 @@ public Avaritia() {

AvaritiaShaders.init();
ModConfig.register();

AvaritiaClient.init();
}

public void setup(final FMLCommonSetupEvent event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package committee.nova.mods.avaritia.api.client.model;

import com.mojang.blaze3d.vertex.VertexConsumer;
import org.jetbrains.annotations.NotNull;

/**
* Name: Avaritia-forge / AlphaOverrideVertexConsumer
* Author: cnlimiter
* CreateTime: 2023/9/24 22:41
* Description:
*/

public class AlphaOverrideVertexConsumer implements VertexConsumer {
private final int alpha;
protected final VertexConsumer delegate;

public AlphaOverrideVertexConsumer(VertexConsumer delegate, int alpha) {
this.delegate = delegate;
this.alpha = alpha;
}

public AlphaOverrideVertexConsumer(VertexConsumer delegate, double alpha) {
this.delegate = delegate;
this.alpha = (int) (255 * alpha);
}

@Override
public @NotNull VertexConsumer vertex(double x, double y, double z) {
delegate.vertex(x, y, z);
return this;
}

@Override
public @NotNull VertexConsumer color(int r, int g, int b, int a) {
delegate.color(r, g, b, alpha);
return this;
}

@Override
public @NotNull VertexConsumer uv(float u, float v) {
delegate.uv(u, v);
return this;
}

@Override
public @NotNull VertexConsumer overlayCoords(int u, int v) {
delegate.overlayCoords(u, v);
return this;
}

@Override
public @NotNull VertexConsumer uv2(int u, int v) {
delegate.uv2(u, v);
return this;
}

@Override
public @NotNull VertexConsumer normal(float x, float y, float z) {
delegate.normal(x, y, z);
return this;
}

@Override
public void endVertex() {
delegate.endVertex();
}

@Override
public void defaultColor(int r, int g, int b, int a) {
delegate.defaultColor(r, g, b, a);
}


@Override
public void unsetDefaultColor() {
delegate.unsetDefaultColor();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package committee.nova.mods.avaritia.api.client.model;

import com.mojang.blaze3d.vertex.PoseStack;
import committee.nova.mods.avaritia.util.client.TextureUtil;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.ItemOverrides;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.core.Direction;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

/**
* Author cnlimiter
* CreateTime 2023/9/24 5:16
* Name IItemRenderer
* Description
*/

public interface IItemRenderer extends PerspectiveModel {
void renderItem(ItemStack stack, ItemDisplayContext transformType, PoseStack mStack, MultiBufferSource source, int packedLight, int packedOverlay);

//Useless methods for IItemRenderer.
//@formatter:off
@Override default@NotNull List<BakedQuad> getQuads(BlockState state, Direction side, RandomSource rand) { return Collections.emptyList(); }
@Override default boolean isCustomRenderer() { return true; }
@Override default@NotNull TextureAtlasSprite getParticleIcon() { return TextureUtil.getMissingSprite(); }
@Override default@NotNull ItemOverrides getOverrides() { return ItemOverrides.EMPTY; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package committee.nova.mods.avaritia.api.client.model;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Transformation;
import committee.nova.mods.avaritia.util.client.TransformUtil;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.world.item.ItemDisplayContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector3f;

/**
* Name: Avaritia-forge / PerspectiveModel
* Author: cnlimiter
* CreateTime: 2023/9/24 5:08
* Description:
*/

public interface PerspectiveModel extends BakedModel{
@Nullable
PerspectiveModelState getModelState();

@Override
default @NotNull BakedModel applyTransform(@NotNull ItemDisplayContext transformType, @NotNull PoseStack pStack, boolean leftFlip) {
PerspectiveModelState modelState = getModelState();
if (modelState != null) {
Transformation transform = getModelState().getTransform(transformType);

Vector3f trans = transform.getTranslation();
pStack.translate(trans.x(), trans.y(), trans.z());

pStack.mulPose(transform.getLeftRotation());

Vector3f scale = transform.getScale();
pStack.scale(scale.x(), scale.y(), scale.z());

pStack.mulPose(transform.getRightRotation());

if (leftFlip) {
TransformUtil.applyLeftyFlip(pStack);
}
return this;
}
return BakedModel.super.applyTransform(transformType, pStack, leftFlip);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package committee.nova.mods.avaritia.api.client.model;

import com.google.common.collect.ImmutableMap;
import com.mojang.math.Transformation;
import net.minecraft.client.resources.model.ModelState;
import net.minecraft.world.item.ItemDisplayContext;

import java.util.Map;

/**
* Name: Avaritia-forge / PerspectiveModelStat
* Author: cnlimiter
* CreateTime: 2023/9/24 5:08
* Description:
*/

public class PerspectiveModelState implements ModelState {
public static final PerspectiveModelState IDENTITY = new PerspectiveModelState(ImmutableMap.of());

private final Map<ItemDisplayContext, Transformation> transforms;
private final boolean isUvLocked;

public PerspectiveModelState(Map<ItemDisplayContext, Transformation> transforms) {
this(transforms, false);
}

public PerspectiveModelState(Map<ItemDisplayContext, Transformation> transforms, boolean isUvLocked) {
this.transforms = ImmutableMap.copyOf(transforms);
this.isUvLocked = isUvLocked;
}

public Transformation getTransform(ItemDisplayContext type) {
return transforms.getOrDefault(type, Transformation.identity());
}

@Override
public boolean isUvLocked() {
return isUvLocked;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package committee.nova.mods.avaritia.api.client.model;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import committee.nova.mods.avaritia.util.client.TransformUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.ItemOverrides;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelState;
import net.minecraft.core.Direction;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.client.model.data.ModelData;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

/**
* Name: Avaritia-forge / WrappedItemModel
* Author: cnlimiter
* CreateTime: 2023/9/24 5:17
* Description:
*/

public abstract class WrappedItemModel implements BakedModel {
protected final BakedModel wrapped;
protected final ModelState parentState;
@Nullable
protected LivingEntity entity;
@Nullable
protected ClientLevel world;

private final ItemOverrides overrideList = new ItemOverrides() {
@Override
public BakedModel resolve(@NotNull BakedModel originalModel, @NotNull ItemStack stack, @Nullable ClientLevel world, @Nullable LivingEntity entity, int seed) {
WrappedItemModel.this.entity = entity;
WrappedItemModel.this.world = world == null ? entity == null ? null : (ClientLevel) entity.level() : null;
return originalModel;
}
};

public WrappedItemModel(BakedModel wrapped) {
this.wrapped = wrapped;
parentState = TransformUtil.stateFromItemTransforms(wrapped.getTransforms());
}

@Override
public @NotNull List<BakedQuad> getQuads(BlockState state, Direction side, @NotNull RandomSource rand) {
return Collections.emptyList();
}

@Override
public @NotNull TextureAtlasSprite getParticleIcon() {
return wrapped.getParticleIcon();
}

@Override
public @NotNull TextureAtlasSprite getParticleIcon(@NotNull ModelData data) {
return wrapped.getParticleIcon(data);
}

@Override
public @NotNull ItemOverrides getOverrides() {
return overrideList;
}


protected void renderWrapped(ItemStack stack, PoseStack pStack, MultiBufferSource buffers, int packedLight, int packedOverlay, boolean fabulous) {
renderWrapped(stack, pStack, buffers, packedLight, packedOverlay, fabulous, Function.identity());
}


protected void renderWrapped(ItemStack stack, PoseStack pStack, MultiBufferSource buffers, int packedLight, int packedOverlay, boolean fabulous, Function<VertexConsumer, VertexConsumer> consOverride) {
BakedModel model = wrapped.getOverrides().resolve(wrapped, stack, world, entity, 0);

ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();
RenderType rType = ItemBlockRenderTypes.getRenderType(stack, fabulous);
VertexConsumer builder = ItemRenderer.getFoilBuffer(buffers, rType, true, stack.hasFoil());
itemRenderer.renderModelLists(model, stack, packedLight, packedOverlay, pStack, consOverride.apply(builder));
}
}
Loading

0 comments on commit c138862

Please sign in to comment.