webgl_materials_nodes.html 60 KB

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