JavaScript Voxel Building Tutorial - Part 1 (Solid Floor + Exterior Walls Construction)


#1
  • This tutorial will help you get started building on your respective plot. Throughout this example I will use my specific coordinates. Simply plug in your respective coordinates to begin your build.

Step 1: System Requirements

Step 2: Chart Out Your Plot On Paper

Step 3: Solid Floor Construction

  • Now that you have your plot sketch, here is an example of how to create a solid floor on the ground level.

    // Function to make a solid floor on the ground position of your plot.
    function makeSolidFloor()
    {
    // Create variables.
    var x;
    var y;
    var z;

      // Set variables to initial voxel build position.
      x = 6272;
      y = 191;
      z = 7000;
      
      // Create a floor which creates voxels down the X axis then when it reaches the limit of your plot on the X axis, increment Z and loop the process until the upper bounds of your plot are reached.
      while (x <= 6472 && z < 7200)
      {
      	do
      	{
      		Voxels.setVoxel(x, y, z, 1.0, 255, 0, 0);
      		x++;
      	}
      	while (x < 6472);	
      	
      	x = 6272;
      	z++;
      }
      
      // Stop script.
      Script.stop();
    

    }

    Script.update.connect(makeSolidFloor);

Step 4: Solid Floor Destruction

  • Now that you have created your new floor, you can remove your floor as follows:

    // Function to erase a solid floor on the ground position of your plot.
    function eraseSolidFloor()
    {
    // Create variables.
    var x;
    var y;
    var z;

      // Set variables to initial voxel build position.
      x = 6272;
      y = 191;
      z = 7000;
      
      // Erases a floor which removes voxels down the X axis then when it reaches the limit of your plot on the X axis, increment Z and loop the process until the upper bounds of your plot are reached.
      while (x <= 6472 && z < 7200)
      {
      	do
      	{
      		Voxels.eraseVoxel(x, y, z, 1.0);
      		x++;
      	}
      	while (x < 6472);	
      	
      	x = 6272;
      	z++;
      }
      
      // Stop script.
      Script.stop();
    

    }

    Script.update.connect(eraseSolidFloor);

Step 5: Exterior Walls Construction

  • After erasing your build, you can just as easily create a set of exterior walls as follows:

    // Function to make exterior walls on the ground position of your plot.
    function makeExteriorWalls()
    {
    // Create variables.
    var x;
    var y;
    var z;

      	// Set X variable to initial voxel build position.
      	x = 6272;
          y = 192;
      
      	// Create left wall down the X axis.
      	do
      	{
      		Voxels.setVoxel(x, y, 7000, 1.0, 255, 0, 0);
      		x++;
      	}
      	while (x < 6472);
      
      	// Set X variable back to your new voxel build position.
      	x = 6272
      
      	// Create right wall down the X axis.
      	do
      	{
      		Voxels.setVoxel(x, y, 7199, 1.0, 255, 0, 0);
      		x++;
      	}
      	while (x < 6472);
      
      	// Set Z variable to initial voxel build position.
      	z = 7000;
      
      	// Create front wall across the Z axis.
      	do
      	{
      		Voxels.setVoxel(6272, y, z, 1.0, 255, 0, 0);
      		z++;
      	}
      	while (z < 7200);
      
      	// Set Z variable back to your new voxel build position.
      	z = 7000;
      
      	// Create back wall across the Z axis.
      	do
      	{
      		Voxels.setVoxel(6471, y, z, 10.0, 255, 0, 0);
      		z++;
      	}
      	while (z < 7200);
      
      	// Stop script.
      	Script.stop();
      }
       
      Script.update.connect(makeExteriorWalls);
    

Step 6: Exterior Walls Destruction

  • Now that you have created your exterior walls, you can remove your exterior walls as follows:

    // Function to erase exterior walls on the ground position of your plot.
    function eraseExteriorWalls()
    {
    // Create variables.
    var x;
    var y;
    var z;

      // Set X variable to initial voxel build position.
      x = 6272;
          y = 192;
    
      // Erase left wall down the X axis.
      do
      {
      	Voxels.eraseVoxel(x, y, 7000, 1.0);
      	x++;
      }
      while (x < 6472);
    
      // Set X variable back to your new voxel build position.
      x = 6272
    
      // Erase right wall down the X axis.
      do
      {
      	Voxels.eraseVoxel(x, y, 7199, 1.0);
      	x++;
      }
      while (x < 6472);
    
      // Set Z variable to initial voxel build position.
      z = 7000;
    
      // Erase front wall across the Z axis.
      do
      {
       	Voxels.eraseVoxel(6272, y, z, 1.0);
      	z++;
      }
      while (z < 7200);
    
      // Set Z variable back to your new voxel build position.
      z = 7000;
    
      // Erase back wall across the Z axis.
      do
      {
      	Voxels.eraseVoxel(6471, y, z, 1.0);
      	z++;
      }
      while (z < 7200);
    
      // Stop script.
      Script.stop();
    

    }

    Script.update.connect(eraseExteriorWalls);


#2

Nice @KevinMThomas, and I see you already used it to mark your plot =)

I’d just like to say to anyone building scripts though, please try them out on your local stack first and make sure they work properly, this will avoid a lot of troubles as the ways to restore builds and lands are not yet functional.

You can do that by opening the “Go To Domain…” interface ( “File > Go To Domain…” or Command + D), then type “localhost” and enter.
You are now running on a temporary local sandbox, here, you can try out your scripts without any consequences as it will be wipe out when you’ll close the interface or change domain again.
Once you made sure your script works correctly, simply open the “Go To Domain…” interface again and type the name of the domain you’d like to join (i.e. alpha.highfidelity.io).


#3

@Atlante45 perhaps I should have used 1 instead of 10 however my thought was 10 would be for a teaching purpose. I personally use size 1 for my biggest block to make fine definition I just wanted to use 10 so that the users can easily see their new small examples.


#4

Yes, for more details about the appropriate way to create a voxel, please read the post linked here.

Also, I have seen you use Script.update.connect(); to execute your script, which is fine since you call Script.stop() at the end of your function.
But Script.update.connect(); calls the fonction you connect every frame in order to let you update whatever you need in your script. It is not necessary here, you could just call eraseExteriorWalls(); instead and it would work the same.

Happy scripting :wink: