Skip to content

Instantly share code, notes, and snippets.

View Vercidium's full-sized avatar

Vercidium Vercidium

View GitHub Profile
@Vercidium
Vercidium / generateArms.cs
Last active May 4, 2019 02:36
Part 1 - Generating Galaxy Axis and Arms
Vector3 GetPoint()
{
Vector3 v;
double armDivisor = Math.PI / armCount;
while (true)
{
// Generate a random normalised point with a weighting
// towards the center controlled by the gravity variable
v = NextV3(-1, 1).Normalized() * Math.Pow(Next(0, 1.0), gravity);
@Vercidium
Vercidium / generateAxis.cs
Last active May 4, 2019 01:21
Generating Galaxy Axis from Stars
var v = GetPoint();
// Add some variance to each star
v += NextV3(-0.5, 0.5);
// Shift stars vertically as they get closer to the center of the galaxy
var s = beamHeight / v.Magnitude;
v.Y = Next(-s, s);
@Vercidium
Vercidium / armScaling.cs
Last active May 4, 2019 02:35
Arm Scaling
Vector3 GetPoint()
{
Vector3 v = Vector3.Zero;
// Generate a point
...
// Calculate the global angle of the point around the axis
var a = Math.Atan2(v.X, v.Z);
@Vercidium
Vercidium / armBending
Last active May 6, 2019 04:29
Arm Bending
Vector3 GetPoint()
{
// Generate the point
...
// Calculate arm scaling
...
// Rotate the point around the galaxy proportional to its magnitude
v *= Matrix4.CreateRotationY(-v.Magnitude * rotationStrength);
@Vercidium
Vercidium / verticalVariance
Last active May 3, 2019 03:44
Vertical Variance
Vector3 position = GetPoint();
position.Y -= heightMagnitude * Math.Sin(position.Magnitude * heightFrequency);
@Vercidium
Vercidium / colourInterpolation.cs
Last active May 4, 2019 02:20
Colour Interpolation
Vector3 position = GetPoint();
position.Y -= heightMagnitude * Math.Sin(position.Magnitude * heightFrequency);
// Colour each point based on their global angle around the axis
Color blendedColour;
var angle = Math.Atan2(position.X, position.Z);
// c1, c2, c3 and c4 are random colours generated at the start of the program
// Blend the colours of adjacent quadrants together
if (angle < -Math.PI / 2)
@Vercidium
Vercidium / multiple Layers
Last active May 4, 2019 02:38
Multiple Arm Layers
// Create multiple layers
for (int o = 0; o < layers; o++)
{
// Create the arms
for (int i = 0; i < 100000; i++)
{
Vector3 position = GetPoint();
position.Y -= heightMagnitude * Math.Sin(position.Magnitude * heightFrequency);
// Colour and store each point
static Matrix4F identityF = Matrix4.Identity.ToFloat();
// This is a combination of a scale, rotate and translate matrix. Rather than calculating three different matrices and multiplying
// them together, we can save float multiplications and additions by using this function
public static Matrix4F ParticleMatrix(float s, float rX, float rZ, Vector3 d)
{
var sX = GetArrayedSinCheap(rX);
var sZ = GetArrayedSinCheap(rZ);
var cX = GetArrayedCosCheap(rX);
var cZ = GetArrayedCosCheap(rZ);
@Vercidium
Vercidium / greedyvoxelmeshing
Last active May 26, 2024 17:17
Greedy Voxel Meshing ported to C#
// Code ported from https://0fps.net/2012/06/30/meshing-in-a-minecraft-game/
// Note this implementation does not support different block types or block normals
// The original author describes how to do this here: https://0fps.net/2012/07/07/meshing-minecraft-part-2/
const int CHUNK_SIZE = 32;
// These variables store the location of the chunk in the world, e.g. (0,0,0), (32,0,0), (64,0,0)
byte[] fileData = r.ReadBytes(bytesRemaining);
var b = 0;
for (int x = 0; x < Constants.MapSizeX; x++)
{
int chunkX = x / Constants.ChunkSize;
for (int z = 0; z < Constants.MapSizeZ; z++)
{
int chunkZ = z / Constants.ChunkSize;