Added ship scanner rendering

This commit is contained in:
Unknown 2019-04-21 23:24:25 +02:00 committed by unknown
parent a70aacdd0f
commit 0e81a69e56
7 changed files with 247 additions and 198 deletions

View file

@ -8,6 +8,7 @@ import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
@ -16,9 +17,14 @@ import net.minecraft.world.World;
import cr0s.warpdrive.block.BlockAbstractContainer;
import cr0s.warpdrive.data.BlockProperties;
import cr0s.warpdrive.data.EnumTier;
import cr0s.warpdrive.render.TileEntityShipScannerRenderer;
import javax.annotation.Nonnull;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class BlockShipScanner extends BlockAbstractContainer {
public BlockShipScanner(final String registryName, final EnumTier enumTier) {
@ -50,6 +56,28 @@ public class BlockShipScanner extends BlockAbstractContainer {
return blockState.getValue(BlockProperties.ACTIVE) ? 0x8 : 0;
}
@SideOnly(Side.CLIENT)
@Override
public void modelInitialisation() {
super.modelInitialisation();
// Bind our TESR to our tile entity
ClientRegistry.bindTileEntitySpecialRenderer(TileEntityShipScanner.class, new TileEntityShipScannerRenderer());
}
@SuppressWarnings("deprecation")
@Override
public boolean isTranslucent(final IBlockState blockState) {
return true;
}
@Nonnull
@SideOnly(Side.CLIENT)
@Override
public BlockRenderLayer getRenderLayer() {
return BlockRenderLayer.TRANSLUCENT;
}
/* @TODO camouflage
@Override
public int colorMultiplier(IBlockAccess blockAccess, int x, int y, int z) {
@ -76,7 +104,7 @@ public class BlockShipScanner extends BlockAbstractContainer {
return false;
}
/**/
@Nonnull
@Override
public TileEntity createNewTileEntity(@Nonnull final World world, final int metadata) {

View file

@ -35,6 +35,7 @@ public class ClientProxy extends CommonProxy {
ModelLoaderRegistry.registerLoader(MyCustomModelLoader.INSTANCE);
MinecraftForge.EVENT_BUS.register(ModelBakeEventHandler.INSTANCE);
MinecraftForge.EVENT_BUS.register(SpriteManager.INSTANCE);
}
@Override

View file

@ -0,0 +1,29 @@
package cr0s.warpdrive.client;
import javax.annotation.Nonnull;
import java.util.HashSet;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class SpriteManager {
public static final SpriteManager INSTANCE = new SpriteManager();
private static final HashSet<ResourceLocation> resourceLocationTextures = new HashSet<>(16);
public static void add(@Nonnull final ResourceLocation resourceLocationTexture) {
resourceLocationTextures.add(resourceLocationTexture);
}
@SubscribeEvent
public void onPreTextureStitchEvent(@Nonnull final TextureStitchEvent.Pre eventPreTextureStitch) {
final TextureMap textureMap = eventPreTextureStitch.getMap();
for (final ResourceLocation resourceLocationTexture : resourceLocationTextures) {
textureMap.registerSprite(resourceLocationTexture);
}
}
}

View file

@ -39,7 +39,7 @@ public abstract class BakedModelAbstractBase implements IMyBakedModel {
protected TextureAtlasSprite spriteParticle;
protected TextureAtlasSprite spriteBlock;
protected int tintIndex = -1;
protected VertexFormat format;
protected VertexFormat format = DefaultVertexFormats.ITEM;
public BakedModelAbstractBase() {
super();

View file

@ -0,0 +1,116 @@
package cr0s.warpdrive.render;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
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.util.EnumFacing;
public class BakedModelShipScanner extends BakedModelAbstractBase {
private TextureAtlasSprite spriteBorder;
private void initSprite() {
if (spriteBorder == null) {
final TextureMap textureMapBlocks = Minecraft.getMinecraft().getTextureMapBlocks();
spriteBorder = textureMapBlocks.getAtlasSprite("warpdrive:blocks/building/ship_scanner-border");
}
}
@Nonnull
@Override
public List<BakedQuad> getQuads(@Nullable final IBlockState blockState, @Nullable final EnumFacing enumFacing, final long rand) {
initSprite();
final boolean isHidden = false;
// get color
final int color = 0xFF80FF80;
// pre-compute dimensions
final int intRadius = 1;
final float radius = intRadius + 0.0F;
final int size = 1 + 2 * intRadius;
// pre-compute coordinates
final float dX_min = 0.0F - radius;
final float dX_max = 1.0F + radius;
final float dY_min = (isHidden ? 1.999F : 0.999F);
final float dY_max = dY_min + 1.0F;
final float dZ_min = 0.0F - radius;
final float dZ_max = 1.0F + radius;
final float dU_min = spriteBorder.getMinU();
final float dU_max = spriteBorder.getMaxU();
final float dV_min = spriteBorder.getMinV();
final float dV_max = spriteBorder.getMaxV();
final List<BakedQuad> quads = new ArrayList<>();
// start drawing
for (int index = 0; index < size; index++) {
final float offsetMin = index == 0 ? 0.0F : 0.001F;
final float offsetMax = index == size - 1 ? 0.0F : 0.001F;
// draw exterior faces
addBakedQuad(quads, spriteBorder, color,
dX_min + index + 1, dY_max, dZ_min - offsetMax, dU_max, dV_min,
dX_min + index + 1, dY_min, dZ_min - offsetMax, dU_max, dV_max,
dX_min + index , dY_min, dZ_min - offsetMin, dU_min, dV_max,
dX_min + index , dY_max, dZ_min - offsetMin, dU_min, dV_min );
addBakedQuad(quads, spriteBorder, color,
dX_max - index - 1, dY_max, dZ_max + offsetMax, dU_max, dV_min,
dX_max - index - 1, dY_min, dZ_max + offsetMax, dU_max, dV_max,
dX_max - index , dY_min, dZ_max + offsetMin, dU_min, dV_max,
dX_max - index , dY_max, dZ_max + offsetMin, dU_min, dV_min);
addBakedQuad(quads, spriteBorder, color,
dX_min - offsetMin, dY_max, dZ_min + index , dU_max, dV_min,
dX_min - offsetMin, dY_min, dZ_min + index , dU_max, dV_max,
dX_min - offsetMax, dY_min, dZ_min + index + 1, dU_min, dV_max,
dX_min - offsetMax, dY_max, dZ_min + index + 1, dU_min, dV_min);
addBakedQuad(quads, spriteBorder, color,
dX_max + offsetMin, dY_max, dZ_max - index , dU_max, dV_min,
dX_max + offsetMin, dY_min, dZ_max - index , dU_max, dV_max,
dX_max + offsetMax, dY_min, dZ_max - index - 1, dU_min, dV_max,
dX_max + offsetMax, dY_max, dZ_max - index - 1, dU_min, dV_min);
// draw interior faces
addBakedQuad(quads, spriteBorder, color,
dX_min + index , dY_max, dZ_min + offsetMin, dU_min, dV_min,
dX_min + index , dY_min, dZ_min + offsetMin, dU_min, dV_max,
dX_min + index + 1, dY_min, dZ_min + offsetMax, dU_max, dV_max,
dX_min + index + 1, dY_max, dZ_min + offsetMax, dU_max, dV_min);
addBakedQuad(quads, spriteBorder, color,
dX_max - index , dY_max, dZ_max - offsetMin, dU_min, dV_min,
dX_max - index , dY_min, dZ_max - offsetMin, dU_min, dV_max,
dX_max - index - 1, dY_min, dZ_max - offsetMax, dU_max, dV_max,
dX_max - index - 1, dY_max, dZ_max - offsetMax, dU_max, dV_min);
addBakedQuad(quads, spriteBorder, color,
dX_min + offsetMax, dY_max, dZ_min + index + 1, dU_min, dV_min,
dX_min + offsetMax, dY_min, dZ_min + index + 1, dU_min, dV_max,
dX_min + offsetMin, dY_min, dZ_min + index , dU_max, dV_max,
dX_min + offsetMin, dY_max, dZ_min + index , dU_max, dV_min);
addBakedQuad(quads, spriteBorder, color,
dX_max - offsetMax, dY_max, dZ_max - index - 1, dU_min, dV_min,
dX_max - offsetMax, dY_min, dZ_max - index - 1, dU_min, dV_max,
dX_max - offsetMin, dY_min, dZ_max - index , dU_max, dV_max,
dX_max - offsetMin, dY_max, dZ_max - index , dU_max, dV_min);
}
return quads;
}
}

View file

@ -1,196 +0,0 @@
package cr0s.warpdrive.render;
public class RenderBlockShipScanner {
}
/* @TODO MC1.10 ISBRH
public class RenderBlockShipScanner implements ISimpleBlockRenderingHandler {
public static int renderId = 0;
public static RenderBlockShipScanner instance = new RenderBlockShipScanner();
@Override
public void renderInventoryBlock(final Block block, final int metadata, final int modelId, final RenderBlocks renderer) {
final float brightness = 1.0F;
// simplified copy from RenderBlocks.renderBlockAsItem()
final Tessellator tessellator = Tessellator.instance;
if (renderer.useInventoryTint) {
final int color = block.getRenderColor(metadata);
final float red = brightness * (float) (color >> 16 & 255) / 255.0F;
final float green = brightness * (float) (color >> 8 & 255) / 255.0F;
final float blue = brightness * (float) (color & 255) / 255.0F;
final float alpha = 1.0F;
// @TODO replace with vertexBuffer.color(red, green, blue, 1.0F);
}
renderer.setRenderBoundsFromBlock(block);
block.setBlockBoundsForItemRender();
renderer.setRenderBoundsFromBlock(block);
GL11.glRotatef(90.0F, 0.0F, 1.0F, 0.0F);
GL11.glTranslatef(-0.5F, -0.5F, -0.5F);
tessellator.startDrawingQuads();
tessellator.setNormal(0.0F, -1.0F, 0.0F);
renderer.renderFaceYNeg(block, 0.0D, 0.0D, 0.0D, renderer.getBlockIconFromSideAndMetadata(block, 0, metadata));
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(0.0F, 1.0F, 0.0F);
renderer.renderFaceYPos(block, 0.0D, 0.0D, 0.0D, renderer.getBlockIconFromSideAndMetadata(block, 1, metadata));
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(0.0F, 0.0F, -1.0F);
renderer.renderFaceZNeg(block, 0.0D, 0.0D, 0.0D, renderer.getBlockIconFromSideAndMetadata(block, 2, metadata));
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(0.0F, 0.0F, 1.0F);
renderer.renderFaceZPos(block, 0.0D, 0.0D, 0.0D, renderer.getBlockIconFromSideAndMetadata(block, 3, metadata));
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(-1.0F, 0.0F, 0.0F);
renderer.renderFaceXNeg(block, 0.0D, 0.0D, 0.0D, renderer.getBlockIconFromSideAndMetadata(block, 4, metadata));
tessellator.draw();
tessellator.startDrawingQuads();
tessellator.setNormal(1.0F, 0.0F, 0.0F);
renderer.renderFaceXPos(block, 0.0D, 0.0D, 0.0D, renderer.getBlockIconFromSideAndMetadata(block, 5, metadata));
tessellator.draw();
GL11.glTranslatef(0.5F, 0.5F, 0.5F);
}
@Override
public boolean renderWorldBlock(final IBlockAccess blockAccess, final int x, final int y, final int z, final Block block, final int modelId, final RenderBlocks renderer) {
if (!(block instanceof BlockShipScanner)) {
return false;
}
final TileEntity tileEntity = blockAccess.getTileEntity(x, y, z);
if (!(tileEntity instanceof TileEntityShipScanner)) {
return false;
}
if (BlockShipScanner.passCurrent == 0) {
// render the controlling block
int renderType = -1;
final Block blockCamouflage = ((TileEntityShipScanner) tileEntity).blockCamouflage;
if (blockCamouflage != null && !Dictionary.BLOCKS_NOCAMOUFLAGE.contains(blockCamouflage)) {
renderType = blockCamouflage.getRenderType();
}
return RenderCommons.renderWorldBlockCamouflaged(x, y, z, block, renderer, renderType, blockCamouflage);
}
// check vertical offset
final Block blockAbove = blockAccess.getBlock(x, y + 1, z);
final boolean isHidden = !blockAbove.isAir(blockAccess, x, y + 1, z)
&& blockAbove.isBlockSolid(blockAccess, x, y + 1, z, blockAccess.getBlockMetadata(x, y + 1, z));
// get brightness factors
final int brightnessForRender = getBrightnessForRender(partialTick);
final int brightnessHigh = brightnessForRender >> 16 & 65535;
final int brightnessLow = Math.max(240, brightnessForRender & 65535);
// render borders
final Tessellator tessellator = Tessellator.instance;
// apply coloring
final int colorMultiplier = 0xFFFFFF; // block.colorMultiplier(blockAccess, x, y, z);
final float fRed = (float) (colorMultiplier >> 16 & 255) / 255.0F;
final float fGreen = (float) (colorMultiplier >> 8 & 255) / 255.0F;
final float fBlue = (float) (colorMultiplier & 255) / 255.0F;
tessellator.setColorOpaque_F(fRed, fGreen, fBlue);
// get icons
final IIcon iconBorder = ((BlockShipScanner) block).getBorderIcon();
// pre-compute dimensions
final int intRadius = 1;
final double radius = intRadius + 0.0D;
final int size = 1 + 2 * intRadius;
// pre-compute coordinates
final double dX_min = x + 0.0D - radius;
final double dX_max = x + 1.0D + radius;
final double dY_min = y + (isHidden ? 1.999D : 0.999D);
final double dY_max = dY_min + 1.0D;
final double dZ_min = z + 0.0D - radius;
final double dZ_max = z + 1.0D + radius;
final double dU_min = iconBorder.getMinU();
final double dU_max = iconBorder.getMaxU();
final double dV_min = iconBorder.getMinV();
final double dV_max = iconBorder.getMaxV();
// start drawing
for (int index = 0; index < size; index++) {
final double offsetMin = index == 0 ? 0.0D : 0.001D;
final double offsetMax = index == size - 1 ? 0.0D : 0.001D;
// draw exterior faces
// template: vertexBuffer.begin(7, DefaultVertexFormats.PARTICLE_POSITION_TEX_COLOR_LMAP);
// template: vertexBuffer.pos(xMinEnd , yMax, 0.0D).tex(uMax, vMax).color(particleRed, particleGreen, particleBlue, alpha).lightmap(brightnessHigh, brightnessLow).endVertex();
tessellator.addVertexWithUV(dX_min + index + 1, dY_max, dZ_min - offsetMax, dU_max, dV_min);
tessellator.addVertexWithUV(dX_min + index + 1, dY_min, dZ_min - offsetMax, dU_max, dV_max);
tessellator.addVertexWithUV(dX_min + index , dY_min, dZ_min - offsetMin, dU_min, dV_max);
tessellator.addVertexWithUV(dX_min + index , dY_max, dZ_min - offsetMin, dU_min, dV_min);
tessellator.addVertexWithUV(dX_max - index - 1, dY_max, dZ_max + offsetMax, dU_max, dV_min);
tessellator.addVertexWithUV(dX_max - index - 1, dY_min, dZ_max + offsetMax, dU_max, dV_max);
tessellator.addVertexWithUV(dX_max - index , dY_min, dZ_max + offsetMin, dU_min, dV_max);
tessellator.addVertexWithUV(dX_max - index , dY_max, dZ_max + offsetMin, dU_min, dV_min);
tessellator.addVertexWithUV(dX_min - offsetMin, dY_max, dZ_min + index , dU_max, dV_min);
tessellator.addVertexWithUV(dX_min - offsetMin, dY_min, dZ_min + index , dU_max, dV_max);
tessellator.addVertexWithUV(dX_min - offsetMax, dY_min, dZ_min + index + 1, dU_min, dV_max);
tessellator.addVertexWithUV(dX_min - offsetMax, dY_max, dZ_min + index + 1, dU_min, dV_min);
tessellator.addVertexWithUV(dX_max + offsetMin, dY_max, dZ_max - index , dU_max, dV_min);
tessellator.addVertexWithUV(dX_max + offsetMin, dY_min, dZ_max - index , dU_max, dV_max);
tessellator.addVertexWithUV(dX_max + offsetMax, dY_min, dZ_max - index - 1, dU_min, dV_max);
tessellator.addVertexWithUV(dX_max + offsetMax, dY_max, dZ_max - index - 1, dU_min, dV_min);
// draw interior faces
tessellator.addVertexWithUV(dX_min + index , dY_max, dZ_min + offsetMin, dU_min, dV_min);
tessellator.addVertexWithUV(dX_min + index , dY_min, dZ_min + offsetMin, dU_min, dV_max);
tessellator.addVertexWithUV(dX_min + index + 1, dY_min, dZ_min + offsetMax, dU_max, dV_max);
tessellator.addVertexWithUV(dX_min + index + 1, dY_max, dZ_min + offsetMax, dU_max, dV_min);
tessellator.addVertexWithUV(dX_max - index , dY_max, dZ_max - offsetMin, dU_min, dV_min);
tessellator.addVertexWithUV(dX_max - index , dY_min, dZ_max - offsetMin, dU_min, dV_max);
tessellator.addVertexWithUV(dX_max - index - 1, dY_min, dZ_max - offsetMax, dU_max, dV_max);
tessellator.addVertexWithUV(dX_max - index - 1, dY_max, dZ_max - offsetMax, dU_max, dV_min);
tessellator.addVertexWithUV(dX_min + offsetMax, dY_max, dZ_min + index + 1, dU_min, dV_min);
tessellator.addVertexWithUV(dX_min + offsetMax, dY_min, dZ_min + index + 1, dU_min, dV_max);
tessellator.addVertexWithUV(dX_min + offsetMin, dY_min, dZ_min + index , dU_max, dV_max);
tessellator.addVertexWithUV(dX_min + offsetMin, dY_max, dZ_min + index , dU_max, dV_min);
tessellator.addVertexWithUV(dX_max - offsetMax, dY_max, dZ_max - index - 1, dU_min, dV_min);
tessellator.addVertexWithUV(dX_max - offsetMax, dY_min, dZ_max - index - 1, dU_min, dV_max);
tessellator.addVertexWithUV(dX_max - offsetMin, dY_min, dZ_max - index , dU_max, dV_max);
tessellator.addVertexWithUV(dX_max - offsetMin, dY_max, dZ_max - index , dU_max, dV_min);
}
return true;
}
@Override
public boolean shouldRender3DInInventory(final int modelId) {
return true;
}
@Override
public int getRenderId() {
return renderId;
}
}
/**/

View file

@ -0,0 +1,71 @@
package cr0s.warpdrive.render;
import cr0s.warpdrive.block.building.TileEntityShipScanner;
import cr0s.warpdrive.client.SpriteManager;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.GlStateManager.DestFactor;
import net.minecraft.client.renderer.GlStateManager.SourceFactor;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
public class TileEntityShipScannerRenderer extends TileEntitySpecialRenderer<TileEntityShipScanner> {
private static List<BakedQuad> bakedQuads;
public TileEntityShipScannerRenderer() {
super();
SpriteManager.add(new ResourceLocation("warpdrive:blocks/building/ship_scanner-border"));
}
@Override
public void render(final TileEntityShipScanner tileEntityShipScanner, final double x, final double y, final double z,
final float partialTicks, final int destroyStage, final float alpha) {
if (!tileEntityShipScanner.getWorld().isBlockLoaded(tileEntityShipScanner.getPos(), false)) {
return;
}
if (bakedQuads == null) {
bakedQuads = new BakedModelShipScanner().getQuads(null, null, 0L);
}
final Tessellator tessellator = Tessellator.getInstance();
GlStateManager.pushAttrib();
GlStateManager.pushMatrix();
GlStateManager.translate(x + 0.5D, y + 0.5D, z + 0.5D);
GlStateManager.enableBlend();
GlStateManager.blendFunc(SourceFactor.SRC_ALPHA, DestFactor.ONE);
// GlStateManager.disableCull();
GlStateManager.disableDepth();
RenderHelper.disableStandardItemLighting();
GlStateManager.disableLighting();
Minecraft.getMinecraft().renderEngine.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
final BufferBuilder worldRenderer = tessellator.getBuffer();
worldRenderer.setTranslation(-0.5, -0.5, -0.5);
worldRenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK);
RenderCommons.renderModelTESR(bakedQuads, worldRenderer, tileEntityShipScanner.getWorld().getCombinedLight(tileEntityShipScanner.getPos(), 15));
tessellator.draw();
worldRenderer.setTranslation(0.0D, 0.0D, 0.0D);
RenderHelper.enableStandardItemLighting();
GlStateManager.enableDepth();
// GlStateManager.enableCull();
GlStateManager.disableBlend();
GlStateManager.popMatrix();
GlStateManager.popAttrib();
}
}