Skip to content

Commit

Permalink
boundingSphere boundingBox now computed onload,
Browse files Browse the repository at this point in the history
 and support for onUpdate and onLoad events.
  • Loading branch information
ponchio committed Sep 30, 2018
1 parent 71d846f commit 62b9d98
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 35 deletions.
8 changes: 4 additions & 4 deletions html/js/nexus.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ Mesh.prototype = {
Instance = function(gl) {
this.gl = gl;
this.onLoad = function() {};
this.onUpdate = function() {};
this.onUpdate = null;
this.drawBudget = drawBudget;
this.attributes = { 'position':0, 'normal':1, 'color':2, 'uv':3 };
}
Expand Down Expand Up @@ -1108,7 +1108,7 @@ function loadNodeTexture(request, context, node, texid) {
m.status[n]--; //ready
node.reqAttempt = 0;
node.context.pending--;
node.instance.onUpdate();
node.instance.onUpdate && node.instance.onUpdate();
updateCache(gl);
}
}
Expand Down Expand Up @@ -1198,7 +1198,7 @@ function readyNode(node) {
m.status[n]--; //ready
node.reqAttempt = 0;
node.context.pending--;
node.instance.onUpdate();
node.instance.onUpdate && node.instance.onUpdate();
updateCache(gl);
}
}
Expand Down Expand Up @@ -1251,7 +1251,7 @@ function setMaxCacheSize(gl, size) {
}

return { Mesh: Mesh, Renderer: Instance, Renderable: Instance, Instance:Instance,
Debug: Debug, contexts: contexts, beginFrame:beginFrame, endFrame:endFrame,
Debug: Debug, contexts: contexts, beginFrame:beginFrame, endFrame:endFrame, updateCache: updateCache,
setTargetError:setTargetError, setTargetFps:setTargetFps, setMaxCacheSize:setMaxCacheSize };

}();
64 changes: 59 additions & 5 deletions html/js/nexus_three.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
function NexusObject(url, renderer, render, material) {
/*
*/

function NexusObject(url, onLoad, onUpdate, renderer, material) {
if(typeof(onLoad) == 'object')
throw "NexusObject constructor has been changed.";

var gl = renderer.context;
var geometry = new THREE.BufferGeometry();
var positions = new Float32Array(3);

geometry.center = function() {
throw "Centering and in general applying matrix to geometry is unsupported.";

/* var s = 1/instance.mesh.sphere.radius;
var pos = instance.mesh.sphere.center;
mesh.position.set(-pos[0]*s, -pos[1]*s, -pos[2]*s);
mesh.scale.set(s, s, s); */
};

var positions = new Float32Array(3);
geometry.addAttribute('position', new THREE.BufferAttribute(positions, 3));


if(!material)
this.autoMaterial = true;
Expand All @@ -17,6 +31,12 @@ function NexusObject(url, renderer, render, material) {
var instance = this.instance = new Nexus.Instance(gl);
instance.open(url);
instance.onLoad = function() {
var c = instance.mesh.sphere.center;
var center = new THREE.Vector3(c[0], c[1], c[2]);
var radius = instance.mesh.sphere.radius;

geometry.boundingSphere = new THREE.Sphere(center, radius);
geometry.boundingBox = mesh.computeBoundingBox();

if(mesh.autoMaterial)
mesh.material = new THREE.MeshLambertMaterial( { color: 0xffffff } );
Expand Down Expand Up @@ -46,9 +66,9 @@ function NexusObject(url, renderer, render, material) {
var indices = new Uint32Array(3);
geometry.setIndex(new THREE.BufferAttribute( indices, 3) );
}
render();
onLoad && onLoad();
};
instance.onUpdate = function() { render(); }
instance.onUpdate = onUpdate;

this.onAfterRender = function(renderer, scene, camera, geometry, material, group) {
if(!instance.isReady) return;
Expand All @@ -63,11 +83,45 @@ function NexusObject(url, renderer, render, material) {
instance.attributes['uv'] = renderer.context.getAttribLocation(program, "uv");

instance.render();

Nexus.updateCache(renderer.context);
}
}

NexusObject.prototype = Object.create(THREE.Mesh.prototype);

NexusObject.prototype.computeBoundingBox = function() {
var instance = this.instance;
var nexus = instance.mesh;
if(!nexus.sphere) return;


var min = new THREE.Vector3( + Infinity, + Infinity, + Infinity );
var max = new THREE.Vector3( - Infinity, - Infinity, - Infinity );

var array = new Float32Array(nexus.sink-1);
//check last level of spheres
var count = 0;
for(var i = 0; i < nexus.sink; i++) {
var patch = nexus.nfirstpatch[i];
if(nexus.patches[patch*3] != nexus.sink)
continue;
var x = nexus.nspheres[i*5];
var y = nexus.nspheres[i*5+1];
var z = nexus.nspheres[i*5+2];
var r = nexus.nspheres[i*5+4]; //tight radius
if(x-r < min.x) min.x = x-r;
if(y-r < min.y) min.y = y-r;
if(z-r < min.z) min.z = z-r;
if(x-r > max.x) max.x = x+r;
if(y-r > max.y) max.y = y+r;
if(z-r > max.z) max.z = z+r;

}
return new THREE.Box3(min, max);
}


NexusObject.prototype.raycast = function(raycaster, intersects) {
var instance = this.instance;
var nexus = instance.mesh;
Expand Down
67 changes: 41 additions & 26 deletions html/threejs.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@

<script>

function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

controls.handleResize();
controls.update();
renderer.render( scene, camera );
}

//function animate() {
// requestAnimationFrame( animate );
// controls.update();
// renderer.render( scene, camera );
//}


var redraw = true;
function animate() {
requestAnimationFrame(animate);

controls.update();
if(redraw)
renderer.render( scene, camera );
redraw = false;
}

var camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 0.1, 100 );
camera.position.z = 4;

Expand All @@ -29,7 +57,7 @@
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
controls.keys = [ 65, 83, 68 ];
controls.addEventListener( 'change', render );
controls.addEventListener( 'change', function() { redraw = true; } );

var scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x050505, 2000, 3500 );
Expand Down Expand Up @@ -58,44 +86,31 @@
var material = new THREE.MeshLambertMaterial( { color: 0xffffff, map: texture } );
*/


function onNexusLoad() {
console.log("loaded");
var s = 1/nexus_obj.geometry.boundingSphere.radius;
var p = nexus_obj.geometry.boundingBox.getCenter().negate();
nexus_obj.position.set(p.x*s, p.y*s, p.y*s); //.set(p.x, p.y, p.z); // = p; //.set(p.x, p.y, p.z);
nexus_obj.scale.set(s, s, s);
redraw = true;
}

function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
}

var model = getURLParameter('model') || "models/gargo.nxz";

var nexus_obj = new NexusObject(model, renderer, render);
var nexus_obj = new NexusObject(model, onNexusLoad, function() { redraw = true; }, renderer);
scene.add(nexus_obj);

window.addEventListener( 'resize', onWindowResize, false );
render();


function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

controls.handleResize();
controls.update();
render();
}

function animate() {
requestAnimationFrame( animate );
controls.update();
}

function render() {
Nexus.beginFrame(renderer.context);
renderer.render( scene, camera );
Nexus.endFrame(renderer.context);
}

animate();


</script>


Expand Down

0 comments on commit 62b9d98

Please sign in to comment.