Skip to content

Commit

Permalink
Added sample documents
Browse files Browse the repository at this point in the history
  • Loading branch information
warrenm committed Nov 3, 2017
1 parent 2bac4e2 commit 7cd16cb
Show file tree
Hide file tree
Showing 29 changed files with 1,752 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ xcuserdata/
*.moved-aside
*.xccheckout
*.xcscmblueprint
.DS_Store

## Obj-C/Swift specific
*.hmap
Expand Down
109 changes: 109 additions & 0 deletions Samples/Boxiverse .stweak/Shaders.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
static float sphere( float3 p, float radius )
{
return length( p ) - radius;
}

static float vmax(float3 v) {
return max(max(v.x, v.y), v.z);
}

static float box(float3 p, float3 b) {
return vmax(abs(p) - b);
}

static float mod(float x, float y) {
return x - y * floor(x / y);
}

static float map(float3 p)
{
p -= float3(2, 2, 4);
p.x=mod(p.x + 5.0, 10.0) - 5.0;
p.y=mod(p.y + 5.0, 10.0) - 5.0;
p.z=mod(p.z + 5.0, 10.0) - 5.0;
return box( p, float3(3, 3, 1) );
}

static float3 getNormal( float3 p )
{
float3 e = float3( 0.001, 0.00, 0.00 );

float deltaX = map( p + e.xyy ) - map( p - e.xyy );
float deltaY = map( p + e.yxy ) - map( p - e.yxy );
float deltaZ = map( p + e.yyx ) - map( p - e.yyx );

return normalize( float3( deltaX, deltaY, deltaZ ) );
}

static float trace( float3 origin, float3 direction, thread float3 &p)
{
float totalDistanceTraveled = 0.0;

for( int i=0; i < 500; ++i)
{
p = origin + direction * totalDistanceTraveled * .95;

float distanceFromPointOnRayToClosestObjectInScene = map( p );
totalDistanceTraveled += distanceFromPointOnRayToClosestObjectInScene;

if( distanceFromPointOnRayToClosestObjectInScene < 0.0001 )
{
break;
}

if( totalDistanceTraveled > 10000.0 )
{
totalDistanceTraveled = 0.0000;
break;
}
}

return totalDistanceTraveled;
}

static float3 calculateLighting(float3 pointOnSurface, float3 surfaceNormal, float3 lightPosition, float3 cameraPosition)
{
float3 fromPointToLight = normalize(lightPosition - pointOnSurface);
float diffuseStrength = clamp( dot( surfaceNormal, fromPointToLight ), 0.0, 1.0 );

float3 diffuseColor = diffuseStrength * float3( 1.0, 0.0, 0.0 );
float3 reflectedLightVector = normalize( reflect( -fromPointToLight, surfaceNormal ) );

float3 fromPointToCamera = normalize( cameraPosition - pointOnSurface );
float specularStrength = pow( clamp( dot(reflectedLightVector, fromPointToCamera), 0.0, 1.0 ), 10.0 );

specularStrength = min( diffuseStrength, specularStrength );
float3 specularColor = specularStrength * float3( 1.0 );

float3 finalColor = diffuseColor + specularColor;

return finalColor;
}

fragment half4 fragment_texture(ProjectedVertex in [[stage_in]],
constant Uniforms &uniforms [[buffer(0)]])
{
float2 uv = in.texCoords.xy * 2.0 - 1.0;

uv.x *= (iResolution.x / iResolution.y);

float2 mouse = (iTouches[0].xy / iResolution.xy) + float2(0.7);
mouse *= 0.2;

float3 cameraPosition = float3(-mouse.x * 20.0 - 10.0, -mouse.y * 20.0 - 10.0, -5 );

float3 cameraDirection = normalize( float3( uv.x, uv.y, 1.0) );

float3 pointOnSurface;
float distanceToClosestPointInScene = trace( cameraPosition, cameraDirection, pointOnSurface );

float3 finalColor = float3(0.0);
if( distanceToClosestPointInScene > 0.0 )
{
float3 lightPosition = float3( 0.0, 4.5, -10.0 );
float3 surfaceNormal = getNormal( pointOnSurface );
finalColor = calculateLighting( pointOnSurface, surfaceNormal, lightPosition, cameraPosition );
}

return half4(float4( finalColor, 1.0));
}
Binary file added Samples/Boxiverse .stweak/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions Samples/Colors1.stweak/Shaders.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
constexpr sampler sampler2d(coord::normalized, filter::linear, address::repeat);

