NormalNode.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. result = 'normal';
  24. break;
  25. case NormalNode.WORLD:
  26. if ( builder.isShader( 'vertex' ) ) {
  27. result = '( modelMatrix * vec4( objectNormal, 0.0 ) ).xyz';
  28. } else {
  29. result = 'inverseTransformDirection( normal, viewMatrix )';
  30. }
  31. break;
  32. }
  33. return builder.format( result, this.getType( builder ), output );
  34. };
  35. NormalNode.prototype.copy = function ( source ) {
  36. TempNode.prototype.copy.call( this, source );
  37. this.scope = source.scope;
  38. return this;
  39. };
  40. NormalNode.prototype.toJSON = function ( meta ) {
  41. var data = this.getJSONNode( meta );
  42. if ( ! data ) {
  43. data = this.createJSONNode( meta );
  44. data.scope = this.scope;
  45. }
  46. return data;
  47. };
  48. NodeLib.addKeyword( 'normal', function () {
  49. return new NormalNode();
  50. } );
  51. NodeLib.addKeyword( 'worldNormal', function () {
  52. return new NormalNode( NormalNode.WORLD );
  53. } );
  54. export { NormalNode };