webgl_materials_nodes.html 70 KB

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