webgl_materials_nodes.html 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247
  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">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 uv = new THREE.UVNode();
  414. var timeScale = new THREE.OperatorNode(
  415. time,
  416. speed,
  417. THREE.OperatorNode.MUL
  418. );
  419. var worldScl = new THREE.OperatorNode(
  420. new THREE.PositionNode(),
  421. worldScale,
  422. THREE.OperatorNode.MUL
  423. );
  424. var posContinuous = new THREE.OperatorNode(
  425. worldScl,
  426. timeScale,
  427. THREE.OperatorNode.ADD
  428. );
  429. var wave = new THREE.Math1Node( posContinuous, THREE.Math1Node.SIN );
  430. wave = new THREE.SwitchNode( wave, 'x' );
  431. var waveScale = new THREE.OperatorNode(
  432. wave,
  433. scale,
  434. THREE.OperatorNode.MUL
  435. );
  436. var displaceY = new THREE.JoinNode(
  437. new THREE.FloatNode(),
  438. waveScale,
  439. new THREE.FloatNode()
  440. );
  441. var displace = new THREE.OperatorNode(
  442. new THREE.NormalNode(),
  443. displaceY,
  444. THREE.OperatorNode.MUL
  445. );
  446. var blend = new THREE.OperatorNode(
  447. new THREE.PositionNode(),
  448. displaceY,
  449. THREE.OperatorNode.ADD
  450. );
  451. var color = new THREE.Math3Node(
  452. colorB,
  453. colorA,
  454. wave,
  455. THREE.Math3Node.MIX
  456. );
  457. mtl.color = color;
  458. mtl.transform = blend;
  459. // GUI
  460. addGui( 'speed', speed.number, function( val ) {
  461. speed.number = val;
  462. }, false, 0, 10 );
  463. addGui( 'scale', scale.number, function( val ) {
  464. scale.number = val;
  465. }, false, 0, 3 );
  466. addGui( 'worldScale', worldScale.number, function( val ) {
  467. worldScale.number = val;
  468. }, false, 0, 1 );
  469. addGui( 'colorA', colorA.value.getHex(), function( val ) {
  470. colorA.value.setHex( val );
  471. }, true );
  472. addGui( 'colorB', colorB.value.getHex(), function( val ) {
  473. colorB.value.setHex( val );
  474. }, true );
  475. addGui( 'useNormals', false, function( val ) {
  476. blend.b = val ? displace : displaceY;
  477. mtl.build();
  478. } );
  479. break;
  480. case 'rim':
  481. // MATERIAL
  482. mtl = new THREE.PhongNodeMaterial();
  483. var intensity = 1.3;
  484. var power = new THREE.FloatNode( 3 );
  485. var color = new THREE.ColorNode( 0xFFFFFF );
  486. var viewZ = new THREE.Math2Node(
  487. new THREE.NormalNode( THREE.NormalNode.VIEW ),
  488. new THREE.Vector3Node( 0, 0, - intensity ),
  489. THREE.Math2Node.DOT
  490. );
  491. var rim = new THREE.OperatorNode(
  492. viewZ,
  493. new THREE.FloatNode( intensity ),
  494. THREE.OperatorNode.ADD
  495. );
  496. var rimPower = new THREE.Math2Node(
  497. rim,
  498. power,
  499. THREE.Math2Node.POW
  500. );
  501. var rimColor = new THREE.OperatorNode(
  502. rimPower,
  503. color,
  504. THREE.OperatorNode.MUL
  505. );
  506. mtl.color = new THREE.ColorNode( 0x111111 );
  507. mtl.emissive = rimColor;
  508. // GUI
  509. addGui( 'color', color.value.getHex(), function( val ) {
  510. color.value.setHex( val );
  511. }, true );
  512. addGui( 'intensity', intensity, function( val ) {
  513. intensity = val;
  514. viewZ.b.z = - intensity;
  515. rim.b.number = intensity;
  516. }, false, 0, 3 );
  517. addGui( 'power', power.number, function( val ) {
  518. power.number = val;
  519. }, false, 0, 6 );
  520. addGui( 'xray', false, function( val ) {
  521. if ( val ) {
  522. mtl.emissive = color;
  523. mtl.alpha = rimPower;
  524. mtl.blending = THREE.AdditiveBlending;
  525. mtl.depthWrite = false;
  526. }
  527. else {
  528. mtl.emissive = rimColor;
  529. mtl.alpha = null;
  530. mtl.blending = THREE.NormalBlending;
  531. mtl.depthWrite = true;
  532. }
  533. mtl.build();
  534. } );
  535. break;
  536. case 'color-adjustment':
  537. // MATERIAL
  538. mtl = new THREE.PhongNodeMaterial();
  539. var texture = new THREE.TextureNode( getTexture( "brick" ) );
  540. var hue = new THREE.FloatNode();
  541. var sataturation = new THREE.FloatNode( 1 );
  542. var vibrance = new THREE.FloatNode();
  543. var brightness = new THREE.FloatNode( 0 );
  544. var contrast = new THREE.FloatNode( 1 );
  545. var hueNode = new THREE.ColorAdjustmentNode( texture, hue, THREE.ColorAdjustmentNode.HUE );
  546. var satNode = new THREE.ColorAdjustmentNode( hueNode, sataturation, THREE.ColorAdjustmentNode.SATURATION );
  547. var vibranceNode = new THREE.ColorAdjustmentNode( satNode, vibrance, THREE.ColorAdjustmentNode.VIBRANCE );
  548. var brightnessNode = new THREE.ColorAdjustmentNode( vibranceNode, brightness, THREE.ColorAdjustmentNode.BRIGHTNESS );
  549. var contrastNode = new THREE.ColorAdjustmentNode( brightnessNode, contrast, THREE.ColorAdjustmentNode.CONTRAST );
  550. mtl.color = contrastNode;
  551. // GUI
  552. addGui( 'hue', hue.number, function( val ) {
  553. hue.number = val;
  554. }, false, 0, Math.PI * 2 );
  555. addGui( 'saturation', sataturation.number, function( val ) {
  556. sataturation.number = val;
  557. }, false, 0, 2 );
  558. addGui( 'vibrance', vibrance.number, function( val ) {
  559. vibrance.number = val;
  560. }, false, - 1, 1 );
  561. addGui( 'brightness', brightness.number, function( val ) {
  562. brightness.number = val;
  563. }, false, 0, .5 );
  564. addGui( 'contrast', contrast.number, function( val ) {
  565. contrast.number = val;
  566. }, false, 0, 2 );
  567. break;
  568. case 'bump':
  569. // MATERIAL
  570. mtl = new THREE.PhongNodeMaterial();
  571. var diffuse = new THREE.TextureNode( getTexture( "brick" ) );
  572. var bump = new THREE.BumpNode( new THREE.TextureNode( getTexture( "brick" ) ) );
  573. bump.scale = new THREE.Vector2Node( - 1.5, - 1.5 );
  574. mtl.color = diffuse;
  575. mtl.normal = bump;
  576. // GUI
  577. addGui( 'scaleX', bump.scale.x, function( val ) {
  578. bump.scale.x = val;
  579. }, false, - 2, 2 );
  580. addGui( 'scaleY', bump.scale.y, function( val ) {
  581. bump.scale.y = val;
  582. }, false, - 2, 2 );
  583. addGui( 'color', true, function( val ) {
  584. mtl.color = val ? diffuse : new THREE.ColorNode( 0xEEEEEE );
  585. mtl.build();
  586. } );
  587. break;
  588. case 'blur':
  589. // MATERIAL
  590. mtl = new THREE.PhongNodeMaterial();
  591. var diffuse = new THREE.TextureNode( getTexture( "brick" ) );
  592. var blur = new THREE.BlurNode( new THREE.TextureNode( getTexture( "brick" ) ) );
  593. mtl.color = blur;
  594. // GUI
  595. addGui( 'radiusX', blur.radius.x, function( val ) {
  596. blur.radius.x = val;
  597. }, false, 0, 15 );
  598. addGui( 'radiusY', blur.radius.y, function( val ) {
  599. blur.radius.y = val;
  600. }, false, 0, 15 );
  601. break;
  602. case 'spherical-reflection':
  603. // MATERIAL
  604. mtl = new THREE.PhongNodeMaterial();
  605. mtl.environment = new THREE.TextureNode( getTexture( "spherical" ), new THREE.ReflectNode( THREE.ReflectNode.SPHERE ) );
  606. break;
  607. case 'fresnel':
  608. // MATERIAL
  609. mtl = new THREE.PhongNodeMaterial();
  610. var reflectance = new THREE.FloatNode( 1.3 );
  611. var power = new THREE.FloatNode( 1 );
  612. var color = new THREE.CubeTextureNode( cubemap );
  613. var viewZ = new THREE.Math2Node(
  614. new THREE.NormalNode( THREE.NormalNode.VIEW ),
  615. new THREE.Vector3Node( 0, 0, - 1 ),
  616. THREE.Math2Node.DOT
  617. );
  618. var theta = new THREE.OperatorNode(
  619. viewZ,
  620. new THREE.FloatNode( 1 ),
  621. THREE.OperatorNode.ADD
  622. );
  623. var thetaPower = new THREE.Math2Node(
  624. theta,
  625. power,
  626. THREE.Math2Node.POW
  627. );
  628. var fresnel = new THREE.OperatorNode(
  629. reflectance,
  630. thetaPower,
  631. THREE.OperatorNode.MUL
  632. );
  633. mtl.color = new THREE.ColorNode( 0x3399FF );
  634. mtl.environment = color;
  635. mtl.environmentAlpha = new THREE.Math1Node( fresnel, THREE.Math1Node.SAT );
  636. // GUI
  637. addGui( 'reflectance', reflectance.number, function( val ) {
  638. reflectance.number = val;
  639. }, false, 0, 3 );
  640. addGui( 'power', power.number, function( val ) {
  641. power.number = val;
  642. }, false, 0, 5 );
  643. break;
  644. case 'layers':
  645. // MATERIAL
  646. mtl = new THREE.PhongNodeMaterial();
  647. var tex1 = new THREE.TextureNode( getTexture( "grass" ) );
  648. var tex2 = new THREE.TextureNode( getTexture( "brick" ) );
  649. var offset = new THREE.FloatNode( 0 );
  650. var scale = new THREE.FloatNode( 1 );
  651. var uv = new THREE.UVNode();
  652. var uvOffset = new THREE.OperatorNode(
  653. offset,
  654. uv,
  655. THREE.OperatorNode.ADD
  656. );
  657. var uvScale = new THREE.OperatorNode(
  658. uvOffset,
  659. scale,
  660. THREE.OperatorNode.MUL
  661. );
  662. var mask = new THREE.TextureNode( getTexture( "decalDiffuse" ), uvScale );
  663. var maskAlphaChannel = new THREE.SwitchNode( mask, 'w' );
  664. var blend = new THREE.Math3Node(
  665. tex1,
  666. tex2,
  667. maskAlphaChannel,
  668. THREE.Math3Node.MIX
  669. );
  670. mtl.color = blend;
  671. // GUI
  672. addGui( 'offset', offset.number, function( val ) {
  673. offset.number = val;
  674. }, false, 0, 1 );
  675. addGui( 'scale', scale.number, function( val ) {
  676. scale.number = val;
  677. }, false, 0, 10 );
  678. break;
  679. case 'saturation':
  680. // MATERIAL
  681. mtl = new THREE.StandardNodeMaterial();
  682. var tex = new THREE.TextureNode( getTexture( "brick" ) );
  683. var sat = new THREE.FloatNode( 0 );
  684. var satrgb = new THREE.FunctionNode( [
  685. "vec3 satrgb(vec3 rgb, float adjustment) {",
  686. //" const vec3 W = vec3(0.2125, 0.7154, 0.0721);", // LUMA
  687. " vec3 intensity = vec3(dot(rgb, LUMA));",
  688. " return mix(intensity, rgb, adjustment);",
  689. "}"
  690. ].join( "\n" ) );
  691. var saturation = new THREE.FunctionCallNode( satrgb );
  692. saturation.inputs.rgb = tex;
  693. saturation.inputs.adjustment = sat;
  694. // or try
  695. //saturation.inputs[0] = tex;
  696. //saturation.inputs[1] = sat;
  697. mtl.color = saturation;
  698. // GUI
  699. addGui( 'saturation', sat.number, function( val ) {
  700. sat.number = val;
  701. }, false, 0, 2 );
  702. break;
  703. case 'top-bottom':
  704. // MATERIAL
  705. mtl = new THREE.PhongNodeMaterial();
  706. var top = new THREE.TextureNode( getTexture( "grass" ) );
  707. var bottom = new THREE.TextureNode( getTexture( "brick" ) );
  708. var normal = new THREE.NormalNode( THREE.NormalNode.WORLD );
  709. var normalY = new THREE.SwitchNode( normal, 'y' );
  710. var hard = new THREE.FloatNode( 9 );
  711. var offset = new THREE.FloatNode( - 2.5 );
  712. var hardClamp = new THREE.OperatorNode(
  713. normalY,
  714. hard,
  715. THREE.OperatorNode.MUL
  716. );
  717. var offsetClamp = new THREE.OperatorNode(
  718. hardClamp,
  719. offset,
  720. THREE.OperatorNode.ADD
  721. );
  722. var clamp0at1 = new THREE.Math1Node( offsetClamp, THREE.Math1Node.SAT );
  723. var blend = new THREE.Math3Node( top, bottom, clamp0at1, THREE.Math3Node.MIX );
  724. mtl.color = blend;
  725. // GUI
  726. addGui( 'hard', hard.number, function( val ) {
  727. hard.number = val;
  728. }, false, 0, 20 );
  729. addGui( 'offset', offset.number, function( val ) {
  730. offset.number = val;
  731. }, false, - 10, 10 );
  732. break;
  733. case 'displace':
  734. // MATERIAL
  735. mtl = new THREE.PhongNodeMaterial();
  736. var time = new THREE.TimerNode();
  737. var scale = new THREE.FloatNode( 2 );
  738. var speed = new THREE.FloatNode( .2 );
  739. var colorA = new THREE.ColorNode( 0xFFFFFF );
  740. var colorB = new THREE.ColorNode( 0x0054df );
  741. var uv = new THREE.UVNode();
  742. var timeScl = new THREE.OperatorNode(
  743. time,
  744. speed,
  745. THREE.OperatorNode.MUL
  746. );
  747. var displaceOffset = new THREE.OperatorNode(
  748. timeScl,
  749. uv,
  750. THREE.OperatorNode.ADD
  751. );
  752. var tex = new THREE.TextureNode( getTexture( "cloud" ), displaceOffset );
  753. var texArea = new THREE.SwitchNode( tex, 'w' );
  754. var displace = new THREE.OperatorNode(
  755. new THREE.NormalNode(),
  756. texArea,
  757. THREE.OperatorNode.MUL
  758. );
  759. var displaceScale = new THREE.OperatorNode(
  760. displace,
  761. scale,
  762. THREE.OperatorNode.MUL
  763. );
  764. var blend = new THREE.OperatorNode(
  765. new THREE.PositionNode(),
  766. displaceScale,
  767. THREE.OperatorNode.ADD
  768. );
  769. var color = new THREE.Math3Node(
  770. colorB,
  771. colorA,
  772. texArea,
  773. THREE.Math3Node.MIX
  774. );
  775. mtl.color = mtl.specular = new THREE.ColorNode( 0 );
  776. mtl.emissive = color;
  777. mtl.transform = blend;
  778. // GUI
  779. addGui( 'speed', speed.number, function( val ) {
  780. speed.number = val;
  781. }, false, 0, 1 );
  782. addGui( 'scale', scale.number, function( val ) {
  783. scale.number = val;
  784. }, false, 0, 10 );
  785. addGui( 'colorA', colorA.value.getHex(), function( val ) {
  786. colorA.value.setHex( val );
  787. }, true );
  788. addGui( 'colorB', colorB.value.getHex(), function( val ) {
  789. colorB.value.setHex( val );
  790. }, true );
  791. break;
  792. case 'smoke':
  793. // MATERIAL
  794. mtl = new THREE.PhongNodeMaterial();
  795. var time = new THREE.TimerNode();
  796. var uv = new THREE.UVNode();
  797. var timeSpeedA = new THREE.OperatorNode(
  798. time,
  799. new THREE.Vector2Node( 0.3, 0.1 ),
  800. THREE.OperatorNode.MUL
  801. );
  802. var timeSpeedB = new THREE.OperatorNode(
  803. time,
  804. new THREE.Vector2Node( 0.15, 0.4 ),
  805. THREE.OperatorNode.MUL
  806. );
  807. var uvOffsetA = new THREE.OperatorNode(
  808. timeSpeedA,
  809. uv,
  810. THREE.OperatorNode.ADD
  811. );
  812. var uvOffsetB = new THREE.OperatorNode(
  813. timeSpeedB,
  814. uv,
  815. THREE.OperatorNode.ADD
  816. );
  817. var cloudA = new THREE.TextureNode( getTexture( "cloud" ), uvOffsetA );
  818. var cloudB = new THREE.TextureNode( getTexture( "cloud" ), uvOffsetB );
  819. var clouds = new THREE.OperatorNode(
  820. cloudA,
  821. cloudB,
  822. THREE.OperatorNode.ADD
  823. );
  824. mtl.environment = new THREE.ColorNode( 0xFFFFFF );
  825. mtl.alpha = clouds;
  826. // GUI
  827. addGui( 'color', mtl.environment.value.getHex(), function( val ) {
  828. mtl.environment.value.setHex( val );
  829. }, true );
  830. break;
  831. case 'camera-depth':
  832. // MATERIAL
  833. var colorA = new THREE.ColorNode( 0xFFFFFF );
  834. var colorB = new THREE.ColorNode( 0x0054df );
  835. var depth = new THREE.CameraNode( THREE.CameraNode.DEPTH );
  836. depth.near.number = 1;
  837. depth.far.number = 200;
  838. var colors = new THREE.Math3Node(
  839. colorB,
  840. colorA,
  841. depth,
  842. THREE.Math3Node.MIX
  843. );
  844. mtl = new THREE.PhongNodeMaterial();
  845. mtl.color = colors;
  846. // GUI
  847. addGui( 'near', depth.near.number, function( val ) {
  848. depth.near.number = val;
  849. }, false, 1, 1200 );
  850. addGui( 'far', depth.far.number, function( val ) {
  851. depth.far.number = val;
  852. }, false, 1, 1200 );
  853. addGui( 'nearColor', colorA.value.getHex(), function( val ) {
  854. colorA.value.setHex( val );
  855. }, true );
  856. addGui( 'farColor', colorB.value.getHex(), function( val ) {
  857. colorB.value.setHex( val );
  858. }, true );
  859. break;
  860. case 'caustic':
  861. // MATERIAL
  862. mtl = new THREE.StandardNodeMaterial();
  863. var hash2 = new THREE.FunctionNode( [
  864. "vec2 hash2(vec2 p) {",
  865. " return fract(sin(vec2(dot(p, vec2(123.4, 748.6)), dot(p, vec2(547.3, 659.3))))*5232.85324);",
  866. "}"
  867. ].join( "\n" ) );
  868. var voronoi = new THREE.FunctionNode( [
  869. // Based off of iq's described here: http://www.iquilezles.org/www/articles/voronoili
  870. "float voronoi(vec2 p, in float time) {",
  871. " vec2 n = floor(p);",
  872. " vec2 f = fract(p);",
  873. " float md = 5.0;",
  874. " vec2 m = vec2(0.0);",
  875. " for (int i = -1; i <= 1; i++) {",
  876. " for (int j = -1; j <= 1; j++) {",
  877. " vec2 g = vec2(i, j);",
  878. " vec2 o = hash2(n + g);",
  879. " o = 0.5 + 0.5 * sin(time + 5.038 * o);",
  880. " vec2 r = g + o - f;",
  881. " float d = dot(r, r);",
  882. " if (d < md) {",
  883. " md = d;",
  884. " m = n+g+o;",
  885. " }",
  886. " }",
  887. " }",
  888. " return md;",
  889. "}"
  890. ].join( "\n" ), [ hash2 ] ); // define hash2 as dependencies
  891. var voronoiLayers = new THREE.FunctionNode( [
  892. // based on https://www.shadertoy.com/view/4tXSDf
  893. "float voronoiLayers(vec2 p, in float time) {",
  894. " float v = 0.0;",
  895. " float a = 0.4;",
  896. " for (int i = 0; i < 3; i++) {",
  897. " v += voronoi(p, time) * a;",
  898. " p *= 2.0;",
  899. " a *= 0.5;",
  900. " }",
  901. " return v;",
  902. "}"
  903. ].join( "\n" ), [ voronoi ] ); // define voronoi as dependencies
  904. var time = new THREE.TimerNode();
  905. var timeScale = new THREE.FloatNode( 2 );
  906. var alpha = new THREE.FloatNode( 1 );
  907. var scale = new THREE.FloatNode( .1 );
  908. var intensity = new THREE.FloatNode( 1.5 );
  909. var color = new THREE.ColorNode( 0xFFFFFF );
  910. var colorA = new THREE.ColorNode( 0xFFFFFF );
  911. var colorB = new THREE.ColorNode( 0x0054df );
  912. var worldPos = new THREE.PositionNode( THREE.PositionNode.WORLD );
  913. var worldPosTop = new THREE.SwitchNode( worldPos, 'xz' );
  914. var worldNormal = new THREE.NormalNode( THREE.NormalNode.WORLD );
  915. var mask = new THREE.SwitchNode( worldNormal, 'y' );
  916. // clamp0at1
  917. mask = new THREE.Math1Node( mask, THREE.Math1Node.SAT );
  918. var timeOffset = new THREE.OperatorNode(
  919. time,
  920. timeScale,
  921. THREE.OperatorNode.MUL
  922. );
  923. var uvPos = new THREE.OperatorNode(
  924. worldPosTop,
  925. scale,
  926. THREE.OperatorNode.MUL
  927. );
  928. var voronoi = new THREE.FunctionCallNode( voronoiLayers );
  929. voronoi.inputs.p = uvPos;
  930. voronoi.inputs.time = timeOffset;
  931. var maskCaustic = new THREE.OperatorNode(
  932. alpha,
  933. mask,
  934. THREE.OperatorNode.MUL
  935. );
  936. var voronoiIntensity = new THREE.OperatorNode(
  937. voronoi,
  938. intensity,
  939. THREE.OperatorNode.MUL
  940. );
  941. var voronoiColors = new THREE.Math3Node(
  942. colorB,
  943. colorA,
  944. new THREE.Math1Node( voronoiIntensity, THREE.Math1Node.SAT ), // mix needs clamp
  945. THREE.Math3Node.MIX
  946. );
  947. var caustic = new THREE.Math3Node(
  948. color,
  949. voronoiColors,
  950. maskCaustic,
  951. THREE.Math3Node.MIX
  952. );
  953. var causticLights = new THREE.OperatorNode(
  954. voronoiIntensity,
  955. maskCaustic,
  956. THREE.OperatorNode.MUL
  957. );
  958. mtl.color = caustic;
  959. mtl.ambient = causticLights;
  960. // GUI
  961. addGui( 'timeScale', timeScale.number, function( val ) {
  962. timeScale.number = val;
  963. }, false, 0, 5 );
  964. addGui( 'intensity', intensity.number, function( val ) {
  965. intensity.number = val;
  966. }, false, 0, 3 );
  967. addGui( 'scale', scale.number, function( val ) {
  968. scale.number = val;
  969. }, false, 0, 1 );
  970. addGui( 'alpha', alpha.number, function( val ) {
  971. alpha.number = val;
  972. }, false, 0, 1 );
  973. addGui( 'color', color.value.getHex(), function( val ) {
  974. color.value.setHex( val );
  975. }, true );
  976. addGui( 'colorA', colorA.value.getHex(), function( val ) {
  977. colorA.value.setHex( val );
  978. }, true );
  979. addGui( 'colorB', colorB.value.getHex(), function( val ) {
  980. colorB.value.setHex( val );
  981. }, true );
  982. break;
  983. case 'soft-body':
  984. // MATERIAL
  985. move = true;
  986. mtl = new THREE.StandardNodeMaterial();
  987. var scale = new THREE.FloatNode( 2 );
  988. var colorA = new THREE.ColorNode( 0xFF6633 );
  989. var colorB = new THREE.ColorNode( 0x3366FF );
  990. var pos = new THREE.PositionNode();
  991. var posNorm = new THREE.Math1Node( pos, THREE.Math1Node.NORMALIZE );
  992. var mask = new THREE.SwitchNode( posNorm, 'y' );
  993. var velocity = new THREE.VelocityNode( mesh, {
  994. type: 'elastic',
  995. spring: .8,
  996. friction: .9
  997. } );
  998. var velocityArea = new THREE.OperatorNode(
  999. mask,
  1000. scale,
  1001. THREE.OperatorNode.MUL
  1002. );
  1003. var softVelocity = new THREE.OperatorNode(
  1004. velocity,
  1005. velocityArea,
  1006. THREE.OperatorNode.MUL
  1007. );
  1008. var softPosition = new THREE.OperatorNode(
  1009. new THREE.PositionNode(),
  1010. softVelocity,
  1011. THREE.OperatorNode.ADD
  1012. );
  1013. var colors = new THREE.Math3Node(
  1014. colorB,
  1015. colorA,
  1016. mask,
  1017. THREE.Math3Node.MIX
  1018. );
  1019. mtl.color = colors;
  1020. mtl.transform = softPosition;
  1021. // GUI
  1022. addGui( 'spring', velocity.params.spring, function( val ) {
  1023. velocity.params.spring = val;
  1024. }, false, 0, .9 );
  1025. addGui( 'friction', velocity.params.friction, function( val ) {
  1026. velocity.params.friction = val;
  1027. }, false, 0, .9 );
  1028. addGui( 'scale', scale.number, function( val ) {
  1029. scale.number = val;
  1030. }, false, 0, 3 );
  1031. addGui( 'softBody', colorA.value.getHex(), function( val ) {
  1032. colorA.value.setHex( val );
  1033. }, true );
  1034. addGui( 'rigidBody', colorB.value.getHex(), function( val ) {
  1035. colorB.value.setHex( val );
  1036. }, true );
  1037. break;
  1038. case 'plush':
  1039. // MATERIAL
  1040. mtl = new THREE.PhongNodeMaterial();
  1041. var color = new THREE.ColorNode( 0x8D8677 );
  1042. var mildness = new THREE.FloatNode( 1.6 );
  1043. var fur = new THREE.FloatNode( .5 );
  1044. var posDirection = new THREE.Math1Node( new THREE.PositionNode( THREE.PositionNode.VIEW ), THREE.Math1Node.NORMALIZE );
  1045. var norDirection = new THREE.Math1Node( new THREE.NormalNode( THREE.NormalNode.VIEW ), THREE.Math1Node.NORMALIZE );
  1046. var viewZ = new THREE.Math2Node(
  1047. posDirection,
  1048. norDirection,
  1049. THREE.Math2Node.DOT
  1050. );
  1051. // without luma correction for now
  1052. var mildnessColor = new THREE.OperatorNode(
  1053. color,
  1054. mildness,
  1055. THREE.OperatorNode.MUL
  1056. );
  1057. var furScale = new THREE.OperatorNode(
  1058. viewZ,
  1059. fur,
  1060. THREE.OperatorNode.MUL
  1061. );
  1062. mtl.color = color;
  1063. mtl.normal = new THREE.TextureNode( getTexture( "grassNormal" ) );
  1064. mtl.normalScale = furScale;
  1065. mtl.environment = mildnessColor;
  1066. mtl.environmentAlpha = new THREE.Math1Node( viewZ, THREE.Math1Node.INVERT );
  1067. mtl.shininess = new THREE.FloatNode( 0 );
  1068. // GUI
  1069. addGui( 'color', color.value.getHex(), function( val ) {
  1070. color.value.setHex( val );
  1071. }, true );
  1072. addGui( 'mildness', mildness.number, function( val ) {
  1073. mildness.number = val;
  1074. }, false, 1, 2 );
  1075. addGui( 'fur', fur.number, function( val ) {
  1076. fur.number = val;
  1077. }, false, 0, 2 );
  1078. break;
  1079. case 'skin':
  1080. case 'skin-phong':
  1081. // MATERIAL
  1082. mtl = name == 'skin' ? new THREE.StandardNodeMaterial() : new THREE.PhongNodeMaterial();
  1083. var skinColor = new THREE.ColorNode( 0xFFC495 );
  1084. var bloodColor = new THREE.ColorNode( 0x6b0602 );
  1085. var wrapLight = new THREE.FloatNode( 1.5 );
  1086. var wrapShadow = new THREE.FloatNode( 0 );
  1087. var directLight = new THREE.LightNode();
  1088. var lightLuminance = new THREE.LuminanceNode( directLight );
  1089. var lightWrap = new THREE.Math3Node(
  1090. wrapShadow,
  1091. wrapLight,
  1092. lightLuminance,
  1093. THREE.Math3Node.SMOOTHSTEP
  1094. );
  1095. var lightTransition = new THREE.OperatorNode(
  1096. lightWrap,
  1097. new THREE.ConstNode( THREE.ConstNode.PI2 ),
  1098. THREE.OperatorNode.MUL
  1099. );
  1100. var wrappedLight = new THREE.Math1Node( lightTransition, THREE.Math1Node.SIN );
  1101. var wrappedLightColor = new THREE.OperatorNode(
  1102. wrappedLight,
  1103. bloodColor,
  1104. THREE.OperatorNode.MUL
  1105. );
  1106. var bloodArea = new THREE.Math1Node( wrappedLightColor, THREE.Math1Node.SAT );
  1107. var totalLight = new THREE.OperatorNode(
  1108. directLight,
  1109. bloodArea,
  1110. THREE.OperatorNode.ADD
  1111. );
  1112. mtl.color = skinColor;
  1113. mtl.light = totalLight;
  1114. if ( name == 'skin' ) {
  1115. // StandardNodeMaterial
  1116. mtl.metalness = new THREE.FloatNode( 0 );
  1117. mtl.roughness = new THREE.FloatNode( 1 );
  1118. mtl.reflectivity = new THREE.FloatNode( 0 );
  1119. mtl.clearCoat = new THREE.FloatNode( .2 );
  1120. mtl.clearCoatRoughness = new THREE.FloatNode( .3 );
  1121. mtl.environment = new THREE.CubeTextureNode( cubemap );
  1122. } else {
  1123. // PhongNodeMaterial
  1124. mtl.specular = new THREE.ColorNode( 0x2f2e2d );
  1125. mtl.shininess = new THREE.FloatNode( 15 );
  1126. }
  1127. // GUI
  1128. addGui( 'skinColor', skinColor.value.getHex(), function( val ) {
  1129. skinColor.value.setHex( val );
  1130. }, true );
  1131. addGui( 'bloodColor', bloodColor.value.getHex(), function( val ) {
  1132. bloodColor.value.setHex( val );
  1133. }, true );
  1134. addGui( 'wrapLight', wrapLight.number, function( val ) {
  1135. wrapLight.number = val;
  1136. }, false, 0, 3 );
  1137. addGui( 'wrapShadow', wrapShadow.number, function( val ) {
  1138. wrapShadow.number = val;
  1139. }, false, - 1, 0 );
  1140. break;
  1141. case 'toon':
  1142. // MATERIAL
  1143. mtl = new THREE.PhongNodeMaterial();
  1144. var count = new THREE.FloatNode( 3.43 );
  1145. var sceneDirectLight = new THREE.LightNode();
  1146. var color = new THREE.ColorNode( 0xAABBFF );
  1147. var lineColor = new THREE.ColorNode( 0xFF0000 );
  1148. var lineSize = new THREE.FloatNode( 0.23 );
  1149. var lineInner = new THREE.FloatNode( 0 );
  1150. // CEL
  1151. var lightLuminance = new THREE.LuminanceNode( sceneDirectLight );
  1152. var preCelLight = new THREE.OperatorNode(
  1153. lightLuminance,
  1154. count,
  1155. THREE.OperatorNode.MUL
  1156. );
  1157. var celLight = new THREE.Math1Node(
  1158. preCelLight,
  1159. THREE.Math1Node.CEIL
  1160. );
  1161. var posCelLight = new THREE.OperatorNode(
  1162. celLight,
  1163. count,
  1164. THREE.OperatorNode.DIV
  1165. );
  1166. // LINE
  1167. var posDirection = new THREE.Math1Node( new THREE.PositionNode( THREE.PositionNode.VIEW ), THREE.Math1Node.NORMALIZE );
  1168. var norDirection = new THREE.Math1Node( new THREE.NormalNode( THREE.NormalNode.VIEW ), THREE.Math1Node.NORMALIZE );
  1169. var viewZ = new THREE.Math2Node(
  1170. posDirection,
  1171. norDirection,
  1172. THREE.Math2Node.DOT
  1173. );
  1174. var lineOutside = new THREE.Math1Node(
  1175. viewZ,
  1176. THREE.Math1Node.ABS
  1177. );
  1178. var line = new THREE.OperatorNode(
  1179. lineOutside,
  1180. new THREE.FloatNode( 1 ),
  1181. THREE.OperatorNode.DIV
  1182. );
  1183. var lineScaled = new THREE.Math3Node(
  1184. line,
  1185. lineSize,
  1186. lineInner,
  1187. THREE.Math3Node.SMOOTHSTEP
  1188. );
  1189. var innerContour = new THREE.Math1Node( new THREE.Math1Node( lineScaled, THREE.Math1Node.SAT ), THREE.Math1Node.INVERT );
  1190. // APPLY
  1191. mtl.color = color;
  1192. mtl.light = posCelLight;
  1193. mtl.shininess = new THREE.FloatNode( 0 );
  1194. mtl.environment = lineColor;
  1195. mtl.environmentAlpha = innerContour;
  1196. // GUI
  1197. addGui( 'color', color.value.getHex(), function( val ) {
  1198. color.value.setHex( val );
  1199. }, true );
  1200. addGui( 'lineColor', lineColor.value.getHex(), function( val ) {
  1201. lineColor.value.setHex( val );
  1202. }, true );
  1203. addGui( 'count', count.number, function( val ) {
  1204. count.number = val;
  1205. }, false, 1, 8 );
  1206. addGui( 'lineSize', lineSize.number, function( val ) {
  1207. lineSize.number = val;
  1208. }, false, 0, 1 );
  1209. addGui( 'lineInner', lineInner.number, function( val ) {
  1210. lineInner.number = val;
  1211. }, false, 0, 1 );
  1212. addGui( 'ignoreIndirectLight', false, function( val ) {
  1213. mtl.ao = val ? new THREE.FloatNode() : undefined;
  1214. mtl.build();
  1215. } );
  1216. break;
  1217. case 'custom-attribute':
  1218. // GEOMETRY
  1219. // add "position" buffer to "custom" attribute
  1220. teapot.attributes[ 'custom' ] = teapot.attributes[ 'position' ];
  1221. // MATERIAL
  1222. mtl = new THREE.PhongNodeMaterial();
  1223. mtl.color = new THREE.AttributeNode( "custom", 3 );
  1224. // or
  1225. //mtl.color = new THREE.AttributeNode( "custom", "vec3" );
  1226. break;
  1227. case 'expression':
  1228. // MATERIAL
  1229. mtl = new THREE.PhongNodeMaterial();
  1230. var speed = new THREE.FloatNode( .5 );
  1231. mtl.color = new THREE.FunctionNode( "myCustomUv + (sin(time*speed)*.5) + (position * .05)", "vec3" );
  1232. mtl.color.keywords[ "speed" ] = speed;
  1233. 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" );
  1234. mtl.transform.keywords[ "speed" ] = speed;
  1235. // add global keyword ( variable or const )
  1236. THREE.NodeLib.addKeyword( 'myCustomUv', function( builder ) {
  1237. return new THREE.ReflectNode();
  1238. } );
  1239. // GUI
  1240. addGui( 'speed', speed.number, function( val ) {
  1241. speed.number = val;
  1242. }, false, 0, 1 );
  1243. break;
  1244. case 'reserved-keywords':
  1245. // MATERIAL
  1246. mtl = new THREE.PhongNodeMaterial();
  1247. var keywordsexample = new THREE.FunctionNode( [
  1248. // use "uv" reserved keyword
  1249. "vec4 keywordsexample( sampler2D texture ) {",
  1250. " return texture2D( texture, myUV ) + vec4( position * myAlpha, 0.0 );",
  1251. "}"
  1252. ].join( "\n" ) );
  1253. // add local keyword ( const only )
  1254. keywordsexample.keywords[ "myAlpha" ] = new THREE.ConstNode( "float myAlpha .05" );
  1255. // add global keyword ( const only )
  1256. THREE.NodeLib.addKeyword( 'myUV', function( builder ) {
  1257. return new THREE.UVNode();
  1258. } );
  1259. // add global const or function
  1260. //THREE.NodeLib.add( new THREE.ConstNode("float MY_CONST .05") )
  1261. // reserved keywords
  1262. console.log( THREE.NodeLib.keywords );
  1263. // keywords conflit? use this to disable:
  1264. //blurtexture.useKeywords = false; // ( true is default )
  1265. mtl.color = new THREE.FunctionCallNode( keywordsexample, [ new THREE.TextureNode( getTexture( "brick" ) ) ] );
  1266. break;
  1267. case 'varying':
  1268. // MATERIAL
  1269. mtl = new THREE.PhongNodeMaterial();
  1270. var varying = new THREE.VarNode( "vec3" );
  1271. var setMyVar = new THREE.FunctionNode( [
  1272. "float setMyVar( vec3 pos ) {",
  1273. // set "myVar" in vertex shader in this example,
  1274. // can be used in fragment shader too or in rest of the current shader
  1275. " myVar = pos;",
  1276. // it is not accept "void" functions for now!
  1277. " return 0.;",
  1278. "}"
  1279. ].join( "\n" ) );
  1280. // add keyword
  1281. setMyVar.keywords[ "myVar" ] = varying;
  1282. var transform = new THREE.FunctionNode( "setMyVar( position * .1 ) + position", "vec3", [ setMyVar ] );
  1283. transform.keywords[ "tex" ] = new THREE.TextureNode( getTexture( "brick" ) );
  1284. mtl.transform = transform;
  1285. mtl.color = varying;
  1286. break;
  1287. case 'triangle-blur':
  1288. // MATERIAL
  1289. mtl = new THREE.PhongNodeMaterial();
  1290. var delta = new THREE.Vector2Node( .5, .25 );
  1291. var alpha = new THREE.FloatNode( 1 );
  1292. var blurtexture = new THREE.FunctionNode( [
  1293. // Reference: TriangleBlurShader.js
  1294. "vec4 blurtexture(sampler2D texture, vec2 uv, vec2 delta) {",
  1295. " vec4 color = vec4( 0.0 );",
  1296. " float total = 0.0;",
  1297. // randomize the lookup values to hide the fixed number of samples
  1298. " float offset = rand( uv );",
  1299. " for ( float t = -BLUR_ITERATIONS; t <= BLUR_ITERATIONS; t ++ ) {",
  1300. " float percent = ( t + offset - 0.5 ) / BLUR_ITERATIONS;",
  1301. " float weight = 1.0 - abs( percent );",
  1302. " color += texture2D( texture, uv + delta * percent ) * weight;",
  1303. " total += weight;",
  1304. " }",
  1305. " return color / total;",
  1306. "}"
  1307. ].join( "\n" ), [ new THREE.ConstNode( "float BLUR_ITERATIONS 10.0" ) ] );
  1308. var blurredTexture = new THREE.FunctionCallNode( blurtexture, {
  1309. texture : new THREE.TextureNode( getTexture( "brick" ) ),
  1310. delta : delta,
  1311. uv : new THREE.UVNode()
  1312. } );
  1313. var color = new THREE.Math3Node(
  1314. new THREE.TextureNode( getTexture( "brick" ) ),
  1315. blurredTexture,
  1316. alpha,
  1317. THREE.Math3Node.MIX
  1318. );
  1319. mtl.color = color;
  1320. // GUI
  1321. addGui( 'alpha', alpha.number, function( val ) {
  1322. alpha.number = val;
  1323. }, false, 0, 1 );
  1324. addGui( 'deltaX', delta.x, function( val ) {
  1325. delta.x = val;
  1326. }, false, 0, 1 );
  1327. addGui( 'deltaY', delta.x, function( val ) {
  1328. delta.y = val;
  1329. }, false, 0, 1 );
  1330. break;
  1331. case 'firefly':
  1332. // MATERIAL
  1333. mtl = new THREE.PhongNodeMaterial();
  1334. var time = new THREE.TimerNode();
  1335. var speed = new THREE.FloatNode( .5 );
  1336. var color = new THREE.ColorNode( 0x98ff00 );
  1337. var timeSpeed = new THREE.OperatorNode(
  1338. time,
  1339. speed,
  1340. THREE.OperatorNode.MUL
  1341. );
  1342. var sinCycleInSecs = new THREE.OperatorNode(
  1343. timeSpeed,
  1344. new THREE.ConstNode( THREE.ConstNode.PI2 ),
  1345. THREE.OperatorNode.MUL
  1346. );
  1347. var cycle = new THREE.Math1Node( sinCycleInSecs, THREE.Math1Node.SIN );
  1348. var cycleColor = new THREE.OperatorNode(
  1349. cycle,
  1350. color,
  1351. THREE.OperatorNode.MUL
  1352. );
  1353. var cos = new THREE.Math1Node( cycleColor, THREE.Math1Node.SIN );
  1354. mtl.color = new THREE.ColorNode( 0 );
  1355. mtl.emissive = cos;
  1356. // GUI
  1357. addGui( 'speed', speed.number, function( val ) {
  1358. speed.number = val;
  1359. }, false, 0, 3 );
  1360. break;
  1361. case 'sss':
  1362. case 'translucent':
  1363. // DISTANCE FORMULA
  1364. var modelPos = new THREE.Vector3Node();
  1365. var viewPos = new THREE.PositionNode( THREE.PositionNode.VIEW );
  1366. var cameraPosition = new THREE.CameraNode( THREE.CameraNode.POSITION );
  1367. var cameraDistance = new THREE.Math2Node(
  1368. modelPos,
  1369. cameraPosition,
  1370. THREE.Math2Node.DISTANCE
  1371. );
  1372. var viewPosZ = new THREE.SwitchNode( viewPos, 'z' );
  1373. var distance = new THREE.OperatorNode(
  1374. cameraDistance,
  1375. viewPosZ,
  1376. THREE.OperatorNode.SUB
  1377. );
  1378. var distanceRadius = new THREE.OperatorNode(
  1379. distance,
  1380. new THREE.FloatNode( 70 ),
  1381. THREE.OperatorNode.ADD
  1382. );
  1383. var objectDepth = new THREE.Math3Node(
  1384. distanceRadius,
  1385. new THREE.FloatNode( 0 ),
  1386. new THREE.FloatNode( 50 ),
  1387. THREE.Math3Node.SMOOTHSTEP
  1388. );
  1389. // RTT ( get back distance )
  1390. rtTexture = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBFormat } );
  1391. var distanceMtl = new THREE.PhongNodeMaterial();
  1392. distanceMtl.environment = objectDepth;
  1393. distanceMtl.side = THREE.BackSide;
  1394. distanceMtl.build();
  1395. rtMaterial = distanceMtl;
  1396. // MATERIAL
  1397. mtl = new THREE.StandardNodeMaterial();
  1398. var backSideDepth = new THREE.TextureNode( rtTexture, new THREE.ScreenUVNode( new THREE.ResolutionNode( renderer ) ) );
  1399. var difference = new THREE.OperatorNode(
  1400. objectDepth,
  1401. backSideDepth,
  1402. THREE.OperatorNode.SUB
  1403. );
  1404. var sss = new THREE.Math3Node(
  1405. new THREE.FloatNode( - .1 ),
  1406. new THREE.FloatNode( .5 ),
  1407. difference,
  1408. THREE.Math3Node.SMOOTHSTEP
  1409. );
  1410. var sssAlpha = new THREE.Math1Node( sss, THREE.Math1Node.SAT );
  1411. var frontColor, backColor;
  1412. if ( name == 'sss' ) {
  1413. var sssOut = new THREE.Math2Node(
  1414. objectDepth,
  1415. sssAlpha,
  1416. THREE.Math2Node.MIN
  1417. );
  1418. frontColor = new THREE.ColorNode( 0xd4cfbb );
  1419. backColor = new THREE.ColorNode( 0xd04327 );
  1420. var color = new THREE.Math3Node(
  1421. backColor,
  1422. frontColor,
  1423. sssOut,
  1424. THREE.Math3Node.MIX
  1425. );
  1426. var light = new THREE.OperatorNode(
  1427. new THREE.LightNode(),
  1428. color,
  1429. THREE.OperatorNode.ADD
  1430. );
  1431. mtl.color = frontColor;
  1432. mtl.roughness = new THREE.FloatNode( .1 );
  1433. mtl.metalness = new THREE.FloatNode( .5 );
  1434. mtl.light = light;
  1435. mtl.environment = color;
  1436. } else {
  1437. frontColor = new THREE.ColorNode( 0xd04327 );
  1438. backColor = new THREE.ColorNode( 0x1a0e14 );
  1439. var color = new THREE.Math3Node(
  1440. frontColor,
  1441. backColor,
  1442. sssAlpha,
  1443. THREE.Math3Node.MIX
  1444. );
  1445. var light = new THREE.OperatorNode(
  1446. new THREE.LightNode(),
  1447. color,
  1448. THREE.OperatorNode.ADD
  1449. );
  1450. mtl.color = new THREE.ColorNode( 0xffffff );
  1451. mtl.roughness = new THREE.FloatNode( .1 );
  1452. mtl.metalness = new THREE.FloatNode( .5 );
  1453. mtl.light = light;
  1454. mtl.environment = color;
  1455. }
  1456. // GUI
  1457. addGui( 'frontColor', frontColor.value.getHex(), function( val ) {
  1458. frontColor.value.setHex( val );
  1459. }, true );
  1460. addGui( 'backColor', backColor.value.getHex(), function( val ) {
  1461. backColor.value.setHex( val );
  1462. }, true );
  1463. addGui( 'area', sss.b.number, function( val ) {
  1464. sss.b.number = val;
  1465. }, false, 0, 1 );
  1466. break;
  1467. }
  1468. // build shader
  1469. mtl.build();
  1470. // set material
  1471. mesh.material = mtl;
  1472. }
  1473. function onWindowResize() {
  1474. var width = window.innerWidth, height = window.innerHeight;
  1475. camera.aspect = width / height;
  1476. camera.updateProjectionMatrix();
  1477. renderer.setSize( width, height );
  1478. if ( rtTexture ) rtTexture.setSize( width, height );
  1479. }
  1480. function animate() {
  1481. var delta = clock.getDelta();
  1482. if ( move ) {
  1483. var time = Date.now() * 0.005;
  1484. mesh.position.z = Math.cos( time ) * 10;
  1485. mesh.position.y = Math.sin( time ) * 10;
  1486. } else {
  1487. mesh.position.z = mesh.position.y = 0;
  1488. }
  1489. //mesh.rotation.z += .01;
  1490. // update material animation and/or gpu calcs (pre-renderer)
  1491. mesh.material.updateFrame( delta );
  1492. // render to texture for sss/translucent material only
  1493. if ( rtTexture ) {
  1494. scene.overrideMaterial = rtMaterial;
  1495. renderer.render( scene, camera, rtTexture, true );
  1496. scene.overrideMaterial = null;
  1497. }
  1498. renderer.render( scene, camera );
  1499. requestAnimationFrame( animate );
  1500. }
  1501. </script>
  1502. </body>
  1503. </html>