Skip to content

Commit

Permalink
Migrate rendering to FastTESR (Better FPS performance)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikka0w0 committed Oct 11, 2017
1 parent 93b332a commit 0ee653e
Show file tree
Hide file tree
Showing 14 changed files with 320 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import rikka.librikka.model.loader.AdvancedModelLoader;
import simelectricity.essential.client.cable.CableStateMapper;
import simelectricity.essential.client.coverpanel.SupportRender;
import simelectricity.essential.client.grid.FastTESRPowerPole;
import simelectricity.essential.client.grid.GridStateMapper;
import simelectricity.essential.client.grid.TileRenderPowerPole;
import simelectricity.essential.client.grid.transformer.FastTESRPowerTransformer;
import simelectricity.essential.client.grid.transformer.TransformerStateMapper;
import simelectricity.essential.client.semachine.SEMachineStateMapper;
import simelectricity.essential.client.semachine.SocketRender;
Expand All @@ -19,6 +21,7 @@
import simelectricity.essential.grid.TilePowerPole2;
import simelectricity.essential.grid.TilePowerPole3.Pole10Kv;
import simelectricity.essential.grid.TilePowerPole3.Pole415vType0;
import simelectricity.essential.grid.transformer.TilePowerTransformerPlaceHolder;
import simelectricity.essential.grid.transformer.TilePowerTransformerWinding.Primary;
import simelectricity.essential.grid.transformer.TilePowerTransformerWinding.Secondary;

Expand Down Expand Up @@ -68,16 +71,19 @@ public static void preStitchTexture(TextureStitchEvent.Pre event) {

SocketRender.stitchTexture(map);
SupportRender.stitchTexture(map);
FastTESRPowerPole.stitchTexture(map);
}

