webgl_materials_nodes.html 62 KB

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