webgl_materials_nodes.html 48 KB

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