Source: render/Renderer.js

  1. import { RenderTargetBack } from './RenderTargetBack.js';
  2. import { WebGLCore } from './WebGL/WebGLCore.js';
  3. import { ShadowMapPass } from './prePass/ShadowMapPass.js';
  4. /**
  5. * A simple forward renderer.
  6. * @constructor
  7. * @memberof zen3d
  8. * @param {HTMLCanvasElement} view - The canvas elements.
  9. * @param {Object} [options=] - The {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext options for webgl context}.
  10. */
  11. function Renderer(view, options) {
  12. var defaultContextParams = {
  13. antialias: true, // antialias
  14. alpha: false, // effect performance, default false
  15. // premultipliedAlpha: false, // effect performance, default false
  16. stencil: true
  17. };
  18. var gl = view.getContext("webgl2", options || defaultContextParams) || view.getContext("webgl", options || defaultContextParams);
  19. this.glCore = new WebGLCore(gl);
  20. console.info("ForwardRenderer use WebGL Version: " + this.glCore.capabilities.version);
  21. this.backRenderTarget = new RenderTargetBack(view);
  22. this.shadowMapPass = new ShadowMapPass();
  23. /**
  24. * Defines whether the shadow pass should automatically update.
  25. * @type {boolean}
  26. * @default true
  27. */
  28. this.shadowAutoUpdate = true;
  29. /**
  30. * If {@link zen3d.Renderer.shadowAutoUpdate} is set true and this set true, shadow will update and set this to false automatically.
  31. * @type {boolean}
  32. * @default false
  33. */
  34. this.shadowNeedsUpdate = false;
  35. /**
  36. * Defines whether the scene should automatically update its matrix.
  37. * @type {boolean}
  38. * @default true
  39. */
  40. this.matrixAutoUpdate = true;
  41. /**
  42. * Defines whether the scene should automatically update its lights.
  43. * @type {boolean}
  44. * @default true
  45. */
  46. this.lightsAutoUpdate = true;
  47. /**
  48. * Defines whether the renderer should automatically clear its output before rendering a frame.
  49. * @type {boolean}
  50. * @default true
  51. */
  52. this.autoClear = true;
  53. }
  54. /**
  55. * Render a scene using a camera.
  56. * The render is done to the renderTarget (if specified) or to the canvas as usual.
  57. * @param {zen3d.Scene} scene - The scene.
  58. * @param {zen3d.Camera} camera - The camera.
  59. * @param {zen3d.RenderTargetBase} [renderTarget=] - The render is done to the renderTarget (if specified) or to the canvas as usual.
  60. * @param {boolean} [forceClear=false] - If set true, the depth, stencil and color buffers will be cleared before rendering even if the renderer's autoClear property is false.
  61. */
  62. Renderer.prototype.render = function(scene, camera, renderTarget, forceClear) {
  63. this.matrixAutoUpdate && scene.updateMatrix();
  64. this.lightsAutoUpdate && scene.updateLights();
  65. if (this.shadowAutoUpdate || this.shadowNeedsUpdate) {
  66. this.shadowMapPass.render(this.glCore, scene);
  67. this.shadowNeedsUpdate = false;
  68. }
  69. if (renderTarget === undefined) {
  70. renderTarget = this.backRenderTarget;
  71. }
  72. this.glCore.renderTarget.setRenderTarget(renderTarget);
  73. if (this.autoClear || forceClear) {
  74. this.glCore.clear(true, true, true);
  75. }
  76. this.glCore.render(scene, camera);
  77. if (!!renderTarget.texture) {
  78. this.glCore.renderTarget.updateRenderTargetMipmap(renderTarget);
  79. }
  80. }
  81. export { Renderer };