Fun and useful JS scripts


#42

Thanks @DebsRegent , I looked at a few js color wheels and at this point I dont know javascript well enough to integrate them into High Fidelity, definitely a hud is the best option, as in a dropdown of sorts, I want to know if we can place voxels on a users screen and position them according to the screen window x,y then we can build interactive huds using voxels. I am open to suggestions.

Meanwhile I have been building a suburban street-scape and toying with a tree building script, just a simple tree but because the green shades are set to random, every one is different.


The script is available for download, share and modify DOWNLOAD SCRIPT

// tree18m.js
// Creates a single tree of 1 meter voxels to about 18 meters high with various shades of green leaves
// Created by Adrian on 4 July 2014
// Copyright 2014 Adrian McCarlie
// Released under Creative Commons By Attribution Share Alike, feel free to modify and share.
// 
//
// Change these 3 values to suit the planting site of the tree.
// 
var X_POS = 8063;
var Y_POS = 201;
var Z_POS = 8065;

//Change the height variable here to change the height of the trunk from the ground to the bottom of the leaves.
//This does not change the size of the leaves.
var height = 10;
//
///////////////////End of User Variables
// 
        var x = X_POS;
        var y = Y_POS + height;
        var z = Z_POS;
        var l = 0;
        var maxl = 2;
        var halfh = 5;
        var h = 0;
        var w = 0;
        var maxw = 2;
        var leaves = 0;
        var width = 3;
        var start = 0;
        var col = 0; 
        
    function rezTree()    
    {
        while (h < halfh) //create leaf stacks to half the height
        {    
            while (width < maxw) //create columns
            {
                while (leaves < width) //create rows of leaves
                { 
                    col = Math.floor((Math.random() * 130) + 100);//generates a random shade of green
                    Voxels.setVoxel(x, y+h, z+start, 1, 40, col, 40); z++; leaves++; //rows getting bigger
                }
                x++; 
                leaves = 0;
                width = width + 2;
                z = Z_POS;
                start=start-1;
            }
            ////////////////////////////////////////////    stop getting bigger start getting smaller horizontally        
            width = width - 4;
            start=start + 2    ;    
            while (width >= 0)
            {            
                while (leaves < width) 
                {
                    col = Math.floor((Math.random() * 130) + 100);
                    Voxels.setVoxel(x, y+h, z+start, 1, 40, col, 40); z++; leaves++;//rows getting smaller
                }
                x++; 
                leaves = 0;
                width = width - 2;
                z = Z_POS;
                start=start+1;                    
            }
            h++;
            maxw = maxw + 2;
            x = X_POS  - h;
            
        }    
            /////////////////////////////////////////////////////////////////////    half way from the bottom up start getting smaller
            var q = -4; 
            x = x  +2;
            width = 1;
            start = start - 1;
            maxw = maxw - 2;
        while (h < (halfh *2)-1)//create stack for second half height

        {    
            while (width < maxw)
            {
                while (leaves < width) 
                {
                    col = Math.floor((Math.random() * 130) + 100);
                    Voxels.setVoxel(x, y+h, z+start, 1, 40, col, 40); z++; leaves++; //rows getting bigger
                }
                x++; 
                leaves = 0;
                width = width + 2;
                z = Z_POS;
                start=start-1;
            }        
            width = width - 4;
            start=start+2    ;    
            while (width >= 0)
            {            
                while (leaves < width) 
                {
                    col = Math.floor((Math.random() * 130) + 100);
                    Voxels.setVoxel(x, y+h, z+start, 1, 40, col, 40); z++; leaves++; //rows getting smaller
                }
                x++; 
                leaves = 0;
                width = width - 2;
                z = Z_POS;
                start=start+1    ;                    
            }
            h++;
            maxw = maxw - 2;
            q++;
            x = X_POS  + q;
            
        }
        x= X_POS+1;
        y = Y_POS;
        z= Z_POS+1;        
            while (y <= Y_POS + height)//build a post 10 high
            {
                Voxels.setVoxel(x, y, z, 1, 100, 80, 10); y++;
            }
            Voxels.setVoxel(x-1, y, z-1, 1, 100, 80, 10); //4 single brown voxels to branch the trunk nicely into the foliage
            Voxels.setVoxel(x-1, y, z+1, 1, 100, 80, 10); 
            Voxels.setVoxel(x+1, y, z+1, 1, 100, 80, 10); 
            Voxels.setVoxel(x+1, y, z-1, 1, 100, 80, 10); 

    Script.stop();
    }

    rezTree();

