Search in sources :

Example 1 with VertexDefinition

use of com.almuradev.content.model.obj.geometry.VertexDefinition in project Almura by AlmuraDev.

the class OBJBakedModel method getQuads.

@Override
public List<BakedQuad> getQuads(@Nullable final IBlockState state, @Nullable final EnumFacing side, final long rand) {
    if (this.quads == null) {
        this.quads = new LinkedList<>();
        final TRSRTransformation transformation = this.state.apply(Optional.empty()).orElse(null);
        Face particleFace = null;
        TextureAtlasSprite particleAtlasSprite = null;
        for (final Group group : this.model.getGroups()) {
            final MaterialDefinition materialDefinition = group.getMaterialDefinition().orElse(null);
            TextureAtlasSprite diffuseSprite = this.spriteOverride;
            if (diffuseSprite == null) {
                if (materialDefinition != null) {
                    if (materialDefinition.getDiffuseTexture().isPresent()) {
                        final ResourceLocation diffuseLocation = materialDefinition.getDiffuseTexture().orElse(null);
                        if (diffuseLocation != null) {
                            if (diffuseLocation.getResourcePath().endsWith(".png")) {
                                diffuseSprite = this.bakedTextureGetter.apply(new ResourceLocation(diffuseLocation.getResourceDomain(), diffuseLocation.getResourcePath().split("\\.")[0]));
                            } else {
                                diffuseSprite = this.bakedTextureGetter.apply(diffuseLocation);
                            }
                        }
                    }
                }
            }
            if (diffuseSprite == null) {
                diffuseSprite = ModelLoader.White.INSTANCE;
            }
            particleAtlasSprite = diffuseSprite;
            for (final Face face : group.getFaces()) {
                particleFace = face;
                final UnpackedBakedQuad.Builder quadBuilder = new UnpackedBakedQuad.Builder(this.format);
                quadBuilder.setContractUVs(true);
                quadBuilder.setTexture(diffuseSprite);
                VertexNormal normal = null;
                for (final VertexDefinition vertexDef : face.getVertices()) {
                    if (normal == null) {
                        normal = vertexDef.getNormal().orElse(null);
                    }
                    for (int e = 0; e < this.format.getElementCount(); e++) {
                        switch(this.format.getElement(e).getUsage()) {
                            case POSITION:
                                final Vertex vertex = vertexDef.getVertex();
                                if (transformation != null) {
                                    final Matrix4f transform = transformation.getMatrix();
                                    final Vector4f position = new Vector4f(vertex.getX(), vertex.getY(), vertex.getZ(), 1f);
                                    final Vector4f transformed = new Vector4f();
                                    transform.transform(position, transformed);
                                    quadBuilder.put(e, transformed.getX(), transformed.getY(), transformed.getZ());
                                } else {
                                    quadBuilder.put(e, vertex.getX(), vertex.getY(), vertex.getZ());
                                }
                                break;
                            case UV:
                                final float u;
                                final float v;
                                if (this.spriteOverride == null) {
                                    final VertexTextureCoordinate textureCoordinate = vertexDef.getTextureCoordinate().orElse(null);
                                    if (textureCoordinate != null) {
                                        u = textureCoordinate.getU();
                                        v = 1f - textureCoordinate.getV();
                                    } else {
                                        u = 0f;
                                        v = 1f;
                                    }
                                } else {
                                    switch(vertexDef.getIndex()) {
                                        case 1:
                                            u = 0f;
                                            v = 0f;
                                            break;
                                        case 2:
                                            u = 1f;
                                            v = 0f;
                                            break;
                                        case 3:
                                            u = 1f;
                                            v = 1f;
                                            break;
                                        case 4:
                                            u = 0f;
                                            v = 1f;
                                            break;
                                        default:
                                            u = 0f;
                                            v = 0f;
                                    }
                                }
                                quadBuilder.put(e, diffuseSprite.getInterpolatedU(u * 16f), diffuseSprite.getInterpolatedV(v * 16f));
                                break;
                            case NORMAL:
                                if (normal != null) {
                                    quadBuilder.put(e, normal.getX(), normal.getY(), normal.getZ());
                                }
                                break;
                            case COLOR:
                                quadBuilder.put(e, 1f, 1f, 1f, 1f);
                                break;
                            default:
                                quadBuilder.put(e);
                        }
                    }
                }
                if (normal != null) {
                    quadBuilder.setQuadOrientation(EnumFacing.getFacingFromVector(normal.getX(), normal.getY(), normal.getZ()));
                }
                this.quads.add(quadBuilder.build());
            }
        }
        if (particleFace != null && particleAtlasSprite != null) {
            // For now, last face = particle generation
            this.particleDiffuseSprite = this.createParticleSpriteFor(particleFace, particleAtlasSprite);
        }
    }
    return this.quads;
}
Also used : TRSRTransformation(net.minecraftforge.common.model.TRSRTransformation) Group(com.almuradev.content.model.obj.geometry.Group) Vertex(com.almuradev.content.model.obj.geometry.Vertex) UnpackedBakedQuad(net.minecraftforge.client.model.pipeline.UnpackedBakedQuad) IMixinTextureAtlasSprite(com.almuradev.content.mixin.iface.IMixinTextureAtlasSprite) TextureAtlasSprite(net.minecraft.client.renderer.texture.TextureAtlasSprite) VertexNormal(com.almuradev.content.model.obj.geometry.VertexNormal) VertexDefinition(com.almuradev.content.model.obj.geometry.VertexDefinition) VertexTextureCoordinate(com.almuradev.content.model.obj.geometry.VertexTextureCoordinate) Matrix4f(javax.vecmath.Matrix4f) Vector4f(javax.vecmath.Vector4f) MaterialDefinition(com.almuradev.content.model.obj.material.MaterialDefinition) ResourceLocation(net.minecraft.util.ResourceLocation) Face(com.almuradev.content.model.obj.geometry.Face)

Aggregations

IMixinTextureAtlasSprite (com.almuradev.content.mixin.iface.IMixinTextureAtlasSprite)1 Face (com.almuradev.content.model.obj.geometry.Face)1 Group (com.almuradev.content.model.obj.geometry.Group)1 Vertex (com.almuradev.content.model.obj.geometry.Vertex)1 VertexDefinition (com.almuradev.content.model.obj.geometry.VertexDefinition)1 VertexNormal (com.almuradev.content.model.obj.geometry.VertexNormal)1 VertexTextureCoordinate (com.almuradev.content.model.obj.geometry.VertexTextureCoordinate)1 MaterialDefinition (com.almuradev.content.model.obj.material.MaterialDefinition)1 Matrix4f (javax.vecmath.Matrix4f)1 Vector4f (javax.vecmath.Vector4f)1 TextureAtlasSprite (net.minecraft.client.renderer.texture.TextureAtlasSprite)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 UnpackedBakedQuad (net.minecraftforge.client.model.pipeline.UnpackedBakedQuad)1 TRSRTransformation (net.minecraftforge.common.model.TRSRTransformation)1