/home/lindsay/xeolabs/xeogl-next/xeogl/src/lighting/ambientLight.js
API Docs for:

File: /home/lindsay/xeolabs/xeogl-next/xeogl/src/lighting/ambientLight.js

  1. /**
  2. An **AmbientLight** defines an ambient light source of fixed intensity and color that affects all {{#crossLink "Mesh"}}Meshes{{/crossLink}}
  3. equally.
  4.  
  5. <a href="../../examples/#lights_ambient"><img src="http://i.giphy.com/l0HlGTxXQWMRVOPwk.gif"></img></a>
  6.  
  7. ## Overview
  8.  
  9. * When {{#crossLink "Mesh"}}Meshes{{/crossLink}} have {{#crossLink "PhongMaterial"}}PhongMaterials{{/crossLink}},
  10. AmbientLight {{#crossLink "AmbientLight/color:property"}}color{{/crossLink}} is multiplied by
  11. PhongMaterial {{#crossLink "PhongMaterial/ambient:property"}}{{/crossLink}} at each rendered fragment of the {{#crossLink "Geometry"}}{{/crossLink}} surface.
  12. * When the Meshes have {{#crossLink "LambertMaterial"}}LambertMaterials{{/crossLink}},
  13. AmbientLight {{#crossLink "AmbientLight/color:property"}}color{{/crossLink}} is multiplied by
  14. LambertMaterial {{#crossLink "LambertMaterial/ambient:property"}}{{/crossLink}} for each rendered triangle of the Geometry surface (ie. flat shaded).
  15. * {{#crossLink "AmbientLight"}}{{/crossLink}}, {{#crossLink "DirLight"}}{{/crossLink}},
  16. {{#crossLink "SpotLight"}}{{/crossLink}} and {{#crossLink "PointLight"}}{{/crossLink}} instances are registered by ID
  17. on {{#crossLink "Scene/lights:property"}}Scene#lights{{/crossLink}} for convenient access.
  18.  
  19. ## Examples
  20.  
  21. * [Ambient light source](../../examples/#lights_ambient)
  22.  
  23. ## Usage
  24.  
  25. In the example below we'll customize the default Scene's light sources, defining an AmbientLight and a couple of
  26. DirLights, then create a Phong-shaded box mesh.
  27.  
  28. ````javascript
  29. new xeogl.AmbientLight({
  30. color: [0.8, 0.8, 0.8],
  31. intensity: 0.5
  32. });
  33.  
  34. new xeogl.DirLight({
  35. dir: [-0.8, -0.4, -0.4],
  36. color: [0.4, 0.4, 0.5],
  37. intensity: 0.5,
  38. space: "view"
  39. });
  40.  
  41. new xeogl.DirLight({
  42. dir: [0.2, -0.8, 0.8],
  43. color: [0.8, 0.8, 0.8],
  44. intensity: 0.5,
  45. space: "view"
  46. });
  47. ````
  48.  
  49. @class AmbientLight
  50. @module xeogl
  51. @submodule lighting
  52. @constructor
  53. @param [owner] {Component} Owner component. When destroyed, the owner will destroy this component as well. Creates this component within the default {{#crossLink "Scene"}}{{/crossLink}} when omitted.
  54. @param [cfg] {*} AmbientLight configuration
  55. @param [cfg.id] {String} Optional ID, unique among all components in the parent {{#crossLink "Scene"}}Scene{{/crossLink}}, generated automatically when omitted.
  56. @param [cfg.meta] {String:Object} Optional map of user-defined metadata to attach to this AmbientLight.
  57. @param [cfg.color=[0.7, 0.7, 0.8]] {Array(Number)} The color of this AmbientLight.
  58. @param [cfg.intensity=[1.0]] {Number} The intensity of this AmbientLight, as a factor in range ````[0..1]````.
  59. @extends Component
  60. */
  61. import {core} from '../core.js';
  62. import {math} from '../math/math.js';
  63. import {Component} from '../component.js';
  64. import {componentClasses} from "./../componentClasses.js";
  65.  
  66. const type = "xeogl.AmbientLight";
  67.  
  68. class AmbientLight extends Component {
  69.  
  70. /**
  71. JavaScript class name for this Component.
  72.  
  73. For example: "xeogl.AmbientLight", "xeogl.MetallicMaterial" etc.
  74.  
  75. @property type
  76. @type String
  77. @final
  78. */
  79. get type() {
  80. return type;
  81. }
  82.  
  83. init(cfg) {
  84. super.init(cfg);
  85. this._state = {
  86. type: "ambient",
  87. color: math.vec3([0.7, 0.7, 0.7]),
  88. intensity: 1.0
  89. };
  90. this.color = cfg.color;
  91. this.intensity = cfg.intensity;
  92. this.scene._lightCreated(this);
  93. }
  94.  
  95. /**
  96. The color of this AmbientLight.
  97.  
  98. @property color
  99. @default [0.7, 0.7, 0.8]
  100. @type Float32Array
  101. */
  102. set color(value) {
  103. this._state.color.set(value || [0.7, 0.7, 0.8]);
  104. this._renderer.setImageForceDirty();
  105. }
  106.  
  107. get color() {
  108. return this._state.color;
  109. }
  110.  
  111. /**
  112. The intensity of this AmbientLight.
  113.  
  114. @property intensity
  115. @default 1.0
  116. @type Number
  117. */
  118. set intensity(value) {
  119. this._state.intensity = value !== undefined ? value : 1.0;
  120. this._renderer.setImageForceDirty();
  121. }
  122.  
  123. get intensity() {
  124. return this._state.intensity;
  125. }
  126.  
  127. destroy() {
  128. super.destroy();
  129. }
  130. }
  131.  
  132. componentClasses[type] = AmbientLight;
  133.  
  134. export {AmbientLight};
  135.