No longer bake the ao and lighting data (breaking change)

This commit is contained in:
Blue (Lukas Rieger) 2020-02-11 19:35:35 +01:00
parent 5b6952a6a5
commit db208d4b8f
15 changed files with 639 additions and 334 deletions

View File

@ -0,0 +1,120 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.model;
import com.flowpowered.math.vector.Vector2f;
import com.flowpowered.math.vector.Vector3f;
public class ExtendedFace extends Face {
private float ao1 = 1f, ao2 = 1f, ao3 = 1f; // ao
private float bl1 = 15f, bl2 = 15f, bl3 = 15f; // block-light
private float sl1 = 15f, sl2 = 15f, sl3 = 15f; // sun-light
public ExtendedFace(
Vector3f p1,
Vector3f p2,
Vector3f p3,
Vector2f uv1,
Vector2f uv2,
Vector2f uv3,
int materialIndex
) {
super(p1, p2, p3, uv1, uv2, uv3, materialIndex);
}
public float getAo1() {
return ao1;
}
public void setAo1(float ao1) {
this.ao1 = ao1;
}
public float getAo2() {
return ao2;
}
public void setAo2(float ao2) {
this.ao2 = ao2;
}
public float getAo3() {
return ao3;
}
public void setAo3(float ao3) {
this.ao3 = ao3;
}
public float getBl1() {
return bl1;
}
public void setBl1(float bl1) {
this.bl1 = bl1;
}
public float getBl2() {
return bl2;
}
public void setBl2(float bl2) {
this.bl2 = bl2;
}
public float getBl3() {
return bl3;
}
public void setBl3(float bl3) {
this.bl3 = bl3;
}
public float getSl1() {
return sl1;
}
public void setSl1(float sl1) {
this.sl1 = sl1;
}
public float getSl2() {
return sl2;
}
public void setSl2(float sl2) {
this.sl2 = sl2;
}
public float getSl3() {
return sl3;
}
public void setSl3(float sl3) {
this.sl3 = sl3;
}
}

View File

@ -0,0 +1,63 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.model;
import de.bluecolored.bluemap.core.threejs.BufferAttribute;
import de.bluecolored.bluemap.core.threejs.BufferGeometry;
public class ExtendedModel extends Model<ExtendedFace> {
@Override
public BufferGeometry toBufferGeometry() {
BufferGeometry geo = super.toBufferGeometry();
int count = getFaces().size();
float[] ao = new float[count * 3];
float[] blockLight = new float[count * 3];
float[] sunLight = new float[count * 3];
for (int itemIndex = 0; itemIndex < count; itemIndex++){
ExtendedFace f = getFaces().get(itemIndex);
ao[itemIndex * 3 + 0] = f.getAo1();
ao[itemIndex * 3 + 1] = f.getAo2();
ao[itemIndex * 3 + 2] = f.getAo3();
blockLight[itemIndex * 3 + 0] = f.getBl1();
blockLight[itemIndex * 3 + 1] = f.getBl2();
blockLight[itemIndex * 3 + 2] = f.getBl3();
sunLight[itemIndex * 3 + 0] = f.getSl1();
sunLight[itemIndex * 3 + 1] = f.getSl2();
sunLight[itemIndex * 3 + 2] = f.getSl3();
}
geo.addAttribute("ao", new BufferAttribute(ao, 1));
geo.addAttribute("blocklight", new BufferAttribute(blockLight, 1));
geo.addAttribute("sunlight", new BufferAttribute(sunLight, 1));
return geo;
}
}

View File

