LightingContextNode.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import ContextNode from '../core/ContextNode.js';
  2. import { float, vec3, add, temp } from '../shadernode/ShaderNodeBaseElements.js';
  3. class LightingContextNode extends ContextNode {
  4. constructor( node, lightingModelNode = null ) {
  5. super( node );
  6. this.lightingModelNode = lightingModelNode;
  7. }
  8. getNodeType( /*builder*/ ) {
  9. return 'vec3';
  10. }
  11. construct( builder ) {
  12. const { lightingModelNode } = this;
  13. const context = this.context = {}; // reset context
  14. const properties = builder.getNodeProperties( this );
  15. const directDiffuse = temp( vec3() ),
  16. directSpecular = temp( vec3() ),
  17. indirectDiffuse = temp( vec3() ),
  18. indirectSpecular = temp( vec3() ),
  19. total = add( directDiffuse, directSpecular, indirectDiffuse, indirectSpecular );
  20. const reflectedLight = {
  21. directDiffuse,
  22. directSpecular,
  23. indirectDiffuse,
  24. indirectSpecular,
  25. total
  26. };
  27. const lighting = {
  28. radiance: temp( vec3() ),
  29. irradiance: temp( vec3() ),
  30. iblIrradiance: temp( vec3() ),
  31. ambientOcclusion: temp( float( 1 ) )
  32. };
  33. Object.assign( properties, reflectedLight, lighting );
  34. Object.assign( context, lighting );
  35. context.reflectedLight = reflectedLight;
  36. context.lightingModelNode = lightingModelNode || context.lightingModelNode;
  37. if ( lightingModelNode?.indirectDiffuse ) lightingModelNode.indirectDiffuse.call( context );
  38. if ( lightingModelNode?.indirectSpecular ) lightingModelNode.indirectSpecular.call( context );
  39. if ( lightingModelNode?.ambientOcclusion ) lightingModelNode.ambientOcclusion.call( context );
  40. return super.construct( builder );
  41. }
  42. generate( builder ) {
  43. const { context } = this;
  44. const type = this.getNodeType( builder );
  45. super.generate( builder, type );
  46. return context.reflectedLight.total.build( builder, type );
  47. }
  48. }
  49. export default LightingContextNode;