NodeMaterial.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.NodeMaterial = function( vertex, fragment ) {
  5. THREE.ShaderMaterial.call( this );
  6. this.vertex = vertex || new THREE.RawNode( new THREE.PositionNode( THREE.PositionNode.PROJECTION ) );
  7. this.fragment = fragment || new THREE.RawNode( new THREE.ColorNode( 0xFF0000 ) );
  8. };
  9. THREE.NodeMaterial.types = {
  10. t : 'sampler2D',
  11. tc : 'samplerCube',
  12. bv1 : 'bool',
  13. iv1 : 'int',
  14. fv1 : 'float',
  15. c : 'vec3',
  16. v2 : 'vec2',
  17. v3 : 'vec3',
  18. v4 : 'vec4'
  19. };
  20. THREE.NodeMaterial.addShortcuts = function( proto, prop, list ) {
  21. function applyShortcut( prop, name ) {
  22. return {
  23. get: function() {
  24. return this[ prop ][ name ];
  25. },
  26. set: function( val ) {
  27. this[ prop ][ name ] = val;
  28. }
  29. };
  30. };
  31. return (function() {
  32. var shortcuts = {};
  33. for ( var i = 0; i < list.length; ++ i ) {
  34. var name = list[ i ];
  35. shortcuts[ name ] = applyShortcut( prop, name );
  36. }
  37. Object.defineProperties( proto, shortcuts );
  38. })();
  39. };
  40. THREE.NodeMaterial.prototype = Object.create( THREE.ShaderMaterial.prototype );
  41. THREE.NodeMaterial.prototype.constructor = THREE.NodeMaterial;
  42. THREE.NodeMaterial.prototype.updateAnimation = function( delta ) {
  43. for ( var i = 0; i < this.requestUpdate.length; ++ i ) {
  44. this.requestUpdate[ i ].updateAnimation( delta );
  45. }
  46. };
  47. THREE.NodeMaterial.prototype.build = function() {
  48. var vertex, fragment;
  49. this.defines = {};
  50. this.uniforms = {};
  51. this.nodeData = {};
  52. this.vertexUniform = [];
  53. this.fragmentUniform = [];
  54. this.vertexTemps = [];
  55. this.fragmentTemps = [];
  56. this.uniformList = [];
  57. this.consts = [];
  58. this.functions = [];
  59. this.requestUpdate = [];
  60. this.requestAttrib = {
  61. uv: [],
  62. color: []
  63. };
  64. this.vertexPars = '';
  65. this.fragmentPars = '';
  66. this.vertexCode = '';
  67. this.fragmentCode = '';
  68. this.vertexNode = '';
  69. this.fragmentNode = '';
  70. var builder = new THREE.BuilderNode( this );
  71. vertex = this.vertex.build( builder.setShader( 'vertex' ), 'v4' );
  72. fragment = this.fragment.build( builder.setShader( 'fragment' ), 'v4' );
  73. if ( this.requestAttrib.uv[ 0 ] ) {
  74. this.addVertexPars( 'varying vec2 vUv;' );
  75. this.addFragmentPars( 'varying vec2 vUv;' );
  76. this.addVertexCode( 'vUv = uv;' );
  77. }
  78. if ( this.requestAttrib.uv[ 1 ] ) {
  79. this.addVertexPars( 'varying vec2 vUv2; attribute vec2 uv2;' );
  80. this.addFragmentPars( 'varying vec2 vUv2;' );
  81. this.addVertexCode( 'vUv2 = uv2;' );
  82. }
  83. if ( this.requestAttrib.color[ 0 ] ) {
  84. this.addVertexPars( 'varying vec4 vColor; attribute vec4 color;' );
  85. this.addFragmentPars( 'varying vec4 vColor;' );
  86. this.addVertexCode( 'vColor = color;' );
  87. }
  88. if ( this.requestAttrib.color[ 1 ] ) {
  89. this.addVertexPars( 'varying vec4 vColor2; attribute vec4 color2;' );
  90. this.addFragmentPars( 'varying vec4 vColor2;' );
  91. this.addVertexCode( 'vColor2 = color2;' );
  92. }
  93. if ( this.requestAttrib.position ) {
  94. this.addVertexPars( 'varying vec3 vPosition;' );
  95. this.addFragmentPars( 'varying vec3 vPosition;' );
  96. this.addVertexCode( 'vPosition = transformed;' );
  97. }
  98. if ( this.requestAttrib.worldPosition ) {
  99. // for future update replace from the native "varying vec3 vWorldPosition" for optimization
  100. this.addVertexPars( 'varying vec3 vWPosition;' );
  101. this.addFragmentPars( 'varying vec3 vWPosition;' );
  102. this.addVertexCode( 'vWPosition = worldPosition.xyz;' );
  103. }
  104. if ( this.requestAttrib.normal ) {
  105. this.addVertexPars( 'varying vec3 vObjectNormal;' );
  106. this.addFragmentPars( 'varying vec3 vObjectNormal;' );
  107. this.addVertexCode( 'vObjectNormal = normal;' );
  108. }
  109. if ( this.requestAttrib.worldNormal ) {
  110. this.addVertexPars( 'varying vec3 vWNormal;' );
  111. this.addFragmentPars( 'varying vec3 vWNormal;' );
  112. this.addVertexCode( 'vWNormal = ( modelMatrix * vec4( objectNormal, 0.0 ) ).xyz;' );
  113. }
  114. this.lights = this.requestAttrib.light;
  115. this.transparent = this.requestAttrib.transparent;
  116. this.vertexShader = [
  117. this.vertexPars,
  118. this.getCodePars( this.vertexUniform, 'uniform' ),
  119. this.getIncludes( this.consts[ 'vertex' ] ),
  120. this.getIncludes( this.functions[ 'vertex' ] ),
  121. 'void main(){',
  122. this.getCodePars( this.vertexTemps ),
  123. vertex,
  124. this.vertexCode,
  125. '}'
  126. ].join( "\n" );
  127. this.fragmentShader = [
  128. this.fragmentPars,
  129. this.getCodePars( this.fragmentUniform, 'uniform' ),
  130. this.getIncludes( this.consts[ 'fragment' ] ),
  131. this.getIncludes( this.functions[ 'fragment' ] ),
  132. 'void main(){',
  133. this.getCodePars( this.fragmentTemps ),
  134. this.fragmentCode,
  135. fragment,
  136. '}'
  137. ].join( "\n" );
  138. this.needsUpdate = true;
  139. this.dispose(); // force update
  140. return this;
  141. };
  142. THREE.NodeMaterial.prototype.define = function( name, value ) {
  143. this.defines[ name ] = value == undefined ? 1 : value;
  144. };
  145. THREE.NodeMaterial.prototype.isDefined = function( name ) {
  146. return this.defines[ name ] != undefined;
  147. };
  148. THREE.NodeMaterial.prototype.mergeUniform = function( uniforms ) {
  149. for ( var name in uniforms ) {
  150. this.uniforms[ name ] = uniforms[ name ];
  151. }
  152. };
  153. THREE.NodeMaterial.prototype.createUniform = function( value, type, ns, needsUpdate ) {
  154. var index = this.uniformList.length;
  155. var uniform = {
  156. type : type,
  157. value : value,
  158. name : ns ? ns : 'nVu' + index,
  159. needsUpdate : needsUpdate
  160. };
  161. this.uniformList.push( uniform );
  162. return uniform;
  163. };
  164. THREE.NodeMaterial.prototype.getVertexTemp = function( uuid, type, ns ) {
  165. if ( ! this.vertexTemps[ uuid ] ) {
  166. var index = this.vertexTemps.length,
  167. name = ns ? ns : 'nVt' + index,
  168. data = { name : name, type : type };
  169. this.vertexTemps.push( data );
  170. this.vertexTemps[ uuid ] = data;
  171. }
  172. return this.vertexTemps[ uuid ];
  173. };
  174. THREE.NodeMaterial.prototype.getFragmentTemp = function( uuid, type, ns ) {
  175. if ( ! this.fragmentTemps[ uuid ] ) {
  176. var index = this.fragmentTemps.length,
  177. name = ns ? ns : 'nVt' + index,
  178. data = { name : name, type : type };
  179. this.fragmentTemps.push( data );
  180. this.fragmentTemps[ uuid ] = data;
  181. }
  182. return this.fragmentTemps[ uuid ];
  183. };
  184. THREE.NodeMaterial.prototype.getIncludes = function( incs ) {
  185. function sortByPosition( a, b ) {
  186. return b.deps - a.deps;
  187. }
  188. return function( incs ) {
  189. if ( ! incs ) return '';
  190. var code = '';
  191. var incs = incs.sort( sortByPosition );
  192. for ( var i = 0; i < incs.length; i ++ ) {
  193. code += incs[ i ].node.src + '\n';
  194. }
  195. return code;
  196. }
  197. }();
  198. THREE.NodeMaterial.prototype.addVertexPars = function( code ) {
  199. this.vertexPars += code + '\n';
  200. };
  201. THREE.NodeMaterial.prototype.addFragmentPars = function( code ) {
  202. this.fragmentPars += code + '\n';
  203. };
  204. THREE.NodeMaterial.prototype.addVertexCode = function( code ) {
  205. this.vertexCode += code + '\n';
  206. };
  207. THREE.NodeMaterial.prototype.addFragmentCode = function( code ) {
  208. this.fragmentCode += code + '\n';
  209. };
  210. THREE.NodeMaterial.prototype.addVertexNode = function( code ) {
  211. this.vertexNode += code + '\n';
  212. };
  213. THREE.NodeMaterial.prototype.clearVertexNode = function() {
  214. var code = this.fragmentNode;
  215. this.fragmentNode = '';
  216. return code;
  217. };
  218. THREE.NodeMaterial.prototype.addFragmentNode = function( code ) {
  219. this.fragmentNode += code + '\n';
  220. };
  221. THREE.NodeMaterial.prototype.clearFragmentNode = function() {
  222. var code = this.fragmentNode;
  223. this.fragmentNode = '';
  224. return code;
  225. };
  226. THREE.NodeMaterial.prototype.getCodePars = function( pars, prefix ) {
  227. prefix = prefix || '';
  228. var code = '';
  229. for ( var i = 0, l = pars.length; i < l; ++ i ) {
  230. var parsType = pars[ i ].type;
  231. var parsName = pars[ i ].name;
  232. var parsValue = pars[ i ].value;
  233. if ( parsType == 't' && parsValue instanceof THREE.CubeTexture ) parsType = 'tc';
  234. var type = THREE.NodeMaterial.types[ parsType ];
  235. if ( type == undefined ) throw new Error( "Node pars " + parsType + " not found." );
  236. code += prefix + ' ' + type + ' ' + parsName + ';\n';
  237. }
  238. return code;
  239. };
  240. THREE.NodeMaterial.prototype.getVertexUniform = function( value, type, ns, needsUpdate ) {
  241. var uniform = this.createUniform( value, type, ns, needsUpdate );
  242. this.vertexUniform.push( uniform );
  243. this.vertexUniform[ uniform.name ] = uniform;
  244. this.uniforms[ uniform.name ] = uniform;
  245. return uniform;
  246. };
  247. THREE.NodeMaterial.prototype.getFragmentUniform = function( value, type, ns, needsUpdate ) {
  248. var uniform = this.createUniform( value, type, ns, needsUpdate );
  249. this.fragmentUniform.push( uniform );
  250. this.fragmentUniform[ uniform.name ] = uniform;
  251. this.uniforms[ uniform.name ] = uniform;
  252. return uniform;
  253. };
  254. THREE.NodeMaterial.prototype.getDataNode = function( uuid ) {
  255. return this.nodeData[ uuid ] = this.nodeData[ uuid ] || {};
  256. };
  257. THREE.NodeMaterial.prototype.include = function( shader, node ) {
  258. var includes;
  259. node = typeof node === 'string' ? THREE.NodeLib.get( node ) : node;
  260. if ( node instanceof THREE.FunctionNode ) {
  261. for ( var i = 0; i < node.includes.length; i ++ ) {
  262. this.include( shader, node.includes[ i ] );
  263. }
  264. includes = this.functions[ shader ] = this.functions[ shader ] || [];
  265. }
  266. else if ( node instanceof THREE.ConstNode ) {
  267. includes = this.consts[ shader ] = this.consts[ shader ] || [];
  268. }
  269. if ( includes[ node.name ] === undefined ) {
  270. for ( var ext in node.extensions ) {
  271. this.extensions[ ext ] = true;
  272. }
  273. includes[ node.name ] = {
  274. node : node,
  275. deps : 1
  276. };
  277. includes.push( includes[ node.name ] );
  278. }
  279. else ++ includes[ node.name ].deps;
  280. };