Sep
13
2013

Halton Sequence c#

halton_sequence_unity_csharp

Halton Sequence script to Unity c#. (image: Left side halton sequence, right side using Random.value).

Needed something to distribute objects evenly on given area.. this works for small amount of objects <200, but if add more, they start to form lines(?)

Converted from:
“Halton Sequence Test” by Sean Davies, licensed under Creative Commons Attribution-Share Alike 3.0 and GNU GPL license.
Work: http://openprocessing.org/visuals/?visualID= 1781
License:  http://creativecommons.org/licenses/by-sa/3.0/ http://creativecommons.org/licenses/GPL/2.0/

Source c#:


// HaltonSequence.cs

using UnityEngine;
using System.Collections;

// converted to unity c# by http://unitycoder.com/blog
// original source: http://www.openprocessing.org/sketch/1920

public class HaltonSequence
{
public Vector3 m_CurrentPos = new Vector3(0.0f,0.0f,0.0f);
long m_Base2 = 0;
long m_Base3 = 0;
long m_Base5 = 0;

public long Increment()
{
float fOneOver3 = 1.0f/3.0f;
float fOneOver5 = 1.0f/5.0f;

long oldBase2 = m_Base2;
m_Base2++;
long diff = m_Base2 ^ oldBase2;

float s = 0.5f;

do
{
if ((oldBase2 & 1) == 1)
m_CurrentPos.x -= s;
else
m_CurrentPos.x += s;

s *= 0.5f;

diff = diff >> 1;
oldBase2 = oldBase2 >> 1;
}
while (diff > 0);

long bitmask = 0x3;
long bitadd  = 0x1;
s = fOneOver3;

m_Base3++;

while (true)
{
if ((m_Base3 & bitmask) == bitmask)
{
m_Base3 += bitadd;
m_CurrentPos.y -= 2 * s;

bitmask = bitmask << 2;
bitadd  = bitadd  << 2;

s *= fOneOver3;
}
else
{
m_CurrentPos.y += s;
break;
}
};
bitmask = 0x7;
bitadd  = 0x3;
long dmax = 0x5;

s = fOneOver5;

m_Base5++;

while (true)
{
if ((m_Base5 & bitmask) == dmax)
{
m_Base5 += bitadd;
m_CurrentPos.z -= 4 * s;

bitmask = bitmask << 3;
dmax = dmax << 3;
bitadd  = bitadd  << 3;

s *= fOneOver5;
}
else
{
m_CurrentPos.z += s;
break;
}
};

return m_Base2;
}

public void Reset()
{
m_CurrentPos.x = 0.0f;
m_CurrentPos.y = 0.0f;
m_CurrentPos.z = 0.0f;
m_Base2 = 0;
m_Base3 = 0;
m_Base5 = 0;
}
}

Source c# (for testing it)


// HaltonTest.cs

using UnityEngine;
using System.Collections;

public class HaltonTest : MonoBehaviour {

HaltonSequence positionsequence = new HaltonSequence();

void Start ()
{
float size = 20.0f*2;
positionsequence.Reset();
Vector3 position = Vector3.zero;
int amount = 200;
for(int i=0;i<amount;i++)
{
positionsequence.Increment();
//position.set(positionsequence.m_CurrentPos);
//Debug.Log(positionsequence.m_CurrentPos);
position = positionsequence.m_CurrentPos;
//position.x -=0.5f;
position.y = 0.0f;
//position.z -=0.5f;
position *= size;
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = position;
}
// random
for(int i=0;i<amount;i++)
{
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = new Vector3(Random.value*size,0,Random.value*size) + new Vector3(size+10,0,0);
}
}
}


11 Comments + Add Comment

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.