Search in sources :

Example 1 with Transformation

use of com.mojang.math.Transformation in project MinecraftForge by MinecraftForge.

the class OBJModel method makeQuad.

private Pair<BakedQuad, Direction> makeQuad(int[][] indices, int tintIndex, Vector4f colorTint, Vector4f ambientColor, TextureAtlasSprite texture, Transformation transform) {
    boolean needsNormalRecalculation = false;
    for (int[] ints : indices) {
        needsNormalRecalculation |= ints.length < 3;
    }
    Vector3f faceNormal = new Vector3f(0, 0, 0);
    if (needsNormalRecalculation) {
        Vector3f a = positions.get(indices[0][0]);
        Vector3f ab = positions.get(indices[1][0]);
        Vector3f ac = positions.get(indices[2][0]);
        Vector3f abs = ab.copy();
        abs.sub(a);
        Vector3f acs = ac.copy();
        acs.sub(a);
        abs.cross(acs);
        abs.normalize();
        faceNormal = abs;
    }
    Vector4f[] pos = new Vector4f[4];
    Vector3f[] norm = new Vector3f[4];
    BakedQuadBuilder builder = new BakedQuadBuilder(texture);
    builder.setQuadTint(tintIndex);
    Vec2 uv2 = new Vec2(0, 0);
    if (ambientToFullbright) {
        int fakeLight = (int) ((ambientColor.x() + ambientColor.y() + ambientColor.z()) * 15 / 3.0f);
        uv2 = new Vec2((fakeLight << 4) / 32767.0f, (fakeLight << 4) / 32767.0f);
        builder.setApplyDiffuseLighting(fakeLight == 0);
    } else {
        builder.setApplyDiffuseLighting(diffuseLighting);
    }
    boolean hasTransform = !transform.isIdentity();
    // The incoming transform is referenced on the center of the block, but our coords are referenced on the corner
    Transformation transformation = hasTransform ? transform.blockCenterToCorner() : transform;
    for (int i = 0; i < 4; i++) {
        int[] index = indices[Math.min(i, indices.length - 1)];
        Vector3f pos0 = positions.get(index[0]);
        Vector4f position = new Vector4f(pos0);
        Vec2 texCoord = index.length >= 2 && texCoords.size() > 0 ? texCoords.get(index[1]) : DEFAULT_COORDS[i];
        Vector3f norm0 = !needsNormalRecalculation && index.length >= 3 && normals.size() > 0 ? normals.get(index[2]) : faceNormal;
        Vector3f normal = norm0;
        Vector4f color = index.length >= 4 && colors.size() > 0 ? colors.get(index[3]) : COLOR_WHITE;
        if (hasTransform) {
            normal = norm0.copy();
            transformation.transformPosition(position);
            transformation.transformNormal(normal);
        }
        ;
        Vector4f tintedColor = new Vector4f(color.x() * colorTint.x(), color.y() * colorTint.y(), color.z() * colorTint.z(), color.w() * colorTint.w());
        putVertexData(builder, position, texCoord, normal, tintedColor, uv2, texture);
        pos[i] = position;
        norm[i] = normal;
    }
    builder.setQuadOrientation(Direction.getNearest(norm[0].x(), norm[0].y(), norm[0].z()));
    Direction cull = null;
    if (detectCullableFaces) {
        if (// vertex.position.x
        Mth.equal(pos[0].x(), 0) && Mth.equal(pos[1].x(), 0) && Mth.equal(pos[2].x(), 0) && Mth.equal(pos[3].x(), 0) && // vertex.normal.x
        norm[0].x() < 0) {
            cull = Direction.WEST;
        } else if (// vertex.position.x
        Mth.equal(pos[0].x(), 1) && Mth.equal(pos[1].x(), 1) && Mth.equal(pos[2].x(), 1) && Mth.equal(pos[3].x(), 1) && // vertex.normal.x
        norm[0].x() > 0) {
            cull = Direction.EAST;
        } else if (// vertex.position.z
        Mth.equal(pos[0].z(), 0) && Mth.equal(pos[1].z(), 0) && Mth.equal(pos[2].z(), 0) && Mth.equal(pos[3].z(), 0) && // vertex.normal.z
        norm[0].z() < 0) {
            // can never remember
            cull = Direction.NORTH;
        } else if (// vertex.position.z
        Mth.equal(pos[0].z(), 1) && Mth.equal(pos[1].z(), 1) && Mth.equal(pos[2].z(), 1) && Mth.equal(pos[3].z(), 1) && // vertex.normal.z
        norm[0].z() > 0) {
            cull = Direction.SOUTH;
        } else if (// vertex.position.y
        Mth.equal(pos[0].y(), 0) && Mth.equal(pos[1].y(), 0) && Mth.equal(pos[2].y(), 0) && Mth.equal(pos[3].y(), 0) && // vertex.normal.z
        norm[0].y() < 0) {
            // can never remember
            cull = Direction.DOWN;
        } else if (// vertex.position.y
        Mth.equal(pos[0].y(), 1) && Mth.equal(pos[1].y(), 1) && Mth.equal(pos[2].y(), 1) && Mth.equal(pos[3].y(), 1) && // vertex.normal.y
        norm[0].y() > 0) {
            cull = Direction.UP;
        }
    }
    return Pair.of(builder.build(), cull);
}
Also used : Transformation(com.mojang.math.Transformation) Vector4f(com.mojang.math.Vector4f) Vec2(net.minecraft.world.phys.Vec2) Vector3f(com.mojang.math.Vector3f) BakedQuadBuilder(net.minecraftforge.client.model.pipeline.BakedQuadBuilder) Direction(net.minecraft.core.Direction)

