NodeMaterial.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. this.updaters = [];
  9. };
  10. THREE.NodeMaterial.types = {
  11. t: 'sampler2D',
  12. tc: 'samplerCube',
  13. bv1: 'bool',
  14. iv1: 'int',
  15. fv1: 'float',
  16. c: 'vec3',
  17. v2: 'vec2',
  18. v3: 'vec3',
  19. v4: 'vec4',
  20. m3: 'mat3',
  21. m4: 'mat4'
  22. };
  23. THREE.NodeMaterial.addShortcuts = function ( proto, prop, list ) {
  24. function applyShortcut( prop, name ) {
  25. return {
  26. get: function () {
  27. return this[ prop ][ name ];
  28. },
  29. set: function ( val ) {
  30. this[ prop ][ name ] = val;
  31. }
  32. };
  33. }
  34. return ( function () {
  35. var shortcuts = {};
  36. for ( var i = 0; i < list.length; ++ i ) {
  37. var name = list[ i ];
  38. shortcuts[ name ] = applyShortcut( prop, name );
  39. }
  40. Object.defineProperties( proto, shortcuts );
  41. } )();
  42. };
  43. THREE.NodeMaterial.prototype = Object.create( THREE.ShaderMaterial.prototype );
  44. THREE.NodeMaterial.prototype.constructor = THREE.NodeMaterial;
  45. THREE.NodeMaterial.prototype.type = "NodeMaterial";
  46. THREE.NodeMaterial.prototype.updateFrame = function ( frame ) {
  47. for ( var i = 0; i < this.updaters.length; ++ i ) {
  48. frame.updateNode( this.updaters[ i ] );
  49. }
  50. };
  51. THREE.NodeMaterial.prototype.onBeforeCompile = function ( shader, renderer ) {
  52. if ( this.needsUpdate ) {
  53. this.build( { dispose: false } );
  54. shader.uniforms = this.uniforms;
  55. shader.vertexShader = this.vertexShader;
  56. shader.fragmentShader = this.fragmentShader;
  57. }
  58. };
  59. THREE.NodeMaterial.prototype.build = function ( params ) {
  60. params = params || {};
  61. params.dispose = params.dispose !== undefined ? params.dispose : true;
  62. var vertex, fragment;
  63. this.nodes = [];
  64. this.defines = {};
  65. this.uniforms = {};
  66. this.attributes = {};
  67. this.extensions = {};
  68. this.nodeData = {};
  69. this.vertexUniform = [];
  70. this.fragmentUniform = [];
  71. this.vars = [];
  72. this.vertexTemps = [];
  73. this.fragmentTemps = [];
  74. this.uniformList = [];
  75. this.consts = [];
  76. this.functions = [];
  77. this.updaters = [];
  78. this.requires = {
  79. uv: [],
  80. color: [],
  81. lights: this.lights,
  82. fog: this.fog
  83. };
  84. this.vertexPars = '';
  85. this.fragmentPars = '';
  86. this.vertexCode = '';
  87. this.fragmentCode = '';
  88. this.vertexNode = '';
  89. this.fragmentNode = '';
  90. this.prefixCode = [
  91. "#ifdef GL_EXT_shader_texture_lod",
  92. " #define texCube(a, b) textureCube(a, b)",
  93. " #define texCubeBias(a, b, c) textureCubeLodEXT(a, b, c)",
  94. " #define tex2D(a, b) texture2D(a, b)",
  95. " #define tex2DBias(a, b, c) texture2DLodEXT(a, b, c)",
  96. "#else",
  97. " #define texCube(a, b) textureCube(a, b)",
  98. " #define texCubeBias(a, b, c) textureCube(a, b, c)",
  99. " #define tex2D(a, b) texture2D(a, b)",
  100. " #define tex2DBias(a, b, c) texture2D(a, b, c)",
  101. "#endif",
  102. "#include <packing>"
  103. ].join( "\n" );
  104. var builder = new THREE.NodeBuilder( this );
  105. vertex = this.vertex.build( builder.setShader( 'vertex' ), 'v4' );
  106. fragment = this.fragment.build( builder.setShader( 'fragment' ), 'v4' );
  107. if ( this.requires.uv[ 0 ] ) {
  108. this.addVertexPars( 'varying vec2 vUv;' );
  109. this.addFragmentPars( 'varying vec2 vUv;' );
  110. this.addVertexCode( 'vUv = uv;' );
  111. }
  112. if ( this.requires.uv[ 1 ] ) {
  113. this.addVertexPars( 'varying vec2 vUv2; attribute vec2 uv2;' );
  114. this.addFragmentPars( 'varying vec2 vUv2;' );
  115. this.addVertexCode( 'vUv2 = uv2;' );
  116. }
  117. if ( this.requires.color[ 0 ] ) {
  118. this.addVertexPars( 'varying vec4 vColor; attribute vec4 color;' );
  119. this.addFragmentPars( 'varying vec4 vColor;' );
  120. this.addVertexCode( 'vColor = color;' );
  121. }
  122. if ( this.requires.color[ 1 ] ) {
  123. this.addVertexPars( 'varying vec4 vColor2; attribute vec4 color2;' );
  124. this.addFragmentPars( 'varying vec4 vColor2;' );
  125. this.addVertexCode( 'vColor2 = color2;' );
  126. }
  127. if ( this.requires.position ) {
  128. this.addVertexPars( 'varying vec3 vPosition;' );
  129. this.addFragmentPars( 'varying vec3 vPosition;' );
  130. this.addVertexCode( 'vPosition = transformed;' );
  131. }
  132. if ( this.requires.worldPosition ) {
  133. this.addVertexPars( 'varying vec3 vWPosition;' );
  134. this.addFragmentPars( 'varying vec3 vWPosition;' );
  135. this.addVertexCode( 'vWPosition = ( modelMatrix * vec4( transformed, 1.0 ) ).xyz;' );
  136. }
  137. if ( this.requires.normal ) {
  138. this.addVertexPars( 'varying vec3 vObjectNormal;' );
  139. this.addFragmentPars( 'varying vec3 vObjectNormal;' );
  140. this.addVertexCode( 'vObjectNormal = normal;' );
  141. }
  142. if ( this.requires.worldNormal ) {
  143. this.addVertexPars( 'varying vec3 vWNormal;' );
  144. this.addFragmentPars( 'varying vec3 vWNormal;' );
  145. this.addVertexCode( 'vWNormal = ( modelMatrix * vec4( objectNormal, 0.0 ) ).xyz;' );
  146. }
  147. this.fog = this.requires.fog;
  148. this.lights = this.requires.lights;
  149. this.transparent = this.requires.transparent || this.blending > THREE.NormalBlending;
  150. this.vertexShader = [
  151. this.prefixCode,
  152. this.vertexPars,
  153. this.getCodePars( this.vertexUniform, 'uniform' ),
  154. this.getIncludes( this.consts[ 'vertex' ] ),
  155. this.getIncludes( this.functions[ 'vertex' ] ),
  156. 'void main(){',
  157. this.getCodePars( this.vertexTemps ),
  158. vertex,
  159. this.vertexCode,
  160. '}'
  161. ].join( "\n" );
  162. this.fragmentShader = [
  163. this.prefixCode,
  164. this.fragmentPars,
  165. this.getCodePars( this.fragmentUniform, 'uniform' ),
  166. this.getIncludes( this.consts[ 'fragment' ] ),
  167. this.getIncludes( this.functions[ 'fragment' ] ),
  168. 'void main(){',
  169. this.getCodePars( this.fragmentTemps ),
  170. this.fragmentCode,
  171. fragment,
  172. '}'
  173. ].join( "\n" );
  174. if ( params.dispose ) {
  175. // force update
  176. this.dispose();
  177. }
  178. return this;
  179. };
  180. THREE.NodeMaterial.prototype.define = function ( name, value ) {
  181. this.defines[ name ] = value == undefined ? 1 : value;
  182. };
  183. THREE.NodeMaterial.prototype.isDefined = function ( name ) {
  184. return this.defines[ name ] != undefined;
  185. };
  186. THREE.NodeMaterial.prototype.mergeUniform = function ( uniforms ) {
  187. for ( var name in uniforms ) {
  188. this.uniforms[ name ] = uniforms[ name ];
  189. }
  190. };
  191. THREE.NodeMaterial.prototype.createUniform = function ( type, node, ns, needsUpdate ) {
  192. var index = this.uniformList.length;
  193. var uniform = new THREE.NodeUniform( {
  194. type: type,
  195. name: ns ? ns : 'nVu' + index,
  196. node: node,
  197. needsUpdate: needsUpdate
  198. } );
  199. this.uniformList.push( uniform );
  200. return uniform;
  201. };
  202. THREE.NodeMaterial.prototype.getVertexTemp = function ( uuid, type, ns ) {
  203. var data = this.vertexTemps[ uuid ];
  204. if ( ! data ) {
  205. var index = this.vertexTemps.length,
  206. name = ns ? ns : 'nVt' + index;
  207. data = { name: name, type: type };
  208. this.vertexTemps.push( data );
  209. this.vertexTemps[ uuid ] = data;
  210. }
  211. return data;
  212. };
  213. THREE.NodeMaterial.prototype.getFragmentTemp = function ( uuid, type, ns ) {
  214. var data = this.fragmentTemps[ uuid ];
  215. if ( ! data ) {
  216. var index = this.fragmentTemps.length,
  217. name = ns ? ns : 'nVt' + index;
  218. data = { name: name, type: type };
  219. this.fragmentTemps.push( data );
  220. this.fragmentTemps[ uuid ] = data;
  221. }
  222. return data;
  223. };
  224. THREE.NodeMaterial.prototype.getVar = function ( uuid, type, ns ) {
  225. var data = this.vars[ uuid ];
  226. if ( ! data ) {
  227. var index = this.vars.length,
  228. name = ns ? ns : 'nVv' + index;
  229. data = { name: name, type: type };
  230. this.vars.push( data );
  231. this.vars[ uuid ] = data;
  232. this.addVertexPars( 'varying ' + type + ' ' + name + ';' );
  233. this.addFragmentPars( 'varying ' + type + ' ' + name + ';' );
  234. }
  235. return data;
  236. };
  237. THREE.NodeMaterial.prototype.getAttribute = function ( name, type ) {
  238. if ( ! this.attributes[ name ] ) {
  239. var varying = this.getVar( name, type );
  240. this.addVertexPars( 'attribute ' + type + ' ' + name + ';' );
  241. this.addVertexCode( varying.name + ' = ' + name + ';' );
  242. this.attributes[ name ] = { varying: varying, name: name, type: type };
  243. }
  244. return this.attributes[ name ];
  245. };
  246. THREE.NodeMaterial.prototype.getIncludes = function () {
  247. function sortByPosition( a, b ) {
  248. return a.deps.length - b.deps.length;
  249. }
  250. return function ( incs ) {
  251. if ( ! incs ) return '';
  252. var code = '', incs = incs.sort( sortByPosition );
  253. for ( var i = 0; i < incs.length; i ++ ) {
  254. if ( incs[ i ].src ) code += incs[ i ].src + '\n';
  255. }
  256. return code;
  257. };
  258. }();
  259. THREE.NodeMaterial.prototype.addVertexPars = function ( code ) {
  260. this.vertexPars += code + '\n';
  261. };
  262. THREE.NodeMaterial.prototype.addFragmentPars = function ( code ) {
  263. this.fragmentPars += code + '\n';
  264. };
  265. THREE.NodeMaterial.prototype.addVertexCode = function ( code ) {
  266. this.vertexCode += code + '\n';
  267. };
  268. THREE.NodeMaterial.prototype.addFragmentCode = function ( code ) {
  269. this.fragmentCode += code + '\n';
  270. };
  271. THREE.NodeMaterial.prototype.addVertexNode = function ( code ) {
  272. this.vertexNode += code + '\n';
  273. };
  274. THREE.NodeMaterial.prototype.clearVertexNode = function () {
  275. var code = this.vertexNode;
  276. this.vertexNode = '';
  277. return code;
  278. };
  279. THREE.NodeMaterial.prototype.addFragmentNode = function ( code ) {
  280. this.fragmentNode += code + '\n';
  281. };
  282. THREE.NodeMaterial.prototype.clearFragmentNode = function () {
  283. var code = this.fragmentNode;
  284. this.fragmentNode = '';
  285. return code;
  286. };
  287. THREE.NodeMaterial.prototype.getCodePars = function ( pars, prefix ) {
  288. prefix = prefix || '';
  289. var code = '';
  290. for ( var i = 0, l = pars.length; i < l; ++ i ) {
  291. var parsType = pars[ i ].type;
  292. var parsName = pars[ i ].name;
  293. var parsValue = pars[ i ].value;
  294. if ( parsType == 't' && parsValue instanceof THREE.CubeTexture ) parsType = 'tc';
  295. var type = THREE.NodeMaterial.types[ parsType ];
  296. if ( type == undefined ) throw new Error( "Node pars " + parsType + " not found." );
  297. code += prefix + ' ' + type + ' ' + parsName + ';\n';
  298. }
  299. return code;
  300. };
  301. THREE.NodeMaterial.prototype.createVertexUniform = function ( type, node, ns, needsUpdate ) {
  302. var uniform = this.createUniform( type, node, ns, needsUpdate );
  303. this.vertexUniform.push( uniform );
  304. this.vertexUniform[ uniform.name ] = uniform;
  305. this.uniforms[ uniform.name ] = uniform;
  306. return uniform;
  307. };
  308. THREE.NodeMaterial.prototype.createFragmentUniform = function ( type, node, ns, needsUpdate ) {
  309. var uniform = this.createUniform( type, node, ns, needsUpdate );
  310. this.fragmentUniform.push( uniform );
  311. this.fragmentUniform[ uniform.name ] = uniform;
  312. this.uniforms[ uniform.name ] = uniform;
  313. return uniform;
  314. };
  315. THREE.NodeMaterial.prototype.getDataNode = function ( uuid ) {
  316. return this.nodeData[ uuid ] = this.nodeData[ uuid ] || {};
  317. };
  318. THREE.NodeMaterial.prototype.include = function ( builder, node, parent, source ) {
  319. var includes;
  320. node = typeof node === 'string' ? THREE.NodeLib.get( node ) : node;
  321. if ( node instanceof THREE.FunctionNode ) {
  322. includes = this.functions[ builder.shader ] = this.functions[ builder.shader ] || [];
  323. } else if ( node instanceof THREE.ConstNode ) {
  324. includes = this.consts[ builder.shader ] = this.consts[ builder.shader ] || [];
  325. }
  326. var included = includes[ node.name ];
  327. if ( ! included ) {
  328. included = includes[ node.name ] = {
  329. node: node,
  330. deps: []
  331. };
  332. includes.push( included );
  333. included.src = node.build( builder, 'source' );
  334. }
  335. if ( node instanceof THREE.FunctionNode && parent && includes[ parent.name ] && includes[ parent.name ].deps.indexOf( node ) == - 1 ) {
  336. includes[ parent.name ].deps.push( node );
  337. if ( node.includes && node.includes.length ) {
  338. var i = 0;
  339. do {
  340. this.include( builder, node.includes[ i ++ ], parent );
  341. } while ( i < node.includes.length );
  342. }
  343. }
  344. if ( source ) {
  345. included.src = source;
  346. }
  347. };
  348. THREE.NodeMaterial.prototype.toJSON = function ( meta ) {
  349. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  350. if ( isRootObject ) {
  351. meta = {
  352. nodes: {}
  353. };
  354. }
  355. if ( meta && ! meta.materials ) meta.materials = {};
  356. if ( ! meta.materials[ this.uuid ] ) {
  357. var data = {};
  358. data.uuid = this.uuid;
  359. data.type = this.type;
  360. meta.materials[ data.uuid ] = data;
  361. if ( this.name !== "" ) data.name = this.name;
  362. if ( this.blending !== THREE.NormalBlending ) data.blending = this.blending;
  363. if ( this.flatShading === true ) data.flatShading = this.flatShading;
  364. if ( this.side !== THREE.FrontSide ) data.side = this.side;
  365. if ( this.transparent === true ) data.transparent = this.transparent;
  366. data.depthFunc = this.depthFunc;
  367. data.depthTest = this.depthTest;
  368. data.depthWrite = this.depthWrite;
  369. if ( this.wireframe === true ) data.wireframe = this.wireframe;
  370. if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
  371. if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap;
  372. if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin;
  373. if ( this.morphTargets === true ) data.morphTargets = true;
  374. if ( this.skinning === true ) data.skinning = true;
  375. data.fog = this.fog;
  376. data.lights = this.lights;
  377. if ( this.visible === false ) data.visible = false;
  378. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  379. data.vertex = this.vertex.toJSON( meta ).uuid;
  380. data.fragment = this.fragment.toJSON( meta ).uuid;
  381. }
  382. meta.material = this.uuid;
  383. return meta;
  384. };