Improve generic rendering pos objects

This commit is contained in:
James Seibel
2024-06-30 14:02:44 -05:00
parent 28c8614550
commit 305a6bb459
5 changed files with 137 additions and 41 deletions
@@ -1,5 +1,6 @@
package com.seibel.distanthorizons.api.interfaces.render;
import com.seibel.distanthorizons.api.objects.math.DhApiVec3f;
import com.seibel.distanthorizons.api.objects.render.DhApiRenderableBox;
import java.util.List;
@@ -12,7 +13,7 @@ public interface IDhApiCustomRenderRegister
IDhApiRenderableBoxGroup createForSingleBox(DhApiRenderableBox cube);
IDhApiRenderableBoxGroup createRelativePositionedGroup(float originBlockX, float originBlockY, float originBlockZ, List<DhApiRenderableBox> cubeList);
IDhApiRenderableBoxGroup createRelativePositionedGroup(DhApiVec3f originBlockPos, List<DhApiRenderableBox> cubeList);
IDhApiRenderableBoxGroup createAbsolutePositionedGroup(List<DhApiRenderableBox> cubeList);
}
@@ -1,6 +1,7 @@
package com.seibel.distanthorizons.api.interfaces.render;
import com.seibel.distanthorizons.api.methods.events.sharedParameterObjects.DhApiRenderParam;
import com.seibel.distanthorizons.api.objects.math.DhApiVec3f;
import com.seibel.distanthorizons.api.objects.render.DhApiRenderableBox;
import java.util.List;
@@ -11,10 +12,8 @@ public interface IDhApiRenderableBoxGroup extends List<DhApiRenderableBox>
long getId();
void setOriginBlockPos(float x, float y, float z);
float getOriginBlockX();
float getOriginBlockY();
float getOriginBlockZ();
void setOriginBlockPos(DhApiVec3f pos);
DhApiVec3f getOriginBlockPos();
void setPreRenderFunc(Consumer<DhApiRenderParam> renderEventParam);
@@ -0,0 +1,93 @@
/*
* This file is part of the Distant Horizons mod
* licensed under the GNU LGPL v3 License.
*
* Copyright (C) 2020-2023 James Seibel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.seibel.distanthorizons.api.objects.math;
/**
* Often used to store block positions or any other
* position in 3D space.
*
* @author James Seibel
* @version 2024-6-3
* @since API 2.2.0
*/
public class DhApiVec3f
{
public float x;
public float y;
public float z;
/** creates a Vec3 at (0,0,0) */
public DhApiVec3f()
{
this.x = 0;
this.y = 0;
this.z = 0;
}
public DhApiVec3f(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
else if (obj != null && this.getClass() == obj.getClass())
{
DhApiVec3f Vec3f = (DhApiVec3f) obj;
if (Float.compare(Vec3f.x, this.x) != 0)
{
return false;
}
else if (Float.compare(Vec3f.y, this.y) != 0)
{
return false;
}
else
{
return Float.compare(Vec3f.z, this.z) == 0;
}
}
else
{
return false;
}
}
@Override
public int hashCode()
{
int i = Float.floatToIntBits(this.x);
i = 31 * i + Float.floatToIntBits(this.y);
return 31 * i + Float.floatToIntBits(this.z);
}
@Override
public String toString() { return "[" + this.x + ", " + this.y + ", " + this.z + "]"; }
}
@@ -19,6 +19,7 @@
package com.seibel.distanthorizons.coreapi.util.math;
import com.seibel.distanthorizons.api.objects.math.DhApiVec3f;
import com.seibel.distanthorizons.coreapi.util.MathUtil;
/**
@@ -56,6 +57,15 @@ public class Vec3f
this.z = z;
}
public Vec3f(DhApiVec3f pos)
{
this.x = pos.x;
this.y = pos.y;
this.z = pos.z;
}
@Override
public boolean equals(Object obj)
{
@@ -25,6 +25,7 @@ import com.seibel.distanthorizons.api.enums.config.EDhApiLoggerMode;
import com.seibel.distanthorizons.api.interfaces.render.IDhApiRenderableBoxGroup;
import com.seibel.distanthorizons.api.interfaces.render.IDhApiCustomRenderRegister;
import com.seibel.distanthorizons.api.methods.events.sharedParameterObjects.DhApiRenderParam;
import com.seibel.distanthorizons.api.objects.math.DhApiVec3f;
import com.seibel.distanthorizons.api.objects.render.DhApiRenderableBox;
import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector;
import com.seibel.distanthorizons.core.logging.ConfigBasedSpamLogger;
@@ -223,14 +224,14 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister
new Color(Color.MAGENTA.getRed(), Color.MAGENTA.getGreen(), Color.MAGENTA.getBlue())));
}
IDhApiRenderableBoxGroup relativePosCubeGroup = DhApi.Delayed.renderRegister.createRelativePositionedGroup(
24f, 140f, 24f,
new DhApiVec3f(24f, 140f, 24f),
relCubeList);
relativePosCubeGroup.setPreRenderFunc((event) ->
{
float x = relativePosCubeGroup.getOriginBlockX();
x += event.partialTicks / 2;
x %= 32;
relativePosCubeGroup.setOriginBlockPos(x, relativePosCubeGroup.getOriginBlockY(), relativePosCubeGroup.getOriginBlockZ());
DhApiVec3f pos = relativePosCubeGroup.getOriginBlockPos();
pos.x += event.partialTicks / 2;
pos.x %= 32;
relativePosCubeGroup.setOriginBlockPos(pos);
});
DhApi.Delayed.renderRegister.add(relativePosCubeGroup);
@@ -249,17 +250,17 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister
}
}
IDhApiRenderableBoxGroup massRelativePosCubeGroup = DhApi.Delayed.renderRegister.createRelativePositionedGroup(
-25f, 140f, 0f,
new DhApiVec3f(-25f, 140f, 0f),
massRelCubeList);
massRelativePosCubeGroup.setPreRenderFunc((event) ->
{
float y = massRelativePosCubeGroup.getOriginBlockY();
y += event.partialTicks / 4;
if (y > 150f)
DhApiVec3f blockPos = massRelativePosCubeGroup.getOriginBlockPos();
blockPos.y += event.partialTicks / 4;
if (blockPos.y > 150f)
{
y = 140f;
blockPos.y = 140f;
}
massRelativePosCubeGroup.setOriginBlockPos(massRelativePosCubeGroup.getOriginBlockX(), y, massRelativePosCubeGroup.getOriginBlockZ());
massRelativePosCubeGroup.setOriginBlockPos(blockPos);
});
DhApi.Delayed.renderRegister.add(massRelativePosCubeGroup);
@@ -303,12 +304,12 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister
}
@Override
public IDhApiRenderableBoxGroup createRelativePositionedGroup(float originBlockX, float originBlockY, float originBlockZ, List<DhApiRenderableBox> cubeList)
{ return new DhApiRenderableBoxGroup(originBlockX, originBlockY, originBlockZ, cubeList, true); }
public IDhApiRenderableBoxGroup createRelativePositionedGroup(DhApiVec3f originBlockPos, List<DhApiRenderableBox> cubeList)
{ return new DhApiRenderableBoxGroup(new Vec3f(originBlockPos), cubeList, true); }
@Override
public IDhApiRenderableBoxGroup createAbsolutePositionedGroup(List<DhApiRenderableBox> boxList)
{ return new DhApiRenderableBoxGroup(0, 0, 0, boxList, false); }
{ return new DhApiRenderableBoxGroup(new Vec3f(0, 0, 0), boxList, false); }
@@ -457,9 +458,9 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister
this.shader.setUniform(this.instancedShaderOffsetUniformLocation,
new Vec3f(
boxGroup.originBlockX,
boxGroup.originBlockY,
boxGroup.originBlockZ
boxGroup.originBlockPos.x,
boxGroup.originBlockPos.y,
boxGroup.originBlockPos.z
));
this.shader.setUniform(this.instancedShaderCameraPosUniformLocation,
@@ -547,9 +548,9 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister
float originOffsetZ = 0;
if (cubeGroup.positionCubesRelativeToGroupOrigin)
{
originOffsetX = cubeGroup.originBlockX;
originOffsetY = cubeGroup.originBlockY;
originOffsetZ = cubeGroup.originBlockZ;
originOffsetX = cubeGroup.originBlockPos.x;
originOffsetY = cubeGroup.originBlockPos.y;
originOffsetZ = cubeGroup.originBlockPos.z;
}
Mat4f boxTransform = Mat4f.createTranslateMatrix(
@@ -590,9 +591,7 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister
private final ArrayList<DhApiRenderableBox> cubeList;
private float originBlockX;
private float originBlockY;
private float originBlockZ;
private final Vec3f originBlockPos;
@Nullable
public Consumer<DhApiRenderParam> beforeRenderFunc;
@@ -612,25 +611,21 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister
public long getId() { return this.id; }
@Override
public void setOriginBlockPos(float x, float y, float z)
public void setOriginBlockPos(DhApiVec3f pos)
{
this.originBlockX = x;
this.originBlockY = y;
this.originBlockZ = z;
this.originBlockPos.x = pos.x;
this.originBlockPos.y = pos.y;
this.originBlockPos.z = pos.z;
}
@Override
public float getOriginBlockX() { return this.originBlockX; }
@Override
public float getOriginBlockY() { return this.originBlockY; }
@Override
public float getOriginBlockZ() { return this.originBlockZ; }
public DhApiVec3f getOriginBlockPos() { return new DhApiVec3f(this.originBlockPos.x, this.originBlockPos.y, this.originBlockPos.z); }
// constructor //
public DhApiRenderableBoxGroup(float originBlockX, float originBlockY, float originBlockZ, List<DhApiRenderableBox> cubeList, boolean positionCubesRelativeToGroupOrigin)
public DhApiRenderableBoxGroup(Vec3f originBlockPos, List<DhApiRenderableBox> cubeList, boolean positionCubesRelativeToGroupOrigin)
{
// TODO save to database
// TODO when?
@@ -638,9 +633,7 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister
this.id = NEXT_ID_ATOMIC_INT.getAndIncrement();
this.cubeList = new ArrayList<>(cubeList);
this.originBlockX = originBlockX;
this.originBlockY = originBlockY;
this.originBlockZ = originBlockZ;
this.originBlockPos = originBlockPos;
this.positionCubesRelativeToGroupOrigin = positionCubesRelativeToGroupOrigin;
}