webgl_materials_nodes.html 61 KB

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