main.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import * as THREE from 'three'
  2. import { GLTFLoader, EXRLoader, RGBELoader, OrbitControls } from 'three/examples/jsm/Addons.js'
  3. import { init, update, onClick, onKeyPress } from './game.js'
  4. import gsap from 'gsap'
  5. import { MotionPathPlugin } from 'gsap/MotionPathPlugin.js'
  6. import Howler from 'howler'
  7. gsap.registerPlugin(MotionPathPlugin)
  8. Howler.autoUnlock = true
  9. const game = {}
  10. game.entities = []
  11. game.loadingManager = new THREE.LoadingManager()
  12. game.gltfLoader = new GLTFLoader(game.loadingManager)
  13. game.textureLoader = new THREE.TextureLoader(game.loadingManager)
  14. game.exrLoader = new EXRLoader(game.loadingManager)
  15. game.rgbeLoader = new RGBELoader(game.loadingManager)
  16. game.clock = new THREE.Clock()
  17. game.camera = new THREE.PerspectiveCamera(60, 1, 0.1, 256)
  18. game.scene = new THREE.Scene()
  19. game.mousePosition = new THREE.Vector2(0, 0)
  20. game.keyboard = {}
  21. game.lookAtFocus = new THREE.Vector3(0, 0, 0)
  22. function loaded() {
  23. game.renderer = new THREE.WebGLRenderer({
  24. canvas:game.canvas,
  25. antialias: true,
  26. })
  27. game.renderer.setSize(game.canvas.offsetWidth, game.canvas.offsetHeight)
  28. game.renderer.shadowMap.enabled = true
  29. game.renderer.shadowMap.type = THREE.PCFSoftShadowMap
  30. game.orbitControls = new OrbitControls(game.camera, game.renderer.domElement)
  31. game.orbitControls.enabled = false
  32. onResize()
  33. init(game).then(() => {
  34. document.getElementById("loading").style.display = "none"
  35. game.clock.start()
  36. attachAnimationFrame()
  37. })
  38. }
  39. function onResize() {
  40. game.renderer.setSize(window.innerWidth, window.innerHeight, true)
  41. game.renderer.setPixelRatio(Math.min(2, window.devicePixelRatio))
  42. game.camera.aspect = game.canvas.offsetWidth / game.canvas.offsetHeight
  43. game.camera.updateProjectionMatrix()
  44. }
  45. function renderOneFrame() {
  46. update(game)
  47. game.orbitControls.update()
  48. if(!game.orbitControls.enabled) {
  49. game.camera.lookAt(game.lookAtFocus)
  50. }
  51. game.renderer.render(game.scene, game.camera)
  52. }
  53. function attachAnimationFrame() {
  54. renderOneFrame()
  55. window.requestAnimationFrame(attachAnimationFrame)
  56. }
  57. export default async function main(canvasElement) {
  58. game.canvas = canvasElement
  59. document.addEventListener("DOMContentLoaded", loaded)
  60. window.addEventListener('resize', onResize)
  61. game.canvas.addEventListener('mousemove', event => {
  62. game.mousePosition.x = 2 * (event.clientX / game.canvas.offsetWidth - 0.5)
  63. game.mousePosition.y = -2 * (event.clientY / game.canvas.offsetHeight - 0.5)
  64. })
  65. game.canvas.addEventListener('touchmove', event => {
  66. if(event.touches.length == 1) {
  67. game.mousePosition.x = 2 * ( event.touches[0].clientX / game.canvas.offsetWidth - 0.5)
  68. game.mousePosition.y = -2 * ( event.touches[0].clientY / game.canvas.offsetHeight - 0.5)
  69. }
  70. })
  71. game.canvas.addEventListener('touchstart', event => {
  72. if(event.touches.length == 1) {
  73. game.mousePosition.x = 2 * ( event.touches[0].clientX / game.canvas.offsetWidth - 0.5)
  74. game.mousePosition.y = -2 * ( event.touches[0].clientY / game.canvas.offsetHeight - 0.5)
  75. }
  76. })
  77. game.canvas.addEventListener('mousedown', event => {
  78. game.mousePosition.x = 2 * (event.clientX / game.canvas.offsetWidth - 0.5)
  79. game.mousePosition.y = -2 * (event.clientY / game.canvas.offsetHeight - 0.5)
  80. })
  81. game.canvas.addEventListener('click', event => {
  82. onClick()
  83. })
  84. window.addEventListener('keydown', event => {
  85. const lastKeyState = game.keyboard[event.code]
  86. game.keyboard[event.code] = true
  87. if (!lastKeyState) {
  88. onKeyPress(event.code)
  89. }
  90. })
  91. window.addEventListener('keyup', event => {
  92. game.keyboard[event.code] = false
  93. })
  94. }