Skip to content
This repository has been archived by the owner on Nov 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #20 from Chocohead/mc1.15
Browse files Browse the repository at this point in the history
Fix memory problems
  • Loading branch information
raoulvdberge authored May 29, 2020
2 parents 3b89450 + 484293a commit 23c9fc4
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 265 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.refinedmods.refinedpipes.render;

import com.google.common.collect.ImmutableList;

import com.refinedmods.refinedpipes.block.PipeBlock;
import com.refinedmods.refinedpipes.tile.PipeTileEntity;

import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.Quaternion;
import net.minecraft.client.renderer.TransformationMatrix;
import net.minecraft.client.renderer.Vector3f;
import net.minecraft.client.renderer.model.BakedQuad;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.client.renderer.model.ItemOverrideList;
Expand All @@ -11,37 +17,29 @@
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.data.EmptyModelData;
import net.minecraftforge.client.model.data.IModelData;
import net.minecraftforge.client.model.pipeline.BakedQuadBuilder;
import net.minecraftforge.client.model.pipeline.TRSRTransformer;
import net.minecraftforge.common.model.TransformationHelper;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;

public class PipeBakedModel implements IBakedModel {
private static final Map<Direction, TransformationMatrix> SIDE_TRANSFORMS = new EnumMap<>(Direction.class);
private final IBakedModel core;
private final IBakedModel extension;
private final IBakedModel straight;
private final Map<ResourceLocation, Map<Direction, IBakedModel>> attachmentModels = new HashMap<>();
private final Map<Direction, IBakedModel> inventoryAttachmentModels = new HashMap<>();
private final IBakedModel inventoryAttachment;
private final Map<ResourceLocation, IBakedModel> attachmentModels;
private final Map<PipeState, List<BakedQuad>> cache = new HashMap<>();

public PipeBakedModel(IBakedModel core, IBakedModel extension, IBakedModel straight, IBakedModel inventoryAttachment, Map<ResourceLocation, IBakedModel> attachmentModels) {
this.core = core;
this.extension = extension;
this.straight = straight;

for (Map.Entry<ResourceLocation, IBakedModel> entry : attachmentModels.entrySet()) {
Map<Direction, IBakedModel> dirToModel = new HashMap<>();

for (Direction dir : Direction.values()) {
dirToModel.put(dir, new TrsrBakedModel(entry.getValue(), dir));
}

this.attachmentModels.put(entry.getKey(), dirToModel);
}

for (Direction direction : Direction.values()) {
this.inventoryAttachmentModels.put(direction, new TrsrBakedModel(inventoryAttachment, direction));
}
this.inventoryAttachment = inventoryAttachment;
this.attachmentModels = attachmentModels;
}

@Override
Expand Down Expand Up @@ -71,9 +69,9 @@ private List<BakedQuad> createQuads(PipeState state) {
if (north && south && !east && !west && !up && !down) {
quads.addAll(straight.getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
} else if (!north && !south && east && west && !up && !down) {
quads.addAll(new TrsrBakedModel(straight, Direction.EAST).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(straight, Direction.EAST, state));
} else if (!north && !south && !east && !west && up && down) {
quads.addAll(new TrsrBakedModel(straight, Direction.UP).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(straight, Direction.UP, state));
} else if (!north && !south && !east && !west && !up && !down) {
quads.addAll(core.getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
} else {
Expand All @@ -84,23 +82,23 @@ private List<BakedQuad> createQuads(PipeState state) {
}

if (east) {
quads.addAll(new TrsrBakedModel(extension, Direction.EAST).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(extension, Direction.EAST, state));
}

if (south) {
quads.addAll(new TrsrBakedModel(extension, Direction.SOUTH).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(extension, Direction.SOUTH, state));
}

if (west) {
quads.addAll(new TrsrBakedModel(extension, Direction.WEST).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(extension, Direction.WEST, state));
}

if (up) {
quads.addAll(new TrsrBakedModel(extension, Direction.UP).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(extension, Direction.UP, state));
}

if (down) {
quads.addAll(new TrsrBakedModel(extension, Direction.DOWN).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(extension, Direction.DOWN, state));
}
}
}
Expand All @@ -110,7 +108,7 @@ private List<BakedQuad> createQuads(PipeState state) {
ResourceLocation attachmentId = state.getAttachmentState()[dir.ordinal()];

if (attachmentId != null) {
quads.addAll(attachmentModels.get(attachmentId).get(dir).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(attachmentModels.get(attachmentId), dir, state));
}
}
}
Expand All @@ -124,33 +122,69 @@ private List<BakedQuad> createQuads(PipeState state) {
boolean invDown = state.getState().get(PipeBlock.INV_DOWN);

if (invNorth && !state.hasAttachmentState(Direction.NORTH)) {
quads.addAll(inventoryAttachmentModels.get(Direction.NORTH).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(inventoryAttachment, Direction.NORTH, state));
}

if (invEast && !state.hasAttachmentState(Direction.EAST)) {
quads.addAll(inventoryAttachmentModels.get(Direction.EAST).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(inventoryAttachment, Direction.EAST, state));
}

if (invSouth && !state.hasAttachmentState(Direction.SOUTH)) {
quads.addAll(inventoryAttachmentModels.get(Direction.SOUTH).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(inventoryAttachment, Direction.SOUTH, state));
}

if (invWest && !state.hasAttachmentState(Direction.WEST)) {
quads.addAll(inventoryAttachmentModels.get(Direction.WEST).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(inventoryAttachment, Direction.WEST, state));
}

if (invUp && !state.hasAttachmentState(Direction.UP)) {
quads.addAll(inventoryAttachmentModels.get(Direction.UP).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(inventoryAttachment, Direction.UP, state));
}

if (invDown && !state.hasAttachmentState(Direction.DOWN)) {
quads.addAll(inventoryAttachmentModels.get(Direction.DOWN).getQuads(state.getState(), state.getSide(), state.getRand(), EmptyModelData.INSTANCE));
quads.addAll(getTransformedQuads(inventoryAttachment, Direction.DOWN, state));
}
}

return quads;
}

private static List<BakedQuad> getTransformedQuads(IBakedModel model, Direction facing, PipeState state) {
TransformationMatrix transformation = SIDE_TRANSFORMS.computeIfAbsent(facing, face -> {
Quaternion quaternion;
if (face == Direction.UP) {
quaternion = TransformationHelper.quatFromXYZ(new Vector3f(90, 0, 0), true);
} else if (face == Direction.DOWN) {
quaternion = TransformationHelper.quatFromXYZ(new Vector3f(270, 0, 0), true);
} else {
double r = Math.PI * (360 - face.getOpposite().getHorizontalIndex() * 90) / 180d;

quaternion = TransformationHelper.quatFromXYZ(new Vector3f(0, (float) r, 0), false);
}

return new TransformationMatrix(null, quaternion, null, null).blockCenterToCorner();
});

ImmutableList.Builder<BakedQuad> quads = ImmutableList.builder();
Direction side = state.getSide();

if (side != null && side.getHorizontalIndex() > -1) {
int faceOffset = 4 + Direction.NORTH.getHorizontalIndex() - facing.getHorizontalIndex();
side = Direction.byHorizontalIndex((side.getHorizontalIndex() + faceOffset) % 4);
}

for (BakedQuad quad : model.getQuads(state.getState(), side, state.getRand(), EmptyModelData.INSTANCE)) {
BakedQuadBuilder builder = new BakedQuadBuilder(quad.func_187508_a());
TRSRTransformer transformer = new TRSRTransformer(builder, transformation);

quad.pipe(transformer);

quads.add(builder.build());
}

return quads.build();
}

@Override
public boolean isAmbientOcclusion() {
return core.isAmbientOcclusion();
Expand Down
130 changes: 0 additions & 130 deletions src/main/java/com/refinedmods/refinedpipes/render/TrsrBakedModel.java

This file was deleted.

Loading

0 comments on commit 23c9fc4

Please sign in to comment.