WebGLRenderStates.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { WebGLLights } from './WebGLLights.js';
  2. function WebGLRenderState() {
  3. const lights = new WebGLLights();
  4. const lightsArray = [];
  5. const shadowsArray = [];
  6. function init() {
  7. lightsArray.length = 0;
  8. shadowsArray.length = 0;
  9. }
  10. function pushLight( light ) {
  11. lightsArray.push( light );
  12. }
  13. function pushShadow( shadowLight ) {
  14. shadowsArray.push( shadowLight );
  15. }
  16. function setupLights() {
  17. lights.setup( lightsArray );
  18. }
  19. function setupLightsView( camera ) {
  20. lights.setupView( lightsArray, camera );
  21. }
  22. const state = {
  23. lightsArray: lightsArray,
  24. shadowsArray: shadowsArray,
  25. lights: lights
  26. };
  27. return {
  28. init: init,
  29. state: state,
  30. setupLights: setupLights,
  31. setupLightsView: setupLightsView,
  32. pushLight: pushLight,
  33. pushShadow: pushShadow
  34. };
  35. }
  36. function WebGLRenderStates() {
  37. let renderStates = new WeakMap();
  38. function get( scene, renderCallDepth ) {
  39. let renderState;
  40. if ( renderStates.has( scene ) === false ) {
  41. renderState = new WebGLRenderState();
  42. renderStates.set( scene, [] );
  43. renderStates.get( scene ).push( renderState );
  44. } else {
  45. if ( renderCallDepth >= renderStates.get( scene ).length ) {
  46. renderState = new WebGLRenderState();
  47. renderStates.get( scene ).push( renderState );
  48. } else {
  49. renderState = renderStates.get( scene )[ renderCallDepth ];
  50. }
  51. }
  52. return renderState;
  53. }
  54. function dispose() {
  55. renderStates = new WeakMap();
  56. }
  57. return {
  58. get: get,
  59. dispose: dispose
  60. };
  61. }
  62. export { WebGLRenderStates };
粤ICP备19079148号