Skip to content

Instantly share code, notes, and snippets.

@PossiblyAShrub
Created June 16, 2020 23:40
Show Gist options
  • Save PossiblyAShrub/42f446bc2956c3d1800da7f5e111086e to your computer and use it in GitHub Desktop.
Save PossiblyAShrub/42f446bc2956c3d1800da7f5e111086e to your computer and use it in GitHub Desktop.
Donut Shader for MagicaVoxel
// Made By @PossiblyAShrub
// Icing Ratio ----------------- controls how much of the donut has icing on it
// Wavy-ness ------------------- controls how wavy the edges of the donut is
// Replace Exisitng Voxels ---- if 0, then the donut SDF brush will replace voxels in it's select area with air
// xs_begin
// author : '@PossiblyAShrub'
// arg : {id = '0' name = 'Icing Ratio' value = '0.4' range = '0 1' step = '0.1' decimal = '2' }
// arg : {id = '1' name = 'Wavy-ness' value = '4' range = '0 8' step = '0.5' decimal = '1' }
// arg : {id = '2' name = 'Replace Existing Voxels' value = '1' range = '0 1' step = '1' decimal = '0' }
// xs_end
//===== user args =====
// uniform float i_args[8];
//===== built-in args =====
// uniform vec3 i_volume_size; // volume size [1-256]
// uniform float i_color_index; // color index [0-255]
// uniform vec3 i_mirror; // mirror mode [0,1]
// uniform vec3 i_axis; // axis mode [0,1]
// uniform float i_iter; // iteration index
//===== built-in functions =====
// float voxel( vec3 v ); // get voxel color index at position v
// returns rand val between 0 and 1 --- essentialy a hash function
float rand(float seed) {
return fract(sin(dot(vec2(seed, -seed), vec2(12.9898, 4.1414))) * 43758.5453);
}
// returns true is voxel should be given the icing color
bool isIcing(vec3 pos, float radius) {
int squiggleMaskX = floor(sin(pos.x) * i_args[1] + (i_args[0] * radius)) + clamp(rand(length(pos)), 4, 7);
int squiggleMaskY = floor(sin(pos.y) * i_args[1] + (i_args[0] * radius)) + clamp(rand(length(pos)), 4, 7);
int squiggleMask = mix(squiggleMaskX, squiggleMaskY, 0.5);
if (pos.z < radius + 1 && pos.z > radius - squiggleMask) return true;
return false;
}
// generate a new voxel color index [0, 255] at position v ( v is at the center of voxel, such as vec3( 1.5, 2.5, 4.5 ) )
float map(vec3 v) {
vec3 center = i_volume_size * 0.5;
vec3 p = ( v - center );
float radius = i_volume_size * 0.2 + clamp(rand(i_args[2]), -2, 2);
if( length(vec2(p.z, length(p.xy) - min(center.x,center.y) + radius)) - radius < 0.5) {
if (isIcing(p, radius)) return i_color_index + 1;
return i_color_index;
}
if (i_args[2] == 0) return 0;
return voxel(v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment