Search in sources :

Example 1 with Quaternion

use of com.mojang.math.Quaternion 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 2 with Quaternion

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

the class TransformationHelper method slerp.

public static Quaternion slerp(Quaternion v0, Quaternion v1, float t) {
    // From https://en.wikipedia.org/w/index.php?title=Slerp&oldid=928959428
    // License: CC BY-SA 3.0 https://creativecommons.org/licenses/by-sa/3.0/
    // Compute the cosine of the angle between the two vectors.
    // If the dot product is negative, slerp won't take
    // the shorter path. Note that v1 and -v1 are equivalent when
    // the negation is applied to all four components. Fix by
    // reversing one quaternion.
    float dot = v0.i() * v1.i() + v0.j() * v1.j() + v0.k() * v1.k() + v0.r() * v1.r();
    if (dot < 0.0f) {
        v1 = new Quaternion(-v1.i(), -v1.j(), -v1.k(), -v1.r());
        dot = -dot;
    }
    // and normalize the result.
    if (dot > THRESHOLD) {
        float x = Mth.lerp(t, v0.i(), v1.i());
        float y = Mth.lerp(t, v0.j(), v1.j());
        float z = Mth.lerp(t, v0.k(), v1.k());
        float w = Mth.lerp(t, v0.r(), v1.r());
        return new Quaternion(x, y, z, w);
    }
    // Since dot is in range [0, DOT_THRESHOLD], acos is safe
    float angle01 = (float) Math.acos(dot);
    float angle0t = angle01 * t;
    float sin0t = Mth.sin(angle0t);
    float sin01 = Mth.sin(angle01);
    float sin1t = Mth.sin(angle01 - angle0t);
    float s1 = sin0t / sin01;
    float s0 = sin1t / sin01;
    return new Quaternion(s0 * v0.i() + s1 * v1.i(), s0 * v0.j() + s1 * v1.j(), s0 * v0.k() + s1 * v1.k(), s0 * v0.r() + s1 * v1.r());
}
Also used : Quaternion(com.mojang.math.Quaternion)

Aggregations

Quaternion (com.mojang.math.Quaternion)2 Transformation (com.mojang.math.Transformation)1 TransformType (net.minecraft.client.renderer.block.model.ItemTransforms.TransformType)1 TextureAtlasSprite (net.minecraft.client.renderer.texture.TextureAtlasSprite)1 Material (net.minecraft.client.resources.model.Material)1 ModelState (net.minecraft.client.resources.model.ModelState)1