mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
102.4k stars 35.35k forks source link

Deformable Objects #200

Closed ghost closed 13 years ago

ghost commented 13 years ago

I'm using Three (very nice so far) and would like to deform objects with my own code. I need to get hold of the vertex/normal arrays and the webgl buffer objects. At the moment I am doing something nasty, like ( for a Sphere):

var handle = deformGeometry.geometryGroups.undefined_0; 
var nVertices      = handle.vertices;
var curPos         = handle.__vertexArray;
var curPosVBO      = handle.__webglVertexBuffer;

Is there a better way to do this? (I'm concerned that if I change the geometry data on the fly it might mess up the renderer.)

Any plans for APIs that expose geometry data in a safe way?

525c1e21-bd67-4735-ac99-b4b0e5262290 commented 13 years ago

You may want to check out the webgl_geometry_dynamic example -- https://github.com/mrdoob/three.js/blob/master/examples/webgl_geometry_dynamic.html

Basically what you're looking for is the __dirtyVertices flag -- https://github.com/mrdoob/three.js/blob/master/examples/webgl_geometry_dynamic.html#L167

There's talk of this all being re-factored shortly but for now this is, I believe, the "supported" method of having dynamic geometry in WebGL.

Note that you can only move vertices around with this method. Faces remain comprised of the same vertices and cannot be added or removed from the mesh. I have some hacks from a fellow three.js user for supporting __dirtyFaces but it's less than pleasant Im afraid.

ghost commented 13 years ago

Many thanks for the pointer, that's very close to what I need.

I guess dirtyVertices tells Three to write out to webglVertextBuffer, ie make a WebGL bufferSubData call. Does it also trigger recalculation of normals or should I do that?

525c1e21-bd67-4735-ac99-b4b0e5262290 commented 13 years ago

Correct. It makes calls to bufferSubData; amongst other calls to refresh applicable data on the card.

I just looked through the code quickly and it looks like you need to recalculate the normals yourself. I assume this is so you can have entire sets of faces (say a cube) moved without having to recalculate the normals.

If you want to check for yourself; setting __dirtyVertices to true causes setMeshBuffers to be called. So that's a good place to start.