webgl_materials_nodes.html 70 KB

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