webgl_materials_nodes.html 70 KB

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