webgl_postprocessing_nodes_pass.html 12 KB

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