webgl_postprocessing_nodes_pass.html 12 KB

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