NormalNode.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { TempNode } from '../core/TempNode.js';
  2. import { NodeLib } from '../core/NodeLib.js';
  3. function NormalNode( scope ) {
  4. TempNode.call( this, 'v3' );
  5. this.scope = scope || NormalNode.VIEW;
  6. }
  7. NormalNode.LOCAL = 'local';
  8. NormalNode.WORLD = 'world';
  9. NormalNode.VIEW = 'view';
  10. NormalNode.prototype = Object.create( TempNode.prototype );
  11. NormalNode.prototype.constructor = NormalNode;
  12. NormalNode.prototype.nodeType = 'Normal';
  13. NormalNode.prototype.getShared = function () {
  14. // if shared is false, TempNode will not create temp variable (for optimization)
  15. return this.scope === NormalNode.WORLD;
  16. };
  17. NormalNode.prototype.build = function ( builder, output, uuid, ns ) {
  18. var contextNormal = builder.context[ this.scope + 'Normal' ];
  19. if ( contextNormal ) {
  20. return contextNormal.build( builder, output, uuid, ns );
  21. }
  22. return TempNode.prototype.build.call( this, builder, output, uuid );
  23. };
  24. NormalNode.prototype.generate = function ( builder, output ) {
  25. var result;
  26. switch ( this.scope ) {
  27. case NormalNode.VIEW:
  28. if ( builder.isShader( 'vertex' ) ) result = 'transformedNormal';
  29. else result = 'geometryNormal';
  30. break;
  31. case NormalNode.LOCAL:
  32. if ( builder.isShader( 'vertex' ) ) {
  33. result = 'objectNormal';
  34. } else {
  35. builder.requires.normal = true;
  36. result = 'vObjectNormal';
  37. }
  38. break;
  39. case NormalNode.WORLD:
  40. if ( builder.isShader( 'vertex' ) ) {
  41. result = 'inverseTransformDirection( transformedNormal, viewMatrix ).xyz';
  42. } else {
  43. builder.requires.worldNormal = true;
  44. result = 'vWNormal';
  45. }
  46. break;
  47. }
  48. return builder.format( result, this.getType( builder ), output );
  49. };
  50. NormalNode.prototype.copy = function ( source ) {
  51. TempNode.prototype.copy.call( this, source );
  52. this.scope = source.scope;
  53. return this;
  54. };
  55. NormalNode.prototype.toJSON = function ( meta ) {
  56. var data = this.getJSONNode( meta );
  57. if ( ! data ) {
  58. data = this.createJSONNode( meta );
  59. data.scope = this.scope;
  60. }
  61. return data;
  62. };
  63. NodeLib.addKeyword( 'viewNormal', function () {
  64. return new NormalNode( NormalNode.VIEW );
  65. } );
  66. NodeLib.addKeyword( 'localNormal', function () {
  67. return new NormalNode( NormalNode.NORMAL );
  68. } );
  69. NodeLib.addKeyword( 'worldNormal', function () {
  70. return new NormalNode( NormalNode.WORLD );
  71. } );
  72. export { NormalNode };