webgl_materials_nodes.html 65 KB

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