简体   繁体   English

如何在JavaScript中生成简单的3D图像

[英]How to generate simple 3D images in JavaScript

I like to generate a simplified version of the following static image in pure JavaScript. 我喜欢用纯JavaScript生成以下静态图像的简化版本。 It should work with 2010 vintage browsers, so I can't wait for Firefox 4 and WebGL. 它应该可以与2010年的老式浏览器一起使用,因此我等不及Firefox 4和WebGL。

I do not need any fancy textures - the task is just to visualise how to stack some boxes. 我不需要任何奇特的纹理-任务只是可视化如何堆叠一些盒子。

装箱单

BTW: the current image is generated with POV-Ray which is overkill for the job - and does not run in the browser ;-) 顺便说一句:当前图像是用POV-Ray生成的,对工作而言是多余的-并且不会在浏览器中运行;-)

As you say you don't need fancy textures I'd recommend for vintage support that you stack images like mentioned earlier. 正如您所说,您不需要花哨的纹理,我建议您使用老式支持,如上面提到的那样堆叠图像。 I made an example for you: http://jsfiddle.net/andersand/5RsEx/ 我为您提供了一个示例: http : //jsfiddle.net/andersand/5RsEx/

The simple function is just to illustrate, and does the drawing of a segmented box. 简单的功能只是为了说明,并绘制了分段框。 Of course you may need to figure out box placement, orientation, and so on if that is your business goal. 当然,如果这是您的业务目标,则可能需要弄清楚盒子的位置,方向等。

That approach could help you with boxes of various height (z axis). 这种方法可以帮助您使用各种高度(z轴)的盒子。 If your boxes also vary in width (x and/or y) you'd need to create more images to suit your needs. 如果您的盒子的宽度(x和/或y)也不同,则需要创建更多图像以适合您的需求。

您是否看过这些库,它们看起来很需要并且可能会完成您的工作: 很棒的JavaScript 3D库 ,但是看起来其中许多都需要WebGL。

If you can do without IE support, or require a plug-in for IE users, or provide server-side rasterization for IE users, you could use computed svg files. 如果您可以在没有IE支持的情况下完成操作,或者需要为IE用户提供插件,或者为IE用户提供服务器端栅格化,则可以使用已计算的svg文件。

You could do a very basic projection of the vertices of the boxes, maybe start with a simple isometric projection and then go to a perspective projection. 您可以对盒子的顶点进行非常基本的投影,可以从简单的等轴测投影开始,然后再进行透视投影。 Use simple 4x4 matrix math as used in OpenGL for this and represent the 3D coordinates as [x, y, z, w] vectors. 为此,请使用OpenGL中使用的简单4x4矩阵数学,并将3D坐标表示为[x,y,z,w]向量。 Since the images are small and simple enough you can get away with simply using a smart rendering order, ie bottom to top, back to front, which will make sure you don't have to worry about mucking about with a depth buffer or other such things. 由于图像足够小且简单,因此您可以简单地使用智能渲染顺序(即从下到上,从后到前)来逃脱操作,这将确保您不必担心深度缓冲或其他类似问题东西。 Should be fairly simple to implement, you don't need third party libs and it will be natively supported in most of the contemporary browsers. 实施起来应该非常简单,您不需要第三方库,并且大多数现代浏览器都将原生支持它。

Okay, I thought this to be an interesting experiment, so I made a working version of what I described above. 好的,我认为这是一个有趣的实验,因此我制作了上面描述的工作版本。 It works in all major browsers with the notable exception of IE. 它适用于所有主要浏览器,IE除外。 SVG support should come to IE with the introduction of IE9. 随着IE9的引入,对IE的SVG支持应随之而来。 I've tested in Opera, Firefox and Safari under OS X and Opera and Firefox under Linux. 我已经在OS X下的Opera,Firefox和Safari以及Linux下的Opera和Firefox中进行了测试。 It might be possible to add IE support by making VML output possible, though I must say that I do not know whether IE permits inlining of VML in XHTML using namespaces like I used here. 通过使VML输出成为可能可能增加IE支持,尽管我必须说,我不知道IE是否允许使用此处使用的命名空间在XHTML中内联VML。

Computed SVG rendering of 3D stacked boxes 3D堆叠盒的SVG渲染计算

Right now it only does isometric projection. 目前,它仅进行等轴测投影。 I might just spend a bit more time on this to add a perspective projection option as well. 我可能会花更多的时间来添加透视投影选项。 That would seem fun and should not be much more than adding another matrix multiplication. 这看起来很有趣,而且不应该只不过是添加另一个矩阵乘法。

Searching for Collada (XML based 3d file) support may be your best bet. 搜索Collada(基于XML的3d文件)支持可能是最好的选择。 Now that canvas has landed, lots of 3d routines are being ported from Flash Actionscript to Javascript. 现在画布已经着陆,许多3d例程已从Flash Actionscript移植到Javascript。

You can export Collada files from all of the major 3D applications, as well as blender ;) 您可以从所有主要的3D应用程序以及Blender中导出Collada文件;)

Try the following as an example; 请尝试以下示例。

http://www.rozengain.com/blog/2007/11/21/parsing-collada-3d-assets-with-javascript-in-the-html-5-canvas-element/ http://www.rozengain.com/blog/2007/11/21/parsing-collada-3d-assets-with-javascript-in-the-html-5-canvas-element/

If you want to rotate a 3D scene using javascript, you may have a few months wait until the engines get released. 如果您想使用JavaScript旋转3D场景,则可能要等上几个月才能发布引擎。 They will most likely be HTML5 dependent. 它们很可能将依赖HTML5。

There are a few WebGL implementations doing the rounds but they are for the bleeding edge browsers and are very unstable. 有一些WebGL实现可以进行这些操作,但是它们是针对最新浏览器的,并且非常不稳定。

Check out this demo and the src for inspiration: 查看此演示和src以获得灵感:

http://www.nihilogic.dk/labs/canvas3dtexture_0.2/ http://www.nihilogic.dk/labs/canvas3dtexture_0.2/

对于Nissan Leaf网站,他们使用Raphaël

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM