webgl_materials_nodes.html 78 KB

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