@ -10,54 +10,15 @@ namespace UnityEngine.Rendering.UnifiedRayTracing
{
{
static private AsyncTerrainToMeshRequest MakeAsyncTerrainToMeshRequest ( int width , int height , Vector3 heightmapScale , float [ , ] heightmap , bool [ , ] holes )
static private AsyncTerrainToMeshRequest MakeAsyncTerrainToMeshRequest ( int width , int height , Vector3 heightmapScale , float [ , ] heightmap , bool [ , ] holes )
{
{
int vertexCount = width * height ;
int vertexCount = width * height ;
var job = new ComputeTerrainMeshJob ( ) ;
var job = new ComputeTerrainMeshJob ( ) ;
job . heightmap = new NativeArray < float > ( vertexCount , Allocator . Persistent ) ;
for ( int i = 0 ; i < vertexCount ; + + i )
job . heightmap [ i ] = heightmap [ i / ( width ) , i % ( width ) ] ;
//WW1MOD This crashes when baking 4k terrains, as it tries to allocate a *2GB* Buffer!.. which is fine *here* but when it gets uploaded to GPU, it goes crashy or error, depending on dx-API/Driver
//Note heightmap size is always pow2+1
//the "else" part is the original code
if ( height = = 4 0 9 7 )
{
const int halfSize = 2 0 4 9 ;
int _vertexCount = halfSize * halfSize ;
Debug . Log ( "Downsizing Terrain heightmap mesh output to 2k to prevent crashes" ) ;
job . heightmap = new NativeArray < float > ( _vertexCount , Allocator . Persistent ) ;
for ( int i = 0 ; i < _vertexCount ; + + i )
{
float h0 = heightmap [ ( ( i * 2 ) + 0 ) / ( width ) , ( ( i * 2 ) + 0 ) % ( width ) ] ;
float h1 = heightmap [ ( ( i * 2 ) + 1 ) / ( width ) , ( ( i * 2 ) + 0 ) % ( width ) ] ;
float h2 = heightmap [ ( ( i * 2 ) + 0 ) / ( width ) , ( ( i * 2 ) + 1 ) % ( width ) ] ;
float h3 = heightmap [ ( ( i * 2 ) + 1 ) / ( width ) , ( ( i * 2 ) + 1 ) % ( width ) ] ;
job . heightmap [ i ] = ( h0 + h1 + h2 + h3 ) / 4f ;
}
job . holes = new NativeArray < bool > ( ( halfSize - 1 ) * ( halfSize - 1 ) , Allocator . Persistent ) ;
for ( int i = 0 ; i < ( halfSize - 1 ) * ( halfSize - 1 ) ; + + i )
{
bool h0 = holes [ ( ( i * 2 ) + 0 ) / ( width - 1 ) , ( ( i * 2 ) + 0 ) % ( width - 1 ) ] ;
bool h1 = holes [ ( ( i * 2 ) + 1 ) / ( width - 1 ) , ( ( i * 2 ) + 0 ) % ( width - 1 ) ] ;
bool h2 = holes [ ( ( i * 2 ) + 0 ) / ( width - 1 ) , ( ( i * 2 ) + 1 ) % ( width - 1 ) ] ;
bool h3 = holes [ ( ( i * 2 ) + 1 ) / ( width - 1 ) , ( ( i * 2 ) + 1 ) % ( width - 1 ) ] ;
job . holes [ i ] = h0 | | h1 | | h2 | | h3 ;
}
height = width = halfSize ;
vertexCount = _vertexCount ;
}
else
{
job . heightmap = new NativeArray < float > ( vertexCount , Allocator . Persistent ) ;
for ( int i = 0 ; i < vertexCount ; + + i )
job . heightmap [ i ] = heightmap [ i / ( width ) , i % ( width ) ] ;
job . holes = new NativeArray < bool > ( ( width - 1 ) * ( height - 1 ) , Allocator . Persistent ) ;
for ( int i = 0 ; i < ( width - 1 ) * ( height - 1 ) ; + + i )
job . holes [ i ] = holes [ i / ( width - 1 ) , i % ( width - 1 ) ] ;
}
job . holes = new NativeArray < bool > ( ( width - 1 ) * ( height - 1 ) , Allocator . Persistent ) ;
for ( int i = 0 ; i < ( width - 1 ) * ( height - 1 ) ; + + i )
job . holes [ i ] = holes [ i / ( width - 1 ) , i % ( width - 1 ) ] ;
job . width = width ;
job . width = width ;
job . height = height ;
job . height = height ;