NodeBuilder.js 18 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { NodeUniform } from './NodeUniform.js';
  5. import { NodeUtils } from './NodeUtils.js';
  6. import { NodeLib } from './NodeLib.js';
  7. import { FunctionNode } from './FunctionNode.js';
  8. import { ConstNode } from './ConstNode.js';
  9. import { StructNode } from './StructNode.js';
  10. import { Vector2Node } from '../inputs/Vector2Node.js';
  11. import { Vector3Node } from '../inputs/Vector3Node.js';
  12. import { Vector4Node } from '../inputs/Vector4Node.js';
  13. import { TextureNode } from '../inputs/TextureNode.js';
  14. import { CubeTextureNode } from '../inputs/CubeTextureNode.js';
  15. var elements = NodeUtils.elements,
  16. constructors = [ 'float', 'vec2', 'vec3', 'vec4' ],
  17. convertFormatToType = {
  18. float: 'f',
  19. vec2: 'v2',
  20. vec3: 'v3',
  21. vec4: 'v4',
  22. mat4: 'v4',
  23. int: 'i'
  24. },
  25. convertTypeToFormat = {
  26. t: 'sampler2D',
  27. tc: 'samplerCube',
  28. b: 'bool',
  29. i: 'int',
  30. f: 'float',
  31. c: 'vec3',
  32. v2: 'vec2',
  33. v3: 'vec3',
  34. v4: 'vec4',
  35. m3: 'mat3',
  36. m4: 'mat4'
  37. };
  38. function NodeBuilder() {
  39. this.caches = [];
  40. this.slots = [];
  41. this.keywords = {};
  42. this.nodeData = {};
  43. this.requires = {
  44. uv: [],
  45. color: [],
  46. lights: false,
  47. fog: false
  48. };
  49. this.includes = {
  50. consts: [],
  51. functions: [],
  52. structs: []
  53. };
  54. this.attributes = {};
  55. this.prefixCode = [
  56. "#ifdef GL_EXT_shader_texture_lod",
  57. " #define texCube(a, b) textureCube(a, b)",
  58. " #define texCubeBias(a, b, c) textureCubeLodEXT(a, b, c)",
  59. " #define tex2D(a, b) texture2D(a, b)",
  60. " #define tex2DBias(a, b, c) texture2DLodEXT(a, b, c)",
  61. "#else",
  62. " #define texCube(a, b) textureCube(a, b)",
  63. " #define texCubeBias(a, b, c) textureCube(a, b, c)",
  64. " #define tex2D(a, b) texture2D(a, b)",
  65. " #define tex2DBias(a, b, c) texture2D(a, b, c)",
  66. "#endif",
  67. "#include <packing>"
  68. ].join( "\n" );
  69. this.parsCode = {
  70. vertex: '',
  71. fragment: ''
  72. };
  73. this.code = {
  74. vertex: '',
  75. fragment: ''
  76. };
  77. this.nodeCode = {
  78. vertex: '',
  79. fragment: ''
  80. };
  81. this.resultCode = {
  82. vertex: '',
  83. fragment: ''
  84. };
  85. this.finalCode = {
  86. vertex: '',
  87. fragment: ''
  88. };
  89. this.inputs = {
  90. uniforms: {
  91. list: [],
  92. vertex: [],
  93. fragment: []
  94. },
  95. vars: {
  96. varying: [],
  97. vertex: [],
  98. fragment: []
  99. }
  100. };
  101. // send to material
  102. this.defines = {};
  103. this.uniforms = {};
  104. this.extensions = {};
  105. this.updaters = [];
  106. this.nodes = [];
  107. // --
  108. this.parsing = false;
  109. this.optimize = true;
  110. this.update();
  111. };
  112. NodeBuilder.prototype = {
  113. constructor: NodeBuilder,
  114. build: function( vertex, fragment ) {
  115. this.buildShader( 'vertex', vertex );
  116. this.buildShader( 'fragment', fragment );
  117. if ( this.requires.uv[ 0 ] ) {
  118. this.addVaryCode( 'varying vec2 vUv;' );
  119. this.addVertexFinalCode( 'vUv = uv;' );
  120. }
  121. if ( this.requires.uv[ 1 ] ) {
  122. this.addVaryCode( 'varying vec2 vUv2;' );
  123. this.addVertexParsCode( 'attribute vec2 uv2;' );
  124. this.addVertexFinalCode( 'vUv2 = uv2;' );
  125. }
  126. if ( this.requires.color[ 0 ] ) {
  127. this.addVaryCode( 'varying vec4 vColor;' );
  128. this.addVertexParsCode( 'attribute vec4 color;' );
  129. this.addVertexFinalCode( 'vColor = color;' );
  130. }
  131. if ( this.requires.color[ 1 ] ) {
  132. this.addVaryCode( 'varying vec4 vColor2;' );
  133. this.addVertexParsCode( 'attribute vec4 color2;' );
  134. this.addVertexFinalCode( 'vColor2 = color2;' );
  135. }
  136. if ( this.requires.position ) {
  137. this.addVaryCode( 'varying vec3 vPosition;' );
  138. this.addVertexFinalCode( 'vPosition = transformed;' );
  139. }
  140. if ( this.requires.worldPosition ) {
  141. this.addVaryCode( 'varying vec3 vWPosition;' );
  142. this.addVertexFinalCode( 'vWPosition = ( modelMatrix * vec4( transformed, 1.0 ) ).xyz;' );
  143. }
  144. if ( this.requires.normal ) {
  145. this.addVaryCode( 'varying vec3 vObjectNormal;' );
  146. this.addVertexFinalCode( 'vObjectNormal = normal;' );
  147. }
  148. if ( this.requires.worldNormal ) {
  149. this.addVaryCode( 'varying vec3 vWNormal;' );
  150. this.addVertexFinalCode( 'vWNormal = ( modelMatrix * vec4( objectNormal, 0.0 ) ).xyz;' );
  151. }
  152. return this;
  153. },
  154. buildShader: function( shader, node ) {
  155. this.resultCode[shader] = node.build( this.setShader( shader ), 'v4' );
  156. },
  157. setMaterial: function( material, renderer ) {
  158. this.material = material;
  159. this.renderer = renderer;
  160. this.requires.lights = material.lights;
  161. this.requires.fog = material.fog;
  162. return this;
  163. },
  164. addCache: function ( name, context ) {
  165. this.caches.push( {
  166. name: name || '',
  167. context: context || {}
  168. } );
  169. return this.update();
  170. },
  171. removeCache: function () {
  172. this.caches.pop();
  173. return this.update();
  174. },
  175. addSlot: function ( name ) {
  176. this.slots.push( {
  177. name: name || ''
  178. } );
  179. return this.update();
  180. },
  181. removeSlot: function () {
  182. this.slots.pop();
  183. return this.update();
  184. },
  185. addVertexCode: function ( code ) {
  186. this.addCode( code, 'vertex' );
  187. },
  188. addFragmentCode: function ( code ) {
  189. this.addCode( code, 'fragment' );
  190. },
  191. addCode: function ( code, shader ) {
  192. this.code[shader || this.shader] += code + '\n';
  193. },
  194. addVertexNodeCode: function ( code ) {
  195. this.addNodeCode( code, 'vertex' );
  196. },
  197. addFragmentNodeCode: function ( code ) {
  198. this.addNodeCode( code, 'fragment' );
  199. },
  200. addNodeCode: function ( code, shader ) {
  201. this.nodeCode[shader || this.shader] += code + '\n';
  202. },
  203. clearNodeCode: function ( shader ) {
  204. shader = shader || this.shader;
  205. var code = this.nodeCode[shader];
  206. this.nodeCode[shader] = '';
  207. return code;
  208. },
  209. clearVertexNodeCode: function ( ) {
  210. return this.clearNodeCode( 'vertex' );
  211. },
  212. clearFragmentNodeCode: function ( ) {
  213. return this.clearNodeCode( 'fragment' );
  214. },
  215. addVertexFinalCode: function ( code ) {
  216. this.addFinalCode( code, 'vertex' );
  217. },
  218. addFragmentFinalCode: function ( code ) {
  219. this.addFinalCode( code, 'fragment' );
  220. },
  221. addFinalCode: function ( code, shader ) {
  222. this.finalCode[shader || this.shader] += code + '\n';
  223. },
  224. addVertexParsCode: function ( code ) {
  225. this.addParsCode( code, 'vertex' );
  226. },
  227. addFragmentParsCode: function ( code ) {
  228. this.addParsCode( code, 'fragment' );
  229. },
  230. addParsCode: function ( code, shader ) {
  231. this.parsCode[shader || this.shader] += code + '\n';
  232. },
  233. addVaryCode: function ( code ) {
  234. this.addVertexParsCode( code );
  235. this.addFragmentParsCode( code );
  236. },
  237. isCache: function ( name ) {
  238. var i = this.caches.length;
  239. while ( i -- ) {
  240. if ( this.caches[ i ].name === name ) return true;
  241. }
  242. return false;
  243. },
  244. isSlot: function ( name ) {
  245. var i = this.slots.length;
  246. while ( i -- ) {
  247. if ( this.slots[ i ].name === name ) return true;
  248. }
  249. return false;
  250. },
  251. update: function () {
  252. var cache = this.caches[ this.caches.length - 1 ];
  253. var slot = this.slots[ this.slots.length - 1 ];
  254. this.slot = slot ? slot.name : '';
  255. this.cache = cache ? cache.name : '';
  256. this.context = cache ? cache.context : {};
  257. return this;
  258. },
  259. define: function ( name, value ) {
  260. this.defines[ name ] = value === undefined ? 1 : value;
  261. },
  262. isDefined: function ( name ) {
  263. return this.defines[ name ] !== undefined;
  264. },
  265. getVar: function ( uuid, type, ns, shader ) {
  266. shader = shader || 'varying';
  267. var vars = this.getVars(shader),
  268. data = vars[ uuid ];
  269. if ( ! data ) {
  270. var index = vars.length,
  271. name = ns ? ns : 'nVv' + index;
  272. data = { name: name, type: type };
  273. vars.push( data );
  274. vars[ uuid ] = data;
  275. }
  276. return data;
  277. },
  278. getTempVar: function ( uuid, type, ns ) {
  279. return this.getVar( uuid, type, ns, this.shader );
  280. },
  281. getAttribute: function ( name, type ) {
  282. if ( ! this.attributes[ name ] ) {
  283. var varying = this.getVar( name, type );
  284. this.addVertexParsCode( 'attribute ' + type + ' ' + name + ';' );
  285. this.addVertexFinalCode( varying.name + ' = ' + name + ';' );
  286. this.attributes[ name ] = { varying: varying, name: name, type: type };
  287. }
  288. return this.attributes[ name ];
  289. },
  290. getCode: function( shader ) {
  291. return [
  292. this.prefixCode,
  293. this.parsCode[ shader ],
  294. this.getVarListCode( this.getVars('varying'), 'varying' ),
  295. this.getVarListCode( this.inputs.uniforms[shader], 'uniform' ),
  296. this.getIncludesCode( 'consts', shader ),
  297. this.getIncludesCode( 'structs', shader ),
  298. this.getIncludesCode( 'functions', shader ),
  299. 'void main() {',
  300. this.getVarListCode( this.getVars(shader) ),
  301. this.code[ shader ],
  302. this.resultCode[ shader ],
  303. this.finalCode[ shader ],
  304. '}'
  305. ].join( "\n" );
  306. },
  307. getVarListCode: function ( vars, prefix ) {
  308. prefix = prefix || '';
  309. var code = '';
  310. for ( var i = 0, l = vars.length; i < l; ++ i ) {
  311. var nVar = vars[i],
  312. type = nVar.type,
  313. name = nVar.name;
  314. var formatType = this.getFormatByType( type );
  315. if ( formatType === undefined ) {
  316. throw new Error( "Node pars " + formatType + " not found." );
  317. }
  318. code += prefix + ' ' + formatType + ' ' + name + ';\n';
  319. }
  320. return code;
  321. },
  322. getVars: function ( shader ) {
  323. return this.inputs.vars[ shader || this.shader ];
  324. },
  325. getNodeData: function ( node ) {
  326. var uuid = node.isNode ? node.uuid : node;
  327. return this.nodeData[ uuid ] = this.nodeData[ uuid ] || {};
  328. },
  329. createUniform: function ( shader, type, node, ns, needsUpdate ) {
  330. var uniforms = this.inputs.uniforms,
  331. index = uniforms.list.length;
  332. var uniform = new NodeUniform( {
  333. type: type,
  334. name: ns ? ns : 'nVu' + index + '_' + THREE.Math.generateUUID().substr(0, 8),
  335. node: node,
  336. needsUpdate: needsUpdate
  337. } );
  338. uniforms.list.push( uniform );
  339. uniforms[shader].push( uniform );
  340. uniforms[shader][ uniform.name ] = uniform;
  341. this.uniforms[ uniform.name ] = uniform;
  342. return uniform;
  343. },
  344. createVertexUniform: function ( type, node, ns, needsUpdate ) {
  345. return this.createUniform( 'vertex', type, node, ns, needsUpdate );
  346. },
  347. createFragmentUniform: function ( type, node, ns, needsUpdate ) {
  348. return this.createUniform( 'fragment', type, node, ns, needsUpdate );
  349. },
  350. include: function ( node, parent, source ) {
  351. var includesStruct;
  352. node = typeof node === 'string' ? NodeLib.get( node ) : node;
  353. if ( node instanceof FunctionNode ) {
  354. includesStruct = this.includes.functions;
  355. } else if ( node instanceof ConstNode ) {
  356. includesStruct = this.includes.consts;
  357. } else if ( node instanceof StructNode ) {
  358. includesStruct = this.includes.structs;
  359. }
  360. var includes = includesStruct[ this.shader ] = includesStruct[ this.shader ] || [];
  361. if (node) {
  362. var included = includes[ node.name ];
  363. if ( ! included ) {
  364. included = includes[ node.name ] = {
  365. node: node,
  366. deps: []
  367. };
  368. includes.push( included );
  369. included.src = node.build( this, 'source' );
  370. }
  371. if ( node instanceof FunctionNode && parent && includes[ parent.name ] && includes[ parent.name ].deps.indexOf( node ) == - 1 ) {
  372. includes[ parent.name ].deps.push( node );
  373. if ( node.includes && node.includes.length ) {
  374. var i = 0;
  375. do {
  376. this.include( node.includes[ i ++ ], parent );
  377. } while ( i < node.includes.length );
  378. }
  379. }
  380. if ( source ) {
  381. included.src = source;
  382. }
  383. return node.name;
  384. } else {
  385. throw new Error("Include not found.");
  386. }
  387. },
  388. colorToVectorProperties: function ( color ) {
  389. return color.replace( 'r', 'x' ).replace( 'g', 'y' ).replace( 'b', 'z' ).replace( 'a', 'w' );
  390. },
  391. colorToVector: function ( color ) {
  392. return color.replace( /c/g, 'v3' );
  393. },
  394. getIncludes: function ( type, shader ) {
  395. return this.includes[type][shader || this.shader];
  396. },
  397. getIncludesCode: function () {
  398. function sortByPosition( a, b ) {
  399. return a.deps.length - b.deps.length;
  400. }
  401. return function getIncludesCode( type, shader ) {
  402. var includes = this.getIncludes( type, shader );
  403. if ( ! includes ) return '';
  404. var code = '',
  405. includes = includes.sort( sortByPosition );
  406. for ( var i = 0; i < includes.length; i ++ ) {
  407. if ( includes[ i ].src ) code += includes[ i ].src + '\n';
  408. }
  409. return code;
  410. };
  411. }(),
  412. getConstructorFromLength: function ( len ) {
  413. return constructors[ len - 1 ];
  414. },
  415. isTypeMatrix: function ( format ) {
  416. return /^m/.test( format );
  417. },
  418. getTypeLength: function ( type ) {
  419. if ( type === 'f' ) return 1;
  420. return parseInt( this.colorToVector( type ).substr( 1 ) );
  421. },
  422. getTypeFromLength: function ( len ) {
  423. if ( len === 1 ) return 'f';
  424. return 'v' + len;
  425. },
  426. findNode: function() {
  427. for(var i = 0; i < arguments.length; i++) {
  428. var nodeCandidate = arguments[i];
  429. if (nodeCandidate !== undefined && nodeCandidate.isNode) {
  430. return nodeCandidate;
  431. }
  432. }
  433. },
  434. resolve: function() {
  435. for(var i = 0; i < arguments.length; i++) {
  436. var nodeCandidate = arguments[i];
  437. if (nodeCandidate !== undefined) {
  438. if (nodeCandidate.isNode) {
  439. return nodeCandidate;
  440. } else if (nodeCandidate.isTexture) {
  441. switch( nodeCandidate.mapping ) {
  442. case THREE.CubeReflectionMapping:
  443. case THREE.CubeRefractionMapping:
  444. return new CubeTextureNode( nodeCandidate );
  445. break;
  446. case THREE.CubeUVReflectionMapping:
  447. case THREE.CubeUVRefractionMapping:
  448. return new TextureCubeNode( new TextureNode( nodeCandidate ) );
  449. break;
  450. default:
  451. return new TextureNode( nodeCandidate );
  452. }
  453. } else if (nodeCandidate.isVector2) {
  454. return new Vector2Node( nodeCandidate );
  455. } else if (nodeCandidate.isVector3) {
  456. return new Vector3Node( nodeCandidate );
  457. } else if (nodeCandidate.isVector4) {
  458. return new Vector4Node( nodeCandidate );
  459. }
  460. }
  461. }
  462. },
  463. format: function ( code, from, to ) {
  464. var typeToType = this.colorToVector( to + ' = ' + from );
  465. switch ( typeToType ) {
  466. case 'f = v2': return code + '.x';
  467. case 'f = v3': return code + '.x';
  468. case 'f = v4': return code + '.x';
  469. case 'f = i': return 'float( ' + code + ' )';
  470. case 'v2 = f': return 'vec2( ' + code + ' )';
  471. case 'v2 = v3': return code + '.xy';
  472. case 'v2 = v4': return code + '.xy';
  473. case 'v2 = i': return 'vec2( float( ' + code + ' ) )';
  474. case 'v3 = f': return 'vec3( ' + code + ' )';
  475. case 'v3 = v2': return 'vec3( ' + code + ', 0.0 )';
  476. case 'v3 = v4': return code + '.xyz';
  477. case 'v3 = i': return 'vec2( float( ' + code + ' ) )';
  478. case 'v4 = f': return 'vec4( ' + code + ' )';
  479. case 'v4 = v2': return 'vec4( ' + code + ', 0.0, 1.0 )';
  480. case 'v4 = v3': return 'vec4( ' + code + ', 1.0 )';
  481. case 'v4 = i': return 'vec4( float( ' + code + ' ) )';
  482. case 'i = f': return 'int( ' + code + ' )';
  483. case 'i = v2': return 'int( ' + code + '.x )';
  484. case 'i = v3': return 'int( ' + code + '.x )';
  485. case 'i = v4': return 'int( ' + code + '.x )';
  486. }
  487. return code;
  488. },
  489. getTypeByFormat: function ( format ) {
  490. return convertFormatToType[ format ] || format;
  491. },
  492. getFormatByType: function ( type ) {
  493. return convertTypeToFormat[ type ] || type;
  494. },
  495. getUuid: function ( uuid, useCache ) {
  496. useCache = useCache !== undefined ? useCache : true;
  497. if ( useCache && this.cache ) uuid = this.cache + '-' + uuid;
  498. return uuid;
  499. },
  500. getElementByIndex: function ( index ) {
  501. return elements[ index ];
  502. },
  503. getIndexByElement: function ( elm ) {
  504. return elements.indexOf( elm );
  505. },
  506. isShader: function ( shader ) {
  507. return this.shader === shader;
  508. },
  509. setShader: function ( shader ) {
  510. this.shader = shader;
  511. return this;
  512. },
  513. mergeUniform: function ( uniforms ) {
  514. for ( var name in uniforms ) {
  515. this.uniforms[ name ] = uniforms[ name ];
  516. }
  517. },
  518. getTexelDecodingFunctionFromTexture: function( code, texture ) {
  519. var gammaOverrideLinear = this.getTextureEncodingFromMap( texture, this.context.gamma && ( this.renderer ? this.renderer.gammaInput : false ) )
  520. return this.getTexelDecodingFunction( code, gammaOverrideLinear );
  521. },
  522. getTexelDecodingFunction: function( value, encoding ) {
  523. var components = this.getEncodingComponents( encoding );
  524. return components[ 0 ] + 'ToLinear' + components[ 1 ].replace( 'value', value );
  525. },
  526. getTexelEncodingFunction: function( value, encoding ) {
  527. var components = this.getEncodingComponents( encoding );
  528. return 'LinearTo' + components[ 0 ] + components[ 1 ].replace( 'value', value );
  529. },
  530. getEncodingComponents: function( encoding ) {
  531. switch ( encoding ) {
  532. case THREE.LinearEncoding:
  533. return [ 'Linear', '( value )' ];
  534. case THREE.sRGBEncoding:
  535. return [ 'sRGB', '( value )' ];
  536. case THREE.RGBEEncoding:
  537. return [ 'RGBE', '( value )' ];
  538. case THREE.RGBM7Encoding:
  539. return [ 'RGBM', '( value, 7.0 )' ];
  540. case THREE.RGBM16Encoding:
  541. return [ 'RGBM', '( value, 16.0 )' ];
  542. case THREE.RGBDEncoding:
  543. return [ 'RGBD', '( value, 256.0 )' ];
  544. case THREE.GammaEncoding:
  545. return [ 'Gamma', '( value, float( GAMMA_FACTOR ) )' ];
  546. default:
  547. throw new Error( 'unsupported encoding: ' + encoding );
  548. }
  549. },
  550. getEncodingComponents: function( encoding ) {
  551. switch ( encoding ) {
  552. case THREE.LinearEncoding:
  553. return [ 'Linear', '( value )' ];
  554. case THREE.sRGBEncoding:
  555. return [ 'sRGB', '( value )' ];
  556. case THREE.RGBEEncoding:
  557. return [ 'RGBE', '( value )' ];
  558. case THREE.RGBM7Encoding:
  559. return [ 'RGBM', '( value, 7.0 )' ];
  560. case THREE.RGBM16Encoding:
  561. return [ 'RGBM', '( value, 16.0 )' ];
  562. case THREE.RGBDEncoding:
  563. return [ 'RGBD', '( value, 256.0 )' ];
  564. case THREE.GammaEncoding:
  565. return [ 'Gamma', '( value, float( GAMMA_FACTOR ) )' ];
  566. default:
  567. throw new Error( 'unsupported encoding: ' + encoding );
  568. }
  569. },
  570. getTextureEncodingFromMap: function ( map, gammaOverrideLinear ) {
  571. var encoding;
  572. if ( ! map ) {
  573. encoding = THREE.LinearEncoding;
  574. } else if ( map.isTexture ) {
  575. encoding = map.encoding;
  576. } else if ( map.isWebGLRenderTarget ) {
  577. console.warn( "THREE.WebGLPrograms.getTextureEncodingFromMap: don't use render targets as textures. Use their .texture property instead." );
  578. encoding = map.texture.encoding;
  579. }
  580. // add backwards compatibility for WebGLRenderer.gammaInput/gammaOutput parameter, should probably be removed at some point.
  581. if ( encoding === THREE.LinearEncoding && gammaOverrideLinear ) {
  582. encoding = THREE.GammaEncoding;
  583. }
  584. return encoding;
  585. }
  586. };
  587. export { NodeBuilder };