mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-24 03:59:50 +02:00
ground with shadows
This commit is contained in:
1 parent
486171f4db
commit
051e9ec74a
3 files changed
+49
-35
No files matched your search
+49
-11
@@ -1,10 +1,8 @@
|
|||||||
import { mauveDark } from "@radix-ui/colors";
|
import { mauveDark } from "@radix-ui/colors";
|
||||||
import { invalidate } from "@react-three/fiber";
|
import { invalidate } from "@react-three/fiber";
|
||||||
import { Quicklime, type QuicklimeEvent } from "quicklime";
|
import { Quicklime, type QuicklimeEvent } from "quicklime";
|
||||||
import { useEffect, useRef } from "react";
|
import { useCallback, useEffect, useRef } from "react";
|
||||||
import { Color, Mesh } from "three";
|
import { Mesh, type WebGLProgramParametersWithUniforms } from "three";
|
||||||
import fragmentShader from "./shaders/fragment.glsl?raw";
|
|
||||||
import vertexShader from "./shaders/vertex.glsl?raw";
|
|
||||||
|
|
||||||
const SIZE = 2 ** 4;
|
const SIZE = 2 ** 4;
|
||||||
|
|
||||||
@@ -26,22 +24,62 @@ export function SceneProps() {
|
|||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const handleBeforeCompile = useCallback(
|
||||||
|
(shader: WebGLProgramParametersWithUniforms) => {
|
||||||
|
shader.uniforms.size = { value: SIZE / 2 };
|
||||||
|
|
||||||
|
shader.vertexShader = `
|
||||||
|
varying vec3 vWorldPosition;
|
||||||
|
|
||||||
|
${shader.vertexShader}
|
||||||
|
`.replace(
|
||||||
|
"#include <worldpos_vertex>",
|
||||||
|
|
||||||
|
`
|
||||||
|
#include <worldpos_vertex>
|
||||||
|
vWorldPosition = (modelMatrix * vec4(transformed, 1.0)).xyz;
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
|
||||||
|
shader.fragmentShader = `
|
||||||
|
varying vec3 vWorldPosition;
|
||||||
|
uniform float size;
|
||||||
|
|
||||||
|
${shader.fragmentShader}
|
||||||
|
`.replace(
|
||||||
|
"#include <color_fragment>",
|
||||||
|
|
||||||
|
`
|
||||||
|
#include <color_fragment>
|
||||||
|
|
||||||
|
vec3 scaled = vWorldPosition / size;
|
||||||
|
float radius = length(scaled.xz);
|
||||||
|
|
||||||
|
if (radius >= 1.0) {
|
||||||
|
discard;
|
||||||
|
}
|
||||||
|
|
||||||
|
diffuseColor.a *= (1.0 - radius);
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<mesh
|
<mesh
|
||||||
position={[0, -(2 ** -8), 0]}
|
position={[0, -(2 ** -8), 0]}
|
||||||
rotation={[-Math.PI / 2, 0, 0]}
|
rotation={[-Math.PI / 2, 0, 0]}
|
||||||
visible={!screenshotReadyEvent.last!}
|
visible={!screenshotReadyEvent.last!}
|
||||||
ref={mesh}
|
ref={mesh}
|
||||||
|
receiveShadow
|
||||||
>
|
>
|
||||||
<planeGeometry args={[SIZE, SIZE]} />
|
<planeGeometry args={[SIZE, SIZE]} />
|
||||||
<shaderMaterial
|
<meshStandardMaterial
|
||||||
fragmentShader={fragmentShader}
|
color={mauveDark.mauve6}
|
||||||
vertexShader={vertexShader}
|
roughness={1}
|
||||||
transparent
|
transparent
|
||||||
uniforms={{
|
onBeforeCompile={handleBeforeCompile}
|
||||||
size: { value: SIZE / 2 },
|
|
||||||
color: { value: new Color(mauveDark.mauve8) },
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
</mesh>
|
</mesh>
|
||||||
);
|
);
|
||||||
|
|||||||
-16
@@ -1,16 +0,0 @@
|
|||||||
varying vec3 vWorldPosition;
|
|
||||||
uniform float size;
|
|
||||||
uniform vec3 color;
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
vec3 scaled = vWorldPosition / size;
|
|
||||||
float radius = length(scaled.xz);
|
|
||||||
|
|
||||||
if(radius >= 1.0) {
|
|
||||||
discard;
|
|
||||||
}
|
|
||||||
|
|
||||||
float a = 1.0 - radius;
|
|
||||||
|
|
||||||
gl_FragColor = vec4(color, a);
|
|
||||||
}
|
|
||||||
-8
@@ -1,8 +0,0 @@
|
|||||||
varying vec3 vWorldPosition;
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
vec4 worldPos = modelMatrix * vec4(position, 1.0);
|
|
||||||
vWorldPosition = worldPos.xyz;
|
|
||||||
|
|
||||||
gl_Position = projectionMatrix * viewMatrix * worldPos;
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user