Example 2 with Transformation

use of com.mojang.math.Transformation in project MinecraftForge by MinecraftForge.

the class DynamicBucketModel method bake.

@Override
public BakedModel bake(IModelConfiguration owner, ModelBakery bakery, Function<Material, TextureAtlasSprite> spriteGetter, ModelState modelTransform, ItemOverrides overrides, ResourceLocation modelLocation) {
    Material particleLocation = owner.isTexturePresent("particle") ? owner.resolveTexture("particle") : null;
    Material baseLocation = owner.isTexturePresent("base") ? owner.resolveTexture("base") : null;
    Material fluidMaskLocation = owner.isTexturePresent("fluid") ? owner.resolveTexture("fluid") : null;
    Material coverLocation = owner.isTexturePresent("cover") ? owner.resolveTexture("cover") : null;
    ModelState transformsFromModel = owner.getCombinedTransform();
    TextureAtlasSprite fluidSprite = fluid != Fluids.EMPTY ? spriteGetter.apply(ForgeHooksClient.getBlockMaterial(fluid.getAttributes().getStillTexture())) : null;
    TextureAtlasSprite coverSprite = (coverLocation != null && (!coverIsMask || baseLocation != null)) ? spriteGetter.apply(coverLocation) : null;
    ImmutableMap<TransformType, Transformation> transformMap = PerspectiveMapWrapper.getTransforms(new CompositeModelState(transformsFromModel, modelTransform));
    TextureAtlasSprite particleSprite = particleLocation != null ? spriteGetter.apply(particleLocation) : null;
    if (particleSprite == null)
        particleSprite = fluidSprite;
    if (particleSprite == null && !coverIsMask)
        particleSprite = coverSprite;
    // if the fluid is lighter than air, will manipulate the initial state to be rotated 180deg to turn it upside down
    if (flipGas && fluid != Fluids.EMPTY && fluid.getAttributes().isLighterThanAir()) {
        modelTransform = new SimpleModelState(modelTransform.getRotation().blockCornerToCenter().compose(new Transformation(null, new Quaternion(0, 0, 1, 0), null, null)).blockCenterToCorner());
    }
    Transformation transform = modelTransform.getRotation();
    ItemMultiLayerBakedModel.Builder builder = ItemMultiLayerBakedModel.builder(owner, particleSprite, new ContainedFluidOverrideHandler(overrides, bakery, owner, this), transformMap);
    if (baseLocation != null) {
        // build base (insidest)
        builder.addQuads(ItemLayerModel.getLayerRenderType(false), ItemLayerModel.getQuadsForSprites(ImmutableList.of(baseLocation), transform, spriteGetter));
    }
    if (fluidMaskLocation != null && fluidSprite != null) {
        TextureAtlasSprite templateSprite = spriteGetter.apply(fluidMaskLocation);
        if (templateSprite != null) {
            // build liquid layer (inside)
            int luminosity = applyFluidLuminosity ? fluid.getAttributes().getLuminosity() : 0;
            int color = tint ? fluid.getAttributes().getColor() : 0xFFFFFFFF;
            builder.addQuads(ItemLayerModel.getLayerRenderType(luminosity > 0), ItemTextureQuadConverter.convertTexture(transform, templateSprite, fluidSprite, NORTH_Z_FLUID, Direction.NORTH, color, 1, luminosity));
            builder.addQuads(ItemLayerModel.getLayerRenderType(luminosity > 0), ItemTextureQuadConverter.convertTexture(transform, templateSprite, fluidSprite, SOUTH_Z_FLUID, Direction.SOUTH, color, 1, luminosity));
        }
    }
    if (coverIsMask) {
        if (coverSprite != null && baseLocation != null) {
            TextureAtlasSprite baseSprite = spriteGetter.apply(baseLocation);
            builder.addQuads(ItemLayerModel.getLayerRenderType(false), ItemTextureQuadConverter.convertTexture(transform, coverSprite, baseSprite, NORTH_Z_COVER, Direction.NORTH, 0xFFFFFFFF, 2));
            builder.addQuads(ItemLayerModel.getLayerRenderType(false), ItemTextureQuadConverter.convertTexture(transform, coverSprite, baseSprite, SOUTH_Z_COVER, Direction.SOUTH, 0xFFFFFFFF, 2));
        }
    } else {
        if (coverSprite != null) {
            builder.addQuads(ItemLayerModel.getLayerRenderType(false), ItemTextureQuadConverter.genQuad(transform, 0, 0, 16, 16, NORTH_Z_COVER, coverSprite, Direction.NORTH, 0xFFFFFFFF, 2));
            builder.addQuads(ItemLayerModel.getLayerRenderType(false), ItemTextureQuadConverter.genQuad(transform, 0, 0, 16, 16, SOUTH_Z_COVER, coverSprite, Direction.SOUTH, 0xFFFFFFFF, 2));
        }
    }
    builder.setParticle(particleSprite);
    return builder.build();
}
Also used : Transformation(com.mojang.math.Transformation) Quaternion(com.mojang.math.Quaternion) TextureAtlasSprite(net.minecraft.client.renderer.texture.TextureAtlasSprite) ModelState(net.minecraft.client.resources.model.ModelState) Material(net.minecraft.client.resources.model.Material) TransformType(net.minecraft.client.renderer.block.model.ItemTransforms.TransformType)

