NormalNode.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.prototype = Object.create( TempNode.prototype );
  13. NormalNode.prototype.constructor = NormalNode;
  14. NormalNode.prototype.nodeType = "Normal";
  15. NormalNode.prototype.getShared = function () {
  16. // if shared is false, TempNode will not create temp variable (for optimization)
  17. return this.scope === NormalNode.WORLD;
  18. };
  19. NormalNode.prototype.generate = function ( builder, output ) {
  20. var result;
  21. switch ( this.scope ) {
  22. case NormalNode.LOCAL:
  23. if ( builder.isShader( 'vertex' ) ) result = 'objectNormal';
  24. else result = 'geometryNormal';
  25. break;
  26. case NormalNode.WORLD:
  27. if ( builder.isShader( 'vertex' ) ) {
  28. result = '( modelMatrix * vec4( objectNormal, 0.0 ) ).xyz';
  29. } else {
  30. result = 'inverseTransformDirection( normal, viewMatrix )';
  31. }
  32. break;
  33. }
  34. return builder.format( result, this.getType( builder ), output );
  35. };
  36. NormalNode.prototype.copy = function ( source ) {
  37. TempNode.prototype.copy.call( this, source );
  38. this.scope = source.scope;
  39. return this;
  40. };
  41. NormalNode.prototype.toJSON = function ( meta ) {
  42. var data = this.getJSONNode( meta );
  43. if ( ! data ) {
  44. data = this.createJSONNode( meta );
  45. data.scope = this.scope;
  46. }
  47. return data;
  48. };
  49. NodeLib.addKeyword( 'normal', function () {
  50. return new NormalNode();
  51. } );
  52. NodeLib.addKeyword( 'worldNormal', function () {
  53. return new NormalNode( NormalNode.WORLD );
  54. } );
  55. export { NormalNode };