Skip to content

Instantly share code, notes, and snippets.

View supahfunk's full-sized avatar

Supah supahfunk

View GitHub Profile
@supahfunk
supahfunk / usePixel.js
Last active August 1, 2023 13:16
Converting Three.js viewport dimensions into pixel or screen percentage units
import { useEffect } from 'react'
import { useThree } from '@react-three/fiber'
import { MathUtils } from 'three'
const isPercentage = (num) => typeof num === 'string' && num.includes('%')
const calculatePosition = (side, value, size, viewport, length) => {
const { mapLinear } = MathUtils
const inverse = side === 'right' || side === 'top' ? -1 : 1
@supahfunk
supahfunk / InstancedMesh.js
Last active January 29, 2024 12:53
React-three fiber instanced mesh with Drei
import { useGLTF, Instances, Instance } from '@react-three/drei'
import { MathUtils } from 'three'
const InstancedMesh = () => {
const { nodes } = useGLTF('./model.glb')
const randomVector = (r) =>
Array(3)
.fill()
.map(() => MathUtils.randFloatSpread(r))
uniform sampler2D textureMap; //image
uniform float amount; //amount of twirl
void main() {
vec2 uv = gl_TexCoord[0].st-0.5;
float angle = atan2(uv.y,uv.x);
float radius = length(uv);
angle+= radius*amount;
vec2 shifted = radius*vec2(cos(angle), sin(angle));
gl_FragColor = texture(textureMap, (shifted+0.5));
}
@supahfunk
supahfunk / matcapmaterial.js
Created February 2, 2022 21:06
Matcap material glsl
/* Vertex shader */
vNormal = normalize(vec3(world * vec4(normal, 0.0))); // Normal in model
/* Fragment shader */
// Move normal to view space
highp vec2 muv = vec2(view * vec4(normalize(vNormal), 0))*0.5+vec2(0.5,0.5);
// read texture inverting Y value
@supahfunk
supahfunk / BasicShaderMaterial.js
Created February 2, 2022 20:45
Shader Material Basic
import { mergeUniforms } from 'three/src/renderers/shaders/UniformsUtils.js'
import { UniformsLib } from 'three/src/renderers/shaders/UniformsLib.js'
export default {
uniforms: mergeUniforms([
UniformsLib.lights,
UniformsLib.fog,
]),
@supahfunk
supahfunk / ffmpeg-convert-web.txt
Last active April 11, 2022 07:34
Extract first frame from video and convert mp4 optimized for web with FFMPEG
/*------------------------------
Extract first frame from video
------------------------------*/
ffmpeg -i input.mov -vf "select=eq(n\,0)" -q:v 3 output.jpg
/*------------------------------
Convert video optimized for web
------------------------------*/
ffmpeg -i input.mov -c:v libx264 -preset veryslow -pix_fmt yuv420p output.mp4
@supahfunk
supahfunk / coveruv.glsl
Last active January 29, 2024 12:39
Cover UV / Contain UV
/*------------------------------
Background Cover UV
--------------------------------
u = basic UV
s = screensize
i = image size
------------------------------*/
vec2 CoverUV(vec2 u, vec2 s, vec2 i) {
float rs = s.x / s.y; // Aspect screen size
float ri = i.x / i.y; // Aspect image size
@supahfunk
supahfunk / getClosestDiffInArray
Created October 21, 2021 10:11
Return the closest difference between two points in array
const getClosestDiffInArray = (length, curr, next) => {
const inside = curr > next ? -(curr - next) : next - curr
const outside = curr > next ? length - curr + next : -(length - next + curr)
return Math.abs(inside) < Math.abs(outside) ? inside : outside
}
console.log('return', getClosestDiffInArray(12, 11, 2))
@supahfunk
supahfunk / react-mapping-ref.js
Last active June 23, 2021 13:40
React useRef in map
const MyComponent = ({ items }) => {
const $items = useRef(items.map(createRef))
useEffect(() => {
console.log($items.current[0].current)
}, [])
return (
{items.map((item, index) => (
<div
@supahfunk
supahfunk / background.js
Created June 22, 2021 14:56
GLSL Background size cover - R3F
const Background = () => {
const { viewport } = useThree()
const scale = useMemo(() => ((40 + 15) / viewport.distance), [viewport])
const [bgTexture] = useTexture(['./bg.png'])
const shaderArgs = useMemo(() => ({
uniforms: {
uTexture: { value: bgTexture },
uResolution: { value: { x: viewport.width, y: viewport.height } },
uImageRegsolution: { value: { x: bgTexture.image.width, y: bgTexture.image.height } },