13
2013
Halton Sequence c#

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);
}
}
}
Related Posts
11 Comments + Add Comment
Leave a comment to mgear
Recent posts
- Testing Unity AI Beta
- Ways to Support UnityCoder Development
- Using UI Slider to Create 5-Star Rating Element
- Game Music Library For Unity (editor plugin)
- Fontastic : Easily Test Fonts in Unity Editor!
- GeoTiff Importer & Terrain Generator for Unity
- Create Baked DropShadow for UI images
- .JP2 Ortho Image Converter to PNG/JPG/TIFF
- Convert LAS/LAZ/PLY pointclouds to GLTF (GLB) Point Meshes (standalone converter)
- Detect SRP (URP or HDRP) with Assembly Definition Version Defines
- [LudumDare57] Theme: Depths
- MotionVector Effect: Object “disappears” when paused
Recent Comments
- on [Asset Store] PolygonCollider2D Optimizer
- on Trajectory Test Scene 2.0
- on Vector3 maths for dummies!
- on UnityHub 3.6.0: Remove Version Control & Cloud Dashboard columns
- on Using RenderDoc with Unity (graphics debugger)
- on UI Scroll View automatic Content height
- on [Asset Store] Point Cloud Viewer & Tools
- on [Asset Store] Point Cloud Viewer & Tools
Coin:
CUgDSbRqFcAumDSAcdKDvuXsw26VdkJe8C8WGUQHBAGS
An article by












“Blue noise sample pattern”
http://www.cs.ubc.ca/~rbridson/docs/bridson-siggraph07-poissondisk.pdf
Distributing GameObjects evenly on a sphere:
http://forum.unity3d.com/threads/215479-Distributing-empty-GameObjects-evenly-on-a-sphere
Fast Uniform Poisson-Disk Sampling in C#:
http://theinstructionlimit.com/fast-uniform-poisson-disk-sampling-in-c
grass distribution:
http://mollyrocket.com/casey/stream_0015.html
poisson disk sampling: (unity)
http://gregschlom.com/devlog/2014/06/29/Poisson-disc-sampling-Unity.html
Object Placement with poisson disk sampling:
http://devmag.org.za/2009/05/03/poisson-disk-sampling/
random distribution on image density mask:
http://forum.unity3d.com/threads/help-with-random-distribution-script.277194/#post-1831558
distributing points on sphere (electrostatic repulsion)
http://nodename.com/blog/2008/09/19/distributing-points-on-the-sphere/
Math for Game Developers – Procedural Generation (Uniform Sampling of a Circle)
Math for Game Developers – Procedural Generation (White and Blue Noise)
Poisson-Disc (js)
http://bl.ocks.org/mbostock/19168c663618b7f07158
others : http://bl.ocks.org/mbostock
New: R2 Sequence
http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/