Object3DNode.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import Node from '../core/Node.js';
  2. import Matrix4Node from '../inputs/Matrix4Node.js';
  3. import Matrix3Node from '../inputs/Matrix3Node.js';
  4. import Vector3Node from '../inputs/Vector3Node.js';
  5. import { NodeUpdateType } from '../core/constants.js';
  6. class Object3DNode extends Node {
  7. static VIEW_MATRIX = 'viewMatrix';
  8. static NORMAL_MATRIX = 'normalMatrix';
  9. static WORLD_MATRIX = 'worldMatrix';
  10. static POSITION = 'position';
  11. static VIEW_POSITION = 'viewPosition';
  12. constructor( scope = Object3DNode.VIEW_MATRIX, object3d = null ) {
  13. super();
  14. this.scope = scope;
  15. this.object3d = object3d;
  16. this.updateType = NodeUpdateType.Object;
  17. this._inputNode = null;
  18. }
  19. getType() {
  20. const scope = this.scope;
  21. if ( scope === Object3DNode.WORLD_MATRIX || scope === Object3DNode.VIEW_MATRIX ) {
  22. return 'mat4';
  23. } else if ( scope === Object3DNode.NORMAL_MATRIX ) {
  24. return 'mat3';
  25. } else if ( scope === Object3DNode.POSITION || scope === Object3DNode.VIEW_POSITION ) {
  26. return 'vec3';
  27. }
  28. }
  29. update( frame ) {
  30. const object = this.object3d !== null ? this.object3d : frame.object;
  31. const inputNode = this._inputNode;
  32. const camera = frame.camera;
  33. const scope = this.scope;
  34. if ( scope === Object3DNode.VIEW_MATRIX ) {
  35. inputNode.value = object.modelViewMatrix;
  36. } else if ( scope === Object3DNode.NORMAL_MATRIX ) {
  37. inputNode.value = object.normalMatrix;
  38. } else if ( scope === Object3DNode.WORLD_MATRIX ) {
  39. inputNode.value = object.matrixWorld;
  40. } else if ( scope === Object3DNode.POSITION ) {
  41. inputNode.value.setFromMatrixPosition( object.matrixWorld );
  42. } else if ( scope === Object3DNode.VIEW_POSITION ) {
  43. inputNode.value.setFromMatrixPosition( object.matrixWorld );
  44. inputNode.value.applyMatrix4( camera.matrixWorldInverse );
  45. }
  46. }
  47. generate( builder, output ) {
  48. const nodeData = builder.getDataFromNode( this );
  49. let inputNode = this._inputNode;
  50. if ( nodeData.inputNode === undefined ) {
  51. const scope = this.scope;
  52. if ( scope === Object3DNode.WORLD_MATRIX || scope === Object3DNode.VIEW_MATRIX ) {
  53. if ( inputNode === null || inputNode.isMatrix4Node !== true ) {
  54. inputNode = new Matrix4Node( /*null*/ );
  55. }
  56. } else if ( scope === Object3DNode.NORMAL_MATRIX ) {
  57. if ( inputNode === null || inputNode.isMatrix3Node !== true ) {
  58. inputNode = new Matrix3Node( /*null*/ );
  59. }
  60. } else if ( scope === Object3DNode.POSITION || scope === Object3DNode.VIEW_POSITION ) {
  61. if ( inputNode === null || inputNode.isVector3Node !== true ) {
  62. inputNode = new Vector3Node();
  63. }
  64. }
  65. this._inputNode = inputNode;
  66. nodeData.inputNode = inputNode;
  67. }
  68. return inputNode.build( builder, output );
  69. }
  70. }
  71. export default Object3DNode;