Skip to content

Instantly share code, notes, and snippets.

View dghez's full-sized avatar
🌝

Robert dghez

🌝
View GitHub Profile
@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
@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 / 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 / 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 / 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 / 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 / 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 / MISC - Random values from interval
Last active March 27, 2019 16:02
Return random values, float or int
import RN from 'random-number';
let rand = RN.generator();
let randInt = (min, max) => {
return rand(min, max, true);
};
let randFloat = (min, max) => {
return rand(min, max, false);
@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 };
}