技术开发 频道

程序员最美情人节礼物:JS渲染的3D玫瑰

 结果:

  这看起来已经很像一个玫瑰花瓣的形状了。在这里也可以试试通过修改一些函数数值,将会出现很多有趣的形状。

  接下来应该给它添加色彩了:

function surface(a, b) {

  var x
= a * 100,

  y
= b * 100,

  radius
= 50,

  x0
= 50,

  y0
= 50;

  
if ((x - x0) * (x - x0) + (y - y0) * (y - y0) < radius * radius) {

  return {

  x: x,

  y: y
* (1 + b) / 2,

  r:
100 + Math.floor((1 - b) * 155), // this will add a gradient

  g:
50,

  b:
50

  };

  }
else {

  return
null;

  }

  }

  
for (a = 0; a < 1; a += .01) {

  
for (b = 0; b < 1; b += .001) {

  
if (point = surface(a, b)) {

  context.fillStyle
= "rgb(" + point.r + "," + point.g + "," + point.b + ")";

  context.fillRect(point.x, point.y,
1, 1);

  }

  }

  }

 

  结果:

  一片带色的花瓣就出现了。

  3D曲面和透视投影

  定义三维表面很简单,比如,来定义一个管状物体:

function surface(a, b) {

  var angle
= a * Math.PI * 2,

  radius
= 100,

  length
= 400;

  return {

  x: Math.cos(angle)
* radius,

  y: Math.sin(angle)
* radius,

  z: b
* length - length / 2, // by subtracting length/2 I have centered the tube at (0, 0, 0)

  r:
0,

  g: Math.floor(b
* 255),

  b:
0

  };

  }

 

  接着添加投影透视图,首先需要我们定义一个摄像头:

  如上图,将摄像头放置在(0,0,Z)位置,画布在X / Y平面。投影到画布上的采样点为:

 var pX, pY, // projected on canvas x and y coordinates

  perspective
= 350,

  halfHeight
= canvas.height / 2,

  halfWidth
= canvas.width / 2,

  cameraZ
= -700;

  
for (a = 0; a < 1; a += .001) {

  
for (b = 0; b < 1; b += .01) {

  
if (point = surface(a, b)) {

  pX
= (point.x * perspective) / (point.z - cameraZ) + halfWidth;

  pY
= (point.y * perspective) / (point.z - cameraZ) + halfHeight;

  context.fillStyle
= "rgb(" + point.r + "," + point.g + "," + point.b + ")";

  context.fillRect(pX, pY,
1, 1);

  }

  }

  }

 

  效果为:

  z-buffer

  z-buffer在计算机图形学中是一个相当普遍的技术,在为物件进行着色时,执行“隐藏面消除”工作,使隐藏物件背后的部分就不会被显示出来。

  上图是用z-buffer技术处理后的玫瑰。(可以看到已经具有立体感了)

  代码如下:

var zBuffer = [],

  zBufferIndex;

  
for (a = 0; a < 1; a += .001) {

  
for (b = 0; b < 1; b += .01) {

  
if (point = surface(a, b)) {

  pX
= Math.floor((point.x * perspective) / (point.z - cameraZ) + halfWidth);

  pY
= Math.floor((point.y * perspective) / (point.z - cameraZ) + halfHeight);

  zBufferIndex
= pY * canvas.width + pX;

  
if ((typeof zBuffer[zBufferIndex] === "undefined") || (point.z < zBuffer[zBufferIndex])) {

  zBuffer[zBufferIndex]
= point.z;

  context.fillStyle
= "rgb(" + point.r + "," + point.g + "," + point.b + ")";

  context.fillRect(pX, pY,
1, 1);

  }

  }

  }

  }

 

  旋转

  你可以使用任何矢量旋转的方法。在代码玫瑰的创建中,我使用的是欧拉旋转。现在将之前编写的管状物进行旋转,实现绕Y轴旋转:

function surface(a, b) {

  var angle
= a * Math.PI * 2,

  radius
= 100,

  length
= 400,

  x
= Math.cos(angle) * radius,

  y
= Math.sin(angle) * radius,

  z
= b * length - length / 2,

  yAxisRotationAngle
= -.4, // in radians!

  rotatedX
= x * Math.cos(yAxisRotationAngle) + z * Math.sin(yAxisRotationAngle),

  rotatedZ
= x * -Math.sin(yAxisRotationAngle) + z * Math.cos(yAxisRotationAngle);

  return {

  x: rotatedX,

  y: y,

  z: rotatedZ,

  r:
0,

  g: Math.floor(b
* 255),

  b:
0

  };

  }

 

  效果:

  蒙特卡罗方法

  关于采样时间,间隔过大过小都会引起极差的视觉感受,所以,需要设置合理的采样间隔,这里使用蒙特卡罗方法。

var i;

  window.setInterval(
function () {

  
for (i = 0; i < 10000; i++) {

  
if (point = surface(Math.random(), Math.random())) {

  pX
= Math.floor((point.x * perspective) / (point.z - cameraZ) + halfWidth);

  pY
= Math.floor((point.y * perspective) / (point.z - cameraZ) + halfHeight);

  zBufferIndex
= pY * canvas.width + pX;

  
if ((typeof zBuffer[zBufferIndex] === "undefined") || (point.z < zBuffer[zBufferIndex])) {

  zBuffer[zBufferIndex]
= point.z;

  context.fillStyle
= "rgb(" + point.r + "," + point.g + "," + point.b + ")";

  context.fillRect(pX, pY,
1, 1);

  }

  }

  }

  },
0);

 

  设置a和b为随机参数,用足够的采样完成表面填充。我每次绘制10000点,然后静待屏幕完成更新。

  另外需要注意的是,如果随机数发生错误时,表面填充效果会出错。有些浏览器中,Math.random的执行是线性的,这就有可能导致表面填充效果出错。这时,就得使用类似Mersenne Twister(一种随机数算法)这样的东西去进行高质量的PRNG采样,从而避免错误的发生。

  完成

  为了使玫瑰的每个部分在同一时间完成并呈现,还需要添加一个功能,为每部分设置一个参数以返回值来进行同步。并用一个分段函数代表玫瑰的各个部分。比如在花瓣部分,我用旋转和变形来创建它们。

  虽然表面采样方法是创建三维图形非常著名的、最古老的方法之一,但这种把蒙特卡罗、z-buffer加入到表面采样中的方法并不常见。对于现实生活场景的制作,这也许算不上很有创意,但它简易的代码实现和很小的体积仍令人满意。

  希望这篇文章能激发计算机图形学爱好者来尝试不同的呈现方法,并从中获得乐趣。(Roman Cortes)

0
相关文章