webgl_materials_nodes.html 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - node material</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Node-Based Material<br />
  13. <a id="serialize" href="javascript:void(0);">Serialize and apply</a>
  14. </div>
  15. <script src="../build/three.js"></script>
  16. <script src='js/geometries/TeapotBufferGeometry.js'></script>
  17. <script src="js/controls/OrbitControls.js"></script>
  18. <script src="js/libs/dat.gui.min.js"></script>
  19. <script type="module">
  20. import './js/nodes/THREE.Nodes.js';
  21. import './js/loaders/NodeMaterialLoader.js';
  22. var container = document.getElementById( 'container' );
  23. var renderer, scene, camera, clock = new THREE.Clock(), fov = 50;
  24. var frame = new THREE.NodeFrame();
  25. var teapot, mesh;
  26. var controls;
  27. var move = false;
  28. var rtTexture, rtMaterial;
  29. var gui;
  30. var library = {};
  31. var serialized = false;
  32. var textures = {
  33. brick: { url: 'textures/brick_diffuse.jpg' },
  34. grass: { url: 'textures/terrain/grasslight-big.jpg' },
  35. grassNormal: { url: 'textures/terrain/grasslight-big-nm.jpg' },
  36. decalDiffuse: { url: 'textures/decal/decal-diffuse.png' },
  37. decalNormal: { url: 'textures/decal/decal-normal.jpg' },
  38. cloud: { url: 'textures/lava/cloud.png' },
  39. spherical: { url: 'textures/envmap.png' }
  40. };
  41. var param = { example: new URL( window.location.href ).searchParams.get( 'e' ) || 'mesh-standard' };
  42. function getTexture( name ) {
  43. var texture = textures[ name ].texture;
  44. if ( ! texture ) {
  45. texture = textures[ name ].texture = new THREE.TextureLoader().load( textures[ name ].url );
  46. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  47. library[ texture.uuid ] = texture;
  48. }
  49. return texture;
  50. }
  51. var cubemap = function () {
  52. var path = "textures/cube/Park2/";
  53. var format = '.jpg';
  54. var urls = [
  55. path + 'posx' + format, path + 'negx' + format,
  56. path + 'posy' + format, path + 'negy' + format,
  57. path + 'posz' + format, path + 'negz' + format
  58. ];
  59. var textureCube = new THREE.CubeTextureLoader().load( urls );
  60. textureCube.format = THREE.RGBFormat;
  61. library[ textureCube.uuid ] = textureCube;
  62. return textureCube;
  63. }();
  64. window.addEventListener( 'load', init );
  65. function init() {
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. renderer.uuid = THREE.Math.generateUUID(); // generate to library
  70. container.appendChild( renderer.domElement );
  71. scene = new THREE.Scene();
  72. camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 1000 );
  73. camera.position.x = 50;
  74. camera.position.z = - 50;
  75. camera.position.y = 30;
  76. camera.target = new THREE.Vector3();
  77. controls = new THREE.OrbitControls( camera, renderer.domElement );
  78. controls.minDistance = 50;
  79. controls.maxDistance = 200;
  80. scene.add( new THREE.AmbientLight( 0x464646 ) );
  81. var light = new THREE.DirectionalLight( 0xffddcc, 1 );
  82. light.position.set( 1, 0.75, 0.5 );
  83. scene.add( light );
  84. var light = new THREE.DirectionalLight( 0xccccff, 1 );
  85. light.position.set( - 1, 0.75, - 0.5 );
  86. scene.add( light );
  87. teapot = new THREE.TeapotBufferGeometry( 15, 18 );
  88. mesh = new THREE.Mesh( teapot );
  89. scene.add( mesh );
  90. library[ renderer.uuid ] = renderer;
  91. library[ camera.uuid ] = camera;
  92. library[ mesh.uuid ] = mesh;
  93. window.addEventListener( 'resize', onWindowResize, false );
  94. updateMaterial();
  95. onWindowResize();
  96. animate();
  97. }
  98. function clearGui() {
  99. if ( gui ) gui.destroy();
  100. gui = new dat.GUI();
  101. gui.add( param, 'example', {
  102. 'basic / mesh-standard': 'mesh-standard',
  103. 'basic / standard': 'standard',
  104. 'basic / physical': 'physical',
  105. 'basic / phong': 'phong',
  106. 'basic / layers': 'layers',
  107. 'basic / rim': 'rim',
  108. 'basic / color-adjustment': 'color-adjustment',
  109. 'basic / uv-transform': 'uv-transform',
  110. 'basic / bump': 'bump',
  111. 'basic / blur': 'blur',
  112. 'basic / spherical-reflection': 'spherical-reflection',
  113. 'adv / fresnel': 'fresnel',
  114. 'adv / saturation': 'saturation',
  115. 'adv / top-bottom': 'top-bottom',
  116. 'adv / skin': 'skin',
  117. 'adv / skin-phong': 'skin-phong',
  118. 'adv / caustic': 'caustic',
  119. 'adv / displace': 'displace',
  120. 'adv / dissolve': 'dissolve',
  121. 'adv / dissolve-fire': 'dissolve-fire',
  122. 'adv / plush': 'plush',
  123. 'adv / toon': 'toon',
  124. 'adv / camera-depth': 'camera-depth',
  125. 'adv / soft-body': 'soft-body',
  126. 'adv / wave': 'wave',
  127. 'adv / triangle-blur': 'triangle-blur',
  128. 'adv / triplanar-mapping': 'triplanar-mapping',
  129. 'adv / render-to-texture': 'rtt',
  130. 'adv / temporal-blur': 'temporal-blur',
  131. 'adv / conditional': 'conditional',
  132. 'adv / expression': 'expression',
  133. 'adv / sss': 'sss',
  134. 'adv / translucent': 'translucent',
  135. 'node / position': 'node-position',
  136. 'node / normal': 'node-normal',
  137. 'misc / smoke': 'smoke',
  138. 'misc / firefly': 'firefly',
  139. 'misc / reserved-keywords': 'reserved-keywords',
  140. 'misc / varying': 'varying',
  141. 'misc / void-function': 'void-function',
  142. 'misc / readonly': 'readonly',
  143. 'misc / label': 'label',
  144. 'misc / custom-attribute': 'custom-attribute'
  145. } ).onFinishChange( function () {
  146. updateMaterial();
  147. } );
  148. gui.open();
  149. }
  150. function addGui( name, value, callback, isColor, min, max ) {
  151. var node;
  152. param[ name ] = value;
  153. if ( isColor ) {
  154. node = gui.addColor( param, name ).onChange( function () {
  155. callback( param[ name ] );
  156. } );
  157. } else if ( typeof value == 'object' ) {
  158. param[ name ] = value[ Object.keys( value )[ 0 ] ];
  159. node = gui.add( param, name, value ).onChange( function () {
  160. callback( param[ name ] );
  161. } );
  162. } else {
  163. node = gui.add( param, name, min, max ).onChange( function () {
  164. callback( param[ name ] );
  165. } );
  166. }
  167. return node;
  168. }
  169. function updateMaterial() {
  170. move = false;
  171. if ( mesh.material ) mesh.material.dispose();
  172. if ( rtTexture ) {
  173. delete library[ rtTexture.texture.uuid ];
  174. rtTexture.dispose();
  175. rtTexture = null;
  176. }
  177. if ( rtMaterial ) {
  178. rtMaterial.dispose();
  179. rtMaterial = null;
  180. }
  181. var name = param.example,
  182. defaultSide = THREE.DoubleSide,
  183. mtl;
  184. clearGui();
  185. switch ( name ) {
  186. case 'phong':
  187. // MATERIAL
  188. mtl = new THREE.PhongNodeMaterial();
  189. //mtl.color = // albedo (vec3)
  190. //mtl.alpha = // opacity (float)
  191. //mtl.specular = // specular color (vec3)
  192. //mtl.shininess = // shininess (float)
  193. //mtl.normal = // normal (vec3)
  194. //mtl.emissive = // emissive color (vec3)
  195. //mtl.ambient = // ambient color (vec3)
  196. //mtl.shadow = // shadowmap (vec3)
  197. //mtl.light = // custom-light (vec3)
  198. //mtl.ao = // ambient occlusion (float)
  199. //mtl.light = // input/output light (vec3)
  200. //mtl.environment = // reflection/refraction (vec3)
  201. //mtl.environmentAlpha = // environment alpha (float)
  202. //mtl.position = // vertex local position (vec3)
  203. var mask = new THREE.SwitchNode( new THREE.TextureNode( getTexture( "decalDiffuse" ) ), 'w' );
  204. mtl.color = new THREE.TextureNode( getTexture( "grass" ) );
  205. mtl.specular = new THREE.FloatNode( .5 );
  206. mtl.shininess = new THREE.FloatNode( 15 );
  207. mtl.environment = new THREE.CubeTextureNode( cubemap );
  208. mtl.environmentAlpha = mask;
  209. mtl.normal = new THREE.NormalMapNode( new THREE.TextureNode( getTexture( "grassNormal" ) ) );
  210. mtl.normal.scale = new THREE.Math1Node( mask, THREE.Math1Node.INVERT );
  211. break;
  212. case 'standard':
  213. // MATERIAL
  214. mtl = new THREE.StandardNodeMaterial();
  215. //mtl.color = // albedo (vec3)
  216. //mtl.alpha = // opacity (float)
  217. //mtl.roughness = // roughness (float)
  218. //mtl.metalness = // metalness (float)
  219. //mtl.normal = // normal (vec3)
  220. //mtl.emissive = // emissive color (vec3)
  221. //mtl.ambient = // ambient color (vec3)
  222. //mtl.shadow = // shadowmap (vec3)
  223. //mtl.light = // custom-light (vec3)
  224. //mtl.ao = // ambient occlusion (float)
  225. //mtl.environment = // reflection/refraction (vec3)
  226. //mtl.position = // vertex local position (vec3)
  227. var mask = new THREE.SwitchNode( new THREE.TextureNode( getTexture( "decalDiffuse" ) ), 'w' );
  228. var normalScale = new THREE.FloatNode( .3 );
  229. var roughnessA = new THREE.FloatNode( .5 );
  230. var metalnessA = new THREE.FloatNode( .5 );
  231. var roughnessB = new THREE.FloatNode( 0 );
  232. var metalnessB = new THREE.FloatNode( 1 );
  233. var roughness = new THREE.Math3Node(
  234. roughnessA,
  235. roughnessB,
  236. mask,
  237. THREE.Math3Node.MIX
  238. );
  239. var metalness = new THREE.Math3Node(
  240. metalnessA,
  241. metalnessB,
  242. mask,
  243. THREE.Math3Node.MIX
  244. );
  245. var normalMask = new THREE.OperatorNode(
  246. new THREE.Math1Node( mask, THREE.Math1Node.INVERT ),
  247. normalScale,
  248. THREE.OperatorNode.MUL
  249. );
  250. mtl.color = new THREE.ColorNode( 0xEEEEEE );
  251. mtl.roughness = roughness;
  252. mtl.metalness = metalness;
  253. mtl.environment = new THREE.CubeTextureNode( cubemap );
  254. mtl.normal = new THREE.NormalMapNode( new THREE.TextureNode( getTexture( "grassNormal" ) ) );
  255. mtl.normal.scale = normalMask;
  256. // GUI
  257. addGui( 'color', mtl.color.value.getHex(), function ( val ) {
  258. mtl.color.value.setHex( val );
  259. }, true );
  260. addGui( 'roughnessA', roughnessA.value, function ( val ) {
  261. roughnessA.value = val;
  262. }, false, 0, 1 );
  263. addGui( 'metalnessA', metalnessA.value, function ( val ) {
  264. metalnessA.value = val;
  265. }, false, 0, 1 );
  266. addGui( 'roughnessB', roughnessB.value, function ( val ) {
  267. roughnessB.value = val;
  268. }, false, 0, 1 );
  269. addGui( 'metalnessB', metalnessB.value, function ( val ) {
  270. metalnessB.value = val;
  271. }, false, 0, 1 );
  272. addGui( 'normalScale', normalScale.value, function ( val ) {
  273. normalScale.value = val;
  274. }, false, 0, 1 );
  275. break;
  276. case 'mesh-standard':
  277. // MATERIAL
  278. var sataturation = new THREE.FloatNode( 1 ),
  279. useNodeMaterial = true,
  280. useMap = true,
  281. useNormals = true;
  282. function updateMaterial() {
  283. var oldMaterial = mtl;
  284. if ( oldMaterial ) oldMaterial.dispose();
  285. mtl = useNodeMaterial ? new THREE.MeshStandardNodeMaterial() : new THREE.MeshStandardMaterial();
  286. // default syntax ( backward-compatible )
  287. mtl.map = useMap ? getTexture( "brick" ) : undefined;
  288. mtl.normalMap = useNormals ? getTexture( "decalNormal" ) : undefined;
  289. mtl.normalScale = oldMaterial ? oldMaterial.normalScale : new THREE.Vector2( .5, .5 );
  290. mtl.envMap = cubemap;
  291. mtl.roughness = oldMaterial ? oldMaterial.roughness : .5;
  292. mtl.metalness = oldMaterial ? oldMaterial.metalness : .5;
  293. // extended syntax ( only for NodeMaterial )
  294. if ( useNodeMaterial && useMap ) {
  295. mtl.map = new THREE.ColorAdjustmentNode(
  296. new THREE.TextureNode( mtl.map ),
  297. sataturation,
  298. THREE.ColorAdjustmentNode.SATURATION
  299. );
  300. }
  301. // apply material
  302. mtl.side = defaultSide;
  303. mtl.needsUpdate = true;
  304. mesh.material = mtl;
  305. }
  306. updateMaterial();
  307. // GUI
  308. addGui( 'use node material', useNodeMaterial, function ( val ) {
  309. useNodeMaterial = val;
  310. updateMaterial();
  311. } );
  312. addGui( 'roughness', mtl.roughness, function ( val ) {
  313. mtl.roughness = val;
  314. }, false, 0, 1 );
  315. addGui( 'metalness', mtl.roughness, function ( val ) {
  316. mtl.metalness = val;
  317. }, false, 0, 1 );
  318. addGui( 'normalX', mtl.normalScale.x, function ( val ) {
  319. mtl.normalScale.x = val;
  320. }, false, - 1, 1 );
  321. addGui( 'normalY', mtl.normalScale.y, function ( val ) {
  322. mtl.normalScale.y = val;
  323. }, false, - 1, 1 );
  324. addGui( 'sat. (node)', sataturation.value, function ( val ) {
  325. sataturation.value = val;
  326. }, false, 0, 2 );
  327. addGui( 'colors', useMap, function ( val ) {
  328. useMap = val;
  329. updateMaterial();
  330. }, false );
  331. addGui( 'normals', useNormals, function ( val ) {
  332. useNormals = val;
  333. updateMaterial();
  334. }, false );
  335. break;
  336. case 'physical':
  337. // MATERIAL
  338. mtl = new THREE.StandardNodeMaterial();
  339. //mtl.color = // albedo (vec3)
  340. //mtl.alpha = // opacity (float)
  341. //mtl.roughness = // roughness (float)
  342. //mtl.metalness = // metalness (float)
  343. //mtl.reflectivity = // reflectivity (float)
  344. //mtl.clearCoat = // clearCoat (float)
  345. //mtl.clearCoatRoughness = // clearCoatRoughness (float)
  346. //mtl.normal = // normal (vec3)
  347. //mtl.emissive = // emissive color (vec3)
  348. //mtl.ambient = // ambient color (vec3)
  349. //mtl.shadow = // shadowmap (vec3)
  350. //mtl.light = // custom-light (vec3)
  351. //mtl.ao = // ambient occlusion (float)
  352. //mtl.environment = // reflection/refraction (vec3)
  353. //mtl.position = // vertex local position (vec3)
  354. var mask = new THREE.SwitchNode( new THREE.TextureNode( getTexture( "decalDiffuse" ) ), 'w' );
  355. var normalScale = new THREE.FloatNode( .3 );
  356. var roughnessA = new THREE.FloatNode( .5 );
  357. var metalnessA = new THREE.FloatNode( .5 );
  358. var roughnessB = new THREE.FloatNode( 0 );
  359. var metalnessB = new THREE.FloatNode( 1 );
  360. var reflectivity = new THREE.FloatNode( 0 );
  361. var clearCoat = new THREE.FloatNode( 1 );
  362. var clearCoatRoughness = new THREE.FloatNode( 1 );
  363. var roughness = new THREE.Math3Node(
  364. roughnessA,
  365. roughnessB,
  366. mask,
  367. THREE.Math3Node.MIX
  368. );
  369. var metalness = new THREE.Math3Node(
  370. metalnessA,
  371. metalnessB,
  372. mask,
  373. THREE.Math3Node.MIX
  374. );
  375. var normalMask = new THREE.OperatorNode(
  376. new THREE.Math1Node( mask, THREE.Math1Node.INVERT ),
  377. normalScale,
  378. THREE.OperatorNode.MUL
  379. );
  380. mtl.color = new THREE.ColorNode( 0xEEEEEE );
  381. mtl.roughness = roughness;
  382. mtl.metalness = metalness;
  383. mtl.reflectivity = reflectivity;
  384. mtl.clearCoat = clearCoat;
  385. mtl.clearCoatRoughness = clearCoatRoughness;
  386. mtl.environment = new THREE.CubeTextureNode( cubemap );
  387. mtl.normal = new THREE.NormalMapNode( new THREE.TextureNode( getTexture( "grassNormal" ) ) );
  388. mtl.normal.scale = normalMask;
  389. // GUI
  390. addGui( 'color', mtl.color.value.getHex(), function ( val ) {
  391. mtl.color.value.setHex( val );
  392. }, true );
  393. addGui( 'reflectivity', reflectivity.value, function ( val ) {
  394. reflectivity.value = val;
  395. }, false, 0, 1 );
  396. addGui( 'clearCoat', clearCoat.value, function ( val ) {
  397. clearCoat.value = val;
  398. }, false, 0, 1 );
  399. addGui( 'clearCoatRoughness', clearCoatRoughness.value, function ( val ) {
  400. clearCoatRoughness.value = val;
  401. }, false, 0, 1 );
  402. addGui( 'roughnessA', roughnessA.value, function ( val ) {
  403. roughnessA.value = val;
  404. }, false, 0, 1 );
  405. addGui( 'metalnessA', metalnessA.value, function ( val ) {
  406. metalnessA.value = val;
  407. }, false, 0, 1 );
  408. addGui( 'roughnessB', roughnessB.value, function ( val ) {
  409. roughnessB.value = val;
  410. }, false, 0, 1 );
  411. addGui( 'metalnessB', metalnessB.value, function ( val ) {
  412. metalnessB.value = val;
  413. }, false, 0, 1 );
  414. addGui( 'normalScale', normalScale.value, function ( val ) {
  415. normalScale.value = val;
  416. }, false, 0, 1 );
  417. break;
  418. case 'wave':
  419. // MATERIAL
  420. mtl = new THREE.PhongNodeMaterial();
  421. var time = new THREE.TimerNode();
  422. var speed = new THREE.FloatNode( 5 );
  423. var scale = new THREE.FloatNode( 1 );
  424. var worldScale = new THREE.FloatNode( .4 );
  425. var colorA = new THREE.ColorNode( 0xFFFFFF );
  426. var colorB = new THREE.ColorNode( 0x0054df );
  427. // used for serialization only
  428. time.name = "time";
  429. speed.name = "speed";
  430. var timeScale = new THREE.OperatorNode(
  431. time,
  432. speed,
  433. THREE.OperatorNode.MUL
  434. );
  435. var worldScl = new THREE.OperatorNode(
  436. new THREE.PositionNode(),
  437. worldScale,
  438. THREE.OperatorNode.MUL
  439. );
  440. var posContinuous = new THREE.OperatorNode(
  441. worldScl,
  442. timeScale,
  443. THREE.OperatorNode.ADD
  444. );
  445. var wave = new THREE.Math1Node( posContinuous, THREE.Math1Node.SIN );
  446. wave = new THREE.SwitchNode( wave, 'x' );
  447. var waveScale = new THREE.OperatorNode(
  448. wave,
  449. scale,
  450. THREE.OperatorNode.MUL
  451. );
  452. var displaceY = new THREE.JoinNode(
  453. new THREE.FloatNode(),
  454. waveScale,
  455. new THREE.FloatNode()
  456. );
  457. var displace = new THREE.OperatorNode(
  458. new THREE.NormalNode(),
  459. displaceY,
  460. THREE.OperatorNode.MUL
  461. );
  462. var blend = new THREE.OperatorNode(
  463. new THREE.PositionNode(),
  464. displaceY,
  465. THREE.OperatorNode.ADD
  466. );
  467. var color = new THREE.Math3Node(
  468. colorB,
  469. colorA,
  470. wave,
  471. THREE.Math3Node.MIX
  472. );
  473. mtl.color = color;
  474. mtl.position = blend;
  475. // GUI
  476. addGui( 'speed', speed.value, function ( val ) {
  477. speed.value = val;
  478. }, false, 0, 10 );
  479. addGui( 'scale', scale.value, function ( val ) {
  480. scale.value = val;
  481. }, false, 0, 3 );
  482. addGui( 'worldScale', worldScale.value, function ( val ) {
  483. worldScale.value = val;
  484. }, false, 0, 1 );
  485. addGui( 'colorA', colorA.value.getHex(), function ( val ) {
  486. colorA.value.setHex( val );
  487. }, true );
  488. addGui( 'colorB', colorB.value.getHex(), function ( val ) {
  489. colorB.value.setHex( val );
  490. }, true );
  491. addGui( 'useNormals', false, function ( val ) {
  492. blend.b = val ? displace : displaceY;
  493. mtl.needsUpdate = true;
  494. } );
  495. break;
  496. case 'rim':
  497. // MATERIAL
  498. mtl = new THREE.PhongNodeMaterial();
  499. defaultSide = THREE.FrontSide;
  500. var intensity = 1.3;
  501. var power = new THREE.FloatNode( 3 );
  502. var color = new THREE.ColorNode( 0xFFFFFF );
  503. var viewZ = new THREE.Math2Node(
  504. new THREE.NormalNode(),
  505. new THREE.Vector3Node( 0, 0, - intensity ),
  506. THREE.Math2Node.DOT
  507. );
  508. var rim = new THREE.OperatorNode(
  509. viewZ,
  510. new THREE.FloatNode( intensity ),
  511. THREE.OperatorNode.ADD
  512. );
  513. var rimPower = new THREE.Math2Node(
  514. rim,
  515. power,
  516. THREE.Math2Node.POW
  517. );
  518. var rimColor = new THREE.OperatorNode(
  519. rimPower,
  520. color,
  521. THREE.OperatorNode.MUL
  522. );
  523. mtl.color = new THREE.ColorNode( 0x111111 );
  524. mtl.emissive = rimColor;
  525. // GUI
  526. addGui( 'color', color.value.getHex(), function ( val ) {
  527. color.value.setHex( val );
  528. }, true );
  529. addGui( 'intensity', intensity, function ( val ) {
  530. intensity = val;
  531. viewZ.b.z = - intensity;
  532. rim.b.value = intensity;
  533. }, false, 0, 3 );
  534. addGui( 'power', power.value, function ( val ) {
  535. power.value = val;
  536. }, false, 0, 6 );
  537. addGui( 'xray', false, function ( val ) {
  538. if ( val ) {
  539. mtl.emissive = color;
  540. mtl.alpha = rimPower;
  541. mtl.blending = THREE.AdditiveBlending;
  542. mtl.depthWrite = false;
  543. } else {
  544. mtl.emissive = rimColor;
  545. mtl.alpha = null;
  546. mtl.blending = THREE.NormalBlending;
  547. mtl.depthWrite = true;
  548. }
  549. mtl.needsUpdate = true;
  550. } );
  551. break;
  552. case 'color-adjustment':
  553. // MATERIAL
  554. mtl = new THREE.PhongNodeMaterial();
  555. var texture = new THREE.TextureNode( getTexture( "brick" ) );
  556. var hue = new THREE.FloatNode();
  557. var sataturation = new THREE.FloatNode( 1 );
  558. var vibrance = new THREE.FloatNode();
  559. var brightness = new THREE.FloatNode( 0 );
  560. var contrast = new THREE.FloatNode( 1 );
  561. var hueNode = new THREE.ColorAdjustmentNode( texture, hue, THREE.ColorAdjustmentNode.HUE );
  562. var satNode = new THREE.ColorAdjustmentNode( hueNode, sataturation, THREE.ColorAdjustmentNode.SATURATION );
  563. var vibranceNode = new THREE.ColorAdjustmentNode( satNode, vibrance, THREE.ColorAdjustmentNode.VIBRANCE );
  564. var brightnessNode = new THREE.ColorAdjustmentNode( vibranceNode, brightness, THREE.ColorAdjustmentNode.BRIGHTNESS );
  565. var contrastNode = new THREE.ColorAdjustmentNode( brightnessNode, contrast, THREE.ColorAdjustmentNode.CONTRAST );
  566. mtl.color = contrastNode;
  567. // GUI
  568. addGui( 'hue', hue.value, function ( val ) {
  569. hue.value = val;
  570. }, false, 0, Math.PI * 2 );
  571. addGui( 'saturation', sataturation.value, function ( val ) {
  572. sataturation.value = val;
  573. }, false, 0, 2 );
  574. addGui( 'vibrance', vibrance.value, function ( val ) {
  575. vibrance.value = val;
  576. }, false, - 1, 1 );
  577. addGui( 'brightness', brightness.value, function ( val ) {
  578. brightness.value = val;
  579. }, false, 0, .5 );
  580. addGui( 'contrast', contrast.value, function ( val ) {
  581. contrast.value = val;
  582. }, false, 0, 2 );
  583. break;
  584. case 'uv-transform':
  585. // MATERIAL
  586. mtl = new THREE.PhongNodeMaterial();
  587. var translate = new THREE.Vector2();
  588. var rotate = 0;
  589. var scale = new THREE.Vector2( 1, 1 );
  590. var texture = new THREE.TextureNode( getTexture( "brick" ) );
  591. texture.uv = new THREE.UVTransformNode();
  592. //texture.uv.uv = new THREE.UVNode( 1 ); // uv2 for example
  593. mtl.color = texture;
  594. // GUI
  595. function updateUVTransform() {
  596. texture.uv.setUvTransform( translate.x, translate.y, scale.x, scale.y, THREE.Math.degToRad( rotate ) );
  597. }
  598. addGui( 'translateX', translate.x, function ( val ) {
  599. translate.x = val;
  600. updateUVTransform();
  601. }, false, 0, 10 );
  602. addGui( 'translateY', translate.y, function ( val ) {
  603. translate.y = val;
  604. updateUVTransform();
  605. }, false, 0, 10 );
  606. addGui( 'scaleX', scale.x, function ( val ) {
  607. scale.x = val;
  608. updateUVTransform();
  609. }, false, .1, 5 );
  610. addGui( 'scaleY', scale.y, function ( val ) {
  611. scale.y = val;
  612. updateUVTransform();
  613. }, false, .1, 5 );
  614. addGui( 'rotate', rotate, function ( val ) {
  615. rotate = val;
  616. updateUVTransform();
  617. }, false, 0, 360 );
  618. break;
  619. case 'bump':
  620. // MATERIAL
  621. mtl = new THREE.PhongNodeMaterial();
  622. var diffuse = new THREE.TextureNode( getTexture( "brick" ) );
  623. var bumpMap = new THREE.BumpMapNode( new THREE.TextureNode( getTexture( "brick" ) ) );
  624. bumpMap.scale = new THREE.FloatNode( .5 );
  625. mtl.color = diffuse;
  626. mtl.normal = bumpMap;
  627. // convert BumpMap to NormalMap
  628. //bumpMap.toNormalMap = true;
  629. //mtl.normal = new THREE.NormalMapNode( bumpMap );
  630. // GUI
  631. addGui( 'scale', bumpMap.scale.value, function ( val ) {
  632. bumpMap.scale.value = val;
  633. }, false, - 2, 2 );
  634. addGui( 'color', true, function ( val ) {
  635. mtl.color = val ? diffuse : new THREE.ColorNode( 0xEEEEEE );
  636. mtl.needsUpdate = true;
  637. } );
  638. break;
  639. case 'blur':
  640. // MATERIAL
  641. mtl = new THREE.PhongNodeMaterial();
  642. var diffuse = new THREE.TextureNode( getTexture( "brick" ) );
  643. var blur = new THREE.BlurNode( new THREE.TextureNode( getTexture( "brick" ) ) );
  644. mtl.color = blur;
  645. // GUI
  646. addGui( 'radiusX', blur.radius.x, function ( val ) {
  647. blur.radius.x = val;
  648. }, false, 0, 15 );
  649. addGui( 'radiusY', blur.radius.y, function ( val ) {
  650. blur.radius.y = val;
  651. }, false, 0, 15 );
  652. break;
  653. case 'spherical-reflection':
  654. // MATERIAL
  655. mtl = new THREE.PhongNodeMaterial();
  656. mtl.environment = new THREE.TextureNode( getTexture( "spherical" ), new THREE.ReflectNode( THREE.ReflectNode.SPHERE ) );
  657. break;
  658. case 'fresnel':
  659. // MATERIAL
  660. mtl = new THREE.PhongNodeMaterial();
  661. var reflectance = new THREE.FloatNode( 1.3 );
  662. var power = new THREE.FloatNode( 1 );
  663. var color = new THREE.CubeTextureNode( cubemap );
  664. var viewZ = new THREE.Math2Node(
  665. new THREE.NormalNode(),
  666. new THREE.Vector3Node( 0, 0, - 1 ),
  667. THREE.Math2Node.DOT
  668. );
  669. var theta = new THREE.OperatorNode(
  670. viewZ,
  671. new THREE.FloatNode( 1 ),
  672. THREE.OperatorNode.ADD
  673. );
  674. var thetaPower = new THREE.Math2Node(
  675. theta,
  676. power,
  677. THREE.Math2Node.POW
  678. );
  679. var fresnel = new THREE.OperatorNode(
  680. reflectance,
  681. thetaPower,
  682. THREE.OperatorNode.MUL
  683. );
  684. mtl.color = new THREE.ColorNode( 0x3399FF );
  685. mtl.environment = color;
  686. mtl.environmentAlpha = new THREE.Math1Node( fresnel, THREE.Math1Node.SATURATE );
  687. // GUI
  688. addGui( 'reflectance', reflectance.value, function ( val ) {
  689. reflectance.value = val;
  690. }, false, 0, 3 );
  691. addGui( 'power', power.value, function ( val ) {
  692. power.value = val;
  693. }, false, 0, 5 );
  694. break;
  695. case 'layers':
  696. // MATERIAL
  697. mtl = new THREE.PhongNodeMaterial();
  698. var tex1 = new THREE.TextureNode( getTexture( "grass" ) );
  699. var tex2 = new THREE.TextureNode( getTexture( "brick" ) );
  700. var offset = new THREE.FloatNode( 0 );
  701. var scale = new THREE.FloatNode( 1 );
  702. var uv = new THREE.UVNode();
  703. var uvOffset = new THREE.OperatorNode(
  704. offset,
  705. uv,
  706. THREE.OperatorNode.ADD
  707. );
  708. var uvScale = new THREE.OperatorNode(
  709. uvOffset,
  710. scale,
  711. THREE.OperatorNode.MUL
  712. );
  713. var mask = new THREE.TextureNode( getTexture( "decalDiffuse" ), uvScale );
  714. var maskAlphaChannel = new THREE.SwitchNode( mask, 'w' );
  715. var blend = new THREE.Math3Node(
  716. tex1,
  717. tex2,
  718. maskAlphaChannel,
  719. THREE.Math3Node.MIX
  720. );
  721. mtl.color = blend;
  722. // GUI
  723. addGui( 'offset', offset.value, function ( val ) {
  724. offset.value = val;
  725. }, false, 0, 1 );
  726. addGui( 'scale', scale.value, function ( val ) {
  727. scale.value = val;
  728. }, false, 0, 10 );
  729. break;
  730. case 'saturation':
  731. // MATERIAL
  732. mtl = new THREE.StandardNodeMaterial();
  733. var tex = new THREE.TextureNode( getTexture( "brick" ) );
  734. var sat = new THREE.FloatNode( 0 );
  735. var satrgb = new THREE.FunctionNode( [
  736. "vec3 satrgb( vec3 rgb, float adjustment ) {",
  737. // include luminance function from LuminanceNode
  738. " vec3 intensity = vec3( luminance( rgb ) );",
  739. " return mix( intensity, rgb, adjustment );",
  740. "}"
  741. ].join( "\n" ), [ THREE.LuminanceNode.Nodes.luminance ] );
  742. var saturation = new THREE.FunctionCallNode( satrgb );
  743. saturation.inputs.rgb = tex;
  744. saturation.inputs.adjustment = sat;
  745. // or try
  746. //saturation.inputs[0] = tex;
  747. //saturation.inputs[1] = sat;
  748. mtl.color = saturation;
  749. // GUI
  750. addGui( 'saturation', sat.value, function ( val ) {
  751. sat.value = val;
  752. }, false, 0, 2 );
  753. break;
  754. case 'top-bottom':
  755. // MATERIAL
  756. mtl = new THREE.PhongNodeMaterial();
  757. var top = new THREE.TextureNode( getTexture( "grass" ) );
  758. var bottom = new THREE.TextureNode( getTexture( "brick" ) );
  759. var normal = new THREE.NormalNode( THREE.NormalNode.WORLD );
  760. var normalY = new THREE.SwitchNode( normal, 'y' );
  761. var hard = new THREE.FloatNode( 9 );
  762. var offset = new THREE.FloatNode( - 2.5 );
  763. var hardClamp = new THREE.OperatorNode(
  764. normalY,
  765. hard,
  766. THREE.OperatorNode.MUL
  767. );
  768. var offsetClamp = new THREE.OperatorNode(
  769. hardClamp,
  770. offset,
  771. THREE.OperatorNode.ADD
  772. );
  773. var clamp0at1 = new THREE.Math1Node( offsetClamp, THREE.Math1Node.SATURATE );
  774. var blend = new THREE.Math3Node( top, bottom, clamp0at1, THREE.Math3Node.MIX );
  775. mtl.color = blend;
  776. // GUI
  777. addGui( 'hard', hard.value, function ( val ) {
  778. hard.value = val;
  779. }, false, 0, 20 );
  780. addGui( 'offset', offset.value, function ( val ) {
  781. offset.value = val;
  782. }, false, - 10, 10 );
  783. break;
  784. case 'displace':
  785. // MATERIAL
  786. mtl = new THREE.PhongNodeMaterial();
  787. var time = new THREE.TimerNode();
  788. var scale = new THREE.FloatNode( 2 );
  789. var speed = new THREE.FloatNode( .2 );
  790. var colorA = new THREE.ColorNode( 0xFFFFFF );
  791. var colorB = new THREE.ColorNode( 0x0054df );
  792. // used for serialization only
  793. time.name = "time";
  794. speed.name = "speed";
  795. var uv = new THREE.UVNode();
  796. var timeScl = new THREE.OperatorNode(
  797. time,
  798. speed,
  799. THREE.OperatorNode.MUL
  800. );
  801. var displaceOffset = new THREE.OperatorNode(
  802. timeScl,
  803. uv,
  804. THREE.OperatorNode.ADD
  805. );
  806. var tex = new THREE.TextureNode( getTexture( "cloud" ), displaceOffset );
  807. var texArea = new THREE.SwitchNode( tex, 'w' );
  808. var displace = new THREE.OperatorNode(
  809. new THREE.NormalNode(),
  810. texArea,
  811. THREE.OperatorNode.MUL
  812. );
  813. var displaceScale = new THREE.OperatorNode(
  814. displace,
  815. scale,
  816. THREE.OperatorNode.MUL
  817. );
  818. var blend = new THREE.OperatorNode(
  819. new THREE.PositionNode(),
  820. displaceScale,
  821. THREE.OperatorNode.ADD
  822. );
  823. var color = new THREE.Math3Node(
  824. colorB,
  825. colorA,
  826. texArea,
  827. THREE.Math3Node.MIX
  828. );
  829. mtl.color = mtl.specular = new THREE.ColorNode( 0 );
  830. mtl.emissive = color;
  831. mtl.position = blend;
  832. // GUI
  833. addGui( 'speed', speed.value, function ( val ) {
  834. speed.value = val;
  835. }, false, 0, 1 );
  836. addGui( 'scale', scale.value, function ( val ) {
  837. scale.value = val;
  838. }, false, 0, 10 );
  839. addGui( 'colorA', colorA.value.getHex(), function ( val ) {
  840. colorA.value.setHex( val );
  841. }, true );
  842. addGui( 'colorB', colorB.value.getHex(), function ( val ) {
  843. colorB.value.setHex( val );
  844. }, true );
  845. break;
  846. case 'dissolve':
  847. // MATERIAL
  848. mtl = new THREE.StandardNodeMaterial();
  849. var color = new THREE.ColorNode( 0xEEEEEE );
  850. var borderColor = new THREE.ColorNode( 0x0054df );
  851. var threshold = new THREE.FloatNode( .1 );
  852. var borderSize = new THREE.FloatNode( .2 );
  853. var tex = new THREE.TextureNode( getTexture( "cloud" ) );
  854. var texArea = new THREE.SwitchNode( tex, 'w' );
  855. var thresholdBorder = new THREE.Math3Node(
  856. new THREE.OperatorNode( threshold, borderSize, THREE.OperatorNode.ADD ),
  857. threshold,
  858. texArea,
  859. THREE.Math3Node.SMOOTHSTEP
  860. );
  861. var thresholdEmissive = new THREE.OperatorNode(
  862. borderColor,
  863. thresholdBorder,
  864. THREE.OperatorNode.MUL
  865. );
  866. // APPLY
  867. mtl.color = color;
  868. mtl.emissive = thresholdEmissive;
  869. mtl.mask = new THREE.CondNode(
  870. texArea, // a: value
  871. threshold, // b: value
  872. THREE.CondNode.GREATER // condition
  873. );
  874. // GUI
  875. addGui( 'threshold', threshold.value, function ( val ) {
  876. threshold.value = val;
  877. }, false, - .3, 1.3 );
  878. addGui( 'borderSize', borderSize.value, function ( val ) {
  879. borderSize.value = val;
  880. }, false, 0, .5 );
  881. addGui( 'color', color.value.getHex(), function ( val ) {
  882. color.value.setHex( val );
  883. }, true );
  884. addGui( 'borderColor', borderColor.value.getHex(), function ( val ) {
  885. borderColor.value.setHex( val );
  886. }, true );
  887. break;
  888. case 'dissolve-fire':
  889. // MATERIAL
  890. mtl = new THREE.StandardNodeMaterial();
  891. var color = new THREE.ColorNode( 0xEEEEEE );
  892. var fireStartColor = new THREE.ColorNode( 0xF7CA78 );
  893. var fireEndColor = new THREE.ColorNode( 0xFF0000 );
  894. var burnedColor = new THREE.ColorNode( 0x000000 );
  895. var threshold = new THREE.FloatNode( .1 );
  896. var fireSize = new THREE.FloatNode( .16 );
  897. var burnedSize = new THREE.FloatNode( .5 );
  898. var timer = new THREE.TimerNode( 0.8 );
  899. var sinCycleInSecs = new THREE.OperatorNode(
  900. timer,
  901. new THREE.ConstNode( THREE.ConstNode.PI2 ),
  902. THREE.OperatorNode.MUL
  903. );
  904. var cycle = new THREE.Math1Node( sinCycleInSecs, THREE.Math1Node.SIN );
  905. // round sin to 0 at 1
  906. cycle = new THREE.OperatorNode( cycle, new THREE.FloatNode( 1 ), THREE.OperatorNode.ADD );
  907. cycle = new THREE.OperatorNode( cycle, new THREE.FloatNode( 2 ), THREE.OperatorNode.DIV );
  908. // offset to +.9
  909. cycle = new THREE.OperatorNode( cycle, new THREE.FloatNode( .9 ), THREE.OperatorNode.ADD );
  910. var tex = new THREE.TextureNode( getTexture( "cloud" ) );
  911. var texArea = new THREE.SwitchNode( tex, 'w' );
  912. var thresholdBorder = new THREE.Math3Node(
  913. new THREE.OperatorNode( threshold, fireSize, THREE.OperatorNode.ADD ),
  914. threshold,
  915. texArea,
  916. THREE.Math3Node.SMOOTHSTEP
  917. );
  918. var fireStartAnimatedColor = new THREE.ColorAdjustmentNode(
  919. fireStartColor,
  920. cycle,
  921. THREE.ColorAdjustmentNode.SATURATION
  922. );
  923. var fireEndAnimatedColor = new THREE.ColorAdjustmentNode(
  924. fireEndColor,
  925. cycle,
  926. THREE.ColorAdjustmentNode.SATURATION
  927. );
  928. var fireColor = new THREE.Math3Node(
  929. fireEndAnimatedColor,
  930. fireStartAnimatedColor,
  931. thresholdBorder,
  932. THREE.Math3Node.MIX
  933. );
  934. var thresholdBurnedBorder = new THREE.Math3Node(
  935. new THREE.OperatorNode( threshold, burnedSize, THREE.OperatorNode.ADD ),
  936. threshold,
  937. texArea,
  938. THREE.Math3Node.SMOOTHSTEP
  939. );
  940. var fireEmissive = new THREE.OperatorNode(
  941. fireColor,
  942. thresholdBorder,
  943. THREE.OperatorNode.MUL
  944. );
  945. var burnedResultColor = new THREE.Math3Node(
  946. color,
  947. burnedColor,
  948. thresholdBurnedBorder,
  949. THREE.Math3Node.MIX
  950. );
  951. // APPLY
  952. mtl.color = burnedResultColor;
  953. mtl.emissive = fireEmissive;
  954. mtl.mask = new THREE.CondNode(
  955. texArea, // a: value
  956. threshold, // b: value
  957. THREE.CondNode.GREATER // condition
  958. );
  959. // GUI
  960. addGui( 'threshold', threshold.value, function ( val ) {
  961. threshold.value = val;
  962. }, false, - .5, 1.5 );
  963. addGui( 'fireSize', fireSize.value, function ( val ) {
  964. fireSize.value = val;
  965. }, false, 0, .5 );
  966. addGui( 'burnedSize', burnedSize.value, function ( val ) {
  967. burnedSize.value = val;
  968. }, false, 0, 1 );
  969. addGui( 'color', color.value.getHex(), function ( val ) {
  970. color.value.setHex( val );
  971. }, true );
  972. addGui( 'fireStartColor', fireStartColor.value.getHex(), function ( val ) {
  973. fireStartColor.value.setHex( val );
  974. }, true );
  975. addGui( 'fireEndColor', fireEndColor.value.getHex(), function ( val ) {
  976. fireEndColor.value.setHex( val );
  977. }, true );
  978. addGui( 'burnedColor', burnedColor.value.getHex(), function ( val ) {
  979. burnedColor.value.setHex( val );
  980. }, true );
  981. addGui( 'timeScale', timer.scale, function ( val ) {
  982. timer.scale = val;
  983. }, false, 0, 2 );
  984. break;
  985. case 'smoke':
  986. // MATERIAL
  987. mtl = new THREE.PhongNodeMaterial();
  988. defaultSide = THREE.FrontSide;
  989. var time = new THREE.TimerNode();
  990. var uv = new THREE.UVNode();
  991. var timeSpeedA = new THREE.OperatorNode(
  992. time,
  993. new THREE.Vector2Node( 0.3, 0.1 ),
  994. THREE.OperatorNode.MUL
  995. );
  996. var timeSpeedB = new THREE.OperatorNode(
  997. time,
  998. new THREE.Vector2Node( 0.15, 0.4 ),
  999. THREE.OperatorNode.MUL
  1000. );
  1001. var uvOffsetA = new THREE.OperatorNode(
  1002. timeSpeedA,
  1003. uv,
  1004. THREE.OperatorNode.ADD
  1005. );
  1006. var uvOffsetB = new THREE.OperatorNode(
  1007. timeSpeedB,
  1008. uv,
  1009. THREE.OperatorNode.ADD
  1010. );
  1011. var cloudA = new THREE.TextureNode( getTexture( "cloud" ), uvOffsetA );
  1012. var cloudB = new THREE.TextureNode( getTexture( "cloud" ), uvOffsetB );
  1013. var clouds = new THREE.OperatorNode(
  1014. cloudA,
  1015. cloudB,
  1016. THREE.OperatorNode.ADD
  1017. );
  1018. mtl.environment = new THREE.ColorNode( 0xFFFFFF );
  1019. mtl.alpha = clouds;
  1020. // GUI
  1021. addGui( 'color', mtl.environment.value.getHex(), function ( val ) {
  1022. mtl.environment.value.setHex( val );
  1023. }, true );
  1024. break;
  1025. case 'camera-depth':
  1026. // MATERIAL
  1027. var colorA = new THREE.ColorNode( 0xFFFFFF );
  1028. var colorB = new THREE.ColorNode( 0x0054df );
  1029. var depth = new THREE.CameraNode( THREE.CameraNode.DEPTH );
  1030. depth.near.value = 1;
  1031. depth.far.value = 200;
  1032. var colors = new THREE.Math3Node(
  1033. colorB,
  1034. colorA,
  1035. depth,
  1036. THREE.Math3Node.MIX
  1037. );
  1038. mtl = new THREE.PhongNodeMaterial();
  1039. mtl.color = colors;
  1040. // GUI
  1041. addGui( 'near', depth.near.value, function ( val ) {
  1042. depth.near.value = val;
  1043. }, false, 1, 1200 );
  1044. addGui( 'far', depth.far.value, function ( val ) {
  1045. depth.far.value = val;
  1046. }, false, 1, 1200 );
  1047. addGui( 'nearColor', colorA.value.getHex(), function ( val ) {
  1048. colorA.value.setHex( val );
  1049. }, true );
  1050. addGui( 'farColor', colorB.value.getHex(), function ( val ) {
  1051. colorB.value.setHex( val );
  1052. }, true );
  1053. break;
  1054. case 'caustic':
  1055. // MATERIAL
  1056. mtl = new THREE.StandardNodeMaterial();
  1057. var hash2 = new THREE.FunctionNode( [
  1058. "vec2 hash2(vec2 p) {",
  1059. " return fract(sin(vec2(dot(p, vec2(123.4, 748.6)), dot(p, vec2(547.3, 659.3))))*5232.85324);",
  1060. "}"
  1061. ].join( "\n" ) );
  1062. var voronoi = new THREE.FunctionNode( [
  1063. // Based off of iq's described here: http://www.iquilezles.org/www/articles/voronoili
  1064. "float voronoi(vec2 p, in float time) {",
  1065. " vec2 n = floor(p);",
  1066. " vec2 f = fract(p);",
  1067. " float md = 5.0;",
  1068. " vec2 m = vec2(0.0);",
  1069. " for (int i = -1; i <= 1; i++) {",
  1070. " for (int j = -1; j <= 1; j++) {",
  1071. " vec2 g = vec2(i, j);",
  1072. " vec2 o = hash2(n + g);",
  1073. " o = 0.5 + 0.5 * sin(time + 5.038 * o);",
  1074. " vec2 r = g + o - f;",
  1075. " float d = dot(r, r);",
  1076. " if (d < md) {",
  1077. " md = d;",
  1078. " m = n+g+o;",
  1079. " }",
  1080. " }",
  1081. " }",
  1082. " return md;",
  1083. "}"
  1084. ].join( "\n" ), [ hash2 ] ); // define hash2 as dependencies
  1085. var voronoiLayers = new THREE.FunctionNode( [
  1086. // based on https://www.shadertoy.com/view/4tXSDf
  1087. "float voronoiLayers(vec2 p, in float time) {",
  1088. " float v = 0.0;",
  1089. " float a = 0.4;",
  1090. " for (int i = 0; i < 3; i++) {",
  1091. " v += voronoi(p, time) * a;",
  1092. " p *= 2.0;",
  1093. " a *= 0.5;",
  1094. " }",
  1095. " return v;",
  1096. "}"
  1097. ].join( "\n" ), [ voronoi ] ); // define voronoi as dependencies
  1098. var time = new THREE.TimerNode();
  1099. var timeScale = new THREE.FloatNode( 2 );
  1100. // used for serialization only
  1101. time.name = "time";
  1102. timeScale.name = "speed";
  1103. var alpha = new THREE.FloatNode( 1 );
  1104. var scale = new THREE.FloatNode( .1 );
  1105. var intensity = new THREE.FloatNode( 1.5 );
  1106. var color = new THREE.ColorNode( 0xFFFFFF );
  1107. var colorA = new THREE.ColorNode( 0xFFFFFF );
  1108. var colorB = new THREE.ColorNode( 0x0054df );
  1109. var worldPos = new THREE.PositionNode( THREE.PositionNode.WORLD );
  1110. var worldPosTop = new THREE.SwitchNode( worldPos, 'xz' );
  1111. var worldNormal = new THREE.NormalNode( THREE.NormalNode.WORLD );
  1112. var mask = new THREE.SwitchNode( worldNormal, 'y' );
  1113. // clamp0at1
  1114. mask = new THREE.Math1Node( mask, THREE.Math1Node.SATURATE );
  1115. var timeOffset = new THREE.OperatorNode(
  1116. time,
  1117. timeScale,
  1118. THREE.OperatorNode.MUL
  1119. );
  1120. var uvPos = new THREE.OperatorNode(
  1121. worldPosTop,
  1122. scale,
  1123. THREE.OperatorNode.MUL
  1124. );
  1125. var voronoi = new THREE.FunctionCallNode( voronoiLayers );
  1126. voronoi.inputs.p = uvPos;
  1127. voronoi.inputs.time = timeOffset;
  1128. var maskCaustic = new THREE.OperatorNode(
  1129. alpha,
  1130. mask,
  1131. THREE.OperatorNode.MUL
  1132. );
  1133. var voronoiIntensity = new THREE.OperatorNode(
  1134. voronoi,
  1135. intensity,
  1136. THREE.OperatorNode.MUL
  1137. );
  1138. var voronoiColors = new THREE.Math3Node(
  1139. colorB,
  1140. colorA,
  1141. new THREE.Math1Node( voronoiIntensity, THREE.Math1Node.SATURATE ), // mix needs clamp
  1142. THREE.Math3Node.MIX
  1143. );
  1144. var caustic = new THREE.Math3Node(
  1145. color,
  1146. voronoiColors,
  1147. maskCaustic,
  1148. THREE.Math3Node.MIX
  1149. );
  1150. var causticLights = new THREE.OperatorNode(
  1151. voronoiIntensity,
  1152. maskCaustic,
  1153. THREE.OperatorNode.MUL
  1154. );
  1155. mtl.color = caustic;
  1156. mtl.ambient = causticLights;
  1157. // GUI
  1158. addGui( 'timeScale', timeScale.value, function ( val ) {
  1159. timeScale.value = val;
  1160. }, false, 0, 5 );
  1161. addGui( 'intensity', intensity.value, function ( val ) {
  1162. intensity.value = val;
  1163. }, false, 0, 3 );
  1164. addGui( 'scale', scale.value, function ( val ) {
  1165. scale.value = val;
  1166. }, false, 0, 1 );
  1167. addGui( 'alpha', alpha.value, function ( val ) {
  1168. alpha.value = val;
  1169. }, false, 0, 1 );
  1170. addGui( 'color', color.value.getHex(), function ( val ) {
  1171. color.value.setHex( val );
  1172. }, true );
  1173. addGui( 'colorA', colorA.value.getHex(), function ( val ) {
  1174. colorA.value.setHex( val );
  1175. }, true );
  1176. addGui( 'colorB', colorB.value.getHex(), function ( val ) {
  1177. colorB.value.setHex( val );
  1178. }, true );
  1179. break;
  1180. case 'soft-body':
  1181. // MATERIAL
  1182. move = true;
  1183. mtl = new THREE.StandardNodeMaterial();
  1184. var scale = new THREE.FloatNode( 2 );
  1185. var colorA = new THREE.ColorNode( 0xFF6633 );
  1186. var colorB = new THREE.ColorNode( 0x3366FF );
  1187. var pos = new THREE.PositionNode();
  1188. var posNorm = new THREE.Math1Node( pos, THREE.Math1Node.NORMALIZE );
  1189. var mask = new THREE.SwitchNode( posNorm, 'y' );
  1190. var velocity = new THREE.VelocityNode( mesh, {
  1191. type: 'elastic',
  1192. spring: .95,
  1193. damping: .95
  1194. } );
  1195. var velocityArea = new THREE.OperatorNode(
  1196. mask,
  1197. scale,
  1198. THREE.OperatorNode.MUL
  1199. );
  1200. var softVelocity = new THREE.OperatorNode(
  1201. velocity,
  1202. velocityArea,
  1203. THREE.OperatorNode.MUL
  1204. );
  1205. var softPosition = new THREE.OperatorNode(
  1206. new THREE.PositionNode(),
  1207. softVelocity,
  1208. THREE.OperatorNode.ADD
  1209. );
  1210. var colors = new THREE.Math3Node(
  1211. colorB,
  1212. colorA,
  1213. mask,
  1214. THREE.Math3Node.MIX
  1215. );
  1216. mtl.color = colors;
  1217. mtl.position = softPosition;
  1218. // GUI
  1219. addGui( 'spring', velocity.params.spring, function ( val ) {
  1220. velocity.params.spring = val;
  1221. }, false, 0, .95 );
  1222. addGui( 'damping', velocity.params.damping, function ( val ) {
  1223. velocity.params.damping = val;
  1224. }, false, 0, .95 );
  1225. addGui( 'scale', scale.value, function ( val ) {
  1226. scale.value = val;
  1227. }, false, 0, 3 );
  1228. addGui( 'softBody', colorA.value.getHex(), function ( val ) {
  1229. colorA.value.setHex( val );
  1230. }, true );
  1231. addGui( 'rigidBody', colorB.value.getHex(), function ( val ) {
  1232. colorB.value.setHex( val );
  1233. }, true );
  1234. break;
  1235. case 'plush':
  1236. // MATERIAL
  1237. mtl = new THREE.PhongNodeMaterial();
  1238. var color = new THREE.ColorNode( 0x8D8677 );
  1239. var mildness = new THREE.FloatNode( 1.6 );
  1240. var fur = new THREE.FloatNode( .5 );
  1241. var posDirection = new THREE.Math1Node( new THREE.PositionNode( THREE.PositionNode.VIEW ), THREE.Math1Node.NORMALIZE );
  1242. var norDirection = new THREE.Math1Node( new THREE.NormalNode(), THREE.Math1Node.NORMALIZE );
  1243. var viewZ = new THREE.Math2Node(
  1244. posDirection,
  1245. norDirection,
  1246. THREE.Math2Node.DOT
  1247. );
  1248. // without luma correction for now
  1249. var mildnessColor = new THREE.OperatorNode(
  1250. color,
  1251. mildness,
  1252. THREE.OperatorNode.MUL
  1253. );
  1254. var furScale = new THREE.OperatorNode(
  1255. viewZ,
  1256. fur,
  1257. THREE.OperatorNode.MUL
  1258. );
  1259. mtl.color = color;
  1260. mtl.normal = new THREE.NormalMapNode( new THREE.TextureNode( getTexture( "grassNormal" ) ) );
  1261. mtl.normal.scale = furScale;
  1262. mtl.environment = mildnessColor;
  1263. mtl.environmentAlpha = new THREE.Math1Node( viewZ, THREE.Math1Node.INVERT );
  1264. mtl.shininess = new THREE.FloatNode( 0 );
  1265. // GUI
  1266. addGui( 'color', color.value.getHex(), function ( val ) {
  1267. color.value.setHex( val );
  1268. }, true );
  1269. addGui( 'mildness', mildness.value, function ( val ) {
  1270. mildness.value = val;
  1271. }, false, 1, 2 );
  1272. addGui( 'fur', fur.value, function ( val ) {
  1273. fur.value = val;
  1274. }, false, 0, 2 );
  1275. break;
  1276. case 'skin':
  1277. case 'skin-phong':
  1278. // MATERIAL
  1279. mtl = name == 'skin' ? new THREE.StandardNodeMaterial() : new THREE.PhongNodeMaterial();
  1280. var skinColor = new THREE.ColorNode( 0xFFC495 );
  1281. var bloodColor = new THREE.ColorNode( 0x6b0602 );
  1282. var wrapLight = new THREE.FloatNode( 1.5 );
  1283. var wrapShadow = new THREE.FloatNode( 0 );
  1284. var directLight = new THREE.LightNode();
  1285. var lightLuminance = new THREE.LuminanceNode( directLight );
  1286. var lightWrap = new THREE.Math3Node(
  1287. wrapShadow,
  1288. wrapLight,
  1289. lightLuminance,
  1290. THREE.Math3Node.SMOOTHSTEP
  1291. );
  1292. var lightTransition = new THREE.OperatorNode(
  1293. lightWrap,
  1294. new THREE.ConstNode( THREE.ConstNode.PI2 ),
  1295. THREE.OperatorNode.MUL
  1296. );
  1297. var wrappedLight = new THREE.Math1Node( lightTransition, THREE.Math1Node.SIN );
  1298. var wrappedLightColor = new THREE.OperatorNode(
  1299. wrappedLight,
  1300. bloodColor,
  1301. THREE.OperatorNode.MUL
  1302. );
  1303. var bloodArea = new THREE.Math1Node( wrappedLightColor, THREE.Math1Node.SATURATE );
  1304. var totalLight = new THREE.OperatorNode(
  1305. directLight,
  1306. bloodArea,
  1307. THREE.OperatorNode.ADD
  1308. );
  1309. mtl.color = skinColor;
  1310. mtl.light = totalLight;
  1311. if ( name == 'skin' ) {
  1312. // StandardNodeMaterial
  1313. mtl.metalness = new THREE.FloatNode( 0 );
  1314. mtl.roughness = new THREE.FloatNode( 1 );
  1315. mtl.reflectivity = new THREE.FloatNode( 0 );
  1316. mtl.clearCoat = new THREE.FloatNode( .2 );
  1317. mtl.clearCoatRoughness = new THREE.FloatNode( .3 );
  1318. mtl.environment = new THREE.CubeTextureNode( cubemap );
  1319. } else {
  1320. // PhongNodeMaterial
  1321. mtl.specular = new THREE.ColorNode( 0x2f2e2d );
  1322. mtl.shininess = new THREE.FloatNode( 15 );
  1323. }
  1324. // GUI
  1325. addGui( 'skinColor', skinColor.value.getHex(), function ( val ) {
  1326. skinColor.value.setHex( val );
  1327. }, true );
  1328. addGui( 'bloodColor', bloodColor.value.getHex(), function ( val ) {
  1329. bloodColor.value.setHex( val );
  1330. }, true );
  1331. addGui( 'wrapLight', wrapLight.value, function ( val ) {
  1332. wrapLight.value = val;
  1333. }, false, 0, 3 );
  1334. addGui( 'wrapShadow', wrapShadow.value, function ( val ) {
  1335. wrapShadow.value = val;
  1336. }, false, - 1, 0 );
  1337. break;
  1338. case 'toon':
  1339. // MATERIAL
  1340. mtl = new THREE.PhongNodeMaterial();
  1341. var count = new THREE.FloatNode( 3.43 );
  1342. var sceneDirectLight = new THREE.LightNode();
  1343. var color = new THREE.ColorNode( 0xAABBFF );
  1344. var lineColor = new THREE.ColorNode( 0xFF0000 );
  1345. var lineSize = new THREE.FloatNode( 0.23 );
  1346. var lineInner = new THREE.FloatNode( 0 );
  1347. // CEL
  1348. var lightLuminance = new THREE.LuminanceNode( sceneDirectLight );
  1349. var preCelLight = new THREE.OperatorNode(
  1350. lightLuminance,
  1351. count,
  1352. THREE.OperatorNode.MUL
  1353. );
  1354. var celLight = new THREE.Math1Node(
  1355. preCelLight,
  1356. THREE.Math1Node.CEIL
  1357. );
  1358. var posCelLight = new THREE.OperatorNode(
  1359. celLight,
  1360. count,
  1361. THREE.OperatorNode.DIV
  1362. );
  1363. // LINE
  1364. var posDirection = new THREE.Math1Node( new THREE.PositionNode( THREE.PositionNode.VIEW ), THREE.Math1Node.NORMALIZE );
  1365. var norDirection = new THREE.Math1Node( new THREE.NormalNode(), THREE.Math1Node.NORMALIZE );
  1366. var viewZ = new THREE.Math2Node(
  1367. posDirection,
  1368. norDirection,
  1369. THREE.Math2Node.DOT
  1370. );
  1371. var lineOutside = new THREE.Math1Node(
  1372. viewZ,
  1373. THREE.Math1Node.ABS
  1374. );
  1375. var line = new THREE.OperatorNode(
  1376. lineOutside,
  1377. new THREE.FloatNode( 1 ),
  1378. THREE.OperatorNode.DIV
  1379. );
  1380. var lineScaled = new THREE.Math3Node(
  1381. line,
  1382. lineSize,
  1383. lineInner,
  1384. THREE.Math3Node.SMOOTHSTEP
  1385. );
  1386. var innerContour = new THREE.Math1Node( new THREE.Math1Node( lineScaled, THREE.Math1Node.SATURATE ), THREE.Math1Node.INVERT );
  1387. // APPLY
  1388. mtl.color = color;
  1389. mtl.light = posCelLight;
  1390. mtl.shininess = new THREE.FloatNode( 0 );
  1391. mtl.environment = lineColor;
  1392. mtl.environmentAlpha = innerContour;
  1393. // GUI
  1394. addGui( 'color', color.value.getHex(), function ( val ) {
  1395. color.value.setHex( val );
  1396. }, true );
  1397. addGui( 'lineColor', lineColor.value.getHex(), function ( val ) {
  1398. lineColor.value.setHex( val );
  1399. }, true );
  1400. addGui( 'count', count.value, function ( val ) {
  1401. count.value = val;
  1402. }, false, 1, 8 );
  1403. addGui( 'lineSize', lineSize.value, function ( val ) {
  1404. lineSize.value = val;
  1405. }, false, 0, 1 );
  1406. addGui( 'lineInner', lineInner.value, function ( val ) {
  1407. lineInner.value = val;
  1408. }, false, 0, 1 );
  1409. addGui( 'ignoreIndirectLight', false, function ( val ) {
  1410. mtl.ao = val ? new THREE.FloatNode() : undefined;
  1411. mtl.needsUpdate = true;
  1412. } );
  1413. break;
  1414. case 'custom-attribute':
  1415. // GEOMETRY
  1416. // add "position" buffer to "custom" attribute
  1417. teapot.attributes[ 'custom' ] = teapot.attributes[ 'position' ];
  1418. // MATERIAL
  1419. mtl = new THREE.PhongNodeMaterial();
  1420. mtl.color = new THREE.AttributeNode( "custom", 3 );
  1421. // or
  1422. //mtl.color = new THREE.AttributeNode( "custom", "vec3" );
  1423. break;
  1424. case 'expression':
  1425. // MATERIAL
  1426. mtl = new THREE.PhongNodeMaterial();
  1427. var speed = new THREE.FloatNode( .5 );
  1428. var myspeed = new THREE.ExpressionNode( "speed * time", "float" );
  1429. myspeed.keywords[ "speed" ] = speed;
  1430. mtl.color = new THREE.ExpressionNode( "myCustomUv + (sin(myspeed)*.5) + (position * .05)", "vec3" );
  1431. mtl.color.keywords[ "myspeed" ] = myspeed;
  1432. mtl.position = new THREE.ExpressionNode( "mod(myspeed,1.0) < 0.5 ? position + (worldNormal*(1.0+sin(myspeed*1.0))*3.0) : position + sin( position.x * sin(myspeed*2.0))", "vec3" );
  1433. mtl.position.keywords[ "myspeed" ] = myspeed;
  1434. // add global keyword ( variable or const )
  1435. THREE.NodeLib.addKeyword( 'myCustomUv', function () {
  1436. return new THREE.ReflectNode();
  1437. } );
  1438. // GUI
  1439. addGui( 'speed', speed.value, function ( val ) {
  1440. speed.value = val;
  1441. }, false, 0, 1 );
  1442. break;
  1443. case 'reserved-keywords':
  1444. // MATERIAL
  1445. mtl = new THREE.PhongNodeMaterial();
  1446. var keywordsexample = new THREE.FunctionNode( [
  1447. // use "uv" reserved keyword
  1448. "vec4 keywordsexample( sampler2D texture ) {",
  1449. " return texture2D( texture, myUV ) + vec4( position * myAlpha, 0.0 );",
  1450. "}"
  1451. ].join( "\n" ) );
  1452. // add local keyword ( const only )
  1453. keywordsexample.keywords[ "myAlpha" ] = new THREE.ConstNode( "float myAlpha .05" );
  1454. // add global keyword ( const only )
  1455. THREE.NodeLib.addKeyword( 'myUV', function () {
  1456. return new THREE.UVNode();
  1457. } );
  1458. // add global const or function
  1459. //THREE.NodeLib.add( new THREE.ConstNode("float MY_CONST .05") )
  1460. // reserved keywords
  1461. console.log( THREE.NodeLib.keywords );
  1462. // keywords conflit? use this to disable:
  1463. //blurtexture.useKeywords = false; // ( true is default )
  1464. mtl.color = new THREE.FunctionCallNode( keywordsexample, [ new THREE.TextureNode( getTexture( "brick" ) ) ] );
  1465. break;
  1466. case 'node-position':
  1467. // MATERIAL
  1468. var node = new THREE.PositionNode();
  1469. mtl = new THREE.PhongNodeMaterial();
  1470. mtl.color = node;
  1471. // GUI
  1472. addGui( 'scope', {
  1473. local: THREE.PositionNode.LOCAL,
  1474. world: THREE.PositionNode.WORLD,
  1475. view: THREE.PositionNode.VIEW
  1476. }, function ( val ) {
  1477. node.scope = val;
  1478. mtl.needsUpdate = true;
  1479. } );
  1480. break;
  1481. case 'node-normal':
  1482. // MATERIAL
  1483. var node = new THREE.NormalNode();
  1484. mtl = new THREE.PhongNodeMaterial();
  1485. mtl.color = node;
  1486. // GUI
  1487. addGui( 'scope', {
  1488. local: THREE.NormalNode.LOCAL,
  1489. world: THREE.NormalNode.WORLD,
  1490. view: THREE.NormalNode.VIEW
  1491. }, function ( val ) {
  1492. node.scope = val;
  1493. mtl.needsUpdate = true;
  1494. } );
  1495. break;
  1496. case 'varying':
  1497. // MATERIAL
  1498. mtl = new THREE.PhongNodeMaterial();
  1499. var varying = new THREE.VarNode( "vec3" );
  1500. varying.value = new THREE.NormalNode( THREE.NormalNode.VIEW );
  1501. // using BypassNode the NormalNode not apply the value in .position slot
  1502. // but set the NormalNode value in VarNode
  1503. // it can be useful to send values between vertex to fragment shader
  1504. // without affect vertex shader
  1505. mtl.position = new THREE.BypassNode( varying );
  1506. mtl.color = varying;
  1507. // you can also set a independent value in .position slot using BypassNode
  1508. // such this expression using ExpressionNode
  1509. mtl.position.value = new THREE.ExpressionNode( "position * ( .1 + abs( sin( time ) ) )", "vec3" );
  1510. break;
  1511. case 'void-function':
  1512. // MATERIAL
  1513. mtl = new THREE.PhongNodeMaterial();
  1514. var varying = new THREE.VarNode( "vec3" );
  1515. // VERTEX
  1516. var setMyVar = new THREE.FunctionNode( [
  1517. "void setMyVar( vec3 pos ) {",
  1518. // set "myVar" in vertex shader in this example,
  1519. // can be used in fragment shader too or in rest of the current shader
  1520. " myVar = pos;",
  1521. "}"
  1522. ].join( "\n" ) );
  1523. // add keyword
  1524. setMyVar.keywords[ "myVar" ] = varying;
  1525. var position = new THREE.ExpressionNode( "setMyVar( position * .1 )", "vec3" );
  1526. position.includes = [ setMyVar ];
  1527. position.keywords[ "tex" ] = new THREE.TextureNode( getTexture( "brick" ) );
  1528. // use BypassNode to "void" functions
  1529. mtl.position = new THREE.BypassNode( position );
  1530. // FRAGMENT
  1531. var clipFromPos = new THREE.FunctionNode( [
  1532. "void clipFromPos( vec3 pos ) {",
  1533. " if ( pos.y < .0 ) discard;",
  1534. "}"
  1535. ].join( "\n" ) );
  1536. var clipFromPosCall = new THREE.FunctionCallNode( clipFromPos, {
  1537. pos: varying
  1538. } );
  1539. mtl.color = new THREE.BypassNode( clipFromPosCall, varying );
  1540. break;
  1541. case 'conditional':
  1542. // MATERIAL
  1543. mtl = new THREE.PhongNodeMaterial();
  1544. var a = new THREE.FloatNode( 0 ),
  1545. b = new THREE.FloatNode( 0 ),
  1546. ifNode = new THREE.ColorNode( 0x0000FF ),
  1547. elseNode = new THREE.ColorNode( 0xFF0000 );
  1548. var cond = new THREE.CondNode( a, b, THREE.CondNode.EQUAL, ifNode, elseNode );
  1549. mtl.color = cond;
  1550. // GUI
  1551. addGui( 'a', a.value, function ( val ) {
  1552. a.value = val;
  1553. }, false, 0, 1 );
  1554. addGui( 'b', b.value, function ( val ) {
  1555. b.value = val;
  1556. }, false, 0, 1 );
  1557. addGui( 'a condition b', {
  1558. EQUAL: THREE.CondNode.EQUAL,
  1559. NOT_EQUAL: THREE.CondNode.NOT_EQUAL,
  1560. GREATER: THREE.CondNode.GREATER,
  1561. GREATER_EQUAL: THREE.CondNode.GREATER_EQUAL,
  1562. LESS: THREE.CondNode.LESS,
  1563. LESS_EQUAL: THREE.CondNode.LESS_EQUAL
  1564. }, function ( val ) {
  1565. cond.op = val;
  1566. mtl.needsUpdate = true;
  1567. } );
  1568. addGui( 'if color', ifNode.value.getHex(), function ( val ) {
  1569. ifNode.value.setHex( val );
  1570. }, true );
  1571. addGui( 'else color', elseNode.value.getHex(), function ( val ) {
  1572. elseNode.value.setHex( val );
  1573. }, true );
  1574. break;
  1575. case 'rtt':
  1576. // MATERIAL
  1577. mtl = new THREE.PhongNodeMaterial();
  1578. var uvTransform = new THREE.UVTransformNode(),
  1579. checker = new THREE.CheckerNode( uvTransform );
  1580. uvTransform.setUvTransform( 0, 0, 2, 2, 0 );
  1581. var rtt = new THREE.RTTNode( 512, 512, checker ),
  1582. bumpMap = new THREE.BumpMapNode( rtt );
  1583. bumpMap.scale.value = .1;
  1584. mtl.color = checker;
  1585. mtl.normal = bumpMap;
  1586. // GUI
  1587. addGui( 'bump', bumpMap.scale.value, function ( val ) {
  1588. bumpMap.scale.value = val;
  1589. }, false, - .5, .5 );
  1590. addGui( 'scale', 2, function ( val ) {
  1591. uvTransform.setUvTransform( 0, 0, val, val, 0 );
  1592. }, false, 0, 8 );
  1593. addGui( 'ignoreColor', false, function ( val ) {
  1594. mtl.color = val ? new THREE.ColorNode( 0xFFFFFF ) : checker;
  1595. mtl.needsUpdate = true;
  1596. } );
  1597. break;
  1598. case 'temporal-blur':
  1599. // MATERIAL
  1600. mtl = new THREE.PhongNodeMaterial();
  1601. var texture = new THREE.TextureNode( getTexture( "brick" ) );
  1602. var rttStore = new THREE.RTTNode( 512, 512, texture );
  1603. var blur = new THREE.BlurNode( rttStore );
  1604. var timer = new THREE.TimerNode( .01, THREE.TimerNode.LOCAL );
  1605. var color = new THREE.Math3Node(
  1606. rttStore,
  1607. blur,
  1608. new THREE.FloatNode( .6 ),
  1609. THREE.Math3Node.MIX
  1610. );
  1611. blur.horizontal = blur.vertical = timer;
  1612. var rttSave = new THREE.RTTNode( 512, 512, color );
  1613. rttSave.saveTo = rttStore;
  1614. mtl.color = rttSave;
  1615. // GUI
  1616. addGui( 'click to reset', false, function () {
  1617. // render a single time
  1618. rttStore.render = true;
  1619. // reset time blur
  1620. timer.value = 0;
  1621. } );
  1622. break;
  1623. case 'readonly':
  1624. // MATERIAL
  1625. mtl = new THREE.PhongNodeMaterial();
  1626. // not use "uniform" input ( for optimization )
  1627. // instead use explicit declaration, for example:
  1628. // vec3( 1.0, 1.0, 1.0 ) instead "uniform vec3"
  1629. // if readonly is true not allow change the value after build the shader material
  1630. mtl.color = new THREE.ColorNode( 0xFFFFFF ).setReadonly( true );
  1631. mtl.specular = new THREE.FloatNode( .5 ).setReadonly( true );
  1632. mtl.shininess = new THREE.FloatNode( 15 ).setReadonly( true );
  1633. break;
  1634. case 'label':
  1635. // MATERIAL
  1636. mtl = new THREE.PhongNodeMaterial();
  1637. // label can be useful for finding the nodes as variables in debug level
  1638. // but this always force the creation of a variable
  1639. // same as the code can be writed in the same line (inline)
  1640. // for optimization this is not recommended
  1641. var colorInput = new THREE.ColorNode( 0xFFFFFF ).setLabel( "colorInput" );
  1642. var specularInput = new THREE.FloatNode( .5 ).setLabel( "specularInput" );
  1643. var colorMix = new THREE.OperatorNode(
  1644. colorInput,
  1645. new THREE.ColorNode( 0x6495ED ).setReadonly( true ),
  1646. THREE.OperatorNode.MUL
  1647. ).setLabel( "colorMix" );
  1648. mtl.color = colorMix;
  1649. mtl.specular = specularInput;
  1650. // default: without use label
  1651. // this is optimized writed the code in a single line (inline)
  1652. // for the reason that this node is used only once in this shader program
  1653. mtl.shininess = new THREE.OperatorNode(
  1654. new THREE.FloatNode( 10 ).setReadonly( true ),
  1655. new THREE.FloatNode( 5 ).setReadonly( true ),
  1656. THREE.OperatorNode.ADD
  1657. );
  1658. mtl.build();
  1659. // show names glsl fragment shader
  1660. // open console e find using CTRL+F "colorMix" for example
  1661. console.log( mtl.fragmentShader );
  1662. break;
  1663. case 'triangle-blur':
  1664. // MATERIAL
  1665. mtl = new THREE.PhongNodeMaterial();
  1666. var delta = new THREE.Vector2Node( .5, .25 );
  1667. var alpha = new THREE.FloatNode( 1 );
  1668. var blurtexture = new THREE.FunctionNode( [
  1669. // Reference: TriangleBlurShader.js
  1670. "vec4 blurtexture(sampler2D texture, vec2 uv, vec2 delta) {",
  1671. " vec4 color = vec4( 0.0 );",
  1672. " float total = 0.0;",
  1673. // randomize the lookup values to hide the fixed number of samples
  1674. " float offset = rand( uv );",
  1675. " for ( float t = -BLUR_ITERATIONS; t <= BLUR_ITERATIONS; t ++ ) {",
  1676. " float percent = ( t + offset - 0.5 ) / BLUR_ITERATIONS;",
  1677. " float weight = 1.0 - abs( percent );",
  1678. " color += texture2D( texture, uv + delta * percent ) * weight;",
  1679. " total += weight;",
  1680. " }",
  1681. " return color / total;",
  1682. "}"
  1683. ].join( "\n" ), [ new THREE.ConstNode( "float BLUR_ITERATIONS 10.0" ) ] );
  1684. var blurredTexture = new THREE.FunctionCallNode( blurtexture, {
  1685. texture: new THREE.TextureNode( getTexture( "brick" ) ),
  1686. delta: delta,
  1687. uv: new THREE.UVNode()
  1688. } );
  1689. var color = new THREE.Math3Node(
  1690. new THREE.TextureNode( getTexture( "brick" ) ),
  1691. blurredTexture,
  1692. alpha,
  1693. THREE.Math3Node.MIX
  1694. );
  1695. mtl.color = color;
  1696. // GUI
  1697. addGui( 'alpha', alpha.value, function ( val ) {
  1698. alpha.value = val;
  1699. }, false, 0, 1 );
  1700. addGui( 'deltaX', delta.x, function ( val ) {
  1701. delta.x = val;
  1702. }, false, 0, 1 );
  1703. addGui( 'deltaY', delta.x, function ( val ) {
  1704. delta.y = val;
  1705. }, false, 0, 1 );
  1706. break;
  1707. case 'triplanar-mapping':
  1708. // MATERIAL
  1709. mtl = new THREE.PhongNodeMaterial();
  1710. var scale = new THREE.FloatNode( .02 );
  1711. var triplanarMapping = new THREE.FunctionNode( [
  1712. // Reference: https://github.com/keijiro/StandardTriplanar
  1713. "vec4 triplanar_mapping( sampler2D texture, vec3 normal, vec3 position, float scale ) {",
  1714. // Blending factor of triplanar mapping
  1715. " vec3 bf = normalize( abs( normal ) );",
  1716. " bf /= dot( bf, vec3( 1.0 ) );",
  1717. // Triplanar mapping
  1718. " vec2 tx = position.yz * scale;",
  1719. " vec2 ty = position.zx * scale;",
  1720. " vec2 tz = position.xy * scale;",
  1721. // Base color
  1722. " vec4 cx = texture2D(texture, tx) * bf.x;",
  1723. " vec4 cy = texture2D(texture, ty) * bf.y;",
  1724. " vec4 cz = texture2D(texture, tz) * bf.z;",
  1725. " return cx + cy + cz;",
  1726. "}"
  1727. ].join( "\n" ) );
  1728. var triplanarMappingTexture = new THREE.FunctionCallNode( triplanarMapping, {
  1729. texture: new THREE.TextureNode( getTexture( "brick" ) ),
  1730. normal: new THREE.NormalNode( THREE.NormalNode.WORLD ),
  1731. position: new THREE.PositionNode( THREE.PositionNode.WORLD ),
  1732. scale: scale,
  1733. } );
  1734. mtl.color = triplanarMappingTexture;
  1735. // GUI
  1736. addGui( 'scale', scale.value, function ( val ) {
  1737. scale.value = val;
  1738. }, false, 0.001, .1 );
  1739. break;
  1740. case 'firefly':
  1741. // MATERIAL
  1742. mtl = new THREE.PhongNodeMaterial();
  1743. var time = new THREE.TimerNode();
  1744. var speed = new THREE.FloatNode( .5 );
  1745. var color = new THREE.ColorNode( 0x98ff00 );
  1746. var timeSpeed = new THREE.OperatorNode(
  1747. time,
  1748. speed,
  1749. THREE.OperatorNode.MUL
  1750. );
  1751. var sinCycleInSecs = new THREE.OperatorNode(
  1752. timeSpeed,
  1753. new THREE.ConstNode( THREE.ConstNode.PI2 ),
  1754. THREE.OperatorNode.MUL
  1755. );
  1756. var cycle = new THREE.Math1Node( sinCycleInSecs, THREE.Math1Node.SIN );
  1757. var cycleColor = new THREE.OperatorNode(
  1758. cycle,
  1759. color,
  1760. THREE.OperatorNode.MUL
  1761. );
  1762. var cos = new THREE.Math1Node( cycleColor, THREE.Math1Node.SIN );
  1763. mtl.color = new THREE.ColorNode( 0 );
  1764. mtl.emissive = cos;
  1765. // GUI
  1766. addGui( 'speed', speed.value, function ( val ) {
  1767. speed.value = val;
  1768. }, false, 0, 3 );
  1769. break;
  1770. case 'sss':
  1771. case 'translucent':
  1772. // DISTANCE FORMULA
  1773. var modelPos = new THREE.Vector3Node();
  1774. var viewPos = new THREE.PositionNode( THREE.PositionNode.VIEW );
  1775. var cameraPosition = new THREE.CameraNode( THREE.CameraNode.POSITION );
  1776. var cameraDistance = new THREE.Math2Node(
  1777. modelPos,
  1778. cameraPosition,
  1779. THREE.Math2Node.DISTANCE
  1780. );
  1781. var viewPosZ = new THREE.SwitchNode( viewPos, 'z' );
  1782. var distance = new THREE.OperatorNode(
  1783. cameraDistance,
  1784. viewPosZ,
  1785. THREE.OperatorNode.SUB
  1786. );
  1787. var distanceRadius = new THREE.OperatorNode(
  1788. distance,
  1789. new THREE.FloatNode( 70 ),
  1790. THREE.OperatorNode.ADD
  1791. );
  1792. var objectDepth = new THREE.Math3Node(
  1793. distanceRadius,
  1794. new THREE.FloatNode( 0 ),
  1795. new THREE.FloatNode( 50 ),
  1796. THREE.Math3Node.SMOOTHSTEP
  1797. );
  1798. // RTT ( get back distance )
  1799. rtTexture = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBFormat } );
  1800. library[ rtTexture.texture.uuid ] = rtTexture.texture;
  1801. var distanceMtl = new THREE.PhongNodeMaterial();
  1802. distanceMtl.environment = objectDepth;
  1803. distanceMtl.side = THREE.BackSide;
  1804. rtMaterial = distanceMtl;
  1805. // MATERIAL
  1806. mtl = new THREE.StandardNodeMaterial();
  1807. var backSideDepth = new THREE.TextureNode( rtTexture.texture, new THREE.ScreenUVNode() );
  1808. var difference = new THREE.OperatorNode(
  1809. objectDepth,
  1810. backSideDepth,
  1811. THREE.OperatorNode.SUB
  1812. );
  1813. var sss = new THREE.Math3Node(
  1814. new THREE.FloatNode( - .1 ),
  1815. new THREE.FloatNode( .5 ),
  1816. difference,
  1817. THREE.Math3Node.SMOOTHSTEP
  1818. );
  1819. var sssAlpha = new THREE.Math1Node( sss, THREE.Math1Node.SATURATE );
  1820. var frontColor, backColor;
  1821. if ( name == 'sss' ) {
  1822. var sssOut = new THREE.Math2Node(
  1823. objectDepth,
  1824. sssAlpha,
  1825. THREE.Math2Node.MIN
  1826. );
  1827. frontColor = new THREE.ColorNode( 0xd4cfbb );
  1828. backColor = new THREE.ColorNode( 0xd04327 );
  1829. var color = new THREE.Math3Node(
  1830. backColor,
  1831. frontColor,
  1832. sssOut,
  1833. THREE.Math3Node.MIX
  1834. );
  1835. var light = new THREE.OperatorNode(
  1836. new THREE.LightNode(),
  1837. color,
  1838. THREE.OperatorNode.ADD
  1839. );
  1840. mtl.color = frontColor;
  1841. mtl.roughness = new THREE.FloatNode( .1 );
  1842. mtl.metalness = new THREE.FloatNode( .5 );
  1843. mtl.light = light;
  1844. mtl.environment = color;
  1845. } else {
  1846. frontColor = new THREE.ColorNode( 0xd04327 );
  1847. backColor = new THREE.ColorNode( 0x1a0e14 );
  1848. var color = new THREE.Math3Node(
  1849. frontColor,
  1850. backColor,
  1851. sssAlpha,
  1852. THREE.Math3Node.MIX
  1853. );
  1854. var light = new THREE.OperatorNode(
  1855. new THREE.LightNode(),
  1856. color,
  1857. THREE.OperatorNode.ADD
  1858. );
  1859. mtl.color = new THREE.ColorNode( 0xffffff );
  1860. mtl.roughness = new THREE.FloatNode( .1 );
  1861. mtl.metalness = new THREE.FloatNode( .5 );
  1862. mtl.light = light;
  1863. mtl.environment = color;
  1864. }
  1865. // GUI
  1866. addGui( 'frontColor', frontColor.value.getHex(), function ( val ) {
  1867. frontColor.value.setHex( val );
  1868. }, true );
  1869. addGui( 'backColor', backColor.value.getHex(), function ( val ) {
  1870. backColor.value.setHex( val );
  1871. }, true );
  1872. addGui( 'area', sss.b.value, function ( val ) {
  1873. sss.b.value = val;
  1874. }, false, 0, 1 );
  1875. break;
  1876. }
  1877. // set material
  1878. mtl.side = defaultSide;
  1879. mesh.material = mtl;
  1880. }
  1881. function onWindowResize() {
  1882. var width = window.innerWidth, height = window.innerHeight;
  1883. camera.aspect = width / height;
  1884. camera.updateProjectionMatrix();
  1885. renderer.setSize( width, height );
  1886. if ( rtTexture ) rtTexture.setSize( width, height );
  1887. }
  1888. document.getElementById( 'serialize' ).addEventListener( 'click', function () {
  1889. if ( serialized ) reset();
  1890. else serialize();
  1891. serialized = ! serialized;
  1892. } );
  1893. function reset() {
  1894. updateMaterial();
  1895. // gui
  1896. var div = document.getElementById( 'serialize' );
  1897. div.textContent = "Serialize and apply";
  1898. }
  1899. function serialize() {
  1900. var json = mesh.material.toJSON();
  1901. // replace uuid to url (facilitates the load of textures using url otherside uuid) e.g:
  1902. var cloud = getTexture( "cloud" );
  1903. THREE.NodeMaterialLoaderUtils.replaceUUID( json, cloud, "cloud" );
  1904. library[ "cloud" ] = cloud;
  1905. // --
  1906. var jsonStr = JSON.stringify( json );
  1907. console.log( jsonStr );
  1908. var loader = new THREE.NodeMaterialLoader( null, library ),
  1909. material = loader.parse( json );
  1910. mesh.material.dispose();
  1911. mesh.material = material;
  1912. // gui
  1913. var div = document.getElementById( 'serialize' );
  1914. div.textContent = "Click to reset - JSON Generate: " + ( jsonStr.length / 1024 ).toFixed( 3 ) + "kB";
  1915. if ( gui ) gui.destroy();
  1916. gui = null;
  1917. }
  1918. function animate() {
  1919. var delta = clock.getDelta();
  1920. if ( move ) {
  1921. var time = Date.now() * 0.005;
  1922. mesh.position.z = Math.cos( time ) * 10;
  1923. mesh.position.y = Math.sin( time ) * 10;
  1924. } else {
  1925. mesh.position.z = mesh.position.y = 0;
  1926. }
  1927. //mesh.rotation.z += .01;
  1928. // update material animation and/or gpu calcs (pre-renderer)
  1929. frame.update( delta ).setRenderer( renderer );
  1930. if ( mesh.material instanceof THREE.NodeMaterial ) {
  1931. frame.updateNode( mesh.material );
  1932. }
  1933. // render to texture for sss/translucent material only
  1934. if ( rtTexture ) {
  1935. scene.overrideMaterial = rtMaterial;
  1936. renderer.setRenderTarget( rtTexture );
  1937. renderer.clear();
  1938. renderer.render( scene, camera );
  1939. scene.overrideMaterial = null;
  1940. }
  1941. renderer.setRenderTarget( null );
  1942. renderer.render( scene, camera );
  1943. requestAnimationFrame( animate );
  1944. }
  1945. </script>
  1946. </body>
  1947. </html>