Applied-Energistics-2-tiler.../src/main/java/appeng/client/render/BusRenderHelper.java

517 lines
13 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
2014-09-24 02:26:27 +02:00
package appeng.client.render;
import java.util.EnumSet;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.util.IIcon;
import net.minecraftforge.common.util.ForgeDirection;
2014-12-29 21:59:05 +01:00
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2014-09-24 02:26:27 +02:00
import appeng.api.AEApi;
import appeng.api.parts.IBoxProvider;
import appeng.api.parts.IPartCollisionHelper;
import appeng.api.parts.IPartRenderHelper;
import appeng.api.parts.ISimplifiedBundle;
import appeng.block.AEBaseBlock;
import appeng.block.networking.BlockCableBus;
import appeng.core.AEConfig;
import appeng.core.features.AEFeature;
@SideOnly(Side.CLIENT)
public class BusRenderHelper implements IPartRenderHelper
{
final public static BusRenderHelper instance = new BusRenderHelper();
double minX = 0;
double minY = 0;
double minZ = 0;
double maxX = 16;
double maxY = 16;
double maxZ = 16;
final AEBaseBlock blk = (AEBaseBlock) AEApi.instance().blocks().blockMultiPart.block();
final BaseBlockRender bbr = new BaseBlockRender();
2014-09-24 02:26:27 +02:00
private ForgeDirection ax = ForgeDirection.EAST;
private ForgeDirection ay = ForgeDirection.UP;
private ForgeDirection az = ForgeDirection.SOUTH;
int color = 0xffffff;
class BoundBoxCalculator implements IPartCollisionHelper
{
public boolean started = false;
float minX;
float minY;
float minZ;
float maxX;
float maxY;
float maxZ;
@Override
public void addBox(double minX, double minY, double minZ, double maxX, double maxY, double maxZ)
{
2014-12-29 15:13:47 +01:00
if ( this.started )
2014-09-24 02:26:27 +02:00
{
this.minX = Math.min( this.minX, (float) minX );
this.minY = Math.min( this.minY, (float) minY );
this.minZ = Math.min( this.minZ, (float) minZ );
this.maxX = Math.max( this.maxX, (float) maxX );
this.maxY = Math.max( this.maxY, (float) maxY );
this.maxZ = Math.max( this.maxZ, (float) maxZ );
}
else
{
2014-12-29 15:13:47 +01:00
this.started = true;
2014-09-24 02:26:27 +02:00
this.minX = (float) minX;
this.minY = (float) minY;
this.minZ = (float) minZ;
this.maxX = (float) maxX;
this.maxY = (float) maxY;
this.maxZ = (float) maxZ;
}
}
@Override
public ForgeDirection getWorldX()
{
2014-12-29 15:13:47 +01:00
return BusRenderHelper.this.ax;
2014-09-24 02:26:27 +02:00
}
@Override
public ForgeDirection getWorldY()
{
2014-12-29 15:13:47 +01:00
return BusRenderHelper.this.ay;
2014-09-24 02:26:27 +02:00
}
@Override
public ForgeDirection getWorldZ()
{
2014-12-29 15:13:47 +01:00
return BusRenderHelper.this.az;
2014-09-24 02:26:27 +02:00
}
@Override
public boolean isBBCollision()
{
return false;
}
2014-09-28 00:50:06 +02:00
}
2014-09-24 02:26:27 +02:00
final BoundBoxCalculator bbc = new BoundBoxCalculator();
2014-09-24 02:26:27 +02:00
int renderingForPass = 0;
int currentPass = 0;
int itemsRendered = 0;
final boolean noAlphaPass = !AEConfig.instance.isFeatureEnabled( AEFeature.AlphaPass );
2014-09-24 02:26:27 +02:00
public int getItemsRendered()
{
2014-12-29 15:13:47 +01:00
return this.itemsRendered;
2014-09-24 02:26:27 +02:00
}
public void setPass(int pass)
{
2014-12-29 15:13:47 +01:00
this.renderingForPass = 0;
this.currentPass = pass;
this.itemsRendered = 0;
2014-09-24 02:26:27 +02:00
}
@Override
public void renderForPass(int pass)
{
2014-12-29 15:13:47 +01:00
this.renderingForPass = pass;
2014-09-24 02:26:27 +02:00
}
public boolean renderThis()
{
2014-12-29 15:13:47 +01:00
if ( this.renderingForPass == this.currentPass || this.noAlphaPass )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.itemsRendered++;
2014-09-24 02:26:27 +02:00
return true;
}
return false;
}
@Override
public void normalRendering()
{
RenderBlocksWorkaround rbw = BusRenderer.instance.renderer;
rbw.calculations = true;
rbw.useTextures = true;
rbw.enableAO = false;
}
@Override
public ISimplifiedBundle useSimplifiedRendering(int x, int y, int z, IBoxProvider p, ISimplifiedBundle sim)
{
RenderBlocksWorkaround rbw = BusRenderer.instance.renderer;
2014-12-29 15:13:47 +01:00
if ( sim != null && rbw.similarLighting( this.blk, rbw.blockAccess, x, y, z, sim ) )
2014-09-24 02:26:27 +02:00
{
rbw.populate( sim );
rbw.faces = EnumSet.allOf( ForgeDirection.class );
rbw.calculations = false;
rbw.useTextures = false;
return sim;
}
else
{
boolean allFaces = rbw.renderAllFaces;
rbw.renderAllFaces = true;
rbw.calculations = true;
rbw.faces.clear();
2014-12-29 15:13:47 +01:00
this.bbc.started = false;
2014-09-24 02:26:27 +02:00
if ( p == null )
{
2014-12-29 15:13:47 +01:00
this.bbc.minX = this.bbc.minY = this.bbc.minZ = 0;
this.bbc.maxX = this.bbc.maxY = this.bbc.maxZ = 16;
2014-09-24 02:26:27 +02:00
}
else
{
2014-12-29 15:13:47 +01:00
p.getBoxes( this.bbc );
if ( this.bbc.minX < 1 )
this.bbc.minX = 1;
if ( this.bbc.minY < 1 )
this.bbc.minY = 1;
if ( this.bbc.minZ < 1 )
this.bbc.minZ = 1;
if ( this.bbc.maxX > 15 )
this.bbc.maxX = 15;
if ( this.bbc.maxY > 15 )
this.bbc.maxY = 15;
if ( this.bbc.maxZ > 15 )
this.bbc.maxZ = 15;
2014-09-24 02:26:27 +02:00
}
2014-12-29 15:13:47 +01:00
this.setBounds( this.bbc.minX, this.bbc.minY, this.bbc.minZ, this.bbc.maxX, this.bbc.maxY, this.bbc.maxZ );
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.bbr.renderBlockBounds( rbw, this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ, this.ax, this.ay, this.az );
rbw.renderStandardBlock( this.blk, x, y, z );
2014-09-24 02:26:27 +02:00
rbw.faces = EnumSet.allOf( ForgeDirection.class );
rbw.renderAllFaces = allFaces;
rbw.calculations = false;
rbw.useTextures = false;
return rbw.getLightingCache();
}
}
@Override
public void setBounds(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
2014-09-24 02:26:27 +02:00
{
this.minX = minX;
this.minY = minY;
this.minZ = minZ;
this.maxX = maxX;
this.maxY = maxY;
this.maxZ = maxZ;
2014-09-24 02:26:27 +02:00
}
public double getBound(ForgeDirection side)
{
switch (side)
{
default:
case UNKNOWN:
return 0.5;
case DOWN:
2014-12-29 15:13:47 +01:00
return this.minY;
2014-09-24 02:26:27 +02:00
case EAST:
2014-12-29 15:13:47 +01:00
return this.maxX;
2014-09-24 02:26:27 +02:00
case NORTH:
2014-12-29 15:13:47 +01:00
return this.minZ;
2014-09-24 02:26:27 +02:00
case SOUTH:
2014-12-29 15:13:47 +01:00
return this.maxZ;
2014-09-24 02:26:27 +02:00
case UP:
2014-12-29 15:13:47 +01:00
return this.maxY;
2014-09-24 02:26:27 +02:00
case WEST:
2014-12-29 15:13:47 +01:00
return this.minX;
2014-09-24 02:26:27 +02:00
}
}
@Override
public void setInvColor(int newColor)
{
2014-12-29 15:13:47 +01:00
this.color = newColor;
2014-09-24 02:26:27 +02:00
}
@Override
public void setTexture(IIcon ico)
{
2014-12-29 15:13:47 +01:00
this.blk.getRendererInstance().setTemporaryRenderIcon( ico );
2014-09-24 02:26:27 +02:00
}
@Override
public void setTexture(IIcon Down, IIcon Up, IIcon North, IIcon South, IIcon West, IIcon East)
{
IIcon[] list = new IIcon[6];
2014-09-24 02:26:27 +02:00
list[0] = Down;
list[1] = Up;
list[2] = North;
list[3] = South;
list[4] = West;
list[5] = East;
2014-12-29 15:13:47 +01:00
this.blk.getRendererInstance().setTemporaryRenderIcons( list[this.mapRotation( ForgeDirection.UP ).ordinal()],
list[this.mapRotation( ForgeDirection.DOWN ).ordinal()], list[this.mapRotation( ForgeDirection.SOUTH ).ordinal()],
list[this.mapRotation( ForgeDirection.NORTH ).ordinal()], list[this.mapRotation( ForgeDirection.EAST ).ordinal()],
list[this.mapRotation( ForgeDirection.WEST ).ordinal()] );
2014-09-24 02:26:27 +02:00
}
public ForgeDirection mapRotation(ForgeDirection dir)
{
2014-12-29 15:13:47 +01:00
ForgeDirection forward = this.az;
ForgeDirection up = this.ay;
2014-09-24 02:26:27 +02:00
ForgeDirection west = ForgeDirection.UNKNOWN;
if ( forward == null || up == null )
return dir;
int west_x = forward.offsetY * up.offsetZ - forward.offsetZ * up.offsetY;
int west_y = forward.offsetZ * up.offsetX - forward.offsetX * up.offsetZ;
int west_z = forward.offsetX * up.offsetY - forward.offsetY * up.offsetX;
for (ForgeDirection dx : ForgeDirection.VALID_DIRECTIONS)
if ( dx.offsetX == west_x && dx.offsetY == west_y && dx.offsetZ == west_z )
west = dx;
if ( dir.equals( forward ) )
return ForgeDirection.SOUTH;
if ( dir.equals( forward.getOpposite() ) )
return ForgeDirection.NORTH;
if ( dir.equals( up ) )
return ForgeDirection.UP;
if ( dir.equals( up.getOpposite() ) )
return ForgeDirection.DOWN;
if ( dir.equals( west ) )
return ForgeDirection.WEST;
if ( dir.equals( west.getOpposite() ) )
return ForgeDirection.EAST;
return ForgeDirection.UNKNOWN;
}
@Override
public void renderInventoryBox(RenderBlocks renderer)
{
2014-12-29 15:13:47 +01:00
renderer.setRenderBounds( this.minX / 16.0, this.minY / 16.0, this.minZ / 16.0, this.maxX / 16.0, this.maxY / 16.0, this.maxZ / 16.0 );
this.bbr.renderInvBlock( EnumSet.allOf( ForgeDirection.class ), this.blk, null, Tessellator.instance, this.color, renderer );
2014-09-24 02:26:27 +02:00
}
@Override
public void renderInventoryFace(IIcon IIcon, ForgeDirection face, RenderBlocks renderer)
{
2014-12-29 15:13:47 +01:00
renderer.setRenderBounds( this.minX / 16.0, this.minY / 16.0, this.minZ / 16.0, this.maxX / 16.0, this.maxY / 16.0, this.maxZ / 16.0 );
this.setTexture( IIcon );
this.bbr.renderInvBlock( EnumSet.of( face ), this.blk, null, Tessellator.instance, this.color, renderer );
2014-09-24 02:26:27 +02:00
}
@Override
public void renderBlock(int x, int y, int z, RenderBlocks renderer)
{
2014-12-29 15:13:47 +01:00
if ( !this.renderThis() )
2014-09-24 02:26:27 +02:00
return;
AEBaseBlock blk = (AEBaseBlock) AEApi.instance().blocks().blockMultiPart.block();
BlockRenderInfo info = blk.getRendererInstance();
ForgeDirection forward = BusRenderHelper.instance.az;
ForgeDirection up = BusRenderHelper.instance.ay;
renderer.uvRotateBottom = info.getTexture( ForgeDirection.DOWN ).setFlip( BaseBlockRender.getOrientation( ForgeDirection.DOWN, forward, up ) );
renderer.uvRotateTop = info.getTexture( ForgeDirection.UP ).setFlip( BaseBlockRender.getOrientation( ForgeDirection.UP, forward, up ) );
renderer.uvRotateEast = info.getTexture( ForgeDirection.EAST ).setFlip( BaseBlockRender.getOrientation( ForgeDirection.EAST, forward, up ) );
renderer.uvRotateWest = info.getTexture( ForgeDirection.WEST ).setFlip( BaseBlockRender.getOrientation( ForgeDirection.WEST, forward, up ) );
renderer.uvRotateNorth = info.getTexture( ForgeDirection.NORTH ).setFlip( BaseBlockRender.getOrientation( ForgeDirection.NORTH, forward, up ) );
renderer.uvRotateSouth = info.getTexture( ForgeDirection.SOUTH ).setFlip( BaseBlockRender.getOrientation( ForgeDirection.SOUTH, forward, up ) );
2014-12-29 15:13:47 +01:00
this.bbr.renderBlockBounds( renderer, this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ, this.ax, this.ay, this.az );
2014-09-24 02:26:27 +02:00
renderer.renderStandardBlock( blk, x, y, z );
}
@Override
public Block getBlock()
{
return AEApi.instance().blocks().blockMultiPart.block();
}
public void setRenderColor(int color)
{
BlockCableBus blk = (BlockCableBus) AEApi.instance().blocks().blockMultiPart.block();
blk.setRenderColor( color );
}
public void prepareBounds(RenderBlocks renderer)
{
2014-12-29 15:13:47 +01:00
this.bbr.renderBlockBounds( renderer, this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ, this.ax, this.ay, this.az );
2014-09-24 02:26:27 +02:00
}
@Override
public void setFacesToRender(EnumSet<ForgeDirection> faces)
{
BusRenderer.instance.renderer.renderFaces = faces;
}
@Override
2014-09-24 02:26:27 +02:00
public void renderBlockCurrentBounds(int x, int y, int z, RenderBlocks renderer)
{
2014-12-29 15:13:47 +01:00
if ( !this.renderThis() )
2014-09-24 02:26:27 +02:00
return;
2014-12-29 15:13:47 +01:00
renderer.renderStandardBlock( this.blk, x, y, z );
2014-09-24 02:26:27 +02:00
}
@Override
public void renderFaceCutout(int x, int y, int z, IIcon ico, ForgeDirection face, float edgeThickness, RenderBlocks renderer)
{
2014-12-29 15:13:47 +01:00
if ( !this.renderThis() )
2014-09-24 02:26:27 +02:00
return;
switch (face)
{
case DOWN:
2014-12-29 15:13:47 +01:00
face = this.ay.getOpposite();
2014-09-24 02:26:27 +02:00
break;
case EAST:
2014-12-29 15:13:47 +01:00
face = this.ax;
2014-09-24 02:26:27 +02:00
break;
case NORTH:
2014-12-29 15:13:47 +01:00
face = this.az.getOpposite();
2014-09-24 02:26:27 +02:00
break;
case SOUTH:
2014-12-29 15:13:47 +01:00
face = this.az;
2014-09-24 02:26:27 +02:00
break;
case UP:
2014-12-29 15:13:47 +01:00
face = this.ay;
2014-09-24 02:26:27 +02:00
break;
case WEST:
2014-12-29 15:13:47 +01:00
face = this.ax.getOpposite();
2014-09-24 02:26:27 +02:00
break;
case UNKNOWN:
break;
default:
break;
}
2014-12-29 15:13:47 +01:00
this.bbr.renderCutoutFace( this.blk, ico, x, y, z, renderer, face, edgeThickness );
2014-09-24 02:26:27 +02:00
}
@Override
public void renderFace(int x, int y, int z, IIcon ico, ForgeDirection face, RenderBlocks renderer)
{
2014-12-29 15:13:47 +01:00
if ( !this.renderThis() )
2014-09-24 02:26:27 +02:00
return;
2014-12-29 15:13:47 +01:00
this.prepareBounds( renderer );
2014-09-24 02:26:27 +02:00
switch (face)
{
case DOWN:
2014-12-29 15:13:47 +01:00
face = this.ay.getOpposite();
2014-09-24 02:26:27 +02:00
break;
case EAST:
2014-12-29 15:13:47 +01:00
face = this.ax;
2014-09-24 02:26:27 +02:00
break;
case NORTH:
2014-12-29 15:13:47 +01:00
face = this.az.getOpposite();
2014-09-24 02:26:27 +02:00
break;
case SOUTH:
2014-12-29 15:13:47 +01:00
face = this.az;
2014-09-24 02:26:27 +02:00
break;
case UP:
2014-12-29 15:13:47 +01:00
face = this.ay;
2014-09-24 02:26:27 +02:00
break;
case WEST:
2014-12-29 15:13:47 +01:00
face = this.ax.getOpposite();
2014-09-24 02:26:27 +02:00
break;
case UNKNOWN:
break;
default:
break;
}
2014-12-29 15:13:47 +01:00
this.bbr.renderFace( x, y, z, this.blk, ico, renderer, face );
2014-09-24 02:26:27 +02:00
}
@Override
public ForgeDirection getWorldX()
{
2014-12-29 15:13:47 +01:00
return this.ax;
2014-09-24 02:26:27 +02:00
}
@Override
public ForgeDirection getWorldY()
{
2014-12-29 15:13:47 +01:00
return this.ay;
2014-09-24 02:26:27 +02:00
}
@Override
public ForgeDirection getWorldZ()
{
2014-12-29 15:13:47 +01:00
return this.az;
2014-09-24 02:26:27 +02:00
}
public void setOrientation(ForgeDirection dx, ForgeDirection dy, ForgeDirection dz)
{
2014-12-29 15:13:47 +01:00
this.ax = dx == null ? ForgeDirection.EAST : dx;
this.ay = dy == null ? ForgeDirection.UP : dy;
this.az = dz == null ? ForgeDirection.SOUTH : dz;
2014-09-24 02:26:27 +02:00
}
public double[] getBounds()
{
2014-12-29 15:13:47 +01:00
return new double[] { this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ };
2014-09-24 02:26:27 +02:00
}
public void setBounds(double[] bounds)
{
if ( bounds == null || bounds.length != 6 )
return;
2014-12-29 15:13:47 +01:00
this.minX = bounds[0];
this.minY = bounds[1];
this.minZ = bounds[2];
this.maxX = bounds[3];
this.maxY = bounds[4];
this.maxZ = bounds[5];
2014-09-24 02:26:27 +02:00
}
}