| 1 |
///////////////////////////////////////////////////////////////////// |
| 2 |
// |
| 3 |
// turntable.js |
| 4 |
// |
| 5 |
// JavaScript for use with `3Djscript' option of \includemovie |
| 6 |
// |
| 7 |
// * Greatly improves the rotational behaviour of the 3D object, |
| 8 |
// prevents it from tilting to the side while dragging the mouse. |
| 9 |
// This is achieved by suppressing the rolling of the camera about |
| 10 |
// its optical axis. |
| 11 |
// |
| 12 |
///////////////////////////////////////////////////////////////////// |
| 13 |
|
| 14 |
// maximum pitch (degrees from horizontal) of the camera |
| 15 |
var max_alpha = 88; |
| 16 |
var min_beta = 90 - max_alpha; // the complement |
| 17 |
var cos_min_beta = Math.cos(min_beta * Math.PI/180); |
| 18 |
var tan_min_beta = Math.tan(min_beta * Math.PI/180); |
| 19 |
|
| 20 |
var camera = scene.cameras.getByIndex(0); |
| 21 |
camera.axis_up = camera.up.subtract(camera.position); |
| 22 |
camera.axis_up.normalize(); |
| 23 |
|
| 24 |
//updates the vertical axis of rotation whenever a predefined view |
| 25 |
//is selected from the drop down list in the 3D toolbar |
| 26 |
var cameraEventHandler = new CameraEventHandler(); |
| 27 |
cameraEventHandler.onEvent = function (e) { |
| 28 |
camera.axis_up = camera.up.subtract(camera.position); |
| 29 |
camera.axis_up.normalize(); |
| 30 |
} |
| 31 |
runtime.addEventHandler(cameraEventHandler); |
| 32 |
|
| 33 |
//suppresses camera rolling and limits camera pitch |
| 34 |
var mouseEventHandler = new MouseEventHandler(); |
| 35 |
mouseEventHandler.onMouseMove = true; |
| 36 |
mouseEventHandler.onEvent = function (e) { |
| 37 |
runtime.setCurrentTool(runtime.TOOL_NAME_ROTATE); |
| 38 |
var c2c = camera.position.subtract(camera.targetPosition); |
| 39 |
var roo = c2c.length; |
| 40 |
c2c.normalize(); |
| 41 |
cos_beta = c2c.dot(camera.axis_up); //cos of enclosed angle |
| 42 |
|
| 43 |
//correct the camera position if it is too high or too low |
| 44 |
if(Math.abs(cos_beta) > cos_min_beta) { |
| 45 |
//auxiliary vectors a & b |
| 46 |
var a = camera.axis_up.scale(cos_beta); |
| 47 |
var b = c2c.subtract(a); |
| 48 |
b.normalize(); |
| 49 |
b.scaleInPlace(tan_min_beta * a.length); |
| 50 |
|
| 51 |
c2c.set(a.add(b)); |
| 52 |
c2c.normalize(); |
| 53 |
|
| 54 |
camera.position.set(camera.targetPosition.add(c2c.scale(roo))); |
| 55 |
cos_beta = c2c.dot(camera.axis_up); |
| 56 |
} |
| 57 |
|
| 58 |
//suppress rolling |
| 59 |
camera.up.set( |
| 60 |
camera.position.add(camera.axis_up).add(c2c.scale(-cos_beta)) |
| 61 |
); |
| 62 |
}; |
| 63 |
runtime.addEventHandler(mouseEventHandler); |