# how do I calculate an evenly spaced grid inside a vector3?

Forgive me if this is a noob question but I am having a problem that I just cannot seem to wrap my head around.

I have a requirement where scene objects need to be procedurally generated at runtime and placed inside my scene inside 'spawning areas'. I figured that I would allow the level designer to determine the size and positioning of these spawning areas, along with the grid size.

What is the best method to generate an evenly spaced grid inside a vector3?

Any assistance would be greatly appreciated.

This editor script that was posted to the forums should get you started...

``````    /*
Author: Matthew J. Colins (Dl)
Date: 12.21.09
http://www.DeleVinciStudios.com
*/

// align in the x translation axis
static function AlignmentTransX () {
// execute alignment for the x axis
AlignOrDistribute(false, "transX");
}

// determine if the function can be executed.
static function ValidateAlignmentTransX () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

// align in the y translation axis
static function AlignmentTransY () {
// execute alignment for the y axis
AlignOrDistribute(false, "transY");
}

// determine if the function can be executed.
static function ValidateAlignmentTransY () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

// align in the z translation axis
static function AlignmentTransZ () {
// execute alignment for the z axis
AlignOrDistribute(false, "transZ");
}

// determine if the function can be executed.
static function ValidateAlignmentTransZ () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

// align the rotation
static function AlignmentRotation () {
// execute alignment in all axes
AlignOrDistribute(false, "rotAll");
}

// determine if the function can be executed.
static function ValidateAlignmentRotation () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

// align in the x scale axis
static function AlignmentScaleX () {
// execute alignment for the x axis
AlignOrDistribute(false, "scaleX");
}

// determine if the function can be executed.
static function ValidateAlignmentScaleX () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

// align in the y scale axis
static function AlignmentScaleY () {
// execute alignment for the y axis
AlignOrDistribute(false, "scaleY");
}

// determine if the function can be executed.
static function ValidateAlignmentScaleY () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

// align in the z scale axis
static function AlignmentScaleZ () {
// execute alignment for the z axis
AlignOrDistribute(false, "scaleZ");
}

// determine if the function can be executed.
static function ValidateAlignmentScaleZ () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

// distribute in the x translation axis
static function DistributeTransX () {
// execute distribution for the x axis
AlignOrDistribute(true, "transX");
}

// determine if the function can be executed.
static function ValidateDistributeTransX () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

// distribute in the y translation axis
static function DistributeTransY () {
// execute distribution for the y axis
AlignOrDistribute(true, "transY");
}

// determine if the function can be executed.
static function ValidateDistributeTransY () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

// distribute in the z translation axis
static function DistributeTransZ () {
// execute distribution for the z axis
AlignOrDistribute(true, "transZ");
}

// determine if the function can be executed.
static function ValidateDistributeTransZ () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

// distribute the rotation
static function DistributeRotation () {
// execute distribution in all axes
AlignOrDistribute(true, "rotAll");
}

// determine if the function can be executed.
static function ValidateDistributeRotation () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

// distribute in the x scale axis
static function DistributeScaleX () {
// execute distribution for the x axis
AlignOrDistribute(true, "scaleX");
}

// determine if the function can be executed.
static function ValidateDistributeScaleX () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

// distribute in the y scale axis
static function DistributeScaleY () {
// execute distribution for the y axis
AlignOrDistribute(true, "scaleY");
}

// determine if the function can be executed.
static function ValidateDistributeScaleY () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

// distribute in the z scale axis
static function DistributeScaleZ () {
// execute distribution for the z axis
AlignOrDistribute(true, "scaleZ");
}

// determine if the function can be executed.
static function ValidateDistributeScaleZ () {
// only return true if there is a transform in the selection.
return (Selection.activeTransform != null);
}

