webgl_materials_nodes.html 56 KB

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