webgl_materials_nodes.html 70 KB

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