webgl_materials_nodes.html 70 KB

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