graphics.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
  2. import Stats from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/libs/stats.module.js';
  3. import {WEBGL} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/WebGL.js';
  4. import {EffectComposer} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/EffectComposer.js';
  5. import {RenderPass} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/RenderPass.js';
  6. import {GlitchPass } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/GlitchPass.js';
  7. import {UnrealBloomPass} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/UnrealBloomPass.js';
  8. export const graphics = (function() {
  9. return {
  10. PostFX: {
  11. UnrealBloomPass: UnrealBloomPass,
  12. GlitchPass: GlitchPass,
  13. },
  14. Graphics: class {
  15. constructor(game) {
  16. }
  17. Initialize() {
  18. if (!WEBGL.isWebGL2Available()) {
  19. return false;
  20. }
  21. this._threejs = new THREE.WebGLRenderer({
  22. antialias: true,
  23. });
  24. this._threejs.shadowMap.enabled = true;
  25. this._threejs.shadowMap.type = THREE.PCFSoftShadowMap;
  26. this._threejs.setPixelRatio(window.devicePixelRatio);
  27. this._threejs.setSize(window.innerWidth, window.innerHeight);
  28. const target = document.getElementById('target');
  29. target.appendChild(this._threejs.domElement);
  30. this._stats = new Stats();
  31. target.appendChild(this._stats.dom);
  32. window.addEventListener('resize', () => {
  33. this._OnWindowResize();
  34. }, false);
  35. const fov = 60;
  36. const aspect = 1920 / 1080;
  37. const near = 1.0;
  38. const far = 1000.0;
  39. this._camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  40. this._camera.position.set(75, 20, 0);
  41. this._scene = new THREE.Scene();
  42. this._CreateLights();
  43. const composer = new EffectComposer(this._threejs);
  44. this._composer = composer;
  45. this._composer.addPass(new RenderPass(this._scene, this._camera));
  46. return true;
  47. }
  48. _CreateLights() {
  49. let light = new THREE.DirectionalLight(0xFFFFFF, 1, 100);
  50. light.position.set(100, 100, 100);
  51. light.target.position.set(0, 0, 0);
  52. light.castShadow = true;
  53. light.shadowCameraVisible = true;
  54. light.shadow.bias = -0.01;
  55. light.shadow.mapSize.width = 2048;
  56. light.shadow.mapSize.height = 2048;
  57. light.shadow.camera.near = 1.0;
  58. light.shadow.camera.far = 500;
  59. light.shadow.camera.left = 200;
  60. light.shadow.camera.right = -200;
  61. light.shadow.camera.top = 200;
  62. light.shadow.camera.bottom = -200;
  63. this._scene.add(light);
  64. light = new THREE.DirectionalLight(0x404040, 1, 100);
  65. light.position.set(-100, 100, -100);
  66. light.target.position.set(0, 0, 0);
  67. light.castShadow = false;
  68. this._scene.add(light);
  69. light = new THREE.DirectionalLight(0x404040, 1, 100);
  70. light.position.set(100, 100, -100);
  71. light.target.position.set(0, 0, 0);
  72. light.castShadow = false;
  73. this._scene.add(light);
  74. }
  75. AddPostFX(passClass, params) {
  76. const pass = new passClass();
  77. for (const k in params) {
  78. pass[k] = params[k];
  79. }
  80. this._composer.addPass(pass);
  81. return pass;
  82. }
  83. _OnWindowResize() {
  84. this._camera.aspect = window.innerWidth / window.innerHeight;
  85. this._camera.updateProjectionMatrix();
  86. this._threejs.setSize(window.innerWidth, window.innerHeight);
  87. this._composer.setSize(window.innerWidth, window.innerHeight);
  88. }
  89. get Scene() {
  90. return this._scene;
  91. }
  92. Render(timeInSeconds) {
  93. this._composer.render();
  94. this._stats.update();
  95. }
  96. }
  97. };
  98. })();