NodeMaterial.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. m4 : 'mat4'
  20. };
  21. THREE.NodeMaterial.addShortcuts = function( proto, prop, list ) {
  22. function applyShortcut( prop, name ) {
  23. return {
  24. get: function() {
  25. return this[ prop ][ name ];
  26. },
  27. set: function( val ) {
  28. this[ prop ][ name ] = val;
  29. }
  30. };
  31. };
  32. return ( function() {
  33. var shortcuts = {};
  34. for ( var i = 0; i < list.length; ++ i ) {
  35. var name = list[ i ];
  36. shortcuts[ name ] = applyShortcut( prop, name );
  37. }
  38. Object.defineProperties( proto, shortcuts );
  39. } )();
  40. };
  41. THREE.NodeMaterial.prototype = Object.create( THREE.ShaderMaterial.prototype );
  42. THREE.NodeMaterial.prototype.constructor = THREE.NodeMaterial;
  43. THREE.NodeMaterial.prototype.updateFrame = function( delta ) {
  44. for ( var i = 0; i < this.requestUpdate.length; ++ i ) {
  45. this.requestUpdate[ i ].updateFrame( delta );
  46. }
  47. };
  48. THREE.NodeMaterial.prototype.build = function() {
  49. var vertex, fragment;
  50. this.defines = {};
  51. this.uniforms = {};
  52. this.attributes = {};
  53. this.nodeData = {};
  54. this.vertexUniform = [];
  55. this.fragmentUniform = [];
  56. this.vars = [];
  57. this.vertexTemps = [];
  58. this.fragmentTemps = [];
  59. this.uniformList = [];
  60. this.consts = [];
  61. this.functions = [];
  62. this.requestUpdate = [];
  63. this.requestAttribs = {
  64. uv: [],
  65. color: []
  66. };
  67. this.vertexPars = '';
  68. this.fragmentPars = '';
  69. this.vertexCode = '';
  70. this.fragmentCode = '';
  71. this.vertexNode = '';
  72. this.fragmentNode = '';
  73. this.prefixCode = [
  74. "#ifdef GL_EXT_shader_texture_lod",
  75. "#define texCube(a, b) textureCube(a, b)",
  76. "#define texCubeBias(a, b, c) textureCubeLodEXT(a, b, c)",
  77. "#define tex2D(a, b) texture2D(a, b)",
  78. "#define tex2DBias(a, b, c) texture2DLodEXT(a, b, c)",
  79. "#else",
  80. "#define texCube(a, b) textureCube(a, b)",
  81. "#define texCubeBias(a, b, c) textureCube(a, b, c)",
  82. "#define tex2D(a, b) texture2D(a, b)",
  83. "#define tex2DBias(a, b, c) texture2D(a, b, c)",
  84. "#endif"
  85. ].join( "\n" );
  86. var builder = new THREE.NodeBuilder( this );
  87. vertex = this.vertex.build( builder.setShader( 'vertex' ), 'v4' );
  88. fragment = this.fragment.build( builder.setShader( 'fragment' ), 'v4' );
  89. if ( this.requestAttribs.uv[ 0 ] ) {
  90. this.addVertexPars( 'varying vec2 vUv;' );
  91. this.addFragmentPars( 'varying vec2 vUv;' );
  92. this.addVertexCode( 'vUv = uv;' );
  93. }
  94. if ( this.requestAttribs.uv[ 1 ] ) {
  95. this.addVertexPars( 'varying vec2 vUv2; attribute vec2 uv2;' );
  96. this.addFragmentPars( 'varying vec2 vUv2;' );
  97. this.addVertexCode( 'vUv2 = uv2;' );
  98. }
  99. if ( this.requestAttribs.color[ 0 ] ) {
  100. this.addVertexPars( 'varying vec4 vColor; attribute vec4 color;' );
  101. this.addFragmentPars( 'varying vec4 vColor;' );
  102. this.addVertexCode( 'vColor = color;' );
  103. }
  104. if ( this.requestAttribs.color[ 1 ] ) {
  105. this.addVertexPars( 'varying vec4 vColor2; attribute vec4 color2;' );
  106. this.addFragmentPars( 'varying vec4 vColor2;' );
  107. this.addVertexCode( 'vColor2 = color2;' );
  108. }
  109. if ( this.requestAttribs.position ) {
  110. this.addVertexPars( 'varying vec3 vPosition;' );
  111. this.addFragmentPars( 'varying vec3 vPosition;' );
  112. this.addVertexCode( 'vPosition = transformed;' );
  113. }
  114. if ( this.requestAttribs.worldPosition ) {
  115. // for future update replace from the native "varying vec3 vWorldPosition" for optimization
  116. this.addVertexPars( 'varying vec3 vWPosition;' );
  117. this.addFragmentPars( 'varying vec3 vWPosition;' );
  118. this.addVertexCode( 'vWPosition = worldPosition.xyz;' );
  119. }
  120. if ( this.requestAttribs.normal ) {
  121. this.addVertexPars( 'varying vec3 vObjectNormal;' );
  122. this.addFragmentPars( 'varying vec3 vObjectNormal;' );
  123. this.addVertexCode( 'vObjectNormal = normal;' );
  124. }
  125. if ( this.requestAttribs.worldNormal ) {
  126. this.addVertexPars( 'varying vec3 vWNormal;' );
  127. this.addFragmentPars( 'varying vec3 vWNormal;' );
  128. this.addVertexCode( 'vWNormal = ( modelMatrix * vec4( objectNormal, 0.0 ) ).xyz;' );
  129. }
  130. this.lights = this.requestAttribs.light;
  131. this.transparent = this.requestAttribs.transparent || this.blendMode > THREE.NormalBlending;
  132. this.vertexShader = [
  133. this.prefixCode,
  134. this.vertexPars,
  135. this.getCodePars( this.vertexUniform, 'uniform' ),
  136. this.getIncludes( this.consts[ 'vertex' ] ),
  137. this.getIncludes( this.functions[ 'vertex' ] ),
  138. 'void main(){',
  139. this.getCodePars( this.vertexTemps ),
  140. vertex,
  141. this.vertexCode,
  142. '}'
  143. ].join( "\n" );
  144. this.fragmentShader = [
  145. this.prefixCode,
  146. this.fragmentPars,
  147. this.getCodePars( this.fragmentUniform, 'uniform' ),
  148. this.getIncludes( this.consts[ 'fragment' ] ),
  149. this.getIncludes( this.functions[ 'fragment' ] ),
  150. 'void main(){',
  151. this.getCodePars( this.fragmentTemps ),
  152. this.fragmentCode,
  153. fragment,
  154. '}'
  155. ].join( "\n" );
  156. this.needsUpdate = true;
  157. this.dispose(); // force update
  158. return this;
  159. };
  160. THREE.NodeMaterial.prototype.define = function( name, value ) {
  161. this.defines[ name ] = value == undefined ? 1 : value;
  162. };
  163. THREE.NodeMaterial.prototype.isDefined = function( name ) {
  164. return this.defines[ name ] != undefined;
  165. };
  166. THREE.NodeMaterial.prototype.mergeUniform = function( uniforms ) {
  167. for ( var name in uniforms ) {
  168. this.uniforms[ name ] = uniforms[ name ];
  169. }
  170. };
  171. THREE.NodeMaterial.prototype.createUniform = function( type, value, ns, needsUpdate ) {
  172. var index = this.uniformList.length;
  173. var uniform = {
  174. type : type,
  175. value : value,
  176. name : ns ? ns : 'nVu' + index,
  177. needsUpdate : needsUpdate
  178. };
  179. this.uniformList.push( uniform );
  180. return uniform;
  181. };
  182. THREE.NodeMaterial.prototype.getVertexTemp = function( uuid, type, ns ) {
  183. var data = this.vertexTemps[ uuid ];
  184. if ( ! data ) {
  185. var index = this.vertexTemps.length,
  186. name = ns ? ns : 'nVt' + index;
  187. data = { name : name, type : type };
  188. this.vertexTemps.push( data );
  189. this.vertexTemps[ uuid ] = data;
  190. }
  191. return data;
  192. };
  193. THREE.NodeMaterial.prototype.getFragmentTemp = function( uuid, type, ns ) {
  194. var data = this.fragmentTemps[ uuid ];
  195. if ( ! data ) {
  196. var index = this.fragmentTemps.length,
  197. name = ns ? ns : 'nVt' + index;
  198. data = { name : name, type : type };
  199. this.fragmentTemps.push( data );
  200. this.fragmentTemps[ uuid ] = data;
  201. }
  202. return data;
  203. };
  204. THREE.NodeMaterial.prototype.getVar = function( uuid, type, ns ) {
  205. var data = this.vars[ uuid ];
  206. if ( ! data ) {
  207. var index = this.vars.length,
  208. name = ns ? ns : 'nVv' + index;
  209. data = { name : name, type : type }
  210. this.vars.push( data );
  211. this.vars[ uuid ] = data;
  212. this.addVertexPars( 'varying ' + type + ' ' + name + ';' );
  213. this.addFragmentPars( 'varying ' + type + ' ' + name + ';' );
  214. }
  215. return data;
  216. };
  217. THREE.NodeMaterial.prototype.getAttribute = function( name, type ) {
  218. if ( ! this.attributes[ name ] ) {
  219. var varying = this.getVar( name, type );
  220. this.addVertexPars( 'attribute ' + type + ' ' + name + ';' );
  221. this.addVertexCode( varying.name + ' = ' + name + ';' );
  222. this.attributes[ name ] = { varying : varying, name : name, type : type };
  223. }
  224. return this.attributes[ name ];
  225. };
  226. THREE.NodeMaterial.prototype.getIncludes = function() {
  227. function sortByPosition( a, b ) {
  228. return a.deps.length - b.deps.length;
  229. }
  230. return function( incs ) {
  231. if ( ! incs ) return '';
  232. var code = '', incs = incs.sort( sortByPosition );
  233. for ( var i = 0; i < incs.length; i ++ ) {
  234. if ( incs[ i ].src ) code += incs[ i ].src + '\n';
  235. }
  236. return code;
  237. }
  238. }();
  239. THREE.NodeMaterial.prototype.addVertexPars = function( code ) {
  240. this.vertexPars += code + '\n';
  241. };
  242. THREE.NodeMaterial.prototype.addFragmentPars = function( code ) {
  243. this.fragmentPars += code + '\n';
  244. };
  245. THREE.NodeMaterial.prototype.addVertexCode = function( code ) {
  246. this.vertexCode += code + '\n';
  247. };
  248. THREE.NodeMaterial.prototype.addFragmentCode = function( code ) {
  249. this.fragmentCode += code + '\n';
  250. };
  251. THREE.NodeMaterial.prototype.addVertexNode = function( code ) {
  252. this.vertexNode += code + '\n';
  253. };
  254. THREE.NodeMaterial.prototype.clearVertexNode = function() {
  255. var code = this.vertexNode;
  256. this.vertexNode = '';
  257. return code;
  258. };
  259. THREE.NodeMaterial.prototype.addFragmentNode = function( code ) {
  260. this.fragmentNode += code + '\n';
  261. };
  262. THREE.NodeMaterial.prototype.clearFragmentNode = function() {
  263. var code = this.fragmentNode;
  264. this.fragmentNode = '';
  265. return code;
  266. };
  267. THREE.NodeMaterial.prototype.getCodePars = function( pars, prefix ) {
  268. prefix = prefix || '';
  269. var code = '';
  270. for ( var i = 0, l = pars.length; i < l; ++ i ) {
  271. var parsType = pars[ i ].type;
  272. var parsName = pars[ i ].name;
  273. var parsValue = pars[ i ].value;
  274. if ( parsType == 't' && parsValue instanceof THREE.CubeTexture ) parsType = 'tc';
  275. var type = THREE.NodeMaterial.types[ parsType ];
  276. if ( type == undefined ) throw new Error( "Node pars " + parsType + " not found." );
  277. code += prefix + ' ' + type + ' ' + parsName + ';\n';
  278. }
  279. return code;
  280. };
  281. THREE.NodeMaterial.prototype.createVertexUniform = function( type, value, ns, needsUpdate ) {
  282. var uniform = this.createUniform( type, value, ns, needsUpdate );
  283. this.vertexUniform.push( uniform );
  284. this.vertexUniform[ uniform.name ] = uniform;
  285. this.uniforms[ uniform.name ] = uniform;
  286. return uniform;
  287. };
  288. THREE.NodeMaterial.prototype.createFragmentUniform = function( type, value, ns, needsUpdate ) {
  289. var uniform = this.createUniform( type, value, ns, needsUpdate );
  290. this.fragmentUniform.push( uniform );
  291. this.fragmentUniform[ uniform.name ] = uniform;
  292. this.uniforms[ uniform.name ] = uniform;
  293. return uniform;
  294. };
  295. THREE.NodeMaterial.prototype.getDataNode = function( uuid ) {
  296. return this.nodeData[ uuid ] = this.nodeData[ uuid ] || {};
  297. };
  298. THREE.NodeMaterial.prototype.include = function( builder, node, parent, source ) {
  299. var includes;
  300. node = typeof node === 'string' ? THREE.NodeLib.get( node ) : node;
  301. if ( node instanceof THREE.FunctionNode ) {
  302. includes = this.functions[ builder.shader ] = this.functions[ builder.shader ] || [];
  303. } else if ( node instanceof THREE.ConstNode ) {
  304. includes = this.consts[ builder.shader ] = this.consts[ builder.shader ] || [];
  305. }
  306. var included = includes[ node.name ];
  307. if ( ! included ) {
  308. included = includes[ node.name ] = {
  309. node : node,
  310. deps : []
  311. };
  312. includes.push( included );
  313. included.src = node.build( builder, 'source' );
  314. }
  315. if ( node instanceof THREE.FunctionNode && parent && includes[ parent.name ] && includes[ parent.name ].deps.indexOf( node ) == - 1 ) {
  316. includes[ parent.name ].deps.push( node );
  317. if ( node.includes && node.includes.length ) {
  318. var i = 0;
  319. do {
  320. this.include( builder, node.includes[ i ++ ], parent );
  321. } while ( i < node.includes.length );
  322. }
  323. }
  324. if ( source ) {
  325. included.src = source;
  326. }
  327. };