Java-获取介于1和;从BuffereImage(Heightmap)上的像素0
我试图在我的游戏中实现高度贴图,所以现在我已经做了几个方法,但是它们太慢了,而且高度返回的数字像1.463563298e14! 如何从heightmap上的像素获取介于0和1之间的值? 以下是我的方法:Java-获取介于1和;从BuffereImage(Heightmap)上的像素0,java,heightmap,Java,Heightmap,我试图在我的游戏中实现高度贴图,所以现在我已经做了几个方法,但是它们太慢了,而且高度返回的数字像1.463563298e14! 如何从heightmap上的像素获取介于0和1之间的值? 以下是我的方法: public void setupHeights() { int pixWidth = heightfile.getWidth(); int pixHeight = heightfile.getHeight(); int w = pixWidth; int h =
public void setupHeights() {
int pixWidth = heightfile.getWidth();
int pixHeight = heightfile.getHeight();
int w = pixWidth;
int h = pixHeight;
this.heights = new float[w][h];
boolean countWidth = true;
for (int z = 0; z < pixHeight; z++) {
depth++;
for (int x = 0; x < pixWidth; x++) {
if (countWidth)
width++;
int height;
if (whiteHigh) {
height = 256 - (-1 * heightfile.getRGB(x, z));
} else {
height = -1 * heightfile.getRGB(x, z);
}
heights[x][z] = height;
numPoints++;
}
countWidth = false;
}
System.out.println("Heights have been set up!");
}
@Override
public void update() {
if (!this.hasWorldObj())
return;
if (heightfile!=null) {
for (Entity e : entities) {
if (e != null && !(e.posX < pos.getX() || e.posZ < pos.getZ() || e.posX > pos.getX() + 1
|| e.posZ > pos.getZ() + 1)) {
{
if (heightfile != null) {
double localX = Math.abs(pos.getX() - e.posX);
double localZ = Math.abs(pos.getZ() - e.posZ);
int x = (int) (localX * width);
int y = (int) (localZ * depth);
e.posY = pos.getY() + getHeight(x, y);
}
}
} else
e = null;
}
}
}
public float getHeight(float xf, float zf) {
int x = worldCoordToIndex(xf);
int z = worldCoordToIndex(zf);
System.out.println("getting height for: " + x + ", " + z);
if (x < 0)
x = 0;
if (z < 0)
z = 0;
if (z >= heights.length) {
System.out.println("WARN getHeight z index out of bounds: " + z);
z = heights.length - 1;
}
if (x >= heights[z].length) {
System.out.println("WARN getHeight x index out of bounds: " + x);
x = heights[z].length - 1;
}
System.out.println("Returned height: " + heights[z][x] * heightScale);
return heights[z][x] * heightScale;
}
private int worldCoordToIndex(float f) {
return (int) Math.floor(f / widthScale);
}
public void setupHeights(){
int pixWidth=heightfile.getWidth();
int pixHeight=heightfile.getHeight();
int w=像素宽度;
int h=像素;
this.heights=新浮点数[w][h];
布尔countWidth=true;
对于(intz=0;zpos.getX()+1
||e.posZ>pos.getZ()+1)){
{
如果(heightfile!=null){
double localX=Math.abs(pos.getX()-e.posX);
double localZ=Math.abs(pos.getZ()-e.posZ);
int x=(int)(localX*宽度);
int y=(int)(localZ*深度);
e、 posY=pos.getY()+getHeight(x,y);
}
}
}否则
e=零;
}
}
}
公共浮动高度(浮动xf、浮动zf){
int x=世界坐标索引(xf);
int z=世界合作指数(zf);
System.out.println(“获取高度:“+x+”,“+z”);
if(x<0)
x=0;
if(z<0)
z=0;
如果(z>=高度.长度){
System.out.println(“警告getHeight z索引超出范围:+z”);
z=高度。长度-1;
}
如果(x>=高度[z]。长度){
System.out.println(“警告getHeight x索引超出范围:+x”);
x=高度[z]。长度-1;
}
System.out.println(“返回高度:+高度[z][x]*高度刻度);
返回高度[z][x]*高度刻度;
}
私有int worldCoordToIndex(浮动f){
返回(整数)数学层(f/宽度刻度);
}
代码有些混乱,方法和目标以及上下文和前提条件也不完全清楚。假设:您有一个灰度buffereImage
。您需要一个类似于HeightMap
的接口,该接口具有一个方法float getHeight(float x,float y)
,该方法接收两个介于0和1之间的值,并从图像返回相应的值(介于0和1之间)。这是正确的吗?代码有些混乱,方法和目标以及上下文和前提条件也不完全清楚。假设:您有一个灰度buffereImage
。您需要一个类似于HeightMap
的接口,该接口具有一个方法float getHeight(float x,float y)
,该方法接收两个介于0和1之间的值,并从图像返回相应的值(介于0和1之间)。这是正确的吗?