From 673aabce50c1210d92d8098d796cd7d200891131 Mon Sep 17 00:00:00 2001 From: neru Date: Tue, 2 Jun 2026 04:42:16 -0300 Subject: [PATCH] style: move patch to its own file --- src/app/fear/scene-components/hallway.tsx | 59 +---------------------- src/app/fear/shader-patch.ts | 57 ++++++++++++++++++++++ 2 files changed, 58 insertions(+), 58 deletions(-) create mode 100644 src/app/fear/shader-patch.ts diff --git a/src/app/fear/scene-components/hallway.tsx b/src/app/fear/scene-components/hallway.tsx index 9572a9f..9f97744 100644 --- a/src/app/fear/scene-components/hallway.tsx +++ b/src/app/fear/scene-components/hallway.tsx @@ -4,64 +4,7 @@ import { useTexture, PositionalAudio } from "@react-three/drei"; import * as THREE from "three"; import { useFrame } from "@react-three/fiber"; - -function ShaderPatch(shader: { vertexShader: string, fragmentShader: string, uniforms: Object }) { - shader.vertexShader = ` - varying float vDepth; - #ifdef USE_MAP - varying vec2 vAffineUv; - #endif - ${shader.vertexShader} - `; - - shader.vertexShader = shader.vertexShader.replace( - `#include `, - ` - vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); - gl_Position = projectionMatrix * mvPosition; - - float precisionModifier = 200.0; - gl_Position.xy /= gl_Position.w; - gl_Position.xy = floor(gl_Position.xy * precisionModifier) / precisionModifier; - gl_Position.xy *= gl_Position.w; - - vDepth = gl_Position.w; - - #ifdef USE_MAP - vAffineUv = vMapUv * gl_Position.w; - #endif - ` - ); - - shader.fragmentShader = ` - varying float vDepth; - #ifdef USE_MAP - varying vec2 vAffineUv; - #endif - ${shader.fragmentShader} - `; - - shader.fragmentShader = shader.fragmentShader.replace( - `#include `, - ` - #ifdef USE_MAP - vec2 flatAffineUV = vAffineUv / max(vDepth, 0.001); - - vec2 warpDiff = flatAffineUV - vMapUv; - float warpDist = length(warpDiff); - - float maxDistortion = 0.25; - - float falloff = maxDistortion / (maxDistortion + warpDist); - - vec2 finalUV = vMapUv + (warpDiff * falloff); - - vec4 texelColor = texture2D( map, finalUV ); - diffuseColor *= texelColor; - #endif - ` - ); -} +import { ShaderPatch } from "../shader-patch"; interface DoorProps { position: [number, number, number]; diff --git a/src/app/fear/shader-patch.ts b/src/app/fear/shader-patch.ts new file mode 100644 index 0000000..cda59a5 --- /dev/null +++ b/src/app/fear/shader-patch.ts @@ -0,0 +1,57 @@ +export function ShaderPatch(shader: { vertexShader: string, fragmentShader: string, uniforms: Object }) { + shader.vertexShader = ` + varying float vDepth; + #ifdef USE_MAP + varying vec2 vAffineUv; + #endif + ${shader.vertexShader} + `; + + shader.vertexShader = shader.vertexShader.replace( + `#include `, + ` + vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); + gl_Position = projectionMatrix * mvPosition; + + float precisionModifier = 200.0; + gl_Position.xy /= gl_Position.w; + gl_Position.xy = floor(gl_Position.xy * precisionModifier) / precisionModifier; + gl_Position.xy *= gl_Position.w; + + vDepth = gl_Position.w; + + #ifdef USE_MAP + vAffineUv = vMapUv * gl_Position.w; + #endif + ` + ); + + shader.fragmentShader = ` + varying float vDepth; + #ifdef USE_MAP + varying vec2 vAffineUv; + #endif + ${shader.fragmentShader} + `; + + shader.fragmentShader = shader.fragmentShader.replace( + `#include `, + ` + #ifdef USE_MAP + vec2 flatAffineUV = vAffineUv / max(vDepth, 0.001); + + vec2 warpDiff = flatAffineUV - vMapUv; + float warpDist = length(warpDiff); + + float maxDistortion = 0.25; + + float falloff = maxDistortion / (maxDistortion + warpDist); + + vec2 finalUV = vMapUv + (warpDiff * falloff); + + vec4 texelColor = texture2D( map, finalUV ); + diffuseColor *= texelColor; + #endif + ` + ); +}