html5rocks / www.html5rocks.com

....a top-notch resource for web developers
https://html5rocks.com
Other
2.22k stars 771 forks source link

3D Models on the '2D' canvas context #291

Closed MikeSourceCode closed 11 years ago

MikeSourceCode commented 11 years ago

I would like to share my steps on how to render 3D models on a canvas element in a '2D' context; i'm open to any editorial improvements.

3D_on_canvas_2D

3D_on_canvas_2D_2

The images attached are not WebGL rather this.context = this.canvas.getContext('2d');

ebidel commented 11 years ago

Sounds interesting! Can you propose an outline?

cc @paullewis

paullewis commented 11 years ago

I'm slightly reticent on this one. While it's interesting, you're going to end up covering some pretty heavy math on modelview and projection matrices. Also given Three.js has a canvas renderer, I fear that most developers wouldn't gain much from rolling their own.

MikeSourceCode commented 11 years ago

Hmmm the math is not thaaat complicated, and the reason i originally did it this way, was because Three.js and some other variants are not cross browser, for example WebGL is not supported by IE; I stumbled upon a solution for cross browsing by using a library that would pick webgl/dx as needed dont remmeber the name right now.

paullewis commented 11 years ago

We're going a little OT here, but the Three.js CanvasRenderer should work in IE, no?

Edit: CanvasRenderer works in IE9+

MikeSourceCode commented 11 years ago

Yes, that's correct

Being objective; given the fact that we are at IE10 with Windows 8 though interesting it would give little benefits in terms of actual application for 3D rendering. What I can do, is to modify my idea and as it is already pretty much a little library, i can tweak the code a little bit so i can turn the tutorial into writing cross-browser 3D letters (for logo, headers, or something like that) using the same technique as i did for the AK47 model; I would leave the math part as a blackbox, and the technical explanation to the calling/implementation of the JS that graphs on the canvas.

I did not mention this earlier but the renderer moves the 3D objects with the mouse movements; this would apply to the text as well; can do it in a Hello World sort of thing.

I am grateful for your responses and again open to any suggestions or ideas into how to turn this into a good article.

Edit: My proposal works on IE8+

MikeSourceCode commented 11 years ago

Something like this

Rhino3d

MikeSourceCode commented 11 years ago

Gomez

Haven't forgotten about providing the outline; i just want to get the idea straight first

:)

MikeSourceCode commented 11 years ago

There, what i did basically is to make a catalog of A-Za-z

Example

What i did is to parse the text on the designated div; match the models against the catalog and now this writes the 3D letters (it still could be used for any 3D models if needed); the explanation can be summarized on the crawling of the Text3D div, matching the catalog, and drawing 3D as a blackbox; the calling code is as follows:

$.get('abc.txt', function(response) { // abc.txt is the catalog of A-Za-z $("canvas").mousemove(function(e){ var myGraph = new Graph({ canvasId: 'Canvas3D', minX: 2, minY: -15, minZ: -10, maxX: 10, maxY: 10, maxZ: 10, unitsPerTick: 1, rotateX: e.pageX * 0.734693877551, // Width rotateY: e.pageY * 0.734693877551, // Height data: response, charSpacing: 60 // Self-explanatory }); }); });

The HTML is as simple as

`

Miguel
`
MikeSourceCode commented 11 years ago

Another Example:

Example2

MikeSourceCode commented 11 years ago

OUTLINE: 3D Models on the '2D' Canvas context

paullewis commented 11 years ago

I appreciate all the work you're putting in here, but I'm going to go with my gut on this one. Developers generally don't need to know about 3D math or how to build an engine. Those that need that functionality from a canvas would simply use Three.js rather than roll their own. And those that do roll their own would need a series of articles or a book to do things properly.

Thanks again for putting together the proposal, but I don't feel it's the right fit for HTML5 Rocks.