Search in sources :

Example 1 with StandardMeshData

use of org.terasology.engine.rendering.assets.mesh.StandardMeshData in project Terasology by MovingBlocks.

the class IconMeshFactory method generateIconMeshData.

public static MeshData generateIconMeshData(TextureRegion tex, int alphaLimit, boolean withContour, Vector4f colorContour) {
    ByteBuffer buffer = tex.getTexture().getData().getBuffers()[0];
    Rectanglei pixelRegion = tex.getPixelRegion();
    int posX = pixelRegion.minX;
    int posY = pixelRegion.minY;
    int stride = tex.getTexture().getWidth() * 4;
    float textureSize = Math.max(tex.getWidth(), tex.getHeight());
    StandardMeshData mesh = new StandardMeshData();
    Vector2f pos = new Vector2f();
    Color color = new Color();
    for (int y = 0; y < tex.getHeight(); y++) {
        for (int x = 0; x < tex.getWidth(); x++) {
            int r = buffer.get((posY + y) * stride + (posX + x) * 4) & 255;
            int g = buffer.get((posY + y) * stride + (posX + x) * 4 + 1) & 255;
            int b = buffer.get((posY + y) * stride + (posX + x) * 4 + 2) & 255;
            int a = buffer.get((posY + y) * stride + (posX + x) * 4 + 3) & 255;
            if (a > alphaLimit) {
                color.setRed(r).setGreen(g).setBlue(b).setAlpha(a);
                pos.set(2f / textureSize * x - 1f, 2f / textureSize * (tex.getHeight() - y - 1) - 1f);
                addPixel(mesh, pos, 2f / textureSize, color);
                if (withContour) {
                    int newX = 0;
                    int newY = 0;
                    int newA = 0;
                    for (int i = 0; i < 4; i++) {
                        newA = alphaLimit + 1;
                        switch(i) {
                            case 0:
                                // check left
                                if (x > 0) {
                                    newX = x - 1;
                                    newY = y;
                                    newA = buffer.get((posY + newY) * stride + (posX + newX) * 4 + 3) & 255;
                                }
                                break;
                            case 1:
                                // check top
                                if (y > 0) {
                                    newX = x;
                                    newY = y - 1;
                                    newA = buffer.get((posY + newY) * stride + (posX + newX) * 4 + 3) & 255;
                                }
                                break;
                            case 2:
                                // check right
                                if (x < 16) {
                                    newX = x + 1;
                                    newY = y;
                                    newA = buffer.get((posY + newY) * stride + (posX + newX) * 4 + 3) & 255;
                                }
                                break;
                            case 3:
                                // check bottom
                                if (y < 16) {
                                    newX = x;
                                    newY = y + 1;
                                    newA = buffer.get((posY + newY) * stride + (posX + newX) * 4 + 3) & 255;
                                }
                                break;
                            default:
                                break;
                        }
                        if (newA < alphaLimit) {
                            color.setRed(colorContour.x).setGreen(colorContour.y).setBlue(colorContour.z).setAlpha(colorContour.w);
                            addPixel(mesh, pos.set(2f * 0.0625f * newX - 0.5f, 0.125f * (15 - newY) - 1f), 0.125f, color);
                        }
                    }
                }
            }
        }
    }
    return mesh;
}
Also used : StandardMeshData(org.terasology.engine.rendering.assets.mesh.StandardMeshData) Vector2f(org.joml.Vector2f) Color(org.terasology.nui.Color) Rectanglei(org.terasology.joml.geom.Rectanglei) ByteBuffer(java.nio.ByteBuffer)

Example 2 with StandardMeshData

use of org.terasology.engine.rendering.assets.mesh.StandardMeshData in project Terasology by MovingBlocks.

the class BoundingBoxRenderer method initialise.

@Override
public void initialise() {
    material = assetManager.getAsset("engine:white", Material.class).get();
    meshData = new StandardMeshData(DrawingMode.LINES, AllocationType.STREAM);
    mesh = Assets.generateAsset(meshData, Mesh.class);
}
Also used : StandardMeshData(org.terasology.engine.rendering.assets.mesh.StandardMeshData) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh)

Example 3 with StandardMeshData

use of org.terasology.engine.rendering.assets.mesh.StandardMeshData in project Terasology by MovingBlocks.

the class GLTFMeshFormat method load.

