NodeMaterial.js 9.7 KB

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