public static void registerTileEntityRenders() {
TileRenderPowerPole.register(TileCableJoint.class);
TileRenderPowerPole.register(TilePowerPole.class);
TileRenderPowerPole.register(TilePowerPole2.class);
TileRenderPowerPole.register(Primary.class);
TileRenderPowerPole.register(Secondary.class);
TileRenderPowerPole.register(Pole10Kv.Type0.class);
TileRenderPowerPole.register(Pole10Kv.Type1.class);
TileRenderPowerPole.register(Pole415vType0.class);
FastTESRPowerPole.register(TileCableJoint.class);
FastTESRPowerPole.register(TilePowerPole.class);
FastTESRPowerPole.register(TilePowerPole2.class);
FastTESRPowerPole.register(Primary.class);
FastTESRPowerPole.register(Secondary.class);
FastTESRPowerPole.register(Pole10Kv.Type0.class);
FastTESRPowerPole.register(Pole10Kv.Type1.class);
FastTESRPowerPole.register(Pole415vType0.class);

ClientRegistry.bindTileEntitySpecialRenderer(TilePowerTransformerPlaceHolder.Render.class, new FastTESRPowerTransformer());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package simelectricity.essential.client.grid;

import net.minecraft.client.renderer.VertexBuffer;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.client.model.animation.FastTESR;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import rikka.librikka.math.MathAssitant;
import rikka.librikka.math.Vec3f;
import rikka.librikka.model.quadbuilder.RawQuadCube;
import rikka.librikka.model.quadbuilder.RawQuadGroup;
import simelectricity.common.ConfigManager;

@SideOnly(Side.CLIENT)
public class FastTESRPowerPole<T extends TileEntity & ISEPowerPole> extends FastTESR<T> {
private static FastTESRPowerPole instance;
private static TextureAtlasSprite texture;

/**
* Do not call this
*/
public static void stitchTexture(TextureMap map) {
texture = map.registerSprite(new ResourceLocation("sime_essential:render/transmission/hv_cable"));
}

public static <T extends TileEntity & ISEPowerPole> void register(Class<T> cls) {
if (FastTESRPowerPole.instance == null)
FastTESRPowerPole.instance = new FastTESRPowerPole();

ClientRegistry.bindTileEntitySpecialRenderer(cls, FastTESRPowerPole.instance);
}

public static RawQuadGroup renderParabolicCable(Vec3f from, Vec3f to, boolean half, float tension, float thickness, TextureAtlasSprite texture) {
RawQuadGroup ret = new RawQuadGroup();

float steps = ConfigManager.parabolaRenderSteps;
float length = from.distanceTo(to);
float b = 4F * tension / length;
float a = -b / length;
float unitLength = length / steps;

float x0, y0, x1, y1;

for (int i = 0; i < steps / (half ? 2 : 1); i++) {
x0 = i * unitLength;
y0 = x0 * x0 * a + x0 * b;
x1 = (i + 1) * unitLength;
y1 = x1 * x1 * a + x1 * b;

ret.add((new RawQuadCube(thickness, MathHelper.sqrt(unitLength*unitLength + (y1 - y0)*(y1 - y0)), thickness, texture))
.rotateAroundZ((float) Math.atan2(y0 - y1, unitLength) * 180F / MathAssitant.PI)
.translateCoord(y0, i * unitLength, 0)
);
}

ret.rotateToVec(from.x, from.y, from.z, to.x, to.y, to.z);
ret.translateCoord(from.x, from.y, from.z);
return ret;
}

@Override
public boolean isGlobalRenderer(TileEntity te) {
return true;
}

@Override
public void renderTileEntityFast(T te, double x, double y, double z, float partialTicks, int destroyStage, VertexBuffer buffer) {
PowerPoleRenderHelper helper = te.getRenderHelper();

if (helper == null)
return;

BlockPos pos = helper.pos;
if (helper.quadBuffer.isEmpty()) {
if (helper.extraWires.isEmpty() && helper.connectionInfo.isEmpty())
return;

for (PowerPoleRenderHelper.ConnectionInfo[] connections : helper.connectionInfo) {
for (PowerPoleRenderHelper.ConnectionInfo info : connections) {
RawQuadGroup group = renderParabolicCable(info.fixedFrom, info.fixedTo, true, info.tension, 0.06F, texture);
group.translateCoord(- pos.getX(), - pos.getY(), - pos.getZ());
group.bake(helper.quadBuffer);
}
}

for (PowerPoleRenderHelper.ExtraWireInfo wire : helper.extraWires) {
RawQuadGroup group = renderParabolicCable(wire.from, wire.to, false, wire.tension, 0.06F, texture);
group.translateCoord(- pos.getX(), - pos.getY(), - pos.getZ());
group.bake(helper.quadBuffer);
}
}


buffer.setTranslation(x-pos.getX(), y-pos.getY(), z-pos.getZ());

int i = 15728640;
for (BakedQuad quad: helper.quadBuffer) {
buffer.addVertexData(quad.getVertexData());
buffer.putBrightness4(i, i, i, i);
buffer.putPosition(pos.getX(), pos.getY(), pos.getZ());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ public class PowerPoleRenderHelper {
public final PowerPoleRenderHelper.Group[] groups;
public final int insulatorPerGroup;
private final IBlockAccess world;

/**
* Buffer
*/
public LinkedList<PowerPoleRenderHelper.ConnectionInfo[]> connectionInfo = new LinkedList();
public LinkedList<PowerPoleRenderHelper.ExtraWireInfo> extraWires = new LinkedList();
public final LinkedList<PowerPoleRenderHelper.ConnectionInfo[]> connectionInfo = new LinkedList();
public final LinkedList<PowerPoleRenderHelper.ExtraWireInfo> extraWires = new LinkedList();

public final LinkedList<BakedQuad> quadBuffer = new LinkedList();

private int addedGroup;

public PowerPoleRenderHelper(IBlockAccess world, BlockPos pos, int rotationMC, int numOfGroup, int insulatorPerGroup) {
Expand Down Expand Up @@ -223,6 +227,9 @@ public final void updateRenderData(BlockPos... neighborPosList) {
this.extraWires.clear();
addNeighors(neighborPosList);
onUpdate();

//Bake Quads
this.quadBuffer.clear();
}

/**
Expand Down

This file was deleted.

Loading

0 comments on commit 0ee653e

Please sign in to comment.