webgl_postprocessing_nodes_pass.html 12 KB

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