webgl_materials_nodes.html 58 KB

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