Skip to content

Commit 60ce5e0

Browse files
author
Kevin
committed
fix: update color in Path component, adjust position in Agents component, enhance texture scaling in Domain component, modify shadow bias in Light component, and refine fog parameters in Stage component
1 parent 4b82b4d commit 60ce5e0

File tree

6 files changed

+83
-70
lines changed

6 files changed

+83
-70
lines changed

src/components/Agent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function Path({
128128
dashScale={5}
129129
depthTest={false}
130130
lineWidth={s.scale.to((s) => s * 6)}
131-
color="white"
131+
color="black"
132132
points={path}
133133
dashed
134134
/>

src/components/Agents.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ export function Agents() {
2929
<Instances
3030
frustumCulled={false}
3131
castShadow
32+
receiveShadow
3233
geometry={geometry}
3334
material={material}
3435
>
3536
{range(contents?.count ?? 0).map((i) => (
36-
<group position={[0, 0.05, 0]}>
37+
<group position={[0, 0.1, 0]}>
3738
<Agent i={i} key={i} scale={2} />
3839
</group>
3940
))}

src/components/Domain.tsx

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { Grid, Instance, Instances, useTexture } from "@react-three/drei";
22
import { useModel } from "hooks/useModel";
3-
import { memo, ReactNode } from "react";
3+
import { memo, ReactNode, Suspense } from "react";
44
import {
5+
DoubleSide,
56
LinearMipmapNearestFilter,
67
NearestFilter,
78
RepeatWrapping,
89
Vector2,
910
} from "three";
1011

12+
import { useThree } from "@react-three/fiber";
1113
import "./checkerMaterial";
1214
import { checkerMaterial } from "./checkerMaterial";
13-
import { useThree } from "@react-three/fiber";
1415

1516
const Obstacles = memo(
1617
({
@@ -21,15 +22,15 @@ const Obstacles = memo(
2122
const { geometry: hull } = useModel("./box-hull.gltf");
2223
const roughness = useTexture("/tile-roughness.png", (t) => {
2324
t.anisotropy = gl.capabilities.getMaxAnisotropy();
24-
t.repeat = new Vector2(width, height);
25+
t.repeat = new Vector2(width * 2, height * 2);
2526
t.wrapS = RepeatWrapping;
2627
t.wrapT = RepeatWrapping;
2728
t.minFilter = LinearMipmapNearestFilter;
2829
t.magFilter = NearestFilter;
2930
});
3031
const texture = useTexture("/tile.png", (t) => {
3132
t.anisotropy = gl.capabilities.getMaxAnisotropy();
32-
t.repeat = new Vector2(width, height);
33+
t.repeat = new Vector2(width * 2, height * 2);
3334
t.wrapS = RepeatWrapping;
3435
t.wrapT = RepeatWrapping;
3536
t.minFilter = LinearMipmapNearestFilter;
@@ -38,17 +39,17 @@ const Obstacles = memo(
3839
return (
3940
<group>
4041
<Instances
41-
scale={[-1, 1, -1]}
42+
scale={[-1, 0.5, -1]}
4243
rotation={[0, Math.PI / 2, 0]}
4344
limit={items?.length ?? 0}
4445
castShadow
4546
position={[height / 2, 0, -width / 2]}
46-
material={checkerMaterial(4)}
47+
material={checkerMaterial(1)}
4748
>
4849
<boxGeometry />
4950
{items?.map?.((item, i) => (
5051
<Instance
51-
color="#444"
52+
color="#555"
5253
key={i}
5354
scale={[item.width, 0.5, item.height]}
5455
position={[
@@ -60,17 +61,16 @@ const Obstacles = memo(
6061
))}
6162
</Instances>
6263
<mesh
63-
receiveShadow
6464
geometry={hull}
6565
rotation={[0, Math.PI / 2, 0]}
6666
scale={[width / 2, 0.3, height / 2]}
6767
position={[0, -0.3, 0]}
6868
>
69-
<meshStandardMaterial color="#666" />
69+
<meshStandardMaterial color="#666" side={DoubleSide} />
7070
</mesh>
7171
<mesh receiveShadow rotation={[-Math.PI / 2, 0, -Math.PI / 2]}>
7272
<meshStandardMaterial
73-
color="#AAA"
73+
color="#ccc"
7474
map={texture}
7575
roughnessMap={roughness}
7676
/>
@@ -103,17 +103,25 @@ export function Domain({
103103
} & DomainProps) {
104104
return (
105105
<>
106-
{items.length ? (
107-
<Obstacles items={items} size={size} />
108-
) : (
109-
<Grid args={[10, 10]} />
110-
)}
111-
<group
112-
scale={[1, 1, 1]}
113-
position={[(size?.height ?? 0) / 2 - 1, 0, -(size?.width ?? 0) / 2 + 1]}
114-
>
115-
{children}
116-
</group>
106+
<Grid
107+
position={[0, -0.6, 0]}
108+
args={[4096, 4096]}
109+
cellColor={"#aaa"}
110+
sectionColor={"#ccc"}
111+
/>
112+
<Suspense fallback={null}>
113+
{items.length && <Obstacles items={items} size={size} />}
114+
<group
115+
scale={[1, 1, 1]}
116+
position={[
117+
(size?.height ?? 0) / 2 - 1,
118+
0,
119+
-(size?.width ?? 0) / 2 + 1,
120+
]}
121+
>
122+
{children}
123+
</group>
124+
</Suspense>
117125
</>
118126
);
119127
}

src/components/Light.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function Light() {
3535
ref={light}
3636
color="#fff2d9"
3737
castShadow
38-
shadow-bias={0.0001}
38+
shadow-bias={-0.0001}
3939
shadow-mapSize={[2048 * 1.5, 2048 * 1.5]}
4040
intensity={4}
4141
/>

src/components/Stage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function Stage() {
1919
position: [0, 5, 10],
2020
}}
2121
>
22-
<fog attach="fog" args={["#eff2f3", 0, 100]} />
22+
<fog attach="fog" args={["#eff2f3", 20, 100]} />
2323
<color attach="background" args={["#afc3cc"]} />
2424
<Light />
2525
<Sky inclination={0.52} sunPosition={[100, 100, 50]} />

src/components/checkerMaterial.tsx

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,42 @@ import {
88
Texture,
99
} from "three";
1010

11-
export const checkerMaterial = memoize((scale: number = 1) => {
12-
const map = new Texture();
13-
const material = new MeshStandardMaterial({
14-
map,
15-
color: new Color(0xffffff),
16-
roughness: 0.5,
17-
roughnessMap: new Texture(),
18-
});
19-
20-
// Setup texture tiling and filtering
21-
map.wrapS = RepeatWrapping;
22-
map.wrapT = RepeatWrapping;
23-
map.magFilter = NearestFilter;
24-
map.minFilter = NearestMipMapNearestFilter;
25-
26-
material.onBeforeCompile = (shader) => {
27-
// Add varyings
28-
shader.vertexShader =
29-
`
11+
export const checkerMaterial = memoize(
12+
(scale: number = 1, materialOptions = {}) => {
13+
const map = new Texture();
14+
const material = new MeshStandardMaterial({
15+
color: new Color(0xffffff),
16+
roughness: 0.5,
17+
...materialOptions,
18+
map,
19+
roughnessMap: new Texture(),
20+
});
21+
22+
// Setup texture tiling and filtering
23+
map.wrapS = RepeatWrapping;
24+
map.wrapT = RepeatWrapping;
25+
map.magFilter = NearestFilter;
26+
map.minFilter = NearestMipMapNearestFilter;
27+
28+
material.onBeforeCompile = (shader) => {
29+
// Add varyings
30+
shader.vertexShader =
31+
`
3032
varying vec3 vWorldPosition;
3133
varying vec3 vWorldNormal;
3234
` +
33-
shader.vertexShader.replace(
34-
"#include <worldpos_vertex>",
35-
`
35+
shader.vertexShader.replace(
36+
"#include <worldpos_vertex>",
37+
`
3638
#include <worldpos_vertex>
3739
vWorldPosition = worldPosition.xyz;
3840
vWorldNormal = normalize(round(normalMatrix * normal));
3941
`
40-
);
42+
);
4143

42-
// Add uniforms and function
43-
shader.fragmentShader =
44-
`
44+
// Add uniforms and function
45+
shader.fragmentShader =
46+
`
4547
varying vec3 vWorldPosition;
4648
varying vec3 vWorldNormal;
4749
uniform float uTileScale;
@@ -73,35 +75,37 @@ export const checkerMaterial = memoize((scale: number = 1) => {
7375
}
7476
7577
float sampleWorld(sampler2D tex, vec3 worldPos, vec3 normal, float scale) {
76-
return noise(vec3(floor(worldPos.x * 64.0), floor(worldPos.y * 64.0), floor(worldPos.z * 64.0)));
78+
return noise(vec3(floor(worldPos.x * 64.0 * scale), floor(worldPos.y * 64.0 * scale), floor(worldPos.z * 64.0 * scale)));
7779
}
7880
` + shader.fragmentShader;
7981

80-
// Replace default UV-based sampling
81-
shader.fragmentShader = shader.fragmentShader.replace(
82-
"#include <map_fragment>",
83-
`
82+
// Replace default UV-based sampling
83+
shader.fragmentShader = shader.fragmentShader.replace(
84+
"#include <map_fragment>",
85+
`
8486
float fac = sampleWorld(map, vWorldPosition, vWorldNormal, uTileScale);
8587
float fac2 = sampleWorld(map, vec3(vWorldPosition.x+1.0, vWorldPosition.y+1.0, vWorldPosition.z+1.0), vWorldNormal, uTileScale);
86-
diffuseColor *= 0.8 + fac * 0.05;
88+
diffuseColor *= 0.8 + fac * 0.1;
8789
`
88-
);
90+
);
8991

90-
// Replace roughness map sampling
91-
shader.fragmentShader = shader.fragmentShader.replace(
92-
"#include <roughnessmap_fragment>",
93-
`
92+
// Replace roughness map sampling
93+
shader.fragmentShader = shader.fragmentShader.replace(
94+
"#include <roughnessmap_fragment>",
95+
`
9496
float roughnessFactor = 0.5;
9597
#ifdef USE_ROUGHNESSMAP
96-
roughnessFactor *= 0.6 + fac2 * 0.2;
98+
roughnessFactor *= 0.6 + fac2 * 0.4;
9799
#endif
98100
`
99-
);
101+
);
100102

101-
// Uniform for tile scale
102-
shader.uniforms.uTileScale = { value: scale };
103-
material.userData.shader = shader;
104-
};
103+
// Uniform for tile scale
104+
shader.uniforms.uTileScale = { value: scale };
105+
material.userData.shader = shader;
106+
};
105107

106-
return material;
107-
});
108+
return material;
109+
},
110+
JSON.stringify
111+
);

0 commit comments

Comments
 (0)