graphics.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 {SSAOPass} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/postprocessing/SSAOPass.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. SSAOPass: SSAOPass,
  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(16, 2, 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(10, 20, 10);
  51. //light.target.position.set(0, 0, 0);
  52. light.castShadow = true;
  53. light.shadow.bias = -0.005;
  54. light.shadow.mapSize.set(4096, 4096);
  55. light.shadow.camera.near = 0.01;
  56. light.shadow.camera.far = 50;
  57. light.shadow.camera.left = 50;
  58. light.shadow.camera.right = -50;
  59. light.shadow.camera.top = 50;
  60. light.shadow.camera.bottom = -50;
  61. light.shadow.radius = 1;
  62. this._scene.add(light);
  63. // cleanup
  64. this._shadowLight = light;
  65. light = new THREE.DirectionalLight(0x404040, 1, 100);
  66. light.position.set(-100, 100, -100);
  67. light.target.position.set(0, 0, 0);
  68. light.castShadow = false;
  69. this._scene.add(light);
  70. light = new THREE.DirectionalLight(0x404040, 1, 100);
  71. light.position.set(100, 100, -100);
  72. light.target.position.set(0, 0, 0);
  73. light.castShadow = false;
  74. this._scene.add(light);
  75. }
  76. AddPostFX(passClass, params) {
  77. const pass = new passClass(this._scene, this._camera);
  78. for (const k in params) {
  79. pass[k] = params[k];
  80. }
  81. this._composer.addPass(pass);
  82. return pass;
  83. }
  84. _OnWindowResize() {
  85. this._camera.aspect = window.innerWidth / window.innerHeight;
  86. this._camera.updateProjectionMatrix();
  87. this._threejs.setSize(window.innerWidth, window.innerHeight);
  88. this._composer.setSize(window.innerWidth, window.innerHeight);
  89. }
  90. get Scene() {
  91. return this._scene;
  92. }
  93. Render(timeInSeconds) {
  94. this._composer.render();
  95. this._stats.update();
  96. }
  97. }
  98. };
  99. })();