ModelNode.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import Node from '../core/Node.js';
  2. import Matrix4Node from '../inputs/Matrix4Node.js';
  3. import Matrix3Node from '../inputs/Matrix3Node.js';
  4. import { NodeUpdateType } from '../core/constants.js';
  5. class ModelNode extends Node {
  6. static VIEW = 'view';
  7. static NORMAL = 'normal';
  8. constructor( scope = ModelNode.VIEW ) {
  9. super();
  10. this.scope = scope;
  11. this.updateType = NodeUpdateType.Object;
  12. this._inputNode = null;
  13. }
  14. getType() {
  15. const scope = this.scope;
  16. if ( scope === ModelNode.VIEW ) {
  17. return 'mat4';
  18. } else if ( scope === ModelNode.NORMAL ) {
  19. return 'mat3';
  20. }
  21. }
  22. update( frame ) {
  23. const object = frame.object;
  24. const inputNode = this._inputNode;
  25. const scope = this.scope;
  26. if ( scope === ModelNode.VIEW ) {
  27. inputNode.value = object.modelViewMatrix;
  28. } else if ( scope === ModelNode.NORMAL ) {
  29. inputNode.value = object.normalMatrix;
  30. }
  31. }
  32. generate( builder, output ) {
  33. const nodeData = builder.getDataFromNode( this );
  34. let inputNode = this._inputNode;
  35. if ( nodeData.inputNode === undefined ) {
  36. const scope = this.scope;
  37. if ( scope === ModelNode.VIEW ) {
  38. if ( inputNode === null || inputNode.isMatrix4Node !== true ) {
  39. inputNode = new Matrix4Node( null );
  40. }
  41. } else if ( scope === ModelNode.NORMAL ) {
  42. if ( inputNode === null || inputNode.isMatrix3Node !== true ) {
  43. inputNode = new Matrix3Node( null );
  44. }
  45. }
  46. this._inputNode = inputNode;
  47. nodeData.inputNode = inputNode;
  48. }
  49. return inputNode.build( builder, output );
  50. }
  51. }
  52. export default ModelNode;