static function AlignOrDistribute(shouldDist, theAxis){

// create some variables to store values
var firstObj = Selection.activeTransform;
var furthestObj = firstObj;
var firstVal = 0.0;
var furthestVal = 0.0;
var curDist = 0.0;
var lastDist = 0.0;
var selCount = 0;

// collect the number of tranforms in the selection and find the object that is furthest away from the active selected object
for (var transform in Selection.transforms)
{
// collect the current distance
curDist = Vector3.Distance(firstObj.position, transform.position);

// get the object with the greatest distance from the first selected object
if (curDist > lastDist)
{
furthestObj = transform;
lastDist = curDist;
}

// increment count
selCount += 1;
}

// distribute or align?
if (shouldDist)
{
// collect the first value and furthest value to distribute between
switch (theAxis)
{
case "transX":
firstVal = firstObj.position.x;
furthestVal = furthestObj.position.x;
break;
case "transY":
firstVal = firstObj.position.y;
furthestVal = furthestObj.position.y;
break;
case "transZ":
firstVal = firstObj.position.z;
furthestVal = furthestObj.position.z;
break;
case "scaleX":
firstVal = firstObj.localScale.x;
furthestVal = furthestObj.localScale.x;
break;
case "scaleY":
firstVal = firstObj.localScale.y;
furthestVal = furthestObj.localScale.y;
break;
case "scaleZ":
firstVal = firstObj.localScale.z;
furthestVal = furthestObj.localScale.z;
break;
default:
break;
}

// calculate the spacing for the distribution
var objSpacing = (firstVal - furthestVal) / (selCount - 1);
var curSpacing = objSpacing;
var rotSpacing = 1.0 / (selCount - 1);
var curRotSpacing = rotSpacing;

// update every object in the selection to distribute evenly
for (var transform in Selection.transforms)
{
switch (theAxis)
{
case "transX":
if ((transform != firstObj) && (transform != furthestObj))
{
transform.position.x = firstVal - curSpacing;
curSpacing += objSpacing;
}
break;
case "transY":
if ((transform != firstObj) && (transform != furthestObj))
{
transform.position.y = firstVal - curSpacing;
curSpacing += objSpacing;
}
break;
case "transZ":
if ((transform != firstObj) && (transform != furthestObj))
{
transform.position.z = firstVal - curSpacing;
curSpacing += objSpacing;
}
break;
case "rotAll":
if ((transform != firstObj) && (transform != furthestObj))
{
transform.rotation = Quaternion.Slerp (firstObj.rotation, furthestObj.rotation, curRotSpacing);
curRotSpacing += rotSpacing;
}
break;
case "scaleX":
if ((transform != firstObj) && (transform != furthestObj))
{
transform.localScale.x = firstVal - curSpacing;
curSpacing += objSpacing;
}
break;
case "scaleY":
if ((transform != firstObj) && (transform != furthestObj))
{
transform.localScale.y = firstVal - curSpacing;
curSpacing += objSpacing;
}
break;
case "scaleZ":
if ((transform != firstObj) && (transform != furthestObj))
{
transform.localScale.z = firstVal - curSpacing;
curSpacing += objSpacing;
}
break;
default:
break;
}
}
}
else
{
// snap every object in the selection to the first objects value
for (var transform in Selection.transforms)
{
switch (theAxis)
{
case "transX":
transform.position.x = firstObj.position.x;
break;
case "transY":
transform.position.y = firstObj.position.y;
break;
case "transZ":
transform.position.z = firstObj.position.z;
break;
case "rotAll":
transform.rotation = firstObj.rotation;
break;
case "scaleX":
transform.localScale.x = firstObj.localScale.x;
break;
case "scaleY":
transform.localScale.y = firstObj.localScale.y;
break;
case "scaleZ":
transform.localScale.z = firstObj.localScale.z;
break;
default:
break;
}
}
}
}

``````

I think you should explain what you mean by "evenly spaced grid inside a vector3". Vector3 is just a data type. Rephrase your question and describe your problem, not what you did.

PS: Sorry to admins for posting this as an answer - but I don't have the ability to comment yet.