/home/lindsay/xeolabs/xeogl-next/xeogl/examples/js/skyboxes/skybox.js
API Docs for:

File: /home/lindsay/xeolabs/xeogl-next/xeogl/examples/js/skyboxes/skybox.js

  1. /**
  2. A **Skybox** is a textured box that does not translate with respect to the
  3. {{#crossLink "Lookat"}}viewing transform{{/crossLink}}, to a provide the appearance of a background
  4. for associated {{#crossLink "Meshes"}}Meshes{{/crossLink}}.
  5.  
  6. <a href="../../examples/#skyboxes_skybox"><img src="http://i.giphy.com/3o7TKIBOOCEC5gJ224.gif"></img></a>
  7.  
  8. ## Examples
  9.  
  10. <ul>
  11. <li>[Basic Skybox](../../examples/#skyboxes_skybox)</li>
  12. <li>[Custom Skybox](../../examples/#skyboxes_skybox_custom)</li>
  13. </ul>
  14.  
  15. ## Usage
  16.  
  17. In the example below we're going to create twenty randomly-positioned and colored {{#crossLink "Mesh"}}Meshes{{/crossLink}}
  18. and wrap them in a Skybox. The Skybox will use the texture image shown below, and the result will appear like the screen capture shown above.
  19.  
  20. <img src="../../assets/images/skyboxMiramarClouds.jpg">
  21.  
  22. ````javascript
  23. // A bunch of random cube Meshes
  24.  
  25. // Share this BoxGeometry among the Meshes
  26. var boxGeometry = new BoxGeometry();
  27.  
  28. for (var i = 0; i < 20; i++) {
  29. new xeogl.Mesh({
  30. geometry: boxGeometry,
  31. transform: new xeogl.Translate({
  32. xyz: [
  33. Math.random() * 15 - 7,
  34. Math.random() * 15 - 7,
  35. Math.random() * 15 - 7
  36. ]
  37. }),
  38. material: new xeogl.PhongMaterial({
  39. diffuse: [
  40. Math.random(),
  41. Math.random(),
  42. Math.random()
  43. ]
  44. })
  45. });
  46. }
  47.  
  48. // A Skybox that wraps our Meshes in a cloudy background
  49. var skybox = new xeogl.Skybox({
  50. src: "textures/skybox/miramarClouds.jpg",
  51. size: 1000 // Default
  52. });
  53.  
  54. // Get the default Scene off the Skybox
  55. var scene = skybox.scene;
  56.  
  57. // Move the camera back a bit
  58. scene.camera.eye = [0, 0, -30];
  59.  
  60. // Slowly orbit the camera on each frame
  61. scene.on("tick", function () {
  62. scene.camera.orbitYaw(0.2);
  63. });
  64. ````
  65.  
  66. @class Skybox
  67. @module xeogl
  68. @submodule skyboxes
  69. @constructor
  70. @param [scene] {Scene} Parent {{#crossLink "Scene"}}Scene{{/crossLink}}, creates this Skybox within the
  71. default {{#crossLink "Scene"}}Scene{{/crossLink}} when omitted
  72. @param [cfg] {*} Skybox configuration
  73. @param [cfg.id] {String} Optional ID, unique among all components in the parent {{#crossLink "Scene"}}Scene{{/crossLink}}, generated automatically when omitted.
  74. @param [cfg.meta] {String:Object} Optional map of user-defined metadata to attach to this Skybox.
  75. @param [cfg.src=[null]] {String} Path to skybox texture
  76. @param [cfg.encoding="linear"] {String} Texture encoding format. See the {{#crossLink "Texture/encoding:property"}}{{/crossLink}} property for more info.
  77. @param [cfg.size=1000] {Number} Size of this Skybox, given as the distance from the center at [0,0,0] to each face.
  78. @param [cfg.active=true] {Boolean} True when this Skybox is visible.
  79. @extends Component
  80. */
  81. xeogl.Skybox = class xeoglSkyBox extends xeogl.Component {
  82.  
  83. init(cfg) {
  84.  
  85. super.init(cfg);
  86.  
  87. this._skyboxMesh = new xeogl.Mesh(this, {
  88.  
  89. geometry: new xeogl.Geometry(this, { // Box-shaped geometry
  90. primitive: "triangles",
  91. positions: [
  92. 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, // v0-v1-v2-v3 front
  93. 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, // v0-v3-v4-v5 right
  94. 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, // v0-v5-v6-v1 top
  95. -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, // v1-v6-v7-v2 left
  96. -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, // v7-v4-v3-v2 bottom
  97. 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1 // v4-v7-v6-v5 back
  98. ],
  99. uv: [
  100. 0.5, 0.6666, 0.25, 0.6666, 0.25, 0.3333, 0.5, 0.3333, 0.5, 0.6666, 0.5, 0.3333, 0.75, 0.3333, 0.75, 0.6666,
  101. 0.5, 0.6666, 0.5, 1, 0.25, 1, 0.25, 0.6666, 0.25, 0.6666, 0.0, 0.6666, 0.0, 0.3333, 0.25, 0.3333,
  102. 0.25, 0, 0.50, 0, 0.50, 0.3333, 0.25, 0.3333, 0.75, 0.3333, 1.0, 0.3333, 1.0, 0.6666, 0.75, 0.6666
  103. ],
  104. indices: [
  105. 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11,
  106. 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23
  107. ]
  108. }),
  109. scale: [2000, 2000, 2000], // Overridden when we initialize the 'size' property, below
  110. rotation: [0, -90, 0],
  111. material: new xeogl.PhongMaterial(this, {
  112. ambient: [0, 0, 0],
  113. diffuse: [0, 0, 0],
  114. specular: [0, 0, 0],
  115. emissive: [1, 1, 1],
  116. emissiveMap: new xeogl.Texture(this, {
  117. src: cfg.src,
  118. flipY: true,
  119. encoding: cfg.encoding || "sRGB"
  120. }),
  121. backfaces: true // Show interior faces of our skybox geometry
  122. }),
  123. stationary: true,
  124. visible: false,
  125. pickable: false,
  126. collidable: false
  127. });
  128.  
  129. this.size = cfg.size; // Sets 'xyz' property on the Mesh's Scale transform
  130. this.active = cfg.active;
  131. }
  132.  
  133.  
  134. /**
  135. * Size of this Skybox, given as the distance from the center at [0,0,0] to each face.
  136. *
  137. * Fires an {{#crossLink "Skybox/size:event"}}{{/crossLink}} event on change.
  138. *
  139. * @property size
  140. * @default 1000
  141. * @type {Number}
  142. */
  143. set size(value) {
  144.  
  145. this._size = value || 1000;
  146.  
  147. this._skyboxMesh.scale = [this._size, this._size, this._size];
  148.  
  149. /**
  150. Fired whenever this Skybox's {{#crossLink "Skybox/size:property"}}{{/crossLink}} property changes.
  151.  
  152. @event size
  153. @param value {Array of Number} The property's new value
  154. */
  155. this.fire("size", this._size);
  156. }
  157.  
  158. get size() {
  159. return this._size;
  160. }
  161.  
  162.  
  163. /**
  164. Indicates whether this Skybox is visible or not.
  165.  
  166. Fires a {{#crossLink "Skybox/active:event"}}{{/crossLink}} event on change.
  167.  
  168. @property active
  169. @default false
  170. @type Boolean
  171. */
  172. set active(value) {
  173.  
  174. this._skyboxMesh.visible = value;
  175.  
  176. /**
  177. Fired whenever this Skybox's {{#crossLink "Skybox/active:property"}}{{/crossLink}} property changes.
  178.  
  179. @event active
  180. @param value {Boolean} The property's new value
  181. */
  182. this.fire("active", this._skyboxMesh.visible);
  183. }
  184.  
  185. get active() {
  186. return this._skyboxMesh.visible;
  187. }
  188. };
  189.