webgl_postprocessing_color_halftone.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. var guiTarget = this.halftonePass.uniforms;
  94. var controller = {
  95. radius: 5,
  96. rotateSampleGrid: 45,
  97. rotateC: 45,
  98. rotateM: 30,
  99. rotateY: 60,
  100. shape: 1
  101. };
  102. var onGUIChange = function() {
  103. guiTarget.radius.value = controller.radius;
  104. guiTarget.rSample.value = controller.rotateSampleGrid * (Math.PI / 180);
  105. guiTarget.rC.value = controller.rotateC * (Math.PI / 180);
  106. guiTarget.rM.value = controller.rotateM * (Math.PI / 180);
  107. guiTarget.rY.value = controller.rotateY * (Math.PI / 180);
  108. guiTarget.shape.value = controller.shape;
  109. };
  110. this.gui = new dat.GUI();
  111. this.gui.add(controller, 'shape', {'dot': 1, 'euclidean-dot': 2, 'ellipse': 3, 'line': 4}).onChange(onGUIChange);
  112. this.gui.add(controller, 'radius', 1, 50).onChange(onGUIChange);
  113. this.gui.add(controller, 'rotateSampleGrid', 0, 360).onChange(onGUIChange);
  114. this.gui.add(controller, 'rotateC', 0, 360).onChange(onGUIChange);
  115. this.gui.add(controller, 'rotateM', 0, 360).onChange(onGUIChange);
  116. this.gui.add(controller, 'rotateY', 0, 360).onChange(onGUIChange);
  117. // reset
  118. onGUIChange();
  119. }
  120. loop() {
  121. requestAnimationFrame(() => { this.loop(); });
  122. var delta = this.clock.getDelta();
  123. //this.group.rotation.y += delta * this.rotationSpeed;
  124. this.composer.render(delta);
  125. }
  126. }
  127. var example = new Example();
  128. </script>
  129. </body>
  130. </html>