webgl_materials_nodes.html 56 KB

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