NormalNode.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.VIEW;
  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 () {
  17. // if shared is false, TempNode will not create temp variable (for optimization)
  18. return this.scope === NormalNode.WORLD;
  19. };
  20. NormalNode.prototype.generate = function ( builder, output ) {
  21. var result;
  22. switch ( this.scope ) {
  23. case NormalNode.VIEW:
  24. if ( builder.isShader( 'vertex' ) ) result = 'transformedNormal';
  25. else result = 'geometryNormal';
  26. break;
  27. case NormalNode.LOCAL:
  28. if ( builder.isShader( 'vertex' ) ) {
  29. result = 'objectNormal';
  30. } else {
  31. builder.requires.normal = true;
  32. result = 'vObjectNormal';
  33. }
  34. break;
  35. case NormalNode.WORLD:
  36. if ( builder.isShader( 'vertex' ) ) {
  37. result = 'inverseTransformDirection( transformedNormal, viewMatrix ).xyz';
  38. } else {
  39. builder.requires.worldNormal = true;
  40. result = 'vWNormal';
  41. }
  42. break;
  43. }
  44. return builder.format( result, this.getType( builder ), output );
  45. };
  46. NormalNode.prototype.copy = function ( source ) {
  47. TempNode.prototype.copy.call( this, source );
  48. this.scope = source.scope;
  49. return this;
  50. };
  51. NormalNode.prototype.toJSON = function ( meta ) {
  52. var data = this.getJSONNode( meta );
  53. if ( ! data ) {
  54. data = this.createJSONNode( meta );
  55. data.scope = this.scope;
  56. }
  57. return data;
  58. };
  59. NodeLib.addKeyword( 'viewNormal', function () {
  60. return new NormalNode( NormalNode.VIEW );
  61. } );
  62. NodeLib.addKeyword( 'localNormal', function () {
  63. return new NormalNode( NormalNode.NORMAL );
  64. } );
  65. NodeLib.addKeyword( 'worldNormal', function () {
  66. return new NormalNode( NormalNode.WORLD );
  67. } );
  68. export { NormalNode };