NodeMaterial.js 11 KB

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