sinisterchipmunk / jax

Framework for creating rich WebGL-enabled applications using JavaScript and Ruby
http://jaxgl.com
MIT License
96 stars 16 forks source link

automatically center camera #67

Open nat-n opened 12 years ago

nat-n commented 12 years ago

It could be handy to have an option somewhere in the model to automatically center the camera within the mesh, since some software generates meshes with only positive vertex values, and this seems to result in the camera which controls the position/orientation of the mesh being outside of it.

sinisterchipmunk commented 12 years ago

On the face of it, I like the idea of this. Obviously it would be bad to reposition the origin by default, but exposing the ability to do so by request to the model would be acceptable.

Implementation may become a bit more hairy, though. Do we take the naïve approach of center = minBounds + (maxBounds - minBounds) / 2? Or do we take a weighted average of all vertex positions? The latter may (arguably) be called more accurate, but it would also take longer, and could possibly stall the JS engine if a lot of vertices are involved.

I'm not sure what the best answer is. I'm hesitant to add two similarly-named functions because that may lead to a confusing API in a library that's already pretty huge. Also, I'm not planning to introduce Web Workers into the core framework until around v3.1 -- this makes the weighted position option much less appealing in the near term but much more doable after v3.1.

Another option that just occurred to me, and frankly the one I'm now leaning toward, is some kind of setOrigin() method which would allow the developer to explicitly assign the position of the camera relative to the mesh. It would then become the developer's job to figure out exactly where the correct "center" should be, whether that's a hard-coded value or some algorithm of the dev's own making. Once the ideal origin has been found, Jax could handle the actual translation. So it becomes a sort of compromise between developer and framework.

Thoughts?