@ -32,38 +32,38 @@ import com.flowpowered.math.vector.Vector3f;
import de.bluecolored.bluemap.core.util.MathUtils;
public class Face {
private Vector3f p1, p2, p3;
private Vector3f n1, n2, n3;
private Vector3f c1, c2, c3;
private Vector2f uv1, uv2, uv3;
private Vector3f p1, p2, p3; // points
private Vector3f n1, n2, n3; // normals
private Vector3f c1, c2, c3; // vertex-colors
private Vector2f uv1, uv2, uv3; // texture UV
private int materialIndex;
private boolean normalizedNormals;
public Face(Vector3f p1, Vector3f p2, Vector3f p3, Vector2f uv1, Vector2f uv2, Vector2f uv3, int materialIndex) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.uv1 = uv1;
this.uv2 = uv2;
this.uv3 = uv3;
this.materialIndex = materialIndex;
Vector3f faceNormal = getFaceNormal();
this.n1 = faceNormal;
this.n2 = faceNormal;
this.n3 = faceNormal;
this.normalizedNormals = true;
Vector3f color = Vector3f.ONE;
this.c1 = color;
this.c2 = color;
this.c3 = color;
}
public void rotate(Quaternionf rotation){
public void rotate(Quaternionf rotation) {
p1 = rotation.rotate(p1);
p2 = rotation.rotate(p2);
p3 = rotation.rotate(p3);
@ -72,25 +72,25 @@ public class Face {
n2 = rotation.rotate(n2);
n3 = rotation.rotate(n3);
}
public void transform(Matrix3f transformation){
public void transform(Matrix3f transformation) {
p1 = transformation.transform(p1);
p2 = transformation.transform(p2);
p3 = transformation.transform(p3);
n1 = transformation.transform(n1);
n2 = transformation.transform(n2);
n3 = transformation.transform(n3);
normalizedNormals = false;
}
public void translate(Vector3f translation){
public void translate(Vector3f translation) {
p1 = translation.add(p1);
p2 = translation.add(p2);
p3 = translation.add(p3);
}
public Vector3f getP1() {
return p1;
}
@ -201,18 +201,18 @@ public class Face {
this.materialIndex = materialIndex;
}
public Vector3f getFaceNormal(){
public Vector3f getFaceNormal() {
return MathUtils.getSurfaceNormal(p1, p2, p3);
}
private void normlizeNormals(){
private void normlizeNormals() {
if (normalizedNormals) return;
n1 = n1.normalize();
n2 = n2.normalize();
n3 = n3.normalize();
normalizedNormals = true;
}
}

View File

@ -25,7 +25,6 @@
package de.bluecolored.bluemap.core.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.flowpowered.math.imaginary.Quaternionf;
@ -34,10 +33,11 @@ import com.flowpowered.math.vector.Vector2f;
import com.flowpowered.math.vector.Vector3f;
import de.bluecolored.bluemap.core.threejs.BufferGeometry;
import de.bluecolored.bluemap.core.threejs.MaterialGroup;
public class Model {
public class Model<T extends Face> {
private List<Face> faces;
private List<T> faces;
public Model() {
this.faces = new ArrayList<>();
@ -47,19 +47,19 @@ public class Model {
* Merges the given Model into this model<br>
* Faces are not cloned: So changes to the faces of the previous model will mirror in this model, but adding and removing faces will not.
*/
public void merge(Model model){
public void merge(Model<T> model){
faces.addAll(model.getFaces());
}
public void addFace(Face face){
public void addFace(T face){
faces.add(face);
}
public void removeFace(Face face){
public void removeFace(T face){
faces.remove(face);
}
public Collection<Face> getFaces(){
public List<T> getFaces(){
return faces;
}
@ -70,13 +70,13 @@ public class Model {
}
public void transform(Matrix3f transformation){
for (Face f : faces){
for (T f : faces){
f.transform(transformation);
}
}
public void translate(Vector3f translation){
for (Face f : faces){
for (T f : faces){
f.translate(translation);
}
}
@ -89,7 +89,7 @@ public class Model {
//reorganize all faces into arrays and create material-groups
int count = faces.size();
List<BufferGeometry.MaterialGroup> groups = new ArrayList<>();
List<MaterialGroup> groups = new ArrayList<>();
int groupStart = 0;
int currentMaterialIndex = -1;
if (count > 0) currentMaterialIndex = faces.get(0).getMaterialIndex();
@ -100,10 +100,10 @@ public class Model {
float[] uv = new float[count * 2 * 3];
for (int itemIndex = 0; itemIndex < count; itemIndex++){
Face f = faces.get(itemIndex);
T f = faces.get(itemIndex);
if (currentMaterialIndex != f.getMaterialIndex()){
groups.add(new BufferGeometry.MaterialGroup(currentMaterialIndex, groupStart * 3, (itemIndex - groupStart) * 3));
groups.add(new MaterialGroup(currentMaterialIndex, groupStart * 3, (itemIndex - groupStart) * 3));
groupStart = itemIndex;
currentMaterialIndex = f.getMaterialIndex();
}
@ -124,24 +124,24 @@ public class Model {
addVector2fToArray( uv, f.getUv3(), (itemIndex * 3 + 2) * 2 );
}
groups.add(new BufferGeometry.MaterialGroup(currentMaterialIndex, groupStart * 3, (count - groupStart) * 3));
groups.add(new MaterialGroup(currentMaterialIndex, groupStart * 3, (count - groupStart) * 3));
return new BufferGeometry(
position,
normal,
color,
uv,
groups.toArray(new BufferGeometry.MaterialGroup[groups.size()])
groups.toArray(new MaterialGroup[groups.size()])
);
}
private void addVector3fToArray(float[] array, Vector3f v, int startIndex){
static void addVector3fToArray(float[] array, Vector3f v, int startIndex){
array[startIndex] = v.getX();
array[startIndex + 1] = v.getY();
array[startIndex + 2] = v.getZ();
}
private void addVector2fToArray(float[] array, Vector2f v, int startIndex){
static void addVector2fToArray(float[] array, Vector2f v, int startIndex){
array[startIndex] = v.getX();
array[startIndex + 1] = v.getY();
}

View File

@ -30,12 +30,12 @@ import com.flowpowered.math.vector.Vector2i;
import com.flowpowered.math.vector.Vector3i;
import com.flowpowered.math.vector.Vector4f;
import de.bluecolored.bluemap.core.model.Model;
import de.bluecolored.bluemap.core.model.ExtendedModel;
/**
* A model, containing additional information about the tile it represents
*/
public class HiresModel extends Model {
public class HiresModel extends ExtendedModel {
private UUID world;
private Vector2i tile;

View File

@ -28,7 +28,6 @@ import com.flowpowered.math.vector.Vector3f;
import com.flowpowered.math.vector.Vector3i;
import com.flowpowered.math.vector.Vector4f;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.render.RenderSettings;
import de.bluecolored.bluemap.core.render.WorldTile;
import de.bluecolored.bluemap.core.render.hires.blockmodel.BlockStateModel;

View File

@ -26,13 +26,15 @@ package de.bluecolored.bluemap.core.render.hires.blockmodel;
import com.flowpowered.math.vector.Vector4f;
import de.bluecolored.bluemap.core.model.ExtendedFace;
import de.bluecolored.bluemap.core.model.ExtendedModel;
import de.bluecolored.bluemap.core.model.Model;
import de.bluecolored.bluemap.core.util.MathUtils;
/**
* A model with some extra information about the BlockState it represents
*/
public class BlockStateModel extends Model {
public class BlockStateModel extends ExtendedModel {
private Vector4f mapColor;
@ -45,7 +47,7 @@ public class BlockStateModel extends Model {
}
@Override
public void merge(Model model) {
public void merge(Model<ExtendedFace> model) {
super.merge(model);
if (model instanceof BlockStateModel){

View File

@ -32,8 +32,8 @@ import com.flowpowered.math.vector.Vector3f;
import com.flowpowered.math.vector.Vector4f;
import com.google.common.collect.Sets;
import de.bluecolored.bluemap.core.model.Face;
import de.bluecolored.bluemap.core.model.Model;
import de.bluecolored.bluemap.core.model.ExtendedFace;
import de.bluecolored.bluemap.core.model.ExtendedModel;
import de.bluecolored.bluemap.core.render.RenderSettings;
import de.bluecolored.bluemap.core.resourcepack.BlockColorCalculator;
import de.bluecolored.bluemap.core.resourcepack.BlockModelResource;
@ -190,7 +190,7 @@ public class LiquidModelBuilder {
return 0;
}
private void createElementFace(Model model, Direction faceDir, Vector3f c0, Vector3f c1, Vector3f c2, Vector3f c3, Vector3f color, int textureId) {
private void createElementFace(ExtendedModel model, Direction faceDir, Vector3f c0, Vector3f c1, Vector3f c2, Vector3f c3, Vector3f color, int textureId) {
//face culling
Block bl = block.getRelativeBlock(faceDir);
@ -206,33 +206,37 @@ public class LiquidModelBuilder {
uvs[2] = new Vector2f(uv.getZ(), uv.getY());
uvs[3] = new Vector2f(uv.getX(), uv.getY());
Face f1 = new Face(c0, c1, c2, uvs[0], uvs[1], uvs[2], textureId);
Face f2 = new Face(c0, c2, c3, uvs[0], uvs[2], uvs[3], textureId);
ExtendedFace f1 = new ExtendedFace(c0, c1, c2, uvs[0], uvs[1], uvs[2], textureId);
ExtendedFace f2 = new ExtendedFace(c0, c2, c3, uvs[0], uvs[2], uvs[3], textureId);
//move face in a tiny bit to avoid z-fighting with waterlogged blocks
f1.translate(faceDir.opposite().toVector().toFloat().mul(0.01));
f2.translate(faceDir.opposite().toVector().toFloat().mul(0.01));
float light = 1f;
if (renderSettings.getLightShadeMultiplier() > 0) {
light = 0f;
for (Direction d : Direction.values()){
Block b = block.getRelativeBlock(d.toVector());
float l = (float) (Math.max(b.getBlockLightLevel(), b.getSunLightLevel()) / 15f) * renderSettings.getLightShadeMultiplier() + (1 - renderSettings.getLightShadeMultiplier());
if (l > light) light = l;
}
}
color = color.mul(light);
float blockLight = bl.getBlockLightLevel();
float sunLight = bl.getSunLightLevel();
f1.setC1(color);
f1.setC2(color);
f1.setC3(color);
f2.setC1(color);
f2.setC2(color);
f2.setC3(color);
f1.setBl1(blockLight);
f1.setBl2(blockLight);
f1.setBl3(blockLight);
f2.setBl1(blockLight);
f2.setBl2(blockLight);
f2.setBl3(blockLight);
f1.setSl1(sunLight);
f1.setSl2(sunLight);
f1.setSl3(sunLight);
f2.setSl1(sunLight);
f2.setSl2(sunLight);
f2.setSl3(sunLight);
//add the face
model.addFace(f1);
model.addFace(f2);

View File

@ -33,7 +33,7 @@ import com.flowpowered.math.vector.Vector3f;
import com.flowpowered.math.vector.Vector3i;
import com.flowpowered.math.vector.Vector4f;
import de.bluecolored.bluemap.core.model.Face;
import de.bluecolored.bluemap.core.model.ExtendedFace;
import de.bluecolored.bluemap.core.render.RenderSettings;
import de.bluecolored.bluemap.core.resourcepack.BlockColorCalculator;
import de.bluecolored.bluemap.core.resourcepack.BlockModelResource;
@ -154,18 +154,12 @@ public class ResourceModelBuilder {
//light calculation
Block facedBlockNeighbor = getRotationRelativeBlock(modelResource.getRotation(), faceDir);
float skyLight = facedBlockNeighbor.getPassedSunLight();
float sunLight = facedBlockNeighbor.getPassedSunLight();
//filter out faces that are not skylighted
if (skyLight == 0f && renderSettings.isExcludeFacesWithoutSunlight()) return;
//filter out faces that are not sunlighted
if (sunLight == 0f && renderSettings.isExcludeFacesWithoutSunlight()) return;
float light = 1f;
if (renderSettings.getLightShadeMultiplier() > 0) {
float blockLight = facedBlockNeighbor.getPassedBlockLight();
light = (Math.max(skyLight, blockLight) / 15f) * renderSettings.getLightShadeMultiplier() + (1 - renderSettings.getLightShadeMultiplier());
if (light > 1) light = 1;
if (light < 0) light = 0;
}
float blockLight = facedBlockNeighbor.getPassedBlockLight();
//UV
Vector4f uv = face.getUv().toFloat().div(16);
@ -195,41 +189,51 @@ public class ResourceModelBuilder {
Texture texture = face.getTexture();
int textureId = texture.getId();
Face f1 = new Face(c0, c1, c2, uvs[0], uvs[1], uvs[2], textureId);
Face f2 = new Face(c0, c2, c3, uvs[0], uvs[2], uvs[3], textureId);
//calculate ao
double ao0 = 1d, ao1 = 1d, ao2 = 1d, ao3 = 1d;
if (renderSettings.getAmbientOcclusionStrenght() > 0f && modelResource.getModel().isAmbientOcclusion()){
ao0 = testAo(modelResource.getRotation(), c0, faceDir);
ao1 = testAo(modelResource.getRotation(), c1, faceDir);
ao2 = testAo(modelResource.getRotation(), c2, faceDir);
ao3 = testAo(modelResource.getRotation(), c3, faceDir);
}
ExtendedFace f1 = new ExtendedFace(c0, c1, c2, uvs[0], uvs[1], uvs[2], textureId);
ExtendedFace f2 = new ExtendedFace(c0, c2, c3, uvs[0], uvs[2], uvs[3], textureId);
//tint the face
Vector3f color = Vector3f.ONE;
if (face.isTinted()){
color = tintColor.getValue();
}
color = color.mul(light);
Vector3f aoColor;
f1.setC1(color);
f1.setC2(color);
f1.setC3(color);
f2.setC1(color);
f2.setC2(color);
f2.setC3(color);
aoColor = color.mul(ao0);
f1.setC1(aoColor);
f2.setC1(aoColor);
f1.setBl1(blockLight);
f1.setBl2(blockLight);
f1.setBl3(blockLight);
f2.setBl1(blockLight);
f2.setBl2(blockLight);
f2.setBl3(blockLight);
aoColor = color.mul(ao1);
f1.setC2(aoColor);
f1.setSl1(sunLight);
f1.setSl2(sunLight);
f1.setSl3(sunLight);
f2.setSl1(sunLight);
f2.setSl2(sunLight);
f2.setSl3(sunLight);
aoColor = color.mul(ao2);
f1.setC3(aoColor);
f2.setC2(aoColor);
//calculate ao
float ao0 = 1f, ao1 = 1f, ao2 = 1f, ao3 = 1f;
if (modelResource.getModel().isAmbientOcclusion()){
ao0 = testAo(modelResource.getRotation(), c0, faceDir);
ao1 = testAo(modelResource.getRotation(), c1, faceDir);
ao2 = testAo(modelResource.getRotation(), c2, faceDir);
ao3 = testAo(modelResource.getRotation(), c3, faceDir);
}
aoColor = color.mul(ao3);
f2.setC3(aoColor);
f1.setAo1(ao0);
f1.setAo2(ao1);
f1.setAo3(ao2);
f2.setAo1(ao0);
f2.setAo2(ao2);
f2.setAo3(ao3);
//add the face
model.addFace(f1);
@ -275,7 +279,7 @@ public class ResourceModelBuilder {
return dir;
}
private double testAo(Vector2f modelRotation, Vector3f vertex, Direction dir){
private float testAo(Vector2f modelRotation, Vector3f vertex, Direction dir){
int occluding = 0;
int x = 0;
@ -322,7 +326,7 @@ public class ResourceModelBuilder {
if (occluding > 3)
occluding = 3;
return Math.max(0.0, Math.min(1.0 - ((double) occluding * renderSettings.getAmbientOcclusionStrenght()), 1.0));
return Math.max(0f, Math.min(1f - (occluding * renderSettings.getAmbientOcclusionStrenght()), 1f));
}
private Vector2f[] rotateUVInner(Vector2f[] uv, int angle){

View File

@ -140,35 +140,39 @@ public class LowresModel {
Map<Vector2i, LowresPoint> points = changes;
changes = new HashMap<>();
int vertexCount = model.position.length / 3;
float[] position = model.attributes.get("position").values();
float[] color = model.attributes.get("color").values();
float[] normal = model.attributes.get("normal").values();
int vertexCount = Math.floorDiv(position.length, 3);
for (int i = 0; i < vertexCount; i++){
int j = i * 3;
int px = Math.round(model.position[j + 0]);
int pz = Math.round(model.position[j + 2]);
int px = Math.round(position[j + 0]);
int pz = Math.round(position[j + 2]);
Vector2i p = new Vector2i(px, pz);
LowresPoint lrp = points.get(p);
if (lrp == null) continue;
model.position[j + 1] = lrp.height;
position[j + 1] = lrp.height;
model.color[j + 0] = lrp.color.getX();
model.color[j + 1] = lrp.color.getY();
model.color[j + 2] = lrp.color.getZ();
color[j + 0] = lrp.color.getX();
color[j + 1] = lrp.color.getY();
color[j + 2] = lrp.color.getZ();
//recalculate normals
int f = Math.floorDiv(i, 3) * 3 * 3;
Vector3f p1 = new Vector3f(model.position[f + 0], model.position[f + 1], model.position[f + 2]);
Vector3f p2 = new Vector3f(model.position[f + 3], model.position[f + 4], model.position[f + 5]);
Vector3f p3 = new Vector3f(model.position[f + 6], model.position[f + 7], model.position[f + 8]);
Vector3f p1 = new Vector3f(position[f + 0], position[f + 1], position[f + 2]);
Vector3f p2 = new Vector3f(position[f + 3], position[f + 4], position[f + 5]);
Vector3f p3 = new Vector3f(position[f + 6], position[f + 7], position[f + 8]);
Vector3f n = MathUtils.getSurfaceNormal(p1, p2, p3);
model.normal[f + 0] = n.getX(); model.normal[f + 1] = n.getY(); model.normal[f + 2] = n.getZ();
model.normal[f + 3] = n.getX(); model.normal[f + 4] = n.getY(); model.normal[f + 5] = n.getZ();
model.normal[f + 6] = n.getX(); model.normal[f + 7] = n.getY(); model.normal[f + 8] = n.getZ();
normal[f + 0] = n.getX(); normal[f + 1] = n.getY(); normal[f + 2] = n.getZ();
normal[f + 3] = n.getX(); normal[f + 4] = n.getY(); normal[f + 5] = n.getZ();
normal[f + 6] = n.getX(); normal[f + 7] = n.getY(); normal[f + 8] = n.getZ();
}
}
}

View File

@ -0,0 +1,148 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
*
*/
package de.bluecolored.bluemap.core.threejs;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.flowpowered.math.GenericMath;
import com.google.common.base.Preconditions;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
/**
* Represents a ThreeJS BufferAttribute
*/
public class BufferAttribute {
private int itemSize;
private boolean normalized;
private float[] values;
/**
* Creates a new {@link BufferAttribute} with the defined item-size
*/
public BufferAttribute(float[] values, int itemSize) {
Preconditions.checkArgument(values.length % itemSize == 0, "The length of the values-array is not a multiple of the item-size!");
this.values = values;
this.itemSize = itemSize;
this.normalized = false;
}
/**
* Creates a new {@link BufferAttribute} with the defined item-size and the
* defined threejs "normalized" attribute
*/
public BufferAttribute(float[] values, int itemSize, boolean normalized) {
Preconditions.checkArgument(values.length % itemSize == 0, "The length of the values-array is not a multiple of the item-size!");
this.values = values;
this.itemSize = itemSize;
this.normalized = normalized;
}
public void writeJson(JsonWriter json) throws IOException {
json.beginObject();
json.name("type").value("Float32Array");
json.name("itemSize").value(itemSize);
json.name("normalized").value(normalized);
json.name("array").beginArray();
for (int i = 0; i < values.length; i++) {
// rounding and remove ".0" to save string space
double d = GenericMath.round(values[i], 4);
if (d == (int) d) json.value((int) d);
else json.value(d);
}
json.endArray();
json.endObject();
}
public int getItemSize() {
return this.itemSize;
}
public boolean isNormalized() {
return this.normalized;
}
public int getValueCount() {
return this.values.length;
}
public int getItemCount() {
return Math.floorDiv(getValueCount(), getItemSize());
}
public float[] values() {
return values;
}
public static BufferAttribute readJson(JsonReader json) throws IOException {
List<Float> list = new ArrayList<>(1000);
int itemSize = 1;
boolean normalized = false;
json.beginObject(); //root
while (json.hasNext()){
String name = json.nextName();
if(name.equals("array")){
json.beginArray(); //array
while (json.hasNext()){
list.add(new Float(json.nextDouble()));
}
json.endArray(); //array
}
else if (name.equals("itemSize")) {
itemSize = json.nextInt();
}
else if (name.equals("normalized")) {
normalized = json.nextBoolean();
}
else json.skipValue();
}
json.endObject(); //root
//collect values in array
float[] values = new float[list.size()];
for (int i = 0; i < values.length; i++) {
values[i] = list.get(i);
}
return new BufferAttribute(values, itemSize, normalized);
}
}

View File

@ -28,10 +28,12 @@ import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import com.flowpowered.math.GenericMath;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
@ -39,274 +41,169 @@ import com.google.gson.stream.JsonWriter;
public class BufferGeometry {
public final float[] position, normal, color, uv;
public final MaterialGroup[] groups;
public Map<String, BufferAttribute> attributes;
public MaterialGroup[] groups;
private BufferGeometry() {}
public BufferGeometry(float[] position, float[] normal, float[] color, float[] uv, MaterialGroup[] groups) {
this.position = position;
this.normal = normal;
this.color = color;
this.uv = uv;
this.attributes = new HashMap<>();
addAttribute("position", new BufferAttribute(position, 3));
addAttribute("normal", new BufferAttribute(normal, 3));
addAttribute("color", new BufferAttribute(color, 3));
addAttribute("uv", new BufferAttribute(uv, 2));
this.groups = groups;
}
public int getFaceCount(){
return Math.floorDiv(position.length, 3);
public void addAttribute(String name, BufferAttribute attribute) {
this.attributes.put(name, attribute);
}
public boolean isValid() {
int faceCount = getFaceCount();
for (BufferAttribute attribute : attributes.values()) {
if (attribute.getItemCount() != faceCount) return false;
}
return true;
}
public int getFaceCount() {
if (attributes.isEmpty()) return 0;
BufferAttribute attribute = attributes.values().iterator().next();
return attribute.getItemCount();
}
public String toJson() {
try {
StringWriter sw = new StringWriter();
Gson gson = new GsonBuilder().create();
JsonWriter json = gson.newJsonWriter(sw);
json.beginObject();
//set special values
json.beginObject(); // main-object
// set special values
json.name("type").value("BufferGeometry");
json.name("uuid").value(UUID.randomUUID().toString().toUpperCase());
json.name("data").beginObject();
json.name("attributes").beginObject();
json.name("position");
floatArray2Json(json, position, 3, false);
json.name("normal");
floatArray2Json(json, normal, 3, true);
json.name("color");
floatArray2Json(json, color, 3, false);
json.name("uv");
floatArray2Json(json, uv, 2, false);
json.endObject(); //attributes
json.name("groups").beginArray();
//write groups into json
for (BufferGeometry.MaterialGroup g : groups){
json.name("data").beginObject(); // data
json.name("attributes").beginObject(); // attributes
for (Entry<String, BufferAttribute> entry : attributes.entrySet()) {
json.name(entry.getKey());
entry.getValue().writeJson(json);
}
json.endObject(); // attributes
json.name("groups").beginArray(); // groups
// write groups into json
for (MaterialGroup g : groups) {
json.beginObject();
json.name("materialIndex").value(g.getMaterialIndex());
json.name("start").value(g.getStart());
json.name("count").value(g.getCount());
json.endObject();
}
json.endArray(); //groups
json.endObject(); //data
json.endObject(); //main-object
//save and return
json.endArray(); // groups
json.endObject(); // data
json.endObject(); // main-object
// save and return
json.flush();
return sw.toString();
} catch (IOException e){
//since we are using a StringWriter there should never be an IO exception thrown
} catch (IOException e) {
// since we are using a StringWriter there should never be an IO exception
// thrown
throw new RuntimeException(e);
}
}
public static BufferGeometry fromJson(String jsonString) throws IOException {
Gson gson = new GsonBuilder().create();
JsonReader json = gson.newJsonReader(new StringReader(jsonString));
List<Float> positionList = new ArrayList<>(300);
List<Float> normalList = new ArrayList<>(300);
List<Float> colorList = new ArrayList<>(300);
List<Float> uvList = new ArrayList<>(200);
List<MaterialGroup> groups = new ArrayList<>(10);
json.beginObject(); //root
while (json.hasNext()){
Map<String, BufferAttribute> attributes = new HashMap<>();
json.beginObject(); // root
while (json.hasNext()) {
String name1 = json.nextName();
if(name1.equals("data")){
json.beginObject(); //data
while (json.hasNext()){
if (name1.equals("data")) {
json.beginObject(); // data
while (json.hasNext()) {
String name2 = json.nextName();
if(name2.equals("attributes")){
json.beginObject(); //attributes
while (json.hasNext()){
if (name2.equals("attributes")) {
json.beginObject(); // attributes
while (json.hasNext()) {
String name3 = json.nextName();
if(name3.equals("position")){
json2FloatList(json, positionList);
}
else if(name3.equals("normal")){
json2FloatList(json, normalList);
}
else if(name3.equals("color")){
json2FloatList(json, colorList);
}
else if(name3.equals("uv")){
json2FloatList(json, uvList);
}
else json.skipValue();
attributes.put(name3, BufferAttribute.readJson(json));
}
json.endObject(); //attributes
json.endObject(); // attributes
}
else if (name2.equals("groups")){
json.beginArray(); //groups
while (json.hasNext()){
else if (name2.equals("groups")) {
json.beginArray(); // groups
while (json.hasNext()) {
MaterialGroup group = new MaterialGroup(0, 0, 0);
json.beginObject(); //group
while (json.hasNext()){
json.beginObject(); // group
while (json.hasNext()) {
String name3 = json.nextName();
if(name3.equals("materialIndex")){
if (name3.equals("materialIndex")) {
group.setMaterialIndex(json.nextInt());
}
else if(name3.equals("start")){
else if (name3.equals("start")) {
group.setStart(json.nextInt());
}
else if(name3.equals("count")){
else if (name3.equals("count")) {
group.setCount(json.nextInt());
}
else json.skipValue();
}
json.endObject(); //group
json.endObject(); // group
groups.add(group);
}
json.endArray(); //groups
json.endArray(); // groups
}
else json.skipValue();
}
json.endObject();//data
json.endObject();// data
}
else json.skipValue();
}
json.endObject(); //root
//check if this is a valid BufferGeometry
int faceCount = Math.floorDiv(positionList.size(), 3);
if (positionList.size() != faceCount * 3) throw new IllegalArgumentException("Wrong count of positions! (Got " + positionList.size() + " but expected " + (faceCount * 3) + ")");
if (normalList.size() != faceCount * 3) throw new IllegalArgumentException("Wrong count of normals! (Got " + normalList.size() + " but expected " + (faceCount * 3) + ")");
if (colorList.size() != faceCount * 3) throw new IllegalArgumentException("Wrong count of colors! (Got " + colorList.size() + " but expected " + (faceCount * 3) + ")");
if (uvList.size() != faceCount * 2) throw new IllegalArgumentException("Wrong count of uvs! (Got " + uvList.size() + " but expected " + (faceCount * 2) + ")");
json.endObject(); // root
groups.sort((g1, g2) -> (int) Math.signum(g1.getStart() - g2.getStart()));
int nextGroup = 0;
for (MaterialGroup g : groups){
if(g.getStart() != nextGroup) throw new IllegalArgumentException("Group did not start at correct index! (Got " + g.getStart() + " but expected " + nextGroup + ")");
if(g.getCount() < 0) throw new IllegalArgumentException("Group has a negative count! (" + g.getCount() + ")");
for (MaterialGroup g : groups) {
if (g.getStart() != nextGroup)
throw new IllegalArgumentException("Group did not start at correct index! (Got " + g.getStart() + " but expected " + nextGroup + ")");
if (g.getCount() < 0) throw new IllegalArgumentException("Group has a negative count! (" + g.getCount() + ")");
nextGroup += g.getCount();
}
BufferGeometry bufferGeometry = new BufferGeometry();
bufferGeometry.attributes = attributes;
bufferGeometry.groups = groups.toArray(new MaterialGroup[groups.size()]);
//collect values in arrays
float[] position = new float[positionList.size()];
for (int i = 0; i < position.length; i++) {
position[i] = positionList.get(i);
}
float[] normal = new float[normalList.size()];
for (int i = 0; i < normal.length; i++) {
normal[i] = normalList.get(i);
}
float[] color = new float[colorList.size()];
for (int i = 0; i < color.length; i++) {
color[i] = colorList.get(i);
}
float[] uv = new float[uvList.size()];
for (int i = 0; i < uv.length; i++) {
uv[i] = uvList.get(i);
}
return new BufferGeometry(position, normal, color, uv,
groups.toArray(new MaterialGroup[groups.size()])
);
return bufferGeometry;
}
private static void json2FloatList(JsonReader json, List<Float> list) throws IOException {
json.beginObject(); //root
while (json.hasNext()){
String name = json.nextName();
if(name.equals("array")){
json.beginArray(); //array
while (json.hasNext()){
list.add(new Float(json.nextDouble()));
}
json.endArray(); //array
}
else json.skipValue();
}
json.endObject(); //root
}
private static void floatArray2Json(JsonWriter json, float[] array, int itemSize, boolean normalized) throws IOException {
json.beginObject();
json.name("type").value("Float32Array");
json.name("itemSize").value(itemSize);
json.name("normalized").value(normalized);
json.name("array").beginArray();
for (int i = 0; i < array.length; i++){
//rounding and remove ".0" to save string space
double d = GenericMath.round(array[i], 4);
if (d == (int) d) json.value((int) d);
else json.value(d);
}
json.endArray();
json.endObject();
}
public static class MaterialGroup {
private int materialIndex;
private int start;
private int count;
public MaterialGroup(int materialIndex, int start, int count) {
this.materialIndex = materialIndex;
this.start = start;
this.count = count;
}
public int getMaterialIndex() {
return materialIndex;
}
public int getStart() {
return start;
}
public int getCount() {
return count;
}
public void setMaterialIndex(int materialIndex) {
this.materialIndex = materialIndex;
}
public void setStart(int start) {
this.start = start;
}
public void setCount(int count) {
this.count = count;
}
}
}

View File

@ -0,0 +1,64 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
*
*/
package de.bluecolored.bluemap.core.threejs;
public class MaterialGroup {
private int materialIndex;
private int start;
private int count;
public MaterialGroup(int materialIndex, int start, int count) {
this.materialIndex = materialIndex;
this.start = start;
this.count = count;
}
public int getMaterialIndex() {
return materialIndex;
}
public int getStart() {
return start;
}
public int getCount() {
return count;
}
public void setMaterialIndex(int materialIndex) {
this.materialIndex = materialIndex;
}
public void setStart(int start) {
this.start = start;
}
public void setCount(int count) {
this.count = count;
}
}

View File

@ -38,8 +38,8 @@ public class ModelUtils {
/**
* Creates a plane-grid with alternating face-rotations.
*/
public static Model makeGrid(Vector2i gridSize){
Model m = new Model();
public static Model<Face> makeGrid(Vector2i gridSize){
Model<Face> m = new Model<Face>();
float y = 0;

View File

@ -65,11 +65,11 @@ public class Block {
return pos;
}
public double getSunLightLevel() {
public float getSunLightLevel() {
return lightData.getSkyLight();
}
public double getBlockLightLevel() {
public float getBlockLightLevel() {
return lightData.getBlockLight();
}
@ -108,15 +108,15 @@ public class Block {
}
private void calculateLight() {
sunLight = (float) getSunLightLevel();
blockLight = (float) getBlockLightLevel();
sunLight = getSunLightLevel();
blockLight = getBlockLightLevel();
if (blockLight > 0 || sunLight > 0) return;
for (Direction direction : Direction.values()) {
Block neighbor = getRelativeBlock(direction);
sunLight = (float) Math.max(neighbor.getSunLightLevel(), sunLight);
blockLight = (float) Math.max(neighbor.getBlockLightLevel(), blockLight);
sunLight = Math.max(neighbor.getSunLightLevel(), sunLight);
blockLight = Math.max(neighbor.getBlockLightLevel(), blockLight);
}
}