webgl_materials_nodes.html 70 KB

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