ConstNode.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.ConstNode = function( src, useDefine ) {
  5. THREE.TempNode.call( this );
  6. this.eval( src || THREE.ConstNode.PI, useDefine );
  7. };
  8. THREE.ConstNode.PI = 'PI';
  9. THREE.ConstNode.PI2 = 'PI2';
  10. THREE.ConstNode.RECIPROCAL_PI = 'RECIPROCAL_PI';
  11. THREE.ConstNode.RECIPROCAL_PI2 = 'RECIPROCAL_PI2';
  12. THREE.ConstNode.LOG2 = 'LOG2';
  13. THREE.ConstNode.EPSILON = 'EPSILON';
  14. THREE.ConstNode.prototype = Object.create( THREE.TempNode.prototype );
  15. THREE.ConstNode.prototype.constructor = THREE.ConstNode;
  16. THREE.ConstNode.prototype.getType = function( builder ) {
  17. return builder.getTypeByFormat( this.type );
  18. };
  19. THREE.ConstNode.prototype.eval = function( src, useDefine ) {
  20. src = ( src || '' ).trim();
  21. var name, type, value;
  22. var rDeclaration = /^([a-z_0-9]+)\s([a-z_0-9]+)\s?\=?\s?(.*?)(\;|$)/i;
  23. var match = src.match( rDeclaration );
  24. this.useDefine = useDefine;
  25. if ( match && match.length > 1 ) {
  26. type = match[ 1 ];
  27. name = match[ 2 ];
  28. value = match[ 3 ];
  29. } else {
  30. name = src;
  31. type = 'fv1';
  32. }
  33. this.name = name;
  34. this.type = type;
  35. this.value = value;
  36. };
  37. THREE.ConstNode.prototype.build = function( builder, output ) {
  38. if ( output === 'source' ) {
  39. if ( this.value ) {
  40. if ( this.useDefine ) {
  41. return '#define ' + this.name + ' ' + this.value;
  42. }
  43. return 'const ' + this.type + ' ' + this.name + ' = ' + this.value + ';';
  44. }
  45. } else {
  46. builder.include( this );
  47. return builder.format( this.name, this.getType( builder ), output );
  48. }
  49. };
  50. THREE.ConstNode.prototype.generate = function( builder, output ) {
  51. return builder.format( this.name, this.getType( builder ), output );
  52. };