| 1 | package net.minecraft.client.renderer.block.model;
|
| 2 |
|
| 3 | import net.minecraft.client.renderer.EnumFaceDirection;
|
| 4 | import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
| 5 | import net.minecraft.client.resources.model.ModelRotation;
|
| 6 | import net.minecraft.util.EnumFacing;
|
| 7 | import net.minecraft.util.MathHelper;
|
| 8 | import net.minecraft.util.Vec3i;
|
| 9 | import org.lwjgl.util.vector.Matrix4f;
|
| 10 | import org.lwjgl.util.vector.Vector3f;
|
| 11 | import org.lwjgl.util.vector.Vector4f;
|
| 12 |
|
| 13 | public class FaceBakery
|
| 14 | {
|
| 15 | private static final float SCALE_ROTATION_22_5 = 1.0F / (float)Math.cos(0.39269909262657166D) - 1.0F;
|
| 16 | private static final float SCALE_ROTATION_GENERAL = 1.0F / (float)Math.cos((Math.PI / 4D)) - 1.0F;
|
| 17 |
|
| 18 | public BakedQuad makeBakedQuad(Vector3f posFrom, Vector3f posTo, BlockPartFace face, TextureAtlasSprite sprite, EnumFacing facing, ModelRotation modelRotationIn, BlockPartRotation partRotation, boolean uvLocked, boolean shade)
|
| 19 | {
|
| 20 | int[] aint = this.makeQuadVertexData(face, sprite, facing, this.getPositionsDiv16(posFrom, posTo), modelRotationIn, partRotation, uvLocked, shade);
|
| 21 | EnumFacing enumfacing = getFacingFromVertexData(aint);
|
| 22 |
|
| 23 | if (uvLocked)
|
| 24 | {
|
| 25 | this.lockUv(aint, enumfacing, face.blockFaceUV, sprite);
|
| 26 | }
|
| 27 |
|
| 28 | if (partRotation == null)
|
| 29 | {
|
| 30 | this.applyFacing(aint, enumfacing);
|
| 31 | }
|
| 32 |
|
| 33 | return new BakedQuad(aint, face.tintIndex, enumfacing);
|
| 34 | }
|
| 35 |
|
| 36 | private int[] makeQuadVertexData(BlockPartFace partFace, TextureAtlasSprite sprite, EnumFacing facing, float[] p_178405_4_, ModelRotation modelRotationIn, BlockPartRotation partRotation, boolean uvLocked, boolean shade)
|
| 37 | {
|
| 38 | int[] aint = new int[28];
|
| 39 |
|
| 40 | for (int i = 0; i < 4; ++i)
|
| 41 | {
|
| 42 | this.fillVertexData(aint, i, facing, partFace, p_178405_4_, sprite, modelRotationIn, partRotation, uvLocked, shade);
|
| 43 | }
|
| 44 |
|
| 45 | return aint;
|
| 46 | }
|
| 47 |
|
| 48 | private int getFaceShadeColor(EnumFacing facing)
|
| 49 | {
|
| 50 | float f = this.getFaceBrightness(facing);
|
| 51 | int i = MathHelper.clamp_int((int)(f * 255.0F), 0, 255);
|
| 52 | return -16777216 | i << 16 | i << 8 | i;
|
| 53 | }
|
| 54 |
|
| 55 | private float getFaceBrightness(EnumFacing facing)
|
| 56 | {
|
| 57 | switch (facing)
|
| 58 | {
|
| 59 | case DOWN:
|
| 60 | return 0.5F;
|
| 61 |
|
| 62 | case UP:
|
| 63 | return 1.0F;
|
| 64 |
|
| 65 | case NORTH:
|
| 66 | case SOUTH:
|
| 67 | return 0.8F;
|
| 68 |
|
| 69 | case WEST:
|
| 70 | case EAST:
|
| 71 | return 0.6F;
|
| 72 |
|
| 73 | default:
|
| 74 | return 1.0F;
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | private float[] getPositionsDiv16(Vector3f pos1, Vector3f pos2)
|
| 79 | {
|
| 80 | float[] afloat = new float[EnumFacing.values().length];
|
| 81 | afloat[EnumFaceDirection.Constants.WEST_INDEX] = pos1.x / 16.0F;
|
| 82 | afloat[EnumFaceDirection.Constants.DOWN_INDEX] = pos1.y / 16.0F;
|
| 83 | afloat[EnumFaceDirection.Constants.NORTH_INDEX] = pos1.z / 16.0F;
|
| 84 | afloat[EnumFaceDirection.Constants.EAST_INDEX] = pos2.x / 16.0F;
|
| 85 | afloat[EnumFaceDirection.Constants.UP_INDEX] = pos2.y / 16.0F;
|
| 86 | afloat[EnumFaceDirection.Constants.SOUTH_INDEX] = pos2.z / 16.0F;
|
| 87 | return afloat;
|
| 88 | }
|
| 89 |
|
| 90 | private void fillVertexData(int[] faceData, int vertexIndex, EnumFacing facing, BlockPartFace partFace, float[] p_178402_5_, TextureAtlasSprite sprite, ModelRotation modelRotationIn, BlockPartRotation partRotation, boolean uvLocked, boolean shade)
|
| 91 | {
|
| 92 | EnumFacing enumfacing = modelRotationIn.rotateFace(facing);
|
| 93 | int i = shade ? this.getFaceShadeColor(enumfacing) : -1;
|
| 94 | EnumFaceDirection.VertexInformation enumfacedirection$vertexinformation = EnumFaceDirection.getFacing(facing).getVertexInformation(vertexIndex);
|
| 95 | Vector3f vector3f = new Vector3f(p_178402_5_[enumfacedirection$vertexinformation.xIndex], p_178402_5_[enumfacedirection$vertexinformation.yIndex], p_178402_5_[enumfacedirection$vertexinformation.zIndex]);
|
| 96 | this.rotatePart(vector3f, partRotation);
|
| 97 | int j = this.rotateVertex(vector3f, facing, vertexIndex, modelRotationIn, uvLocked);
|
| 98 | this.storeVertexData(faceData, j, vertexIndex, vector3f, i, sprite, partFace.blockFaceUV);
|
| 99 | }
|
| 100 |
|
| 101 | private void storeVertexData(int[] faceData, int storeIndex, int vertexIndex, Vector3f position, int shadeColor, TextureAtlasSprite sprite, BlockFaceUV faceUV)
|
| 102 | {
|
| 103 | int i = storeIndex * 7;
|
| 104 | faceData[i] = Float.floatToRawIntBits(position.x);
|
| 105 | faceData[i + 1] = Float.floatToRawIntBits(position.y);
|
| 106 | faceData[i + 2] = Float.floatToRawIntBits(position.z);
|
| 107 | faceData[i + 3] = shadeColor;
|
| 108 | faceData[i + 4] = Float.floatToRawIntBits(sprite.getInterpolatedU((double)faceUV.func_178348_a(vertexIndex)));
|
| 109 | faceData[i + 4 + 1] = Float.floatToRawIntBits(sprite.getInterpolatedV((double)faceUV.func_178346_b(vertexIndex)));
|
| 110 | }
|
| 111 |
|
| 112 | private void rotatePart(Vector3f p_178407_1_, BlockPartRotation partRotation)
|
| 113 | {
|
| 114 | if (partRotation != null)
|
| 115 | {
|
| 116 | Matrix4f matrix4f = this.getMatrixIdentity();
|
| 117 | Vector3f vector3f = new Vector3f(0.0F, 0.0F, 0.0F);
|
| 118 |
|
| 119 | switch (partRotation.axis)
|
| 120 | {
|
| 121 | case X:
|
| 122 | Matrix4f.rotate(partRotation.angle * 0.017453292F, new Vector3f(1.0F, 0.0F, 0.0F), matrix4f, matrix4f);
|
| 123 | vector3f.set(0.0F, 1.0F, 1.0F);
|
| 124 | break;
|
| 125 |
|
| 126 | case Y:
|
| 127 | Matrix4f.rotate(partRotation.angle * 0.017453292F, new Vector3f(0.0F, 1.0F, 0.0F), matrix4f, matrix4f);
|
| 128 | vector3f.set(1.0F, 0.0F, 1.0F);
|
| 129 | break;
|
| 130 |
|
| 131 | case Z:
|
| 132 | Matrix4f.rotate(partRotation.angle * 0.017453292F, new Vector3f(0.0F, 0.0F, 1.0F), matrix4f, matrix4f);
|
| 133 | vector3f.set(1.0F, 1.0F, 0.0F);
|
| 134 | }
|
| 135 |
|
| 136 | if (partRotation.rescale)
|
| 137 | {
|
| 138 | if (Math.abs(partRotation.angle) == 22.5F)
|
| 139 | {
|
| 140 | vector3f.scale(SCALE_ROTATION_22_5);
|
| 141 | }
|
| 142 | else
|
| 143 | {
|
| 144 | vector3f.scale(SCALE_ROTATION_GENERAL);
|
| 145 | }
|
| 146 |
|
| 147 | Vector3f.add(vector3f, new Vector3f(1.0F, 1.0F, 1.0F), vector3f);
|
| 148 | }
|
| 149 | else
|
| 150 | {
|
| 151 | vector3f.set(1.0F, 1.0F, 1.0F);
|
| 152 | }
|
| 153 |
|
| 154 | this.rotateScale(p_178407_1_, new Vector3f(partRotation.origin), matrix4f, vector3f);
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | public int rotateVertex(Vector3f position, EnumFacing facing, int vertexIndex, ModelRotation modelRotationIn, boolean uvLocked)
|
| 159 | {
|
| 160 | if (modelRotationIn == ModelRotation.X0_Y0)
|
| 161 | {
|
| 162 | return vertexIndex;
|
| 163 | }
|
| 164 | else
|
| 165 | {
|
| 166 | this.rotateScale(position, new Vector3f(0.5F, 0.5F, 0.5F), modelRotationIn.getMatrix4d(), new Vector3f(1.0F, 1.0F, 1.0F));
|
| 167 | return modelRotationIn.rotateVertex(facing, vertexIndex);
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | private void rotateScale(Vector3f position, Vector3f rotationOrigin, Matrix4f rotationMatrix, Vector3f scale)
|
| 172 | {
|
| 173 | Vector4f vector4f = new Vector4f(position.x - rotationOrigin.x, position.y - rotationOrigin.y, position.z - rotationOrigin.z, 1.0F);
|
| 174 | Matrix4f.transform(rotationMatrix, vector4f, vector4f);
|
| 175 | vector4f.x *= scale.x;
|
| 176 | vector4f.y *= scale.y;
|
| 177 | vector4f.z *= scale.z;
|
| 178 | position.set(vector4f.x + rotationOrigin.x, vector4f.y + rotationOrigin.y, vector4f.z + rotationOrigin.z);
|
| 179 | }
|
| 180 |
|
| 181 | private Matrix4f getMatrixIdentity()
|
| 182 | {
|
| 183 | Matrix4f matrix4f = new Matrix4f();
|
| 184 | matrix4f.setIdentity();
|
| 185 | return matrix4f;
|
| 186 | }
|
| 187 |
|
| 188 | public static EnumFacing getFacingFromVertexData(int[] faceData)
|
| 189 | {
|
| 190 | Vector3f vector3f = new Vector3f(Float.intBitsToFloat(faceData[0]), Float.intBitsToFloat(faceData[1]), Float.intBitsToFloat(faceData[2]));
|
| 191 | Vector3f vector3f1 = new Vector3f(Float.intBitsToFloat(faceData[7]), Float.intBitsToFloat(faceData[8]), Float.intBitsToFloat(faceData[9]));
|
| 192 | Vector3f vector3f2 = new Vector3f(Float.intBitsToFloat(faceData[14]), Float.intBitsToFloat(faceData[15]), Float.intBitsToFloat(faceData[16]));
|
| 193 | Vector3f vector3f3 = new Vector3f();
|
| 194 | Vector3f vector3f4 = new Vector3f();
|
| 195 | Vector3f vector3f5 = new Vector3f();
|
| 196 | Vector3f.sub(vector3f, vector3f1, vector3f3);
|
| 197 | Vector3f.sub(vector3f2, vector3f1, vector3f4);
|
| 198 | Vector3f.cross(vector3f4, vector3f3, vector3f5);
|
| 199 | float f = (float)Math.sqrt((double)(vector3f5.x * vector3f5.x + vector3f5.y * vector3f5.y + vector3f5.z * vector3f5.z));
|
| 200 | vector3f5.x /= f;
|
| 201 | vector3f5.y /= f;
|
| 202 | vector3f5.z /= f;
|
| 203 | EnumFacing enumfacing = null;
|
| 204 | float f1 = 0.0F;
|
| 205 |
|
| 206 | for (EnumFacing enumfacing1 : EnumFacing.values())
|
| 207 | {
|
| 208 | Vec3i vec3i = enumfacing1.getDirectionVec();
|
| 209 | Vector3f vector3f6 = new Vector3f((float)vec3i.getX(), (float)vec3i.getY(), (float)vec3i.getZ());
|
| 210 | float f2 = Vector3f.dot(vector3f5, vector3f6);
|
| 211 |
|
| 212 | if (f2 >= 0.0F && f2 > f1)
|
| 213 | {
|
| 214 | f1 = f2;
|
| 215 | enumfacing = enumfacing1;
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | if (enumfacing == null)
|
| 220 | {
|
| 221 | return EnumFacing.UP;
|
| 222 | }
|
| 223 | else
|
| 224 | {
|
| 225 | return enumfacing;
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | public void lockUv(int[] p_178409_1_, EnumFacing facing, BlockFaceUV p_178409_3_, TextureAtlasSprite p_178409_4_)
|
| 230 | {
|
| 231 | for (int i = 0; i < 4; ++i)
|
| 232 | {
|
| 233 | this.lockVertexUv(i, p_178409_1_, facing, p_178409_3_, p_178409_4_);
|
| 234 | }
|
| 235 | }
|
| 236 |
|
| 237 | private void applyFacing(int[] p_178408_1_, EnumFacing p_178408_2_)
|
| 238 | {
|
| 239 | int[] aint = new int[p_178408_1_.length];
|
| 240 | System.arraycopy(p_178408_1_, 0, aint, 0, p_178408_1_.length);
|
| 241 | float[] afloat = new float[EnumFacing.values().length];
|
| 242 | afloat[EnumFaceDirection.Constants.WEST_INDEX] = 999.0F;
|
| 243 | afloat[EnumFaceDirection.Constants.DOWN_INDEX] = 999.0F;
|
| 244 | afloat[EnumFaceDirection.Constants.NORTH_INDEX] = 999.0F;
|
| 245 | afloat[EnumFaceDirection.Constants.EAST_INDEX] = -999.0F;
|
| 246 | afloat[EnumFaceDirection.Constants.UP_INDEX] = -999.0F;
|
| 247 | afloat[EnumFaceDirection.Constants.SOUTH_INDEX] = -999.0F;
|
| 248 |
|
| 249 | for (int i = 0; i < 4; ++i)
|
| 250 | {
|
| 251 | int j = 7 * i;
|
| 252 | float f = Float.intBitsToFloat(aint[j]);
|
| 253 | float f1 = Float.intBitsToFloat(aint[j + 1]);
|
| 254 | float f2 = Float.intBitsToFloat(aint[j + 2]);
|
| 255 |
|
| 256 | if (f < afloat[EnumFaceDirection.Constants.WEST_INDEX])
|
| 257 | {
|
| 258 | afloat[EnumFaceDirection.Constants.WEST_INDEX] = f;
|
| 259 | }
|
| 260 |
|
| 261 | if (f1 < afloat[EnumFaceDirection.Constants.DOWN_INDEX])
|
| 262 | {
|
| 263 | afloat[EnumFaceDirection.Constants.DOWN_INDEX] = f1;
|
| 264 | }
|
| 265 |
|
| 266 | if (f2 < afloat[EnumFaceDirection.Constants.NORTH_INDEX])
|
| 267 | {
|
| 268 | afloat[EnumFaceDirection.Constants.NORTH_INDEX] = f2;
|
| 269 | }
|
| 270 |
|
| 271 | if (f > afloat[EnumFaceDirection.Constants.EAST_INDEX])
|
| 272 | {
|
| 273 | afloat[EnumFaceDirection.Constants.EAST_INDEX] = f;
|
| 274 | }
|
| 275 |
|
| 276 | if (f1 > afloat[EnumFaceDirection.Constants.UP_INDEX])
|
| 277 | {
|
| 278 | afloat[EnumFaceDirection.Constants.UP_INDEX] = f1;
|
| 279 | }
|
| 280 |
|
| 281 | if (f2 > afloat[EnumFaceDirection.Constants.SOUTH_INDEX])
|
| 282 | {
|
| 283 | afloat[EnumFaceDirection.Constants.SOUTH_INDEX] = f2;
|
| 284 | }
|
| 285 | }
|
| 286 |
|
| 287 | EnumFaceDirection enumfacedirection = EnumFaceDirection.getFacing(p_178408_2_);
|
| 288 |
|
| 289 | for (int i1 = 0; i1 < 4; ++i1)
|
| 290 | {
|
| 291 | int j1 = 7 * i1;
|
| 292 | EnumFaceDirection.VertexInformation enumfacedirection$vertexinformation = enumfacedirection.getVertexInformation(i1);
|
| 293 | float f8 = afloat[enumfacedirection$vertexinformation.xIndex];
|
| 294 | float f3 = afloat[enumfacedirection$vertexinformation.yIndex];
|
| 295 | float f4 = afloat[enumfacedirection$vertexinformation.zIndex];
|
| 296 | p_178408_1_[j1] = Float.floatToRawIntBits(f8);
|
| 297 | p_178408_1_[j1 + 1] = Float.floatToRawIntBits(f3);
|
| 298 | p_178408_1_[j1 + 2] = Float.floatToRawIntBits(f4);
|
| 299 |
|
| 300 | for (int k = 0; k < 4; ++k)
|
| 301 | {
|
| 302 | int l = 7 * k;
|
| 303 | float f5 = Float.intBitsToFloat(aint[l]);
|
| 304 | float f6 = Float.intBitsToFloat(aint[l + 1]);
|
| 305 | float f7 = Float.intBitsToFloat(aint[l + 2]);
|
| 306 |
|
| 307 | if (MathHelper.epsilonEquals(f8, f5) && MathHelper.epsilonEquals(f3, f6) && MathHelper.epsilonEquals(f4, f7))
|
| 308 | {
|
| 309 | p_178408_1_[j1 + 4] = aint[l + 4];
|
| 310 | p_178408_1_[j1 + 4 + 1] = aint[l + 4 + 1];
|
| 311 | }
|
| 312 | }
|
| 313 | }
|
| 314 | }
|
| 315 |
|
| 316 | private void lockVertexUv(int p_178401_1_, int[] p_178401_2_, EnumFacing facing, BlockFaceUV p_178401_4_, TextureAtlasSprite p_178401_5_)
|
| 317 | {
|
| 318 | int i = 7 * p_178401_1_;
|
| 319 | float f = Float.intBitsToFloat(p_178401_2_[i]);
|
| 320 | float f1 = Float.intBitsToFloat(p_178401_2_[i + 1]);
|
| 321 | float f2 = Float.intBitsToFloat(p_178401_2_[i + 2]);
|
| 322 |
|
| 323 | if (f < -0.1F || f >= 1.1F)
|
| 324 | {
|
| 325 | f -= (float)MathHelper.floor_float(f);
|
| 326 | }
|
| 327 |
|
| 328 | if (f1 < -0.1F || f1 >= 1.1F)
|
| 329 | {
|
| 330 | f1 -= (float)MathHelper.floor_float(f1);
|
| 331 | }
|
| 332 |
|
| 333 | if (f2 < -0.1F || f2 >= 1.1F)
|
| 334 | {
|
| 335 | f2 -= (float)MathHelper.floor_float(f2);
|
| 336 | }
|
| 337 |
|
| 338 | float f3 = 0.0F;
|
| 339 | float f4 = 0.0F;
|
| 340 |
|
| 341 | switch (facing)
|
| 342 | {
|
| 343 | case DOWN:
|
| 344 | f3 = f * 16.0F;
|
| 345 | f4 = (1.0F - f2) * 16.0F;
|
| 346 | break;
|
| 347 |
|
| 348 | case UP:
|
| 349 | f3 = f * 16.0F;
|
| 350 | f4 = f2 * 16.0F;
|
| 351 | break;
|
| 352 |
|
| 353 | case NORTH:
|
| 354 | f3 = (1.0F - f) * 16.0F;
|
| 355 | f4 = (1.0F - f1) * 16.0F;
|
| 356 | break;
|
| 357 |
|
| 358 | case SOUTH:
|
| 359 | f3 = f * 16.0F;
|
| 360 | f4 = (1.0F - f1) * 16.0F;
|
| 361 | break;
|
| 362 |
|
| 363 | case WEST:
|
| 364 | f3 = f2 * 16.0F;
|
| 365 | f4 = (1.0F - f1) * 16.0F;
|
| 366 | break;
|
| 367 |
|
| 368 | case EAST:
|
| 369 | f3 = (1.0F - f2) * 16.0F;
|
| 370 | f4 = (1.0F - f1) * 16.0F;
|
| 371 | }
|
| 372 |
|
| 373 | int j = p_178401_4_.func_178345_c(p_178401_1_) * 7;
|
| 374 | p_178401_2_[j + 4] = Float.floatToRawIntBits(p_178401_5_.getInterpolatedU((double)f3));
|
| 375 | p_178401_2_[j + 4 + 1] = Float.floatToRawIntBits(p_178401_5_.getInterpolatedV((double)f4));
|
| 376 | }
|
| 377 | }
|
| 378 |
|