Skip to content

Instantly share code, notes, and snippets.

@GarethIW
Created March 25, 2019 08:27
Show Gist options
  • Save GarethIW/b4d2233f9bbaf078cd87f02e9a6bc3a8 to your computer and use it in GitHub Desktop.
Save GarethIW/b4d2233f9bbaf078cd87f02e9a6bc3a8 to your computer and use it in GitHub Desktop.
Constructor with step
/////////////////////////////////////////////////////////////////////////
//
// PicaVoxel - The tiny voxel engine for Unity - http://picavoxel.com
// By Gareth Williams - @garethiw - http://gareth.pw
//
// Source code distributed under standard Asset Store licence:
// http://unity3d.com/legal/as_terms
//
/////////////////////////////////////////////////////////////////////////
using PicaVoxel;
using UnityEngine;
using System.Collections;
namespace PicaVoxel
{
[AddComponentMenu("PicaVoxel/Utilities/Constructor")]
public class Constructor : MonoBehaviour
{
public Volume PicaVoxelVolume;
public float StepDelay = 0.02f;
public float StartRadius = 0f;
public float StopRadius = 0f;
public int StepVoxels = 2;
private float radius;
private float currentTime = 0f;
private Voxel[] originalVoxels;
// Hold the to-be-activated voxel's indexes
int currX = 0, currY = 0, currZ = 0;
// Use this for initialization
private void Start()
{
if (PicaVoxelVolume == null) return;
originalVoxels = new Voxel[PicaVoxelVolume.XSize*PicaVoxelVolume.YSize*PicaVoxelVolume.ZSize];
Helper.CopyVoxelsInBox(ref PicaVoxelVolume.GetCurrentFrame().Voxels, ref originalVoxels,
new PicaVoxelPoint(PicaVoxelVolume.XSize, PicaVoxelVolume.YSize, PicaVoxelVolume.ZSize),
new PicaVoxelPoint(PicaVoxelVolume.XSize, PicaVoxelVolume.YSize, PicaVoxelVolume.ZSize), true);
PicaVoxelVolume.GetCurrentFrame().Voxels =
new Voxel[PicaVoxelVolume.XSize*PicaVoxelVolume.YSize*PicaVoxelVolume.ZSize];
PicaVoxelVolume.UpdateAllChunks();
radius = StartRadius;
}
// Update is called once per frame
private void Update()
{
if (PicaVoxelVolume == null) return;
Frame frame = PicaVoxelVolume.GetCurrentFrame();
currentTime += Time.deltaTime;
if (currentTime >= StepDelay)
{
// Construct one voxel each step
for(var i=0;i<StepVoxels;i++)
Construct ();
currentTime = 0;
frame.UpdateChunks(false);
}
}
private void Construct()
{
Vector3 posZero = PicaVoxelVolume.transform.position +
(transform.rotation*
(-PicaVoxelVolume.Pivot + (Vector3.one*(PicaVoxelVolume.VoxelSize*0.5f))));
Vector3 oneX = PicaVoxelVolume.transform.rotation*(new Vector3(PicaVoxelVolume.VoxelSize, 0, 0));
Vector3 oneY = PicaVoxelVolume.transform.rotation*(new Vector3(0f, PicaVoxelVolume.VoxelSize, 0));
Vector3 oneZ = PicaVoxelVolume.transform.rotation*(new Vector3(0, 0, PicaVoxelVolume.VoxelSize));
Vector3 constructorPosition = transform.position;
Frame frame = PicaVoxelVolume.GetCurrentFrame();
// Try and activate the next voxel if we've not seen all voxels yet
if (currY < PicaVoxelVolume.YSize) {
Vector3 checkPos = posZero;
Vector3 xmult = oneX * currX;
Vector3 ymult = oneY * currY;
Vector3 zmult = oneZ * currZ;
checkPos.x = posZero.x + xmult.x + ymult.x + zmult.x;
checkPos.y = posZero.y + xmult.y + ymult.y + zmult.y;
checkPos.z = posZero.z + xmult.z + ymult.z + zmult.z;
if (!frame.Voxels [currX + PicaVoxelVolume.XSize * (currY + PicaVoxelVolume.YSize * currZ)].Active &&
originalVoxels [currX + PicaVoxelVolume.XSize * (currY + PicaVoxelVolume.YSize * currZ)].Active) {
frame.Voxels [currX + PicaVoxelVolume.XSize * (currY + PicaVoxelVolume.YSize * currZ)] =
originalVoxels [currX + PicaVoxelVolume.XSize * (currY + PicaVoxelVolume.YSize * currZ)];
frame.SetChunkAtVoxelPositionDirty (currX, currY, currZ);
}
// Look for the next active voxel in the original voxel array which we copied initially
do {
if (currY >= PicaVoxelVolume.YSize) {
break;
}
// The order of iteration is x, z and y axes
currX += 1;
if (currX >= PicaVoxelVolume.XSize) {
currX = 0;
currZ += 1;
if (currZ >= PicaVoxelVolume.ZSize) {
currZ = 0;
currY += 1;
}
}
} while (!originalVoxels [currX + PicaVoxelVolume.XSize * (currY + PicaVoxelVolume.YSize * currZ)].Active);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment