webgl_materials_nodes.html 52 KB

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