Skip to content

Instantly share code, notes, and snippets.

@evilactually
Created February 12, 2024 04:21
Show Gist options
  • Save evilactually/f142b526b4591854aaeeb1f169304cfe to your computer and use it in GitHub Desktop.
Save evilactually/f142b526b4591854aaeeb1f169304cfe to your computer and use it in GitHub Desktop.
from sdf import *
def smoothstep(x, edge0=0, edge1=1):
# Scale x to the range between edge0 and edge1
x = np.clip((x - edge0) / (edge1 - edge0), 0.0, 1.0)
# Smooth interpolation function (Hermite interpolation)
return x * x * (3 - 2 * x)
def _vec(*arrs):
return np.stack(arrs, axis=-1)
def _length(a):
return np.linalg.norm(a, axis=1)
def sdfSurface2(p):
p = p + _vec(1.9, 0.0, 0.0)
return p[:,1] - (-3.65 + 0.2*np.power(p[:,0],2.0) + 2.0*p[:,1] )
def sdfEllipsoid(p, r):
k0 = _length(p/r)
k1 = _length(p/(r*r))
return k0*(k0-1.0)/k1
@sdf3
def vase(radius=1, center=ORIGIN):
def f(p):
x = p[:,0]
y = p[:,1]*0.4
z = p[:,2]
angle = np.arctan2(z,x)
r = 0.5 + 0.3*np.square(y) + 0.06*np.sin(y*8.0 + angle*10.0)
d = _length(p[:,[0,2]]) - r
thickness = 0.05*smoothstep(y/0.4,4.2,2.0);
bottom = 3.0 - 0.1*2.0
d = np.where(p[:,1] > -bottom,
np.abs(d) - thickness,
np.where(d - thickness + 0.01 < 0, p[:,1] + bottom, d - thickness))
d = np.maximum(p[:,1]-3.7,d)
d = np.maximum(-p[:,1]-3.0,d)
d = np.maximum(d, -sdfSurface2(p) )
#d = np.maximum(-p[:,0]-0.0,d)
er = 1.3
d = np.maximum(d, -sdfEllipsoid(p + _vec(0.0,3.5-0.1,0.0), _vec(er,0.5,er)))
return d
return f
f = vase(1)
f.save('vase.stl', step=0.01,bounds=((-4, -5, -4), (4, 5, 4)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment