先看个三维曲线的代码:
圆环面螺线:
#http://xuxzmail.blog.163.com/blog/static/25131916200976114621705/ #Toroidal spiral vertices = 1000 t = from 0 to (2*PI) r = 5 n = 20 x = (r+sin(20*t))*cos(t) y = (r+sin(20*t))*sin(t) z = cos(n*t) r = 10; x = x*r y = y*r z = z*r
圆环面螺线管(极坐标原理):
#http://xuxzmail.blog.163.com/blog/static/25131916200976114621705/ #Toroidal spiral vertices = D1:36 D2:720 u = from 0 to (2*PI) D1 v = from 0 to (2*PI) D2 r = 5 n = 20 xq = (r+sin(20*v))*cos(v) yq = (r+sin(20*v))*sin(v) zq = cos(n*v) #由线变管 xd = array_difference(xq, 36) yd = array_difference(yq, 36) zd = array_difference(zq, 36) len = sqrt(xd*xd + yd*yd + zd*zd) a = xd/len b = yd/len c = zd/len radius =0.2 ac = sqrt(a*a + c*c) angleY = atan2(ac, b) angleXZ = atan2(a,c) m = cos(u) * radius n = sin(u) * radius e = sin(angleY) f = cos(angleY) g = sin(angleXZ) h = cos(angleXZ) x0 = m y0 = -n*e z0 = n*f x1 = x0*h + z0*g y1 = y0 z1 = -x0*g + z0*h x = x1 + xq y = y1 + yq z = z1 + zq
圆环面螺线管(矩阵原理):
#http://xuxzmail.blog.163.com/blog/static/25131916200976114621705/ #Toroidal spiral vertices = D1:36 D2:720 u = from 0 to (2*PI) D1 v = from 0 to (2*PI) D2 r = 5 n = 20 xq = (r+sin(20*v))*cos(v) yq = (r+sin(20*v))*sin(v) zq = cos(n*v) #由线变管 xd = array_difference(xq, 36) yd = array_difference(yq, 36) zd = array_difference(zq, 36) len = sqrt(xd*xd + yd*yd + zd*zd) len = max(len, 0.00001) a = xd/len b = yd/len c = zd/len len = sqrt(b*b + c*c) len = max(len, 0.00001) m = c/len n = -b/len i = b*n - c*m j = - a*n k = a*m radius = 0.2 x0 = cos(u) * radius z0 = sin(u) * radius x = x0*i + xq y = x0*j + z0*m + yq z = x0*k + z0*n + zq
这是我所写的最复杂的脚本代码了,为了写这种脚本,我不惜将脚本解析器实现对自定义变量的支持.
不过这两种写法都会有点瑕疵,因为在某个情况下拐点时,会出现扭曲,这种瑕疵似乎很难解决.
再举个节的例子:
line_torus_knot(37)
vertices = 10000 t = from 0 to (6*PI) p = 3 q = 7 r = 2 + cos(q/p*t) x = r*sin(t) y = sin(q/p*t) z = r*cos(t) r = 0.5 + 0.5*sin(t) g = 0.5 + 0.5*y b = 0.5 + 0.5*cos(t)
pipe_torus_knot(37)极坐标原理
vertices = D1:36 D2:360 u = from 0 to (2*PI) D1 v = from 0 to (6*PI) D2 p = 3 q = 7 r = 2 + cos(q/p*v) xq = r*sin(v) yq = sin(q/p*v) zq = r*cos(v) r = 0.5 + 0.5*sin(v) g = 0.5 + 0.5*yq b = 0.5 + 0.5*cos(v) #由线变管 xd = array_difference(xq, 36) yd = array_difference(yq, 36) zd = array_difference(zq, 36) len = sqrt(xd*xd + yd*yd + zd*zd) a = xd/len b = yd/len c = zd/len radius = 0.2 ac = sqrt(a*a + c*c) angleY = atan2(ac, b) angleXZ = atan2(a,c) m = cos(u) * radius n = sin(u) * radius e = sin(angleY) f = cos(angleY) g = sin(angleXZ) h = cos(angleXZ) x0 = m y0 = -n*e z0 = n*f x1 = x0*h + z0*g y1 = y0 z1 = -x0*g + z0*h x = x1 + xq y = y1 + yq z = z1 + zq
pipe_torus_knot(37)矩阵原理
vertices = D1:36 D2:360 u = from 0 to (2*PI) D1 v = from 0 to (6*PI) D2 p = 3 q = 7 r = 2 + cos(q/p*v) xq = r*sin(v) yq = sin(q/p*v) zq = r*cos(v) r = 0.5 + 0.5*sin(v) g = 0.5 + 0.5*yq b = 0.5 + 0.5*cos(v) #由线变管 xd = array_difference(xq, 36) yd = array_difference(yq, 36) zd = array_difference(zq, 36) len = sqrt(xd*xd + yd*yd + zd*zd) px = xd/len py = yd/len pz = zd/len len = sqrt(py*py + pz*pz) len = max(len, 0.00001) m = pz/len n = -py/len i = py*n - pz*m j = - px*n k = px*m radius = 0.2 x0 = cos(u) * radius z0 = sin(u) * radius x = x0*i + xq y = x0*j + z0*m + yq z = x0*k + z0*n + zq