NormalNode.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.getShared = 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. // to use vObjectNormal as vertex normal
  28. //builder.requires.normal = true;
  29. result = 'normal';
  30. break;
  31. case NormalNode.WORLD:
  32. if ( builder.isShader( 'vertex' ) ) {
  33. return '( modelMatrix * vec4( objectNormal, 0.0 ) ).xyz';
  34. } else {
  35. builder.requires.worldNormal = true;
  36. result = 'vWNormal';
  37. }
  38. break;
  39. case NormalNode.VIEW:
  40. result = 'vNormal';
  41. break;
  42. }
  43. return builder.format( result, this.getType( builder ), output );
  44. };
  45. NormalNode.prototype.copy = function ( source ) {
  46. TempNode.prototype.copy.call( this, source );
  47. this.scope = source.scope;
  48. };
  49. NormalNode.prototype.toJSON = function ( meta ) {
  50. var data = this.getJSONNode( meta );
  51. if ( ! data ) {
  52. data = this.createJSONNode( meta );
  53. data.scope = this.scope;
  54. }
  55. return data;
  56. };
  57. NodeLib.addKeyword( 'normal', function () {
  58. return new NormalNode();
  59. } );
  60. NodeLib.addKeyword( 'worldNormal', function () {
  61. return new NormalNode( NormalNode.WORLD );
  62. } );
  63. NodeLib.addKeyword( 'viewNormal', function () {
  64. return new NormalNode( NormalNode.VIEW );
  65. } );
  66. export { NormalNode };