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;
}
Aggregations