#43

makeCircle Script - Function to make a circle in voxels.

DOWNLOAD


#44

A script to create a checkered floor.
Default makes a 20 mr x 20 mtr black and white checkered floor using 1 meter voxels.

The script is scalable so long as you stick to an even number in the size variable.
This is to maintain the alternating flow, otherwise you end up with a striped floor instead (also useful sometimes).

Download link https://gist.github.com/Adrianl3d/1d91dde37c730f5c7337

And because its so tiny here is the script in text for your perusal, use, misuse and abuse.

// checkeredFloor.js
// Simple 20m x 20m floor with black and white checker pattern using 1 meter voxels.
// Scalable by changing value at "var size = " but must be an even number or the result will be stripes.
// Created by Adrian on 9 July 2014
// Copyright 2014 Adrian McCarlie
// Released under Creative Commons By Attribution Share Alike, feel free to modify and share.

// YOU MUST CHANGE these 3 values to suit the starting position (South/West corner) of the floor.
var X_POS = 200;
var Y_POS = 1000;
var Z_POS = 200;

//
var size = 20; // This must be an even number.
var color = 255;
//
var x = X_POS;
var y = Y_POS;
var z = Z_POS;

  function checkeredFloor ()
  {
  	while (z < (Z_POS + size))
  	{
  		while (x < (X_POS + size-1))
  		{
  			if (color == 1)
  			{
  				color = 255;
  			}
  			else 
  			{
  				color = 1;
  			} 
  			Voxels.setVoxel(x, y, z, 1, color, color, color);		
  			x++
  		}
  		x = X_POS;
  		z++;
  	}			
  Script.stop();			
  }
  checkeredFloor();

#45

Extracting an official chess board was slightly more challenging, 8x8 squares with the far right one being white.
I had to add another loop to fill the last line, I made this to be 1 meter by 0.125 thick, could easily be used as a tabletop.

Pic is a bit dodgy because there is the usual couple of dropouts but you get the idea.

download here https://gist.github.com/Adrianl3d/bbd6b38bd051188a29ae

// chessBoard.js
// Simple 1 meter square chessboard with black and white checker pattern using 0.125 meter voxels.
// Created by Adrian on 9 July 2014
// Copyright 2014 Adrian McCarlie
// Released under Creative Commons By Attribution Share Alike, feel free to modify and share.

// YOU MUST CHANGE these 3 values to suit the position of the chess board.
var X_POS = 200;
var Y_POS = 1000;
var Z_POS = 200;

//
var size = 1;
var color = 255;
//
var x = X_POS;
var y = Y_POS;
var z = Z_POS;

  function chessBoard()
  {
  	while (z < (Z_POS + size))
  	{
  		while (x < (X_POS + size-0.125))
  		{
  			if (color == 1){color = 255;}else {color = 1;}  
  			Voxels.setVoxel(x, y, z, 0.125, color, color, color);		
  			x=x+0.125;
  		}
  		x = X_POS;
  		z=z+0.125;
  	}
  	x = X_POS + 0.8750
  	z = Z_POS;
  	while (z < (Z_POS + size))	
  	{
  		if (color == 225){color = 1;}else {color = 225;}
  		Voxels.setVoxel(x, y, z, 0.125, color, color, color);
  		z=z+0.125;	
  	}	
  Script.stop();			
  }
  chessBoard();