NodeMaterial.js 13 KB

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