webgl_postprocessing_nodes_pass.html 12 KB

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