Created
May 19, 2019 13:30
-
-
Save hohserg1/d3cabc4dec48372f2fea3aebd4ccb8e6 to your computer and use it in GitHub Desktop.
TileAssortedEnchantTableRenderer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hohserg.assortedenchanttable.render | |
import hohserg.assortedenchanttable.Main | |
import hohserg.assortedenchanttable.blocks.BlockAssortedEnchantTable.TileAssortedEnchantmentTable | |
import hohserg.assortedenchanttable.utils.Vec | |
import net.minecraft.client.renderer.tileentity.{TileEntityEnchantmentTableRenderer, TileEntityRendererDispatcher, TileEntitySpecialRenderer} | |
import net.minecraft.client.renderer.vertex.DefaultVertexFormats | |
import net.minecraft.client.renderer.{BufferBuilder, GlStateManager, Tessellator} | |
import net.minecraft.item.EnumDyeColor | |
import net.minecraft.tileentity.TileEntityEnchantmentTable | |
import net.minecraft.util.ResourceLocation | |
import org.lwjgl.opengl._ | |
import scala.language.implicitConversions | |
class TileAssortedEnchantTableRenderer extends TileEntitySpecialRenderer[TileAssortedEnchantmentTable] { | |
private val vanilaRender = TileEntityRendererDispatcher.instance.renderers.get(classOf[TileEntityEnchantmentTable]).asInstanceOf[TileEntityEnchantmentTableRenderer] | |
val decalOffset = 0.0001f | |
def applyDecal(v1: Vec, v2: Vec, v3: Vec, v4: Vec): Poly = { | |
val normal = (vectByPoints(v1, v2) cross vectByPoints(v1, v3)).normalise * decalOffset | |
def scalingAddition(v1: Vec, v3: Vec) = vectByPoints(v1, v3).normalise * decalOffset | |
(v1 + normal + scalingAddition(v3, v1), | |
v2 + normal + scalingAddition(v4, v2), | |
v3 + normal + scalingAddition(v1, v3), | |
v4 + normal + scalingAddition(v2, v4)) | |
} | |
private def vectByPoints(v1: Vec, v3: Vec) = Vec(v3.x - v1.x, v3.y - v1.y, v3.z - v1.z) | |
val y = 12f / 16 | |
val p1: Poly = applyDecal((0, y, 0), (0, y, 1), (1, y, 1), (1, y, 0)) | |
val p2: Poly = applyDecal((1, 1, 0), (1, 1, 1), (1, 0, 1), (1, 0, 0)) | |
val p3: Poly = applyDecal((1, 1, 1), (0, 1, 1), (0, 0, 1), (1, 0, 1)) | |
val p4: Poly = applyDecal((0, 1, 1), (0, 1, 0), (0, 0, 0), (0, 0, 1)) | |
val p5: Poly = applyDecal((0, 1, 0), (1, 1, 0), (1, 0, 0), (0, 0, 0)) | |
def renderGems(gemIndex: Int): Unit = { | |
bindTexture(new ResourceLocation(Main.assortedenchanttableModId, "textures/gems.png")) | |
GlStateManager.color(1, 1, 1, 1) | |
GlStateManager.disableLighting() | |
implicit val bb = Tessellator.getInstance().getBuffer | |
bb.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX) | |
val gemOffset = gemIndex * 1f / 4 | |
renderQuad(p1, (0, gemOffset)) | |
renderQuad(p2, (0.5f, gemOffset)) | |
renderQuad(p3, (0.5f, gemOffset)) | |
renderQuad(p4, (0.5f, gemOffset)) | |
renderQuad(p5, (0.5f, gemOffset)) | |
Tessellator.getInstance.draw() | |
GlStateManager.enableLighting() | |
} | |
type Poly = (Vec, Vec, Vec, Vec) | |
type UV = (Float, Float) | |
private def renderQuad(p: Poly, uv: UV)(implicit bb: BufferBuilder): Unit = { | |
val (v1, v2, v3, v4) = p | |
val (u, v) = uv | |
bb.pos(v1.x, v1.y, v1.z).tex(0.5 + u, 0 + v).endVertex() | |
bb.pos(v2.x, v2.y, v2.z).tex(0 + u, 0 + v).endVertex() | |
bb.pos(v3.x, v3.y, v3.z).tex(0 + u, 0.25 + v).endVertex() | |
bb.pos(v4.x, v4.y, v4.z).tex(0.5 + u, 0.25 + v).endVertex() | |
} | |
private def renderQuad1(p: Poly, uv: (UV, UV, UV, UV))(implicit bb: BufferBuilder): Unit = { | |
val (v1, v2, v3, v4) = p | |
val (uv1, uv2, uv3, uv4) = uv | |
val (u_1, v_1) = uv1 | |
val (u_2, v_2) = uv2 | |
val (u_3, v_3) = uv3 | |
val (u_4, v_4) = uv4 | |
bb.pos(v1.x, v1.y, v1.z).tex(u_1, v_1).endVertex() | |
bb.pos(v2.x, v2.y, v2.z).tex(u_2, v_2).endVertex() | |
bb.pos(v3.x, v3.y, v3.z).tex(u_3, v_3).endVertex() | |
bb.pos(v4.x, v4.y, v4.z).tex(u_4, v_4).endVertex() | |
} | |
def renderCarpet(color: EnumDyeColor): Unit = { | |
} | |
override def render(te: TileAssortedEnchantmentTable, x: Double, y: Double, z: Double, partialTicks: Float, destroyStage: Int, alpha: Float): Unit = { | |
if (!te.inv.getStackInSlot(0).isEmpty) { | |
vanilaRender.render(te, x, y, z, partialTicks, destroyStage, alpha) | |
} | |
GlStateManager.pushMatrix() | |
GlStateManager.translate(x.toFloat, y.toFloat, z.toFloat) | |
renderGems(te.gemIndex) | |
te.carpetColor.foreach(renderCarpet) | |
GlStateManager.popMatrix() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment