Advertisement
Guest User

Untitled

a guest
Aug 29th, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import "./style.css"
  2. import * as THREE from "three"
  3. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls"
  4. import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader"
  5. import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader"
  6. import WebGL from "three/examples/jsm/capabilities/WebGL"
  7. import { gsap } from "gsap"
  8.  
  9. let scene,
  10.     camera,
  11.     renderer,
  12.     analyzer,
  13.     controls,
  14.     canvas,
  15.     tick = 0,
  16.     clock = new THREE.Clock(),
  17.     composer
  18.  
  19. const sizes = {
  20.     width: window.innerWidth,
  21.     height: window.innerHeight,
  22. }
  23.  
  24. function _init() {
  25.     _createScene()
  26.     _createCamera()
  27.     _createRenderer()
  28.     _addLoaders()
  29.     _createControls()
  30.     _compatibility()
  31.  
  32.     const audioBtn = document.querySelector("#audio-btn")
  33.     audioBtn.addEventListener(
  34.         "click",
  35.         () => {
  36.             const tl = new gsap.timeline({
  37.                 onComplete: () => {
  38.                     _loadMusic()
  39.                 },
  40.             })
  41.  
  42.             tl.set("#audio-btn-icon", { transition: "none" }).to("#audio-btn-label", { "--clip": 1, duration: 0.6 }).to("#audio-btn-icon", { rotation: 140, scale: 0.8, duration: 0.5 }, "<0.05").to("#audio-btn-icon", { opacity: 0, duration: 0.65 }, "<").to(audioBtn, { width: 66, ease: "back.inOut(2)", duration: 0.9 }, "<0.2")
  43.         },
  44.         { once: true }
  45.     )
  46.  
  47.     renderer.setAnimationLoop(() => {
  48.         _update()
  49.         _render()
  50.     })
  51. }
  52.  
  53. function destroy() {
  54.     renderer.dispose()
  55.     _removeListeners()
  56. }
  57.  
  58. function _update() {
  59.     const elapsed = clock.getElapsedTime()
  60.  
  61.     if (!!analyzer) {
  62.         const d = analyzer.getFrequencyData()
  63.  
  64.         const o = d.reduce((prev, curr) => prev + curr, 0)
  65.  
  66.         tick += 0.01
  67.         camera.position.x = Math.sin(tick * 0.63) * 2.7 * 0
  68.         camera.position.y = Math.sin(tick * 0.84) * 2.15 * 0
  69.         camera.position.z = Math.cos(tick * 0.39) * 4.5 * 0
  70.         camera.lookAt(scene.position)
  71.     }
  72. }
  73.  
  74. function _createScene() {
  75.     canvas = document.querySelector("canvas.webgl")
  76.     scene = new THREE.Scene()
  77.  
  78.     const ambientLight = new THREE.AmbientLight(0xffffff, 0.8)
  79.     scene.add(ambientLight)
  80.  
  81.     const directionalLight = new THREE.DirectionalLight(0xffffff, 0.6)
  82.     directionalLight.castShadow = true
  83.     directionalLight.shadow.mapSize.set(1024, 1024)
  84.     directionalLight.shadow.camera.far = 15
  85.     directionalLight.shadow.camera.left = -7
  86.     directionalLight.shadow.camera.top = 7
  87.     directionalLight.shadow.camera.right = 7
  88.     directionalLight.shadow.camera.bottom = -7
  89.     directionalLight.position.set(-5, 5, 0)
  90.     scene.add(directionalLight)
  91. }
  92.  
  93. function _createCamera() {
  94.     camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 100)
  95.     camera.position.set(2, 10, 20)
  96.     scene.add(camera)
  97. }
  98.  
  99. function _createRenderer() {
  100.     renderer = new THREE.WebGLRenderer({
  101.         canvas: canvas,
  102.         alpha: false,
  103.         antialias: true,
  104.     })
  105.     renderer.shadowMap.enabled = true
  106.     renderer.shadowMap.type = THREE.PCFSoftShadowMap
  107.     renderer.setSize(sizes.width, sizes.height)
  108.     renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
  109. }
  110.  
  111. function _loadMusic() {
  112.     const listener = new THREE.AudioListener()
  113.  
  114.     const audio = new THREE.Audio(listener)
  115.     const file = "/music/axelf.mp3"
  116.  
  117.     if (/(iPad|iPhone|iPod)/g.test(navigator.userAgent)) {
  118.         const loader = new THREE.AudioLoader()
  119.         loader.load(file, (buffer) => {
  120.             const tl = new gsap.timeline({
  121.                 onComplete: () => {
  122.                     audio.setBuffer(buffer)
  123.                     audio.setLoop(true)
  124.                     audio.setVolume(0.1)
  125.  
  126.                     analyzer = new THREE.AudioAnalyser(audio, 128)
  127.  
  128.                     gsap.delayedCall(0.3, () => {
  129.                         audio.play()
  130.                     })
  131.  
  132.                     resolve()
  133.                 },
  134.             })
  135.             tl.add("start")
  136.                 .to("#audio-btn", {
  137.                     scale: 0,
  138.                     duration: 0.6,
  139.                     ease: "back.in(2)",
  140.                 })
  141.                 .to("#audio-btn", { autoAlpha: true, duration: 0.65 }, "start+=0.3")
  142.         }),
  143.             (progress) => {
  144.                 gsap.to("#audio-btn-loader", {
  145.                     "--scale": () => progress.loaded / progress.total,
  146.                     duration: 0.15,
  147.                     overwrite: true,
  148.                     ease: "power1.out",
  149.                 })
  150.             }
  151.     } else {
  152.         const mediaElement = new Audio(file)
  153.         mediaElement.play()
  154.  
  155.         audio.setMediaElementSource(mediaElement)
  156.     }
  157. }
  158.  
  159. function _addLoaders() {
  160.  
  161.     const dracoLoader = new DRACOLoader()
  162.     dracoLoader.setDecoderPath("/draco/")
  163.  
  164.     const gltfLoader = new GLTFLoader()
  165.     gltfLoader.setDRACOLoader(dracoLoader)
  166.  
  167.     gltfLoader.load("/models/Eskelunden/glTF/eskelunden.gltf", (gltf) => {
  168.         console.log(gltf.scene.children)
  169.         gltf.scene.children[0].material.wireframe = false
  170.  
  171.         gltf.scene.scale.set(0.025, 0.025, 0.025)
  172.         scene.add(gltf.scene)
  173.     })
  174. }
  175.  
  176. function _createControls() {
  177.     controls = new OrbitControls(camera, canvas)
  178.     controls.target.set(0, 0.75, 0)
  179.     controls.enableDamping = true
  180. }
  181.  
  182. function _render() {
  183.     renderer.render(scene, camera)
  184. }
  185.  
  186. function _compatibility() {
  187.     if (WebGL.isWebGLAvailable()) {
  188.         // Initiate function or other initializations here
  189.         _update()
  190.     } else {
  191.         const warning = WebGL.getWebGLErrorMessage()
  192.         document.getElementById("container").appendChild(warning)
  193.     }
  194. }
  195.  
  196. function _addListeners() {
  197.     window.addEventListener("resize", _resizeCb, { passive: true })
  198. }
  199.  
  200. function _removeListeners() {
  201.     window.removeEventListener("resize", _resizeCb, { passive: true })
  202. }
  203.  
  204. function _onResize() {
  205.     camera.aspect = container.clientWidth / container.clientHeight
  206.     camera.updateProjectionMatrix()
  207.  
  208.     renderer.setSize(container.clientWidth, container.clientHeight)
  209.     composer.setSize(container.clientWidth, container.clientHeight)
  210. }
  211.  
  212. _init()
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement