webgl_postprocessing_nodes_pass.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing with nodes</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Node-Based Post-Processing
  12. </div>
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  26. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  27. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  28. import { NodePass } from './jsm/nodes/postprocessing/NodePass.js';
  29. import * as Nodes from './jsm/nodes/Nodes.js';
  30. let camera, scene, renderer, composer;
  31. let object, light, nodepass;
  32. let gui;
  33. const clock = new THREE.Clock();
  34. const frame = new Nodes.NodeFrame();
  35. const param = { example: 'color-adjustment' };
  36. const textureLoader = new THREE.TextureLoader();
  37. const lensflare2 = textureLoader.load( 'textures/lensflare/lensflare0.png' );
  38. lensflare2.wrapS = lensflare2.wrapT = THREE.RepeatWrapping;
  39. const decalNormal = textureLoader.load( 'textures/decal/decal-normal.jpg' );
  40. decalNormal.wrapS = decalNormal.wrapT = THREE.RepeatWrapping;
  41. init();
  42. animate();
  43. function clearGui() {
  44. if ( gui ) gui.destroy();
  45. gui = new GUI();
  46. gui.add( param, 'example', {
  47. 'basic / color-adjustment': 'color-adjustment',
  48. 'basic / blends': 'blends',
  49. 'basic / fade': 'fade',
  50. 'basic / invert': 'invert',
  51. 'basic / blur': 'blur',
  52. 'adv / saturation': 'saturation',
  53. 'adv / refraction': 'refraction',
  54. 'adv / mosaic': 'mosaic'
  55. } ).onFinishChange( function () {
  56. updateMaterial();
  57. } );
  58. gui.open();
  59. }
  60. function addGui( name, value, callback, isColor, min, max ) {
  61. let node;
  62. param[ name ] = value;
  63. if ( isColor ) {
  64. node = gui.addColor( param, name ).onChange( function () {
  65. callback( param[ name ] );
  66. } );
  67. } else if ( typeof value == 'object' ) {
  68. param[ name ] = value[ Object.keys( value )[ 0 ] ];
  69. node = gui.add( param, name, value ).onChange( function () {
  70. callback( param[ name ] );
  71. } );
  72. } else {
  73. node = gui.add( param, name, min, max ).onChange( function () {
  74. callback( param[ name ] );
  75. } );
  76. }
  77. return node;
  78. }
  79. function updateMaterial() {
  80. const name = param.example;
  81. let screen, fade, scale;
  82. clearGui();
  83. switch ( name ) {
  84. case 'color-adjustment':
  85. screen = new Nodes.ScreenNode();
  86. const hue = new Nodes.FloatNode();
  87. const sataturation = new Nodes.FloatNode( 1 );
  88. const vibrance = new Nodes.FloatNode();
  89. const brightness = new Nodes.FloatNode( 0 );
  90. const contrast = new Nodes.FloatNode( 1 );
  91. const hueNode = new Nodes.ColorAdjustmentNode( screen, hue, Nodes.ColorAdjustmentNode.HUE );
  92. const satNode = new Nodes.ColorAdjustmentNode( hueNode, sataturation, Nodes.ColorAdjustmentNode.SATURATION );
  93. const vibranceNode = new Nodes.ColorAdjustmentNode( satNode, vibrance, Nodes.ColorAdjustmentNode.VIBRANCE );
  94. const brightnessNode = new Nodes.ColorAdjustmentNode( vibranceNode, brightness, Nodes.ColorAdjustmentNode.BRIGHTNESS );
  95. const contrastNode = new Nodes.ColorAdjustmentNode( brightnessNode, contrast, Nodes.ColorAdjustmentNode.CONTRAST );
  96. nodepass.input = contrastNode;
  97. // GUI
  98. addGui( 'hue', hue.value, function ( val ) {
  99. hue.value = val;
  100. }, false, 0, Math.PI * 2 );
  101. addGui( 'saturation', sataturation.value, function ( val ) {
  102. sataturation.value = val;
  103. }, false, 0, 2 );
  104. addGui( 'vibrance', vibrance.value, function ( val ) {
  105. vibrance.value = val;
  106. }, false, - 1, 1 );
  107. addGui( 'brightness', brightness.value, function ( val ) {
  108. brightness.value = val;
  109. }, false, 0, .5 );
  110. addGui( 'contrast', contrast.value, function ( val ) {
  111. contrast.value = val;
  112. }, false, 0, 2 );
  113. break;
  114. case 'fade':
  115. // PASS
  116. const color = new Nodes.ColorNode( 0xFFFFFF );
  117. const percent = new Nodes.FloatNode( .5 );
  118. fade = new Nodes.MathNode(
  119. new Nodes.ScreenNode(),
  120. color,
  121. percent,
  122. Nodes.MathNode.MIX
  123. );
  124. nodepass.input = fade;
  125. // GUI
  126. addGui( 'color', color.value.getHex(), function ( val ) {
  127. color.value.setHex( val );
  128. }, true );
  129. addGui( 'fade', percent.value, function ( val ) {
  130. percent.value = val;
  131. }, false, 0, 1 );
  132. break;
  133. case 'invert':
  134. // PASS
  135. const alpha = new Nodes.FloatNode( 1 );
  136. screen = new Nodes.ScreenNode();
  137. const inverted = new Nodes.MathNode( screen, Nodes.MathNode.INVERT );
  138. fade = new Nodes.MathNode(
  139. screen,
  140. inverted,
  141. alpha,
  142. Nodes.MathNode.MIX
  143. );
  144. nodepass.input = fade;
  145. // GUI
  146. addGui( 'alpha', alpha.value, function ( val ) {
  147. alpha.value = val;
  148. }, false, 0, 1 );
  149. break;
  150. case 'blends':
  151. // PASS
  152. const multiply = new Nodes.OperatorNode(
  153. new Nodes.ScreenNode(),
  154. new Nodes.TextureNode( lensflare2 ),
  155. Nodes.OperatorNode.ADD
  156. );
  157. nodepass.input = multiply;
  158. // GUI
  159. addGui( 'blend', {
  160. 'addition': Nodes.OperatorNode.ADD,
  161. 'subtract': Nodes.OperatorNode.SUB,
  162. 'multiply': Nodes.OperatorNode.MUL,
  163. 'division': Nodes.OperatorNode.DIV
  164. }, function ( val ) {
  165. multiply.op = val;
  166. nodepass.needsUpdate = true;
  167. } );
  168. break;
  169. case 'saturation':
  170. // PASS
  171. screen = new Nodes.ScreenNode();
  172. const sat = new Nodes.FloatNode( 0 );
  173. const satrgb = new Nodes.FunctionNode( [
  174. "vec3 satrgb( vec3 rgb, float adjustment ) {",
  175. // include luminance function from LuminanceNode
  176. " vec3 intensity = vec3( luminance( rgb ) );",
  177. " return mix( intensity, rgb, adjustment );",
  178. "}"
  179. ].join( "\n" ), [ Nodes.LuminanceNode.Nodes.luminance ] );
  180. const saturation = new Nodes.FunctionCallNode( satrgb );
  181. saturation.inputs.rgb = screen;
  182. saturation.inputs.adjustment = sat;
  183. nodepass.input = saturation;
  184. // GUI
  185. addGui( 'saturation', sat.value, function ( val ) {
  186. sat.value = val;
  187. }, false, 0, 2 );
  188. break;
  189. case 'refraction':
  190. // PASS
  191. const normal = new Nodes.TextureNode( decalNormal );
  192. const normalXY = new Nodes.SwitchNode( normal, 'xy' );
  193. scale = new Nodes.FloatNode( .5 );
  194. const normalXYFlip = new Nodes.MathNode(
  195. normalXY,
  196. Nodes.MathNode.INVERT
  197. );
  198. const offsetNormal = new Nodes.OperatorNode(
  199. normalXYFlip,
  200. new Nodes.FloatNode( .5 ),
  201. Nodes.OperatorNode.ADD
  202. );
  203. const scaleTexture = new Nodes.OperatorNode(
  204. new Nodes.SwitchNode( normal, 'z' ),
  205. offsetNormal,
  206. Nodes.OperatorNode.MUL
  207. );
  208. const scaleNormal = new Nodes.MathNode(
  209. new Nodes.FloatNode( 1 ),
  210. scaleTexture,
  211. scale,
  212. Nodes.MathNode.MIX
  213. );
  214. const offsetCoord = new Nodes.OperatorNode(
  215. new Nodes.UVNode(),
  216. scaleNormal,
  217. Nodes.OperatorNode.MUL
  218. );
  219. screen = new Nodes.ScreenNode( offsetCoord );
  220. nodepass.input = screen;
  221. // GUI
  222. addGui( 'scale', scale.value, function ( val ) {
  223. scale.value = val;
  224. }, false, 0, 1 );
  225. addGui( 'invert', false, function ( val ) {
  226. offsetNormal.a = val ? normalXYFlip : normalXY;
  227. nodepass.needsUpdate = true;
  228. } );
  229. break;
  230. case 'mosaic':
  231. // PASS
  232. scale = new Nodes.FloatNode( 128 );
  233. fade = new Nodes.FloatNode( 1 );
  234. const uv = new Nodes.UVNode();
  235. const blocks = new Nodes.OperatorNode(
  236. uv,
  237. scale,
  238. Nodes.OperatorNode.MUL
  239. );
  240. const blocksSize = new Nodes.MathNode(
  241. blocks,
  242. Nodes.MathNode.FLOOR
  243. );
  244. const mosaicUV = new Nodes.OperatorNode(
  245. blocksSize,
  246. scale,
  247. Nodes.OperatorNode.DIV
  248. );
  249. const fadeScreen = new Nodes.MathNode(
  250. uv,
  251. mosaicUV,
  252. fade,
  253. Nodes.MathNode.MIX
  254. );
  255. nodepass.input = new Nodes.ScreenNode( fadeScreen );
  256. // GUI
  257. addGui( 'scale', scale.value, function ( val ) {
  258. scale.value = val;
  259. }, false, 16, 1024 );
  260. addGui( 'fade', fade.value, function ( val ) {
  261. fade.value = val;
  262. }, false, 0, 1 );
  263. addGui( 'mask', false, function ( val ) {
  264. fadeScreen.c = val ? new Nodes.TextureNode( lensflare2 ) : fade;
  265. nodepass.needsUpdate = true;
  266. } );
  267. break;
  268. case 'blur':
  269. // PASS
  270. const size = renderer.getDrawingBufferSize( new THREE.Vector2() );
  271. const blurScreen = new Nodes.BlurNode( new Nodes.ScreenNode() );
  272. blurScreen.size = new THREE.Vector2( size.width, size.height );
  273. nodepass.input = blurScreen;
  274. // GUI
  275. addGui( 'blurX', blurScreen.radius.x, function ( val ) {
  276. blurScreen.radius.x = val;
  277. }, false, 0, 15 );
  278. addGui( 'blurY', blurScreen.radius.y, function ( val ) {
  279. blurScreen.radius.y = val;
  280. }, false, 0, 15 );
  281. break;
  282. }
  283. nodepass.needsUpdate = true;
  284. // test serialization
  285. /*
  286. let library = {};
  287. library[ lensflare2.uuid ] = lensflare2;
  288. library[ decalNormal.uuid ] = decalNormal;
  289. let json = nodepass.toJSON();
  290. nodepass.input = new Nodes.NodeMaterialLoader( null, library ).parse( json ).value;
  291. */
  292. }
  293. function init() {
  294. renderer = new THREE.WebGLRenderer();
  295. renderer.setPixelRatio( window.devicePixelRatio );
  296. renderer.setSize( window.innerWidth, window.innerHeight );
  297. document.body.appendChild( renderer.domElement );
  298. //
  299. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  300. camera.position.z = 400;
  301. scene = new THREE.Scene();
  302. scene.fog = new THREE.Fog( 0x0066FF, 1, 1000 );
  303. object = new THREE.Object3D();
  304. scene.add( object );
  305. const geometry = new THREE.SphereGeometry( 1, 4, 4 );
  306. for ( let i = 0; i < 100; i ++ ) {
  307. const material = new THREE.MeshPhongMaterial( { color: 0x888888 + ( Math.random() * 0x888888 ), flatShading: true } );
  308. const mesh = new THREE.Mesh( geometry, material );
  309. mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  310. mesh.position.multiplyScalar( Math.random() * 400 );
  311. mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  312. mesh.scale.x = mesh.scale.y = mesh.scale.z = 10 + ( Math.random() * 40 );
  313. object.add( mesh );
  314. }
  315. scene.add( new THREE.AmbientLight( 0x999999 ) );
  316. light = new THREE.DirectionalLight( 0xffffff );
  317. light.position.set( 1, 1, 1 );
  318. scene.add( light );
  319. // postprocessing
  320. composer = new EffectComposer( renderer );
  321. composer.addPass( new RenderPass( scene, camera ) );
  322. nodepass = new NodePass();
  323. composer.addPass( nodepass );
  324. //
  325. updateMaterial();
  326. window.addEventListener( 'resize', onWindowResize );
  327. }
  328. function onWindowResize() {
  329. camera.aspect = window.innerWidth / window.innerHeight;
  330. camera.updateProjectionMatrix();
  331. renderer.setSize( window.innerWidth, window.innerHeight );
  332. composer.setSize( window.innerWidth, window.innerHeight );
  333. }
  334. function animate() {
  335. requestAnimationFrame( animate );
  336. const delta = clock.getDelta();
  337. object.rotation.x += 0.005;
  338. object.rotation.y += 0.01;
  339. frame.update( delta ).updateNode( nodepass.material );
  340. composer.render();
  341. }
  342. </script>
  343. </body>
  344. </html>