NormalNode.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from '../core/TempNode.js';
  5. import { NodeLib } from '../core/NodeLib.js';
  6. function NormalNode( scope ) {
  7. TempNode.call( this, 'v3' );
  8. this.scope = scope || NormalNode.LOCAL;
  9. }
  10. NormalNode.LOCAL = 'local';
  11. NormalNode.WORLD = 'world';
  12. NormalNode.VIEW = 'view';
  13. NormalNode.prototype = Object.create( TempNode.prototype );
  14. NormalNode.prototype.constructor = NormalNode;
  15. NormalNode.prototype.nodeType = "Normal";
  16. NormalNode.prototype.isShared = function ( builder ) {
  17. switch ( this.scope ) {
  18. case NormalNode.WORLD:
  19. return true;
  20. }
  21. return false;
  22. };
  23. NormalNode.prototype.generate = function ( builder, output ) {
  24. var result;
  25. switch ( this.scope ) {
  26. case NormalNode.LOCAL:
  27. builder.requires.normal = true;
  28. result = builder.isShader( 'vertex' ) ? 'normal' : 'vObjectNormal';
  29. break;
  30. case NormalNode.WORLD:
  31. builder.requires.worldNormal = true;
  32. result = builder.isShader( 'vertex' ) ? '( modelMatrix * vec4( objectNormal, 0.0 ) ).xyz' : 'vWNormal';
  33. break;
  34. case NormalNode.VIEW:
  35. result = 'normal';
  36. break;
  37. }
  38. return builder.format( result, this.getType( builder ), output );
  39. };
  40. NormalNode.prototype.copy = function ( source ) {
  41. TempNode.prototype.copy.call( this, source );
  42. this.scope = source.scope;
  43. };
  44. NormalNode.prototype.toJSON = function ( meta ) {
  45. var data = this.getJSONNode( meta );
  46. if ( ! data ) {
  47. data = this.createJSONNode( meta );
  48. data.scope = this.scope;
  49. }
  50. return data;
  51. };
  52. NodeLib.addKeyword( 'normal', function () {
  53. return new NormalNode();
  54. } );
  55. NodeLib.addKeyword( 'worldNormal', function () {
  56. return new NormalNode( NormalNode.WORLD );
  57. } );
  58. NodeLib.addKeyword( 'viewNormal', function () {
  59. return new NormalNode( NormalNode.VIEW );
  60. } );
  61. export { NormalNode };