style: move patch to its own file
This commit is contained in:
@@ -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 <project_vertex>`,
|
||||
`
|
||||
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 <map_fragment>`,
|
||||
`
|
||||
#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];
|
||||
|
||||
@@ -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 <project_vertex>`,
|
||||
`
|
||||
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 <map_fragment>`,
|
||||
`
|
||||
#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
|
||||
`
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user