NodeMaterial.js 13 KB

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