webgl_postprocessing_color_halftone.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl</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. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. .info {
  15. position: absolute;
  16. background-color: black;
  17. opacity: 0.8;
  18. color: white;
  19. text-align: center;
  20. top: 0px;
  21. width: 100%;
  22. }
  23. .info a {
  24. color: #00ffff;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <script src="../build/three.js"></script>
  30. <script src="js/postprocessing/EffectComposer.js"></script>
  31. <script src="js/postprocessing/RenderPass.js"></script>
  32. <script src="js/postprocessing/ShaderPass.js"></script>
  33. <script src="js/postprocessing/HalftonePass.js"></script>
  34. <script src="js/shaders/CopyShader.js"></script>
  35. <script src="js/shaders/HalftoneShader.js"></script>
  36. <script src="js/shaders/DepthLimitedBlurShader.js"></script>
  37. <script src="js/shaders/UnpackDepthRGBAShader.js"></script>
  38. <script src="js/Detector.js"></script>
  39. <script src='js/libs/dat.gui.min.js'></script>
  40. <div id="info">
  41. <a href="http://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - Halftone shader by @meatbags
  42. </div>
  43. <script>
  44. if (!Detector.webgl) {
  45. Detector.addGetWebGLMessage();
  46. }
  47. class Example {
  48. constructor() {
  49. this.wrapper = document.createElement('div');
  50. document.body.appendChild(this.wrapper);
  51. this.clock = new THREE.Clock();
  52. this.width = window.innerWidth;
  53. this.height = window.innerHeight;
  54. this.camera = new THREE.PerspectiveCamera(75, this.width / this.height, 1, 1000);
  55. this.camera.position.z = 20;
  56. this.rotationSpeed = Math.PI / 8;
  57. this.initScene();
  58. this.initRender();
  59. this.initGUI();
  60. this.loop();
  61. }
  62. initScene() {
  63. var geo = new THREE.BoxBufferGeometry(1, 1, 1);
  64. var mat = new THREE.MeshBasicMaterial({});
  65. this.group = new THREE.Group();
  66. for (var i=0, len=20; i<len; ++i) {
  67. var mesh = new THREE.Mesh(geo.clone(), mat.clone());
  68. mesh.position.x = Math.random() * 10 - 5;
  69. mesh.position.y = Math.random() * 10 - 5;
  70. mesh.position.z = Math.random() * 10 - 5;
  71. this.group.add(mesh);
  72. }
  73. this.scene = new THREE.Scene();
  74. this.scene.background = new THREE.Color(0x111111);
  75. this.scene.add(this.group);
  76. }
  77. initRender() {
  78. // 3js renderer
  79. this.renderer = new THREE.WebGLRenderer({antialias: true});
  80. this.renderer.setPixelRatio(window.devicePixelRatio);
  81. this.renderer.setSize(window.innerWidth, window.innerHeight);
  82. // effect composer
  83. this.composer = new THREE.EffectComposer(this.renderer);
  84. this.renderPass = new THREE.RenderPass(this.scene, this.camera);
  85. this.halftonePass = new THREE.HalftonePass(this.width, this.height);
  86. this.halftonePass.renderToScreen = true;
  87. this.composer.addPass(this.renderPass);
  88. this.composer.addPass(this.halftonePass);
  89. // add to doc
  90. this.wrapper.appendChild(this.renderer.domElement);
  91. }
  92. initGUI() {
  93. this.controller = {
  94. size: 10
  95. };
  96. this.onGUIChange = function() {
  97. for (var key in this.controller) {
  98. if (this.halftonePass.uniforms.hasOwnProperty(key)) {
  99. this.halftonePass.uniforms[key] = this.controller[key];
  100. }
  101. }
  102. }
  103. this.gui = new dat.GUI();
  104. this.gui.add(this.controller, 'size', 1, 50).onChange(this.onGUIChange);
  105. }
  106. loop() {
  107. requestAnimationFrame(() => { this.loop(); });
  108. var delta = this.clock.getDelta();
  109. this.group.rotation.y += delta * this.rotationSpeed;
  110. this.composer.render(delta);
  111. }
  112. }
  113. var example = new Example();
  114. </script>
  115. </body>
  116. </html>