Example 3 with Transformation

use of com.mojang.math.Transformation in project MinecraftForge by MinecraftForge.

the class ItemLayerModel method bake.

@Override
public BakedModel bake(IModelConfiguration owner, ModelBakery bakery, Function<Material, TextureAtlasSprite> spriteGetter, ModelState modelTransform, ItemOverrides overrides, ResourceLocation modelLocation) {
    ImmutableMap<ItemTransforms.TransformType, Transformation> transformMap = PerspectiveMapWrapper.getTransforms(new CompositeModelState(owner.getCombinedTransform(), modelTransform));
    Transformation transform = modelTransform.getRotation();
    TextureAtlasSprite particle = spriteGetter.apply(owner.isTexturePresent("particle") ? owner.resolveTexture("particle") : textures.get(0));
    ItemMultiLayerBakedModel.Builder builder = ItemMultiLayerBakedModel.builder(owner, particle, overrides, transformMap);
    for (int i = 0; i < textures.size(); i++) {
        TextureAtlasSprite tas = spriteGetter.apply(textures.get(i));
        boolean fullbright = fullbrightLayers.contains(i);
        RenderType rt = getLayerRenderType(fullbright);
        builder.addQuads(rt, getQuadsForSprite(i, tas, transform, fullbright));
    }
    return builder.build();
}
Also used : Transformation(com.mojang.math.Transformation) TextureAtlasSprite(net.minecraft.client.renderer.texture.TextureAtlasSprite) RenderType(net.minecraft.client.renderer.RenderType)

Example 4 with Transformation

use of com.mojang.math.Transformation in project MinecraftForge by MinecraftForge.

the class ModelLoaderRegistry method deserializeTRSR.

private static void deserializeTRSR(JsonDeserializationContext context, EnumMap<ItemTransforms.TransformType, Transformation> transforms, JsonObject transform, String name, ItemTransforms.TransformType itemCameraTransform) {
    if (transform.has(name)) {
        Transformation t = context.deserialize(transform.remove(name), Transformation.class);
        transforms.put(itemCameraTransform, t.blockCenterToCorner());
    }
}
Also used : Transformation(com.mojang.math.Transformation)

