NodeMaterial.js 13 KB

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