main.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
  2. import {OrbitControls} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/OrbitControls.js';
  3. import {EffectComposer} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/EffectComposer.js';
  4. import {RenderPass} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/RenderPass.js';
  5. import {UnrealBloomPass} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/UnrealBloomPass.js';
  6. import {GlitchPass} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/GlitchPass.js';
  7. import {ShaderPass} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/ShaderPass.js';
  8. import {SSAOPass} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/SSAOPass.js';
  9. import {LuminosityShader} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/shaders/LuminosityShader.js';
  10. const _VS = `
  11. varying vec2 vUv;
  12. void main() {
  13. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  14. vUv = uv;
  15. }
  16. `;
  17. const _FS = `
  18. #include <common>
  19. uniform sampler2D tDiffuse;
  20. varying vec2 vUv;
  21. void main() {
  22. vec4 diffuse = texture2D(tDiffuse, vUv);
  23. gl_FragColor = (diffuse - 0.5) * 4.0 + 0.5;
  24. }
  25. `;
  26. const CrapShader = {
  27. uniforms: {
  28. tDiffuse: null,
  29. },
  30. vertexShader: _VS,
  31. fragmentShader: _FS,
  32. };
  33. class PostProcessingDemo {
  34. constructor() {
  35. this._Initialize();
  36. }
  37. _Initialize() {
  38. this._threejs = new THREE.WebGLRenderer({
  39. antialias: true,
  40. });
  41. this._threejs.shadowMap.enabled = true;
  42. this._threejs.shadowMap.type = THREE.PCFSoftShadowMap;
  43. this._threejs.setPixelRatio(window.devicePixelRatio);
  44. this._threejs.setSize(window.innerWidth, window.innerHeight);
  45. document.body.appendChild(this._threejs.domElement);
  46. window.addEventListener('resize', () => {
  47. this._OnWindowResize();
  48. }, false);
  49. const fov = 60;
  50. const aspect = 1920 / 1080;
  51. const near = 1.0;
  52. const far = 500.0;
  53. this._camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  54. this._camera.position.set(75, 20, 0);
  55. this._scene = new THREE.Scene();
  56. this._composer = new EffectComposer(this._threejs);
  57. this._composer.addPass(new RenderPass(this._scene, this._camera));
  58. this._composer.addPass(new UnrealBloomPass({x: 1024, y: 1024}, 2.0, 0.0, 0.75));
  59. this._composer.addPass(new GlitchPass());
  60. this._composer.addPass(new ShaderPass(CrapShader));
  61. let light = new THREE.DirectionalLight(0xFFFFFF, 1.0);
  62. light.position.set(20, 100, 10);
  63. light.target.position.set(0, 0, 0);
  64. light.castShadow = true;
  65. light.shadow.bias = -0.001;
  66. light.shadow.mapSize.width = 2048;
  67. light.shadow.mapSize.height = 2048;
  68. light.shadow.camera.near = 0.1;
  69. light.shadow.camera.far = 500.0;
  70. light.shadow.camera.near = 0.5;
  71. light.shadow.camera.far = 500.0;
  72. light.shadow.camera.left = 100;
  73. light.shadow.camera.right = -100;
  74. light.shadow.camera.top = 100;
  75. light.shadow.camera.bottom = -100;
  76. this._scene.add(light);
  77. light = new THREE.AmbientLight(0x101010);
  78. this._scene.add(light);
  79. const controls = new OrbitControls(
  80. this._camera, this._threejs.domElement);
  81. controls.target.set(0, 20, 0);
  82. controls.update();
  83. const loader = new THREE.CubeTextureLoader();
  84. const texture = loader.load([
  85. './resources/posx.jpg',
  86. './resources/negx.jpg',
  87. './resources/posy.jpg',
  88. './resources/negy.jpg',
  89. './resources/posz.jpg',
  90. './resources/negz.jpg',
  91. ]);
  92. this._scene.background = texture;
  93. const plane = new THREE.Mesh(
  94. new THREE.PlaneGeometry(1000, 1000, 10, 10),
  95. new THREE.MeshStandardMaterial({
  96. color: 0x808080,
  97. }));
  98. plane.castShadow = false;
  99. plane.receiveShadow = true;
  100. plane.rotation.x = -Math.PI / 2;
  101. this._scene.add(plane);
  102. const knot = new THREE.Mesh(
  103. new THREE.TorusKnotGeometry(5, 1.5, 100, 16),
  104. new THREE.MeshStandardMaterial({
  105. color: 0xFFFFFF,
  106. }));
  107. knot.position.set(0, 15, 0);
  108. knot.castShadow = true;
  109. knot.receiveShadow = true;
  110. this._scene.add(knot);
  111. this._knot = knot;
  112. for (let x = -8; x < 8; x++) {
  113. for (let y = -8; y < 8; y++) {
  114. const box = new THREE.Mesh(
  115. new THREE.BoxGeometry(2, 10, 2),
  116. new THREE.MeshStandardMaterial({
  117. color: 0x808080,
  118. }));
  119. box.position.set(Math.random() + x * 20, Math.random() * 4.0 + 5.0, Math.random() + y * 20);
  120. box.castShadow = true;
  121. box.receiveShadow = true;
  122. this._scene.add(box);
  123. }
  124. }
  125. this._RAF();
  126. }
  127. _OnWindowResize() {
  128. this._camera.aspect = window.innerWidth / window.innerHeight;
  129. this._camera.updateProjectionMatrix();
  130. this._threejs.setSize(window.innerWidth, window.innerHeight);
  131. this._composer.setSize(window.innerWidth, window.innerHeight);
  132. }
  133. _RAF() {
  134. requestAnimationFrame(() => {
  135. // this._knot.rotateY(0.01);
  136. // this._knot.rotateZ(0.005);
  137. // this._threejs.render(this._scene, this._camera);
  138. this._composer.render();
  139. this._RAF();
  140. });
  141. }
  142. }
  143. let _APP = null;
  144. window.addEventListener('DOMContentLoaded', () => {
  145. _APP = new PostProcessingDemo();
  146. });