Model transform

This commit is contained in:
paulevsGitch 2021-07-24 21:30:48 +03:00
parent 1ea7221f9f
commit f853ac75d9
2 changed files with 16 additions and 6 deletions

View file

@ -95,12 +95,12 @@ public class OBJBlockModel implements UnbakedModel, BakedModel {
sprites[i] = textureGetter.apply(materials.get(i));
}
quadsBaked.clear();
quadsUnbaked.forEach(quad -> quadsBaked.add(quad.bake(sprites)));
quadsUnbaked.forEach(quad -> quadsBaked.add(quad.bake(sprites, modelState)));
for (Direction dir: BlocksHelper.DIRECTIONS) {
List<UnbakedQuad> unbaked = quadsUnbakedMap.get(dir);
List<BakedQuad> baked = quadsBakedMap.get(dir);
baked.clear();
unbaked.forEach(quad -> baked.add(quad.bake(sprites)));
unbaked.forEach(quad -> baked.add(quad.bake(sprites, modelState)));
}
return this;
}

View file

@ -1,14 +1,18 @@
package ru.bclib.client.models;
import com.mojang.math.Matrix4f;
import com.mojang.math.Vector3f;
import com.mojang.math.Vector4f;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.ModelState;
import net.minecraft.core.Direction;
@Environment(EnvType.CLIENT)
public class UnbakedQuad {
private static final Vector4f POS = new Vector4f();
private float[] data = new float[20]; // 4 points with 3 positions and 2 uvs, 4 * (3 + 2)
private Direction dir = Direction.UP;
private boolean useShading = false;
@ -39,15 +43,21 @@ public class UnbakedQuad {
return result;
}
public BakedQuad bake(TextureAtlasSprite[] sprites) {
public BakedQuad bake(TextureAtlasSprite[] sprites, ModelState modelState) {
Matrix4f matrix = modelState.getRotation().getMatrix();
TextureAtlasSprite sprite = sprites[spriteIndex];
int[] vertexData = new int[32];
for (int i = 0; i < 4; i++) {
int index = i << 3;
int dataIndex = i * 5;
vertexData[index] = Float.floatToIntBits(data[dataIndex++]); // X
vertexData[index | 1] = Float.floatToIntBits(data[dataIndex++]); // Y
vertexData[index | 2] = Float.floatToIntBits(data[dataIndex++]); // Z
float x = data[dataIndex++]; // X
float y = data[dataIndex++]; // Y
float z = data[dataIndex++]; // Z
POS.set(x, y, z, 0);
POS.transform(matrix);
vertexData[index] = Float.floatToIntBits(POS.x()); // X
vertexData[index | 1] = Float.floatToIntBits(POS.y()); // Y
vertexData[index | 2] = Float.floatToIntBits(POS.z()); // Z
vertexData[index | 3] = -1; // Unknown constant
vertexData[index | 4] = Float.floatToIntBits(sprite.getU(data[dataIndex++])); // U
vertexData[index | 5] = Float.floatToIntBits(sprite.getV(data[dataIndex])); // V