@Override
public MeshData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
    try (Reader in = new InputStreamReader(inputs.get(0).openStream())) {
        GLTF gltf = gson.fromJson(in, GLTF.class);
        checkVersionSupported(urn, gltf);
        checkMeshPresent(urn, gltf);
        GLTFMesh gltfMesh = gltf.getMeshes().get(0);
        checkPrimitivePresent(urn, gltfMesh);
        GLTFPrimitive gltfPrimitive = gltfMesh.getPrimitives().get(0);
        List<byte[]> loadedBuffers = loadBinaryBuffers(urn, gltf);
        StandardMeshData meshData = new StandardMeshData();
        for (MeshAttributeSemantic semantic : MeshAttributeSemantic.values()) {
            GLTFAccessor gltfAccessor = getAccessor(semantic, gltfPrimitive, gltf);
            if (gltfAccessor != null && gltfAccessor.getBufferView() != null) {
                GLTFBufferView bufferView = gltf.getBufferViews().get(gltfAccessor.getBufferView());
                switch(semantic) {
                    case Position:
                        GLTFAttributeMapping.readVec3FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.position);
                        break;
                    case Normal:
                        GLTFAttributeMapping.readVec3FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.normal);
                        break;
                    case Texcoord_0:
                        GLTFAttributeMapping.readVec2FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.uv0);
                        break;
                    case Texcoord_1:
                        GLTFAttributeMapping.readVec2FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.uv1);
                        break;
                    case Color_0:
                        GLTFAttributeMapping.readColor4FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.color0);
                        break;
                }
            }
        }
        GLTFAccessor indicesAccessor = getIndicesAccessor(gltfPrimitive, gltf, urn);
        if (indicesAccessor.getBufferView() == null) {
            throw new IOException("Missing buffer view for indices accessor in " + urn);
        }
        GLTFBufferView indicesBuffer = gltf.getBufferViews().get(indicesAccessor.getBufferView());
        checkIndicesBuffer(indicesBuffer);
        TIntArrayList indices = new TIntArrayList();
        readBuffer(loadedBuffers.get(indicesBuffer.getBuffer()), indicesAccessor, indicesBuffer, indices);
        for (int x = 0; x < indices.size(); x++) {
            meshData.indices.put(indices.get(x));
        }
        return meshData;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) StandardMeshData(org.terasology.engine.rendering.assets.mesh.StandardMeshData) GLTFMesh(org.terasology.engine.rendering.gltf.model.GLTFMesh) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) TIntArrayList(gnu.trove.list.array.TIntArrayList) GLTFBufferView(org.terasology.engine.rendering.gltf.model.GLTFBufferView) GLTF(org.terasology.engine.rendering.gltf.model.GLTF) GLTFPrimitive(org.terasology.engine.rendering.gltf.model.GLTFPrimitive) GLTFAccessor(org.terasology.engine.rendering.gltf.model.GLTFAccessor)

Example 4 with StandardMeshData

use of org.terasology.engine.rendering.assets.mesh.StandardMeshData in project Terasology by MovingBlocks.

the class BlockSelectionRenderer method initialize.

private void initialize() {
    StandardMeshData overlayMeshData = new StandardMeshData();
    buildBlockMesh(overlayMeshData, 1.0f, textureRegion, Color.white);
    overlayMesh = Assets.generateAsset(overlayMeshData, Mesh.class);
    StandardMeshData overlayMeshData2 = new StandardMeshData();
    buildBlockMesh(overlayMeshData2, 1.0f, textureRegion, new Color(1.0f, 1.0f, 1.0f, .2f));
    overlayMesh2 = Assets.generateAsset(overlayMeshData2, Mesh.class);
    blockSelectionMat = Assets.getMaterial("engine:prog.blockSelection").get();
}
Also used : StandardMeshData(org.terasology.engine.rendering.assets.mesh.StandardMeshData) Color(org.terasology.nui.Color) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh)

Example 5 with StandardMeshData

use of org.terasology.engine.rendering.assets.mesh.StandardMeshData in project Terasology by MovingBlocks.

the class BlockMeshShapeGenerator method getStandaloneMesh.

@Override
public Mesh getStandaloneMesh() {
    if (mesh == null || mesh.isDisposed()) {
        Block block = getBlock();
        StandardMeshData meshData = new StandardMeshData();
        int nextIndex = 0;
        Vector3f light0 = new Vector3f(1, 1, 1);
        for (BlockPart dir : BlockPart.allParts()) {
            BlockMeshPart part = block.getPrimaryAppearance().getPart(dir);
            if (part != null) {
                for (int i = 0; i < part.size(); i++) {
                    meshData.position.put(part.getVertex(i));
                    meshData.color0.put(Color.white);
                    meshData.normal.put(part.getNormal(i));
                    meshData.uv0.put(part.getTexCoord(i));
                    meshData.light0.put(light0);
                }
                for (int i = 0; i < part.indicesSize(); ++i) {
                    meshData.indices.put(nextIndex + part.getIndex(i));
                }
                if (block.isDoubleSided()) {
                    for (int i = 0; i < part.indicesSize(); i += 3) {
                        meshData.indices.put(nextIndex + part.getIndex(i + 1));
                        meshData.indices.put(nextIndex + part.getIndex(i));
                        meshData.indices.put(nextIndex + part.getIndex(i + 2));
                    }
                }
                nextIndex += part.size();
            }
        }
        mesh = Assets.generateAsset(new ResourceUrn(getBaseUrn(), block.getURI().toString()), meshData, Mesh.class);
    }
    return mesh;
}
Also used : BlockPart(org.terasology.engine.world.block.BlockPart) StandardMeshData(org.terasology.engine.rendering.assets.mesh.StandardMeshData) Vector3f(org.joml.Vector3f) Block(org.terasology.engine.world.block.Block) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) BlockMeshPart(org.terasology.engine.world.block.shapes.BlockMeshPart)

Aggregations

StandardMeshData (org.terasology.engine.rendering.assets.mesh.StandardMeshData)6 Mesh (org.terasology.engine.rendering.assets.mesh.Mesh)4 Vector3f (org.joml.Vector3f)2 Color (org.terasology.nui.Color)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ByteBuffer (java.nio.ByteBuffer)1 Vector2f (org.joml.Vector2f)1 GLTF (org.terasology.engine.rendering.gltf.model.GLTF)1 GLTFAccessor (org.terasology.engine.rendering.gltf.model.GLTFAccessor)1 GLTFBufferView (org.terasology.engine.rendering.gltf.model.GLTFBufferView)1 GLTFMesh (org.terasology.engine.rendering.gltf.model.GLTFMesh)1 GLTFPrimitive (org.terasology.engine.rendering.gltf.model.GLTFPrimitive)1 Block (org.terasology.engine.world.block.Block)1 BlockPart (org.terasology.engine.world.block.BlockPart)1 BlockMeshPart (org.terasology.engine.world.block.shapes.BlockMeshPart)1 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)1 Rectanglei (org.terasology.joml.geom.Rectanglei)1