fragment half4 fragment_texture(
ProjectedVertex in [[stage_in]],
constant Uniforms &uniforms [[buffer(0)]],
texture2d<float, access::sample> texture0 [[texture(0)]],
texture2d<float, access::sample> texture1 [[texture(1)]],
texture2d<float, access::sample> texture2 [[texture(2)]],
texture2d<float, access::sample> texture3 [[texture(3)]])
{
return half4(half2(in.texCoords), abs(cos(iGlobalTime)), 1);
}
Binary file added Samples/Colors1.stweak/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions Samples/Fractal Needles.stweak/Shaders.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
constant float BASE_ANGLE = 3.5;
constant float ANGLE_DELTA = 0.02;
constant float XOFF = 0.7;

static float2x2 mm2(float a){
float c = cos(a), s = sin(a);
float2x2 m;
m[0].x = c;
m[0].y = -s;
m[1].x = s;
m[1].y = c;
return m;//float2x2(c,-s,s,c);
}

static float fun(float2 p, float t, float featureSize)
{
p.x = sin(p.x*1.0+t*1.2)*sin(t+p.x*0.1) * 2;
p += float2(sin(p.x * 1.5) * 0.1);

return smoothstep(0.0, featureSize, abs(p.y));
}

fragment half4 fragment_texture(ProjectedVertex in [[stage_in]],
constant Uniforms &uniforms [[buffer(0)]])
{
float aspect = iResolution.x/iResolution.y;
float featureSize = 120./((iResolution.x*aspect+iResolution.y));

float2 p = in.texCoords.xy * 6.5 - 2.3;
p.x *= aspect;
p.y = abs(p.y);

float3 col( 0, 0, 0);

for(float i=0.;i<26.;i++)
{
float3 col2;
col2 = (sin(float3(3.3,2.5,2.2) + float3(i) * 0.15) * 0.5 + 0.54) * (1 - fun(p, iGlobalTime, featureSize));
col = max(col,col2);

p.x -= XOFF;
p.y -= sin(iGlobalTime*0.11+1.5)*1.5+1.5;
p*= mm2(i*ANGLE_DELTA+BASE_ANGLE);

float2 pa = float2(abs(p.x-.9),abs(p.y));
float2 pb = float2(p.x,abs(p.y));

p = mix(pa,pb,smoothstep(-.07,.07,sin(iGlobalTime*0.24)+.1));
}

return half4(half3(col), 1.0);

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
140 changes: 140 additions & 0 deletions Samples/Metamarble.stweak/Shaders.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
constant const float kMinDist = 0;
constant const float kMaxDist = 50;
constant const float kTiny = 0.001;
constant const int kMaxMarchingSteps = 200;

float fSphere(float r, float3 pos)
{
return length(pos) - r;
}

static void pR(thread float2 &p, float a) {
p = cos(a)*p + sin(a)*float2(p.y, -p.x);
}

// Shortcut for 45-degrees rotation
static void pR45(thread float2 &p) {
p = (p + float2(p.y, -p.x))*sqrt(0.5);
}

static float sceneSDF(float3 pos, float t) {
float r = 1.4 * abs(cos(t)) + 0.3;
float s = 4.5;
float s1 = fSphere(r, pos + s*float3( -0.30, 0.2*cos(1.4*t), -0.25));
float s2 = fSphere(r, pos + s*float3( 0, 0.23*cos(t), 0.25));
float s3 = fSphere(r, pos + s*float3( 0.25, 0.19*cos(0.7*t), -0.33));
float s4 = fSphere(r, pos + s*float3( 0.60, 0, 0.25));
float s5 = fSphere(r, pos + s*float3( -0.70, 0.7, -0.50));
float s6 = fSphere(r, pos + s*float3( -0.25, 0.7, 0.25));
float s7 = fSphere(r, pos + s*float3( 0.60, -0.7, 0.50));
float s8 = fSphere(r, pos + s*float3( -0.33, -0.7, -0.25));
float s9 = fSphere(r, pos + s*float3( -0.75, -0.7, 0.50));
float k = 2.8;
return -log(exp(-k*s1) + exp(-k*s2) + exp(-k*s3) +
exp(-k*s4) + exp(-k*s5) + exp(-k*s6) +
exp(-k*s7) + exp(-k*s8) + exp(-k*s9)) / k;
}

float3 estimatedNormal(float3 pos, float time) {
return normalize(float3(
sceneSDF(float3(pos.x + kTiny, pos.y, pos.z), time) - sceneSDF(float3(pos.x - kTiny, pos.y, pos.z), time),
sceneSDF(float3(pos.x, pos.y + kTiny, pos.z), time) - sceneSDF(float3(pos.x, pos.y - kTiny, pos.z), time),
sceneSDF(float3(pos.x, pos.y, pos.z + kTiny), time) - sceneSDF(float3(pos.x, pos.y, pos.z - kTiny), time)));
}

float raymarch(float3 rayOrigin, float3 rayDir, float min, float max, float time) {
float param = min;
for (int i = 0; i < kMaxMarchingSteps; ++i) {
float dist = sceneSDF(rayOrigin + param * rayDir, time);
if (dist < kTiny) {
return param;
}
param += dist;
if (param >= max) {
return max;
}
}
return max;
}

float3 brdf(float3 pos, float3 norm, float3 eye) {
float ambient = 0.2;
float specPow = 120;
float3 specColor(0.8, 0.8, 0.8);
float gray = 0.1 + 0.85*smoothstep(0.45, 0.55, fmod(abs(pos.y * 2), 1));
if (gray < 0.5) specPow = 500;
float3 surfColor(gray);

float3 light1Color(0.6, 0.6, 0.5);
float3 light1Dir = normalize(float3(-2, -2, -1));
float3 halfway = normalize(-light1Dir + normalize(eye - pos));
float intens1 = saturate(dot(-light1Dir, norm));
float hdotn = saturate(dot(halfway, norm));
float specIntens1 = powr(hdotn, specPow);

float3 light2Color(0.5, 0.5, 0.6);
float3 light2Dir = normalize(float3(2, -2, -1));

halfway = normalize(-light2Dir + normalize(eye - pos));
float intens2 = saturate(dot(-light2Dir, norm));
hdotn = saturate(dot(halfway, norm));
float specIntens2 = powr(hdotn, specPow);

return intens1 * light1Color * surfColor +
specIntens1 * specColor +
intens2 * light2Color * surfColor +
specIntens2 * specColor +
ambient * surfColor;
}

static float4x4 rotateY(float theta) {
float c = cos(theta);
float s = sin(theta);

return float4x4(
float4(c, 0, s, 0),
float4(0, 1, 0, 0),
float4(-s, 0, c, 0),
float4(0, 0, 0, 1));
}

static float4x4 viewMatrix(float3 eye, float3 center, float3 up) {
auto f = normalize(center - eye);
auto s = cross(f, up);
auto u = cross(s, f);
return float4x4(
float4(s, 0.0),
float4(u, 0.0),
float4(-f, 0.0),
float4(0.0, 0.0, 0.0, 1)
);
}

float radians(float deg) { return deg * (3.14159 / 180); }

float3 rayDirection(float fov, float2 dims, float2 coords) {
float2 uv = coords - 0.5 * dims;
float z = dims.y / tan(radians(fov) * 0.5);
return normalize(float3(uv.x, -uv.y, -z));
}

fragment half4 fragment_texture(ProjectedVertex in [[stage_in]],
constant Uniforms &uniforms [[buffer(0)]])
{
float rad = 3 * sin(2 * iGlobalTime * 0) + 10;
//float3 eye(rad * cos(iGlobalTime), 0, rad * sin(iGlobalTime));
float3 eye(5, 8, -32);
float3 at(0, 0, 0);
float3 up(0, 1, 0);

float3 dir = rayDirection(45.0, iResolution.xy, in.position.xy);
dir = (viewMatrix(eye, at, up) * float4(dir, 1)).xyz;
float dist = raymarch(eye, dir, kMinDist, kMaxDist, iGlobalTime);
if (dist > kMaxDist - kTiny) {
return half4(0.98, 0.9, 0.9, 1);
}
float3 intersect = eye + dist * dir;
float3 normal = estimatedNormal(eye + dist * dir, iGlobalTime);
float3 color = brdf(intersect, normal, eye);
return half4(half3(color), 1);
}
Binary file added Samples/Metamarble.stweak/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions Samples/Multitouch 3.stweak/Shaders.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
fragment half4 fragment_texture(ProjectedVertex in [[stage_in]],
constant Uniforms &uniforms [[buffer(0)]])
{
float rad = 50;
float2 p = in.position.xy * 0.5 + 1;

float usd = 100;
for (int i = 0; i < 10; ++i) {
if (iTouches[i].w) {
float2 center = iTouches[i].xy;
float sdf = length(center - p) - rad;
usd = min(usd, sdf);
}
}

float4 inc(1, 1, 1, 1);
float4 outc(0, 0, 0, 1);
float mx = smoothstep(0, 1, usd / 100);
float4 color = mix(inc, outc, mx);

return half4(color);
}
Binary file added Samples/Multitouch 3.stweak/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions Samples/Noah Chromatic Rings.stweak/Shaders.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
static float f(float2 uv, float time)
{
float dist = length(uv);

const float pi = 3.14159;
const float ringIndexMultiplier = 6.0;
float ang = atan2(uv.y, uv.x) / pi + 1.0;
float ringIndex = ceil(dist * ringIndexMultiplier);
float direction = (fmod(ringIndex, 2.0) * 2.0 - 1.0);
float v = fmod(floor(ang * 20.0 + pow(ringIndex, 1.1) + time * direction), 2.0);

//uv = vec2(pow(abs(uv.x), 1.1), pow(abs(uv.y), 1.1));
uv = abs(uv);
float2 dotUV = fract((uv + time * 0.05) * 15.0) - 0.5;
float dotRadius = 0.25 + 0.1 * sin(time * 0.5 + v * pi * 0.6);
float dotValue = smoothstep(0.03, 0.05, length(dotUV) - dotRadius);

v = mix(1.0 - dotValue, dotValue, v);
v *= smoothstep(0.9, 1.0, dist * ringIndexMultiplier);
v += 1.0 - smoothstep(0.2, 0.21, abs(1.0 - fract(dist * ringIndexMultiplier)));
return v;
}

fragment half4 fragment_texture(ProjectedVertex in [[stage_in]],
constant Uniforms &uniforms [[buffer(0)]])
{
float aspect = iResolution.x / iResolution.y;
float2 uv = in.texCoords.xy * 2 - float2(1);
uv.x *= aspect;

float a = 27;

float r = f(uv, iGlobalTime);
float g = f(uv, iGlobalTime + 0.01 * a);
float b = f(uv, iGlobalTime - 0.01 * a);

return half4(r, g, b, 1.0);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions Samples/Noisy.stweak/Shaders.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
constexpr sampler sampler2d(coord::normalized, filter::linear, address::repeat);

fragment half4 fragment_texture(ProjectedVertex in [[stage_in]],
constant Uniforms &uniforms [[buffer(0)]],
texture2d<float, access::sample> texture0 [[texture(0)]])
{
float4 c = texture0.sample(sampler2d, in.texCoords / 1);
return half4(c);
}
Binary file added Samples/Noisy.stweak/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 7cd16cb

Please sign in to comment.