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