3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1]

Author Topic: Fully customize-able unlimited terrain  (Read 2159 times)

« on: April 02, 2012, 11:34:02 AM »
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!  ;D


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!  ;)
« Last Edit: April 03, 2012, 08:32:28 AM by RobertooMolvadoo »
Rocket Rumble, a 3D Rad puzzle game:
http://www.3drad.com/forum/index.php?topic=9896.0
« Reply #1 on: April 02, 2012, 12:09:18 PM »
Really useful and considered work, Robertoo ! Thanks.  :)
« Last Edit: April 02, 2012, 12:13:43 PM by Alec »
« Reply #2 on: April 03, 2012, 08:31:59 AM »
Added something new. Now you can enable a rectangle where you don't want imposters to be visible.
I used this for a cabin in the woods, I didn't want trees to stick trough my building.  :P
Rocket Rumble, a 3D Rad puzzle game:
http://www.3drad.com/forum/index.php?topic=9896.0
« Reply #3 on: April 03, 2012, 08:49:15 AM »
Added something new. Now you can enable a rectangle where you don't want imposters to be visible.
I used this for a cabin in the woods, I didn't want trees to stick trough my building.  :P

I remember you asking about this - well done on getting the solution, and thanks for sharing with the community.  8)
« Reply #4 on: December 09, 2012, 09:48:37 AM »
I REALLY have to thank you for this!!!!!!!!!!!!!!
« Reply #5 on: December 11, 2012, 04:43:34 AM »
...And you're welcome.


 ;D
Rocket Rumble, a 3D Rad puzzle game:
http://www.3drad.com/forum/index.php?topic=9896.0

fourdee

« Reply #6 on: December 14, 2012, 07:05:49 AM »
This is awesome!
Thanks for posting this excellent script, very clean and interesting.
Its nice to see people sharing knowledge and their own projects on this site, makes the world a better place! :D
Pages: [1]