////////////////////////// // add our own lights ////////////////////////// //first remove any scene lights for (var i = scene.lights.count - 1; i >= 0; i--) { scene.lights.removeByIndex(i); } lights = new Object(); lights.init = function() { //from left l1 = scene.createLight(); l1.color.set3(1,1,1); l1.brightness = .4; l1.direction.set3(1,0,0); //NB: direction, not source! //from right l2 = scene.createLight(); l2.color.set3(1,1,1); l2.brightness = .4; l2.direction.set3(-1,0,0); //from bottom l3 = scene.createLight(); l3.color.set3(1,1,1); l3.brightness = .4; l3.direction.set3(0,0,1); //from top l4 = scene.createLight(); l4.color.set3(1,1,1); l4.brightness = .4; l4.direction.set3(0,0,-1); //from front l5 = scene.createLight(); l5.color.set3(1,1,1); l5.brightness = .4; l5.direction.set3(0,1,0); //from back l6 = scene.createLight(); l6.color.set3(1,1,1); l6.brightness = .4; l6.direction.set3(0,-1,0); } lights.init(); //////////////////////////////////////////////////////////////////////// // get rid of Gimbal rotation; as a result the z-axis will always be up; // this part of the script should work off the shelf with _any_ 3D model //////////////////////////////////////////////////////////////////////// var max_theta = 88; // maximum pitch (degrees from horizontal) of the camera var sin_max_theta = Math.sin(max_theta * Math.PI/180); var tan_max_theta = Math.tan(max_theta * Math.PI/180); mouseEventHandler = new MouseEventHandler(); mouseEventHandler.onMouseMove = true; mouseEventHandler.onEvent = function(event) { if(event.currentTool == runtime.TOOL_NAME_ROTATE) { var camera = scene.cameras.getByIndex(0); var c2c = camera.position.subtract(camera.targetPosition); var roo = c2c.length; try { var sin_theta = c2c.z / roo; if(sin_theta == 1) {//top view c2c.z = roo * sin_max_theta; c2c.y = -c2c.z / tan_max_theta; c2c.x = 0 } else if(sin_theta == -1) {//bottom view c2c.z = -roo * sin_max_theta; c2c.y = c2c.z / tan_max_theta; c2c.x = 0 } else if(sin_max_theta < sin_theta){//camera too high c2c.z = Math.sqrt(c2c.x * c2c.x + c2c.y * c2c.y) * tan_max_theta; c2c.normalize(); c2c.scaleInPlace(roo); } else if(sin_theta < -sin_max_theta){//camera too low c2c.z = -Math.sqrt(c2c.x * c2c.x + c2c.y * c2c.y) * tan_max_theta; c2c.normalize(); c2c.scaleInPlace(roo); } //corrected camera position camera.position.set(camera.targetPosition.add(c2c)); } catch(e) {} camera.roll=0; //z-axis always up } } runtime.addEventHandler(mouseEventHandler);