I wanted a nice little script to manage my unlimited terrain and make me able to change some things without having to change tons of numbers, so I created a this. And then I thought, why not share it with you!
Click to enlarge.
The script:
How to use it?
1. Add your tile-able terrain and your character or car. Put your terrain far below the ground. This piece will not be used.
2. Add a script object and paste my script into it.
3. Link the script to the terrain.
4. Link the script to the character. Make sure you do it this order!
5. Open the script, on the top there are some things you can change.
Tips
Use a smaller grid to use less imposters. This can save a lot performance.
Don't enable shadows. It will save a lot performance.
Use a black sunlight and a pointlight or fog to cover up the 'respawning' tiles. Optional, if you use a skybox, set the fog to the same color as the bottom of the skybox to make it look seamless.
Well, that's all. I hope it helps!
Click to enlarge.
The script:
Code: [Select]
///------------------------------------------------------------------------------------------------------------------------------
///SETTINGS
///------------------------------------------------------------------------------------------------------------------------------
int GridLength = 7; ///Set length of the imposter grid
int GridWidth = 7; ///Set width of the imposter grid
float Length = 10; ///Set length of one tile
float Width = 10; ///Set width of one tile
float StaticOffsetX = 0; ///Set X offset of the grid
float StaticOffsetY = 0; ///Set Y offset of the grid
float StaticOffsetZ = 0; ///Set Z offset of the grid
int Terrain = OBJ_0; ///Make sure the script is linked to these objects
int Player = OBJ_22;
bool Exception = false; ///Set to 'true' to enable a rectangle where you don't want imposters to be visible
///Set to 'false' to disable
float ExcepLocXmin = 0; ///Set the execption location x value, minimum
float ExcepLocXmax = 0; ///Set the execption location x value, maximum
float ExcepLocZmin = 15; ///Set the execption location z value, minimum
float ExcepLocZmax = 25; ///Set the execption location z value, maximum
bool Shadows = false; ///Set to 'true' to enable shadows, set to 'false' to disable shadows
///------------------------------------------------------------------------------------------------------------------------------
///
///------------------------------------------------------------------------------------------------------------------------------
int Imposters = GridLength*GridWidth;
Vector3[] TerrainLoc(Imposters);
Vector3 TerrainMainLoc;
Quaternion TerrainRot;
bool Changed;
Vector3 ExcepLoc;
void Main()
{
///Initialize imposters
if (iInitializing())
{
iObjectImpostersCreate(Terrain,Imposters);
TerrainMainLoc = Vector3(0,0,0);
for (int i=0; i<Imposters; i++)
{
int z = i/GridWidth;
TerrainLoc[i].z = z*Length - (0.5*Length*(GridLength - 1)) + StaticOffsetZ;
int x = i;
int Times = x/GridWidth;
for (int i=0; i<Times; i++) if (x >= GridWidth) x -= GridWidth;
TerrainLoc[i].x = x*Width - (0.5*Width*(GridWidth - 1)) + StaticOffsetX;
TerrainLoc[i].y = StaticOffsetY;
if (Exception)
{
if (TerrainLoc[i].x >= ExcepLocXmin && TerrainLoc[i].x <= ExcepLocXmax && TerrainLoc[i].z >= ExcepLocZmin && TerrainLoc[i].z <= ExcepLocZmax) TerrainLoc[i].y = -50;
else TerrainLoc[i].y = StaticOffsetY;
}
iObjectImposterSet(Terrain,i,TerrainRot,TerrainLoc[i]);
iObjectImposterShadowEnable(Terrain,i,Shadows);
}
}
///Move grid
Vector3 PlayerLoc;
iObjectLocation(Player,PlayerLoc);
Vector3 Distance = PlayerLoc - TerrainMainLoc;
if (Distance.z > Length)
{
TerrainMainLoc.z += Length;
for (int i=0; i<Imposters; i++) TerrainLoc[i].z += Length;
}
if (Distance.z < -Length)
{
TerrainMainLoc.z -= Length;
for (int i=0; i<Imposters; i++) TerrainLoc[i].z -= Length;
}
if (Distance.x > Width)
{
TerrainMainLoc.x += Width;
for (int i=0; i<Imposters; i++) TerrainLoc[i].x += Width;
}
if (Distance.x < -Width)
{
TerrainMainLoc.x -= Width;
for (int i=0; i<Imposters; i++) TerrainLoc[i].x -= Width;
}
///Exception
if (Distance.z > Length || Distance.z < -Length || Distance.x > Width || Distance.x < -Width) Changed = true;
if (Changed)
{
for (int i=0; i<Imposters; i++)
{
if (Exception)
{
if (TerrainLoc[i].x >= ExcepLocXmin && TerrainLoc[i].x <= ExcepLocXmax && TerrainLoc[i].z >= ExcepLocZmin && TerrainLoc[i].z <= ExcepLocZmax) TerrainLoc[i].y = -50;
else TerrainLoc[i].y = StaticOffsetY;
}
iObjectImposterSet(Terrain,i,TerrainRot,TerrainLoc[i]);
}
}
Changed = false;
if (iDeinitializing()) iObjectImpostersDestroy(Terrain);
}
How to use it?
1. Add your tile-able terrain and your character or car. Put your terrain far below the ground. This piece will not be used.
2. Add a script object and paste my script into it.
3. Link the script to the terrain.
4. Link the script to the character. Make sure you do it this order!
5. Open the script, on the top there are some things you can change.
Tips
Use a smaller grid to use less imposters. This can save a lot performance.
Don't enable shadows. It will save a lot performance.
Use a black sunlight and a pointlight or fog to cover up the 'respawning' tiles. Optional, if you use a skybox, set the fog to the same color as the bottom of the skybox to make it look seamless.
Well, that's all. I hope it helps!