Example 5 with Transformation

use of com.mojang.math.Transformation in project MinecraftForge by MinecraftForge.

the class ModelLoaderRegistry method deserializeTransform.

public static Optional<ModelState> deserializeTransform(JsonDeserializationContext context, JsonElement transformData) {
    if (!transformData.isJsonObject()) {
        try {
            Transformation base = context.deserialize(transformData, Transformation.class);
            return Optional.of(new SimpleModelState(ImmutableMap.of(), base.blockCenterToCorner()));
        } catch (JsonParseException e) {
            throw new JsonParseException("transform: expected a string, object or valid base transformation, got: " + transformData);
        }
    } else {
        JsonObject transform = transformData.getAsJsonObject();
        EnumMap<ItemTransforms.TransformType, Transformation> transforms = Maps.newEnumMap(ItemTransforms.TransformType.class);
        deserializeTRSR(context, transforms, transform, "thirdperson", ItemTransforms.TransformType.THIRD_PERSON_RIGHT_HAND);
        deserializeTRSR(context, transforms, transform, "thirdperson_righthand", ItemTransforms.TransformType.THIRD_PERSON_RIGHT_HAND);
        deserializeTRSR(context, transforms, transform, "thirdperson_lefthand", ItemTransforms.TransformType.THIRD_PERSON_LEFT_HAND);
        deserializeTRSR(context, transforms, transform, "firstperson", ItemTransforms.TransformType.FIRST_PERSON_RIGHT_HAND);
        deserializeTRSR(context, transforms, transform, "firstperson_righthand", ItemTransforms.TransformType.FIRST_PERSON_RIGHT_HAND);
        deserializeTRSR(context, transforms, transform, "firstperson_lefthand", ItemTransforms.TransformType.FIRST_PERSON_LEFT_HAND);
        deserializeTRSR(context, transforms, transform, "head", ItemTransforms.TransformType.HEAD);
        deserializeTRSR(context, transforms, transform, "gui", ItemTransforms.TransformType.GUI);
        deserializeTRSR(context, transforms, transform, "ground", ItemTransforms.TransformType.GROUND);
        deserializeTRSR(context, transforms, transform, "fixed", ItemTransforms.TransformType.FIXED);
        int k = transform.entrySet().size();
        if (transform.has("matrix"))
            k--;
        if (transform.has("translation"))
            k--;
        if (transform.has("rotation"))
            k--;
        if (transform.has("scale"))
            k--;
        if (transform.has("post-rotation"))
            k--;
        if (transform.has("origin"))
            k--;
        if (k > 0) {
            throw new JsonParseException("transform: allowed keys: 'thirdperson', 'firstperson', 'gui', 'head', 'matrix', 'translation', 'rotation', 'scale', 'post-rotation', 'origin'");
        }
        Transformation base = Transformation.identity();
        if (!transform.entrySet().isEmpty()) {
            base = context.deserialize(transform, Transformation.class);
        }
        ModelState state = new SimpleModelState(Maps.immutableEnumMap(transforms), base);
        return Optional.of(state);
    }
}
Also used : Transformation(com.mojang.math.Transformation) ModelState(net.minecraft.client.resources.model.ModelState) ItemTransforms(net.minecraft.client.renderer.block.model.ItemTransforms)

Aggregations

Transformation (com.mojang.math.Transformation)5 TextureAtlasSprite (net.minecraft.client.renderer.texture.TextureAtlasSprite)2 ModelState (net.minecraft.client.resources.model.ModelState)2 Quaternion (com.mojang.math.Quaternion)1 Vector3f (com.mojang.math.Vector3f)1 Vector4f (com.mojang.math.Vector4f)1 RenderType (net.minecraft.client.renderer.RenderType)1 ItemTransforms (net.minecraft.client.renderer.block.model.ItemTransforms)1 TransformType (net.minecraft.client.renderer.block.model.ItemTransforms.TransformType)1 Material (net.minecraft.client.resources.model.Material)1 Direction (net.minecraft.core.Direction)1 Vec2 (net.minecraft.world.phys.Vec2)1 BakedQuadBuilder (net.minecraftforge.client.model.pipeline.BakedQuadBuilder)1