/home/lindsay/xeolabs/xeogl-next/xeogl/examples/js/helpers/axisHelper.js
API Docs for:

File: /home/lindsay/xeolabs/xeogl-next/xeogl/examples/js/helpers/axisHelper.js

  1. {
  2.  
  3. /**s
  4.  
  5. Helper widget that indicates the World coordinate axis.
  6.  
  7. The helper works by tracking updates on a xeogl.Camera and orienting a gnomon accordingly.
  8.  
  9. @class AxisHelper
  10. @constructor
  11. @param cfg {*} Configuration
  12. @param cfg.camera {xeogl.Camera} A {{#crossLink "xeogl.Camera"}}{{/crossLink}} to observe.
  13. @param [cfg.size] {Int16Array} Pixel dimensions of helper's canvas, [250, 250] by default.
  14. */
  15. xeogl.AxisHelper = function (cfg) {
  16.  
  17. var camera = cfg.camera;
  18.  
  19. if (!camera) {
  20. throw "Param expected: camera";
  21. }
  22.  
  23. var size = cfg.size || [250, 250];
  24.  
  25. var canvas = camera.scene.canvas;
  26.  
  27. // Create canvas for this helper
  28.  
  29. var canvasId = "xeogl-axisHelper-canvas-" + xeogl.math.createUUID();
  30. var body = document.getElementsByTagName("body")[0];
  31. var div = document.createElement('div');
  32. var style = div.style;
  33. style.height = size[0] + "px";
  34. style.width = size[1] + "px";
  35. style.padding = "0";
  36. style.margin = "0";
  37. style.float = "left";
  38. style.left = "410px";
  39. style.bottom = "350px";
  40. style.position = "absolute";
  41. style["z-index"] = "1000000";
  42. // style["background-color"] = "rgba(0,0,0,0.3)";
  43. div.innerHTML += '<canvas id="' + canvasId + '" style="width: ' + size[0] + 'px; height: ' + size[1] + 'px; float: left; margin: 0; padding: 0;"></canvas>';
  44. body.appendChild(div);
  45. var helperCanvas = document.getElementById(canvasId);
  46.  
  47. canvas.on("boundary",
  48. function (boundary) {
  49. style.left = boundary[0] + 10 + "px";
  50. style.bottom = (boundary[0] + 20) + "px";
  51. });
  52.  
  53. // The scene containing this helper
  54. var scene = new xeogl.Scene({
  55. canvas: helperCanvas,
  56. transparent: true
  57. });
  58.  
  59. // Custom lights
  60. scene.clearLights();
  61.  
  62. new xeogl.AmbientLight(scene, {
  63. color: [0.45, 0.45, 0.5],
  64. intensity: 0.9
  65. });
  66.  
  67. new xeogl.DirLight(scene, {
  68. dir: [-0.5, 0.5, -0.6],
  69. color: [0.8, 0.8, 0.7],
  70. intensity: 1.0,
  71. space: "view"
  72. });
  73.  
  74. new xeogl.DirLight(scene, {
  75. dir: [0.5, -0.5, -0.6],
  76. color: [0.8, 0.8, 0.8],
  77. intensity: 1.0,
  78. space: "view"
  79. });
  80.  
  81. // Rotate helper in synch with target camera
  82.  
  83. var helperCamera = scene.camera;
  84.  
  85. camera.on("matrix", function () {
  86.  
  87. var eye = camera.eye;
  88. var look = camera.look;
  89. var up = camera.up;
  90.  
  91. var eyeLook = xeogl.math.mulVec3Scalar(xeogl.math.normalizeVec3(xeogl.math.subVec3(eye, look, [])), 22);
  92.  
  93. helperCamera.look = [0, 0, 0];
  94. helperCamera.eye = eyeLook;
  95. helperCamera.up = up;
  96. });
  97.  
  98. // ----------------- Components that are shared among more than one mesh ---------------
  99.  
  100. var arrowHead = new xeogl.CylinderGeometry(scene, {
  101. radiusTop: 0.01,
  102. radiusBottom: 0.6,
  103. height: 1.7,
  104. radialSegments: 20,
  105. heightSegments: 1,
  106. openEnded: false
  107. });
  108.  
  109. var arrowShaft = new xeogl.CylinderGeometry(scene, {
  110. radiusTop: 0.2,
  111. radiusBottom: 0.2,
  112. height: 4.5,
  113. radialSegments: 20,
  114. heightSegments: 1,
  115. openEnded: false
  116. });
  117.  
  118. var axisMaterial = new xeogl.PhongMaterial(scene, { // Red by convention
  119. ambient: [0.0, 0.0, 0.0],
  120. specular: [.6, .6, .3],
  121. shininess: 80,
  122. lineWidth: 2
  123. });
  124.  
  125. var xAxisMaterial = new xeogl.PhongMaterial(scene, { // Red by convention
  126. diffuse: [1, 0.3, 0.3],
  127. ambient: [0.0, 0.0, 0.0],
  128. specular: [.6, .6, .3],
  129. shininess: 80,
  130. lineWidth: 2
  131. });
  132.  
  133. var xAxisLabelMaterial = new xeogl.PhongMaterial(scene, { // Red by convention
  134. emissive: [1, 0.3, 0.3],
  135. ambient: [0.0, 0.0, 0.0],
  136. specular: [.6, .6, .3],
  137. shininess: 80,
  138. lineWidth: 2
  139. });
  140.  
  141. var yAxisMaterial = new xeogl.PhongMaterial(scene, { // Green by convention
  142. diffuse: [0.3, 1, 0.3],
  143. ambient: [0.0, 0.0, 0.0],
  144. specular: [.6, .6, .3],
  145. shininess: 80,
  146. lineWidth: 2
  147. });
  148.  
  149. var yAxisLabelMaterial = new xeogl.PhongMaterial(scene, { // Green by convention
  150. emissive: [0.3, 1, 0.3],
  151. ambient: [0.0, 0.0, 0.0],
  152. specular: [.6, .6, .3],
  153. shininess: 80,
  154. lineWidth: 2
  155. });
  156.  
  157.  
  158. var zAxisMaterial = new xeogl.PhongMaterial(scene, { // Blue by convention
  159. diffuse: [0.3, 0.3, 1],
  160. ambient: [0.0, 0.0, 0.0],
  161. specular: [.6, .6, .3],
  162. shininess: 80,
  163. lineWidth: 2
  164. });
  165.  
  166. var zAxisLabelMaterial = new xeogl.PhongMaterial(scene, {
  167. emissive: [0.3, 0.3, 1],
  168. ambient: [0.0, 0.0, 0.0],
  169. specular: [.6, .6, .3],
  170. shininess: 80,
  171. lineWidth: 2
  172. });
  173.  
  174. var ballMaterial = new xeogl.PhongMaterial(scene, {
  175. diffuse: [0.5, 0.5, 0.5],
  176. ambient: [0.0, 0.0, 0.0],
  177. specular: [.6, .6, .3],
  178. shininess: 80,
  179. lineWidth: 2
  180. });
  181.  
  182.  
  183. // ----------------- Meshes ------------------------------
  184.  
  185. var meshes = [
  186.  
  187. // Sphere behind gnomon
  188.  
  189. new xeogl.Mesh(scene, {
  190. geometry: new xeogl.SphereGeometry(scene, {
  191. radius: 9.0,
  192. heightSegments: 60,
  193. widthSegments: 60
  194. }),
  195. material: new xeogl.PhongMaterial(scene, {
  196. diffuse: [0.0, 0.0, 0.0],
  197. emissive: [0.1, 0.1, 0.1],
  198. ambient: [0.1, 0.1, 0.2],
  199. specular: [0, 0, 0],
  200. alpha: 0.4,
  201. alphaMode: "blend",
  202. frontface: "cw"
  203. }),
  204. pickable: false,
  205. collidable: false,
  206. visible: !!cfg.visible
  207. }),
  208.  
  209. // Ball at center of axis
  210.  
  211. new xeogl.Mesh(scene, { // Arrow
  212. geometry: new xeogl.SphereGeometry(scene, {
  213. radius: 1.0
  214. }),
  215. material: ballMaterial,
  216. pickable: false,
  217. collidable: false,
  218. visible: !!cfg.visible
  219. }),
  220.  
  221. // X-axis arrow, shaft and label
  222.  
  223. new xeogl.Mesh(scene, { // Arrow
  224. geometry: arrowHead,
  225. material: xAxisMaterial,
  226. pickable: false,
  227. collidable: false,
  228. visible: !!cfg.visible,
  229. position: [-5, 0, 0],
  230. rotation: [0, 0, 90]
  231. }),
  232.  
  233. new xeogl.Mesh(scene, { // Shaft
  234. geometry: arrowShaft,
  235. material: xAxisMaterial,
  236. pickable: false,
  237. collidable: false,
  238. visible: !!cfg.visible,
  239. position: [-2, 0, 0],
  240. rotation: [0, 0, 90]
  241. }),
  242.  
  243. new xeogl.Mesh(scene, { // Label
  244. geometry: new xeogl.VectorTextGeometry(scene, {text: "X", size: 1.5}),
  245. material: xAxisLabelMaterial,
  246. pickable: false,
  247. collidable: false,
  248. visible: !!cfg.visible,
  249. position: [-7, 0, 0],
  250. billboard: "spherical"
  251. }),
  252.  
  253. // Y-axis arrow, shaft and label
  254.  
  255. new xeogl.Mesh(scene, { // Arrow
  256. geometry: arrowHead,
  257. material: yAxisMaterial,
  258. pickable: false,
  259. collidable: false,
  260. visible: !!cfg.visible,
  261. position: [0, 5, 0]
  262. }),
  263.  
  264. new xeogl.Mesh(scene, { // Shaft
  265. geometry: arrowShaft,
  266. material: yAxisMaterial,
  267. pickable: false,
  268. collidable: false,
  269. visible: !!cfg.visible,
  270. position: [0, 2, 0]
  271. }),
  272.  
  273. new xeogl.Mesh(scene, { // Label
  274. geometry: new xeogl.VectorTextGeometry(scene, {text: "Y", size: 1.5}),
  275. material: yAxisLabelMaterial,
  276. pickable: false,
  277. collidable: false,
  278. visible: !!cfg.visible,
  279. position: [0, 7, 0],
  280. billboard: "spherical"
  281. }),
  282.  
  283. // Z-axis arrow, shaft and label
  284.  
  285. new xeogl.Mesh(scene, { // Arrow
  286. geometry: arrowHead,
  287. material: zAxisMaterial,
  288. pickable: false,
  289. collidable: false,
  290. visible: !!cfg.visible,
  291. position: [0, 0, 5],
  292. rotation: [90, 0, 0]
  293. }),
  294.  
  295. new xeogl.Mesh(scene, { // Shaft
  296. geometry: arrowShaft,
  297. material: zAxisMaterial,
  298. pickable: false,
  299. collidable: false,
  300. visible: !!cfg.visible,
  301. position: [0, 0, 2],
  302. rotation: [90, 0, 0]
  303. }),
  304.  
  305. new xeogl.Mesh(scene, { // Label
  306. geometry: new xeogl.VectorTextGeometry(scene, {text: "Z", size: 1.5}),
  307. material: zAxisLabelMaterial,
  308. pickable: false,
  309. collidable: false,
  310. visible: !!cfg.visible,
  311. position: [0, 0, 7],
  312. billboard: "spherical"
  313. })
  314. ];
  315.  
  316. /** Shows or hides this helper
  317. *
  318. * @param visible
  319. */
  320. this.setVisible = function (visible) {
  321. for (var i = 0; i < meshes.length; i++) {
  322. meshes[i].visible = visible;
  323. }
  324. }
  325. };
  326. }
  327.