Skip to content

Instantly share code, notes, and snippets.

View dghez's full-sized avatar
🌝

Robert dghez

🌝
View GitHub Profile
@dghez
dghez / THREEJS-loadingManager.js
Last active February 20, 2023 15:33
Global loader manager for ThreeJs + react hook
/* eslint-disable no-multi-assign */
import { useMemo } from 'react'
import { TextureLoader, NearestFilter, FloatType, HalfFloatType, LinearEncoding } from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js'
import staticData from './data.yml'
class Resources {
@dghez
dghez / normalizedMouse.js
Created March 4, 2019 16:35
Normalized mouse with center in center of the screen, for threejs
const mousePos = { x: 0, y: 0 }
export default function (evt) {
mousePos.x = (evt.clientX / window.innerWidth) * 2 - 1
mousePos.y = -(evt.clientY / window.innerHeight) * 2 + 1
return mousePos
}
@dghez
dghez / Nuxk-baseComponent
Created February 13, 2019 13:13
Base component for vue
<template>
<div :class="$style.wrapper"></div>
</template>
<script>
import Vue from 'vue'
import Component, { mixins } from 'vue-class-component'
@Component({
components: {},
props: {}
@dghez
dghez / THREE-WorldToScreen.js
Created January 21, 2019 11:43
Unproject the vector and gives back screen coordinates x,y
import { Vector3 } from 'three'
const v = new Vector3()
export default (v3, camera) => {
v.copy(v3)
v.project(camera)
return {
x: v.x * window.innerWidth * 0.5 + window.innerWidth * 0.5,
@dghez
dghez / distanceCameraBasedOnWidth.js
Last active March 28, 2021 15:00
Calculate the distance of the camera to fit a given width
import { MathUtils } from "three";
export default function distance(width, camera) {
const vFOV = MathUtils.degToRad(camera.fov);
const distance = width / camera.aspect / (2 * Math.tan(vFOV / 2));
const heightPlane = 2 * Math.tan(vFOV / 2) * distance;
return { distance, height: heightPlane };
}
@dghez
dghez / isTouch.js
Created January 18, 2019 13:37
Detect if is a touch device
export default function isTouch() {
return 'ontouchstart' in document.documentElement
}
@dghez
dghez / General_Mouse position.js
Last active January 25, 2019 14:16
Different values from mouse position
const windowSizes = {
w: document.body.clientWidth,
h: window.innerHeight,
}
// pixels pos
const mousePosPixels = {
x: 0,
y: 0,
}
@dghez
dghez / Threejs-ElementSizeOnFov.js
Last active February 1, 2023 10:15
Visible Portion of some element at certain distance, plane.
import {MathUtils} from "three";
// this gives the "visible" plane sizes that is visible from the camera at a certain distance
export default function(distance, camera) {
const vFOV = MathUtils.degToRad(camera.fov);
const height = 2 * Math.tan(vFOV / 2) * distance;
const width = height * camera.aspect;
return { width, height };
}
@dghez
dghez / Threejs-Mouseposition.js
Last active November 29, 2018 16:18
Mouse position - Threejs
// NOT 100% SURE IT'S THE RIGHT WAY TO DO THAT
// DECLARE IT AND REUSE IT
var vector = new THREE.Vector3() // create temp vec
var position = new THREE.Vector3() // create temp vec
onMouseMove (evt) {
let x = (evt.clientX / window.innerWidth) * 2 - 1 // percentuage and center
let y = -(evt.clientY / window.innerHeight) * 2 + 1
vector.set(x, y, 0.5) // create temp vec