Java 将高度贴图拆分为象限
我正在使用四叉树系统渲染地形。我需要使用方法Java 将高度贴图拆分为象限,java,arrays,quadtree,heightmap,Java,Arrays,Quadtree,Heightmap,我正在使用四叉树系统渲染地形。我需要使用方法splitHeightmap(float[]originalMap,int quadrant)将高度图拆分为四个部分,象限是0-3之间的数字。映射需要拆分为四分之一,因此如果将0作为象限传递,则数组的左下四分之一将作为新的浮点数组返回。我有一些基本代码,但我不确定如何根据所需象限对地图进行实际采样: protected float[] splitHeightMap(float[] heightMap, TerrainQuadrant quadrant)
splitHeightmap(float[]originalMap,int quadrant)
将高度图拆分为四个部分,象限是0-3之间的数字。映射需要拆分为四分之一,因此如果将0作为象限传递,则数组的左下四分之一将作为新的浮点数组返回。我有一些基本代码,但我不确定如何根据所需象限对地图进行实际采样:
protected float[] splitHeightMap(float[] heightMap, TerrainQuadrant quadrant) {
float[] newHeightMap = new float[size >> 1];
int newSize = size >> 1;
for (int i = 0; i < newSize; i++)
for (int j = 0; j < newSize; j++)
newHeightMap[i * newSize + j] = sampleHeightAt(heightMap, i, j);
return newHeightMap;
}
protected float sampleHeightAt(float[] heightMap, int x, int z) {
return heightMap[z + x * size];
}
protectedfloat[]splitHeightMap(float[]heightMap,地形象限){
float[]newHeightMap=newfloat[size>>1];
int newSize=size>>1;
对于(int i=0;i
编辑:我已经写了我认为应该有效的东西,但是我得到了索引65792的ArrayIndexOutOfBoundsException(原始高度映射为512x512):
protectedfloat[]splitHeightMap(float[]heightMap,地形象限){
float[]newHeightMap=newfloat[(大小>>1)*(大小>>1)];
int newSize=size>>1;
int xOffset=0,zOffset=0;
int xCount=0,yCount=0;
开关(象限){
右下角:
xOffset=新闻化;
打破
案例左上角:
zOffset=新闻化;
打破
右上角:
xOffset=新闻化;
zOffset=新闻化;
打破
违约:
打破
}
对于(int x=xOffset;x
如果我理解正确,那么您必须使用二维数组。使用这种类型的数组很容易获取数组的任何区域
您的heightMap
现在将成为float[][]
类型,因此您必须更正填充此数组的代码
我为您实现了一个示例,并使用了4x4阵列:
| 2 2 3 3 |
| 2 2 3 3 |
| 0 0 1 1 |
| 0 0 1 1 |
据我所知,您希望获取像所有“0”、所有“1”、所有“2”和所有“3”这样的区域
public static void main ( String[] args )
{
//setting up initial array 'heightMap' (you can name it like you want)
float[][] f = { { 2, 2, 3, 3 }, { 2, 2, 3, 3 }, { 0, 0, 1, 1 }, { 0, 0, 1, 1 } };
float[][] f2 = splitHeightMap ( f, TerrainQuadrant.BotttomRight );
for ( float[] floats : f2 )
{
System.out.println ( Arrays.toString ( floats ) );
}
}
protected static float[][] splitHeightMap ( float[][] heightMap, TerrainQuadrant quadrant )
{
//this gives you half of the 'heightMap' length
int newSize = heightMap.length >> 1;
float[][] newHeightMap = new float[ newSize ][ newSize ];
//its your offsets, indicating from what place to start iteration
int xOffset = 0;
int yOffset = newSize;
//its max values to reach while iterating
int xRestriction = newSize;
int yRestriction = heightMap.length;
//setting up params according to 'quadrant'
switch ( quadrant )
{
case BottomRight:
xOffset = newSize;
yOffset = newSize;
xRestriction = heightMap.length;
break;
case TopLeft:
yOffset = 0;
yRestriction = newSize;
break;
case TopRight:
yOffset = 0;
xOffset = newSize;
xRestriction = heightMap.length;
yRestriction = newSize;
break;
default:
break;
}
//counters not to reach new array bounds
int xCount = 0, yCount = 0;
for ( int y = yOffset; y < yRestriction; y++ )
{
//taking row at 'y' position
float[] row = heightMap[ y ];
for ( int x = xOffset; x < xRestriction; x++ )
{
//taking value from 'y' row at 'x' position.
float value = row[ x ];
//set fetched value to new map.
newHeightMap[ yCount ][ xCount ] = value;
//increase x position, but do not touch row
xCount++;
}
//new row - new 'x' position
xCount = 0;
yCount++;
}
return newHeightMap;
}
改变它-改变主要方法。这是我的荣幸。根据这个答案,线性读写的差异非常小,几乎不会影响性能。
public static void main ( String[] args )
{
//setting up initial array 'heightMap' (you can name it like you want)
float[][] f = { { 2, 2, 3, 3 }, { 2, 2, 3, 3 }, { 0, 0, 1, 1 }, { 0, 0, 1, 1 } };
float[][] f2 = splitHeightMap ( f, TerrainQuadrant.BotttomRight );
for ( float[] floats : f2 )
{
System.out.println ( Arrays.toString ( floats ) );
}
}
protected static float[][] splitHeightMap ( float[][] heightMap, TerrainQuadrant quadrant )
{
//this gives you half of the 'heightMap' length
int newSize = heightMap.length >> 1;
float[][] newHeightMap = new float[ newSize ][ newSize ];
//its your offsets, indicating from what place to start iteration
int xOffset = 0;
int yOffset = newSize;
//its max values to reach while iterating
int xRestriction = newSize;
int yRestriction = heightMap.length;
//setting up params according to 'quadrant'
switch ( quadrant )
{
case BottomRight:
xOffset = newSize;
yOffset = newSize;
xRestriction = heightMap.length;
break;
case TopLeft:
yOffset = 0;
yRestriction = newSize;
break;
case TopRight:
yOffset = 0;
xOffset = newSize;
xRestriction = heightMap.length;
yRestriction = newSize;
break;
default:
break;
}
//counters not to reach new array bounds
int xCount = 0, yCount = 0;
for ( int y = yOffset; y < yRestriction; y++ )
{
//taking row at 'y' position
float[] row = heightMap[ y ];
for ( int x = xOffset; x < xRestriction; x++ )
{
//taking value from 'y' row at 'x' position.
float value = row[ x ];
//set fetched value to new map.
newHeightMap[ yCount ][ xCount ] = value;
//increase x position, but do not touch row
xCount++;
}
//new row - new 'x' position
xCount = 0;
yCount++;
}
return newHeightMap;
}
| 1 1 |
| 1 1 |