Source: objects/Scene.js

  1. import { OBJECT_TYPE } from '../const.js';
  2. import { LightCache } from '../render/LightCache.js';
  3. import { RenderList } from '../render/RenderList.js';
  4. import { Object3D } from './Object3D.js';
  5. /**
  6. * Scenes allow you to set up what and where is to be rendered by zen3d.
  7. * This is where you place objects, lights and cameras.
  8. * @constructor
  9. * @memberof zen3d
  10. * @extends zen3d.Object3D
  11. */
  12. function Scene() {
  13. Object3D.call(this);
  14. this.type = OBJECT_TYPE.SCENE;
  15. /**
  16. * If not null, it will force everything in the scene to be rendered with that material.
  17. * @type {zen3d.Material}
  18. * @default null
  19. */
  20. this.overrideMaterial = null;
  21. /**
  22. * A {@link zen3d.Fog} instance defining the type of fog that affects everything rendered in the scene.
  23. * @type {zen3d.Fog}
  24. * @default null
  25. */
  26. this.fog = null;
  27. /**
  28. * User-defined clipping planes specified as {@link zen3d.Plane} objects in world space.
  29. * These planes apply to the scene.
  30. * Points in space whose dot product with the plane is negative are cut away.
  31. * @type {zen3d.Plane[]}
  32. * @default []
  33. */
  34. this.clippingPlanes = [];
  35. /**
  36. * Defines whether disable shadow sampler feature.
  37. * Shader with sampler2DShadow uniforms may cause unknown error on some android phones, set disableShadowSampler to true to avoid these bugs.
  38. * @type {boolean}
  39. * @default false
  40. */
  41. this.disableShadowSampler = false;
  42. /**
  43. * A {@link zen3d.LightCache} instance that collected all lights info after the calling of {@link zen3d.Scene#updateLights}.
  44. * @type {zen3d.LightCache}
  45. * @default zen3d.LightCache()
  46. */
  47. this.lights = new LightCache();
  48. this._renderListMap = new WeakMap();
  49. }
  50. Scene.prototype = Object.assign(Object.create(Object3D.prototype), /** @lends zen3d.Scene.prototype */{
  51. constructor: Scene,
  52. /**
  53. * Update {@link zen3d.RenderList} for the scene and camera.
  54. * @param {zen3d.Camera} camera - The camera.
  55. * @return {RenderList} - The result render list.
  56. */
  57. updateRenderList: function(camera) {
  58. if (!this._renderListMap.has(camera)) {
  59. this._renderListMap.set(camera, new RenderList());
  60. }
  61. var renderList = this._renderListMap.get(camera);
  62. renderList.startCount();
  63. this._doUpdateRenderList(this, camera, renderList);
  64. renderList.endCount();
  65. renderList.sort();
  66. return renderList;
  67. },
  68. /**
  69. * Get {@link zen3d.RenderList} for the scene and camera.
  70. * The Render List must be updated before this calling.
  71. * @param {zen3d.Camera} camera - The camera.
  72. * @return {RenderList} - The target render list.
  73. */
  74. getRenderList: function(camera) {
  75. return this._renderListMap.get(camera);
  76. },
  77. /**
  78. * Update all lights in the scene.
  79. * @return {LightCache} - An instance of {@link LightCache} whitch contains all lights in the scene.
  80. */
  81. updateLights: function() {
  82. var lights = this.lights;
  83. lights.startCount();
  84. this._doUpdateLights(this);
  85. lights.endCount();
  86. return lights;
  87. },
  88. _doUpdateRenderList: function(object, camera, renderList) {
  89. if (!object.visible) {
  90. return;
  91. }
  92. if (!!object.geometry && !!object.material) { // renderable
  93. renderList.add(object, camera);
  94. }
  95. // handle children by recursion
  96. var children = object.children;
  97. for (var i = 0, l = children.length; i < l; i++) {
  98. this._doUpdateRenderList(children[i], camera, renderList);
  99. }
  100. },
  101. _doUpdateLights: function(object) {
  102. if (!object.visible) {
  103. return;
  104. }
  105. if (OBJECT_TYPE.LIGHT === object.type) { // light
  106. this.lights.add(object);
  107. }
  108. // handle children by recursion
  109. var children = object.children;
  110. for (var i = 0, l = children.length; i < l; i++) {
  111. this._doUpdateLights(children[i]);
  112. }
  113. }
  114. });
  115. export { Scene };