webgl_materials_nodes.html 70 KB

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