webgl_materials_nodes.html 70 KB

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