use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class ParticleDataSpriteComponent method copyFrom.
@Override
public void copyFrom(ParticleDataSpriteComponent other) {
this.texture = other.texture;
this.textureSize = new Vector2f(other.textureSize);
}
use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class AtlasFormat method process.
private void process(GridDefinition grid, Texture texture, Vector2ic size, Map<Name, SubtextureData> out) {
if (grid.getTileSize() == null) {
logger.error("Bad grid definition - missing mandatory property tileSize");
return;
}
if (grid.getGridDimensions() == null) {
logger.error("Bad grid definition - missing mandatory property gridDimensions");
return;
}
Vector2f offset = new Vector2f(0, 0);
if (grid.getGridOffset() != null) {
offset.set((float) grid.getGridOffset().x / size.x(), (float) grid.getGridOffset().y / size.y());
}
Vector2f tileSize = new Vector2f((float) grid.getTileSize().x / size.x(), (float) grid.getTileSize().y / size.y());
int tileX = 0;
int tileY = 0;
for (String name : grid.getTileNames()) {
if (!name.isEmpty()) {
Vector2f pos = new Vector2f(offset);
pos.x += tileX * tileSize.x;
pos.y += tileY * tileSize.y;
Rectanglef tileLocation = new Rectanglef(offset.x + tileX * tileSize.x, offset.y + tileY * tileSize.y, 0.0f, 0.0f).setSize(tileSize.x, tileSize.y);
out.put(new Name(name), new SubtextureData(texture, shrinkRegion(tileLocation)));
}
tileX++;
if (tileX == grid.getGridDimensions().x) {
tileX = 0;
tileY++;
}
}
}
use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class SphereBuilder method build.
// refrence: https://github.com/caosdoar/spheres/blob/master/src/spheres.cpp
public StandardMeshData build() {
StandardMeshData meshData = new StandardMeshData();
Matrix4f mat = new Matrix4f().setRotationXYZ(0, 0, (float) (Math.PI / 2.0f));
Vector4f pos = new Vector4f();
Vector3f normal = new Vector3f();
Vector2f uv0 = new Vector2f();
Vector3f loc = new Vector3f();
float s = 0.0f;
float t = 1.0f;
float ds = 1.0f / verticalCuts;
float dt = 1.0f / horizontalCuts;
for (int j = 0; j <= horizontalCuts; ++j) {
double polar = (Math.PI * j) / horizontalCuts;
double sp = Math.sin(polar);
double cp = Math.cos(polar);
s = 0.0f;
for (int i = 0; i <= verticalCuts; ++i) {
double azimuth = (2.0 * Math.PI * i) / verticalCuts;
double sa = Math.sin(azimuth);
double ca = Math.cos(azimuth);
if (this.normals) {
normal.set((float) (sp * ca), (float) cp, (float) (sp * sa));
meshData.normal.put(normal);
}
if (this.textured) {
uv0.set(s, t);
meshData.uv0.put(uv0);
}
s += ds;
pos.set((float) ((sp * ca) * radius), (float) (cp * radius), (float) ((sp * sa) * radius), 1.0f);
mat.transform(pos);
loc.set(pos.x, pos.y, pos.z);
meshData.position.put(loc);
}
t -= dt;
}
for (int j = 0; j < horizontalCuts; ++j) {
int aStart = (j * (verticalCuts + 1));
int bStart = (j + 1) * (verticalCuts + 1);
for (int i = 0; i < verticalCuts; ++i) {
int a = aStart + i;
int a1 = aStart + ((i + 1) % (verticalCuts + 1));
int b = bStart + i;
int b1 = bStart + ((i + 1) % (verticalCuts + 1));
meshData.indices.putAll(a, b1, b);
meshData.indices.putAll(a1, b1, a);
}
}
return meshData;
}
use of org.joml.Vector2f 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.joml.Vector2f in project Terasology by MovingBlocks.
the class GLTFAttributeMapping method readVec2FBuffer.
public static void readVec2FBuffer(byte[] buffer, GLTFAccessor accessor, GLTFBufferView bufferView, VertexAttributeBinding<Vector2fc, Vector2f> mapping) {
if (accessor.getComponentType() != GLTFComponentType.FLOAT) {
return;
}
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer, bufferView.getByteOffset() + accessor.getByteOffset(), bufferView.getByteLength() - accessor.getByteOffset());
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
int gap = 0;
if (bufferView.getByteStride() > 0) {
gap = bufferView.getByteStride() - accessor.getComponentType().getByteLength() * accessor.getType().getDimension();
}
Vector2f pos = new Vector2f();
if (byteBuffer.position() < byteBuffer.limit()) {
for (int i = 0; i < accessor.getType().getDimension(); i++) {
pos.setComponent(i, byteBuffer.getFloat());
}
mapping.put(pos);
}
while (byteBuffer.position() < byteBuffer.limit() - gap) {
byteBuffer.position(byteBuffer.position() + gap);
for (int i = 0; i < accessor.getType().getDimension(); i++) {
pos.setComponent(i, byteBuffer.getFloat());
}
mapping.put(pos);
}
}
Aggregations