Skip to content

Instantly share code, notes, and snippets.

View dghez's full-sized avatar
🌝

Robert dghez

🌝
View GitHub Profile
@dghez
dghez / GLSL-Rotate on axis
Created March 26, 2019 23:39
Rotate on a specific axe
mat4 rotationMatrix(vec3 axis, float angle) {
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
0.0, 0.0, 0.0, 1.0);
@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 };
}