webgl_materials_nodes.html 59 KB

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