Tuesday, March 27, 2012

The Objects and The Logic

Essentially, here is how things work at the moment.

My 'ground' a single mesh (three.js object) that consists of a geometry and material.  The geometry is a plane 512x512 and is the material texturing comes from a GLSL (WebGL Shader Language) that uses a series of JPGs that represent grass, bare earth, dirt and bedrock.  The fragment shader (that applies the textures to the material) uses an array of vertex 'height's that I keep locally to 'merge' the textures together and show the right combination based on the 'height' of the terrain.  You can read more directly from the horses mouth who published the technique to me in: http://chandler.prallfamily.com/2011/07/dynamic-terrain-without-heightmaps/


When I 'deform' the terrain under the dozer, I simply change the 'y' value of the vertex(es) under the dozer and 'sync' that to the webgl side of the house (which talks to the GPU) by setting some dirty flags.  The display updates on the render animation cycle.  It's kind of a brute-force approach, however, to FIND the vertices under the dozer that I will optimize a bit later.

           var width = 8;
                for(var i = 0; i < plane.geometry.vertices.length;i++){
                    if(
                    (dozer.position.x  >  plane.geometry.vertices[i].position.x-width &&
                        dozer.position.x  <  plane.geometry.vertices[i].position.x+width) &&
                        (dozer.position.z  >  plane.geometry.vertices[i].position.z-width &&
                        dozer.position.z  <  plane.geometry.vertices[i].position.z+width)   
                )
                    {
                        if(bladeHeight <  plane.geometry.vertices[i].position.y)
                        plane.geometry.vertices[i].position.y=bladeHeight;     
                        plane.geometry.__dirtyVertices = true;
                        plane.geometry.__dirtyNormals = true; 
                        attributes.displacement.needsUpdate = true;
                }
            }
At the moment, the deformation occurs under the 'middle' of the dozer instead of near the blade.  Another thing to fix later.

The bulldozer is a mesh created by importing a model and I am applying no texture to it at the moment..just an off-yellow color.    Keeping the dozer 'on the ground' is done by shooting a ray (a three.js concept) with a starting point 1000 units 'above' the dozer straight down.  Wherever it intersects with the ground mesh is where the dozer y coordinate will be.

        ray = new THREE.Ray();
                ray.origin.y = 1000;
                ray.direction = new THREE.Vector3( 0, -1, 0 );
                ray.origin.x = dozer.position.x;
                ray.origin.z = dozer.position.z;  
        intersects = ray.intersectObject(plane);
        if ( intersects.length > 0 ) {
                    dozer.position.y = (intersects[ 0 ].point.y);
        }
At the moment, the dozer stays flat and level...regardless of the slope of the land under it.

No comments:

Post a Comment