1
1
"use client" ;
2
2
3
- import React , { useEffect , useState } from "react" ;
3
+ import React , { useEffect , useRef , useMemo , useState } from "react" ;
4
4
import StarnetLayout from "@/components/Layout/Starnet" ;
5
- import ImageAnnotation from "@/components/Projects/(classifications)/Annotation" ;
6
- import InventoryPage from "@/components/Inventory/Grid/Grid" ;
7
- import TelescopeComponent from "@/constants/Structures/Telescope" ;
8
- import ZoodexComponent from "@/constants/Structures/Zoodex" ;
9
- import { CreateStructure } from "@/components/Structures/Build/CreateDedicatedStructure" ;
10
- import AllClassifications from "@/content/Starnet/YourClassifications" ;
5
+ // import { TopographicMap } from "@/components/topographic-map";
11
6
12
7
export default function TestPage ( ) {
13
8
return (
14
9
< StarnetLayout >
15
10
< >
16
- < AllClassifications initialType = "planet" />
11
+ < div style = { { width : "100vw" , height : "100vh" } } >
12
+ < TopographicMap />
13
+ </ div >
17
14
</ >
18
15
</ StarnetLayout >
19
16
) ;
@@ -30,4 +27,104 @@ export default function TestPage() {
30
27
},
31
28
]} />
32
29
33
- */
30
+ */
31
+
32
+ import { Canvas } from "@react-three/fiber" ;
33
+ import * as THREE from "three" ;
34
+ import { createNoise2D } from "simplex-noise" ;
35
+ import alea from "alea" ;
36
+
37
+ // Topographic shaders
38
+ const topographicVertexShader = `
39
+ varying vec2 vUv;
40
+ varying float vElevation;
41
+
42
+ void main() {
43
+ vUv = uv;
44
+ vElevation = position.z;
45
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
46
+ }
47
+ ` ;
48
+
49
+ const topographicFragmentShader = `
50
+ varying vec2 vUv;
51
+ varying float vElevation;
52
+ uniform vec3 uBaseColor;
53
+ uniform vec3 uContourColor;
54
+ uniform float uBands;
55
+ uniform float uContourWidth;
56
+
57
+ void main() {
58
+ float elevation = vElevation;
59
+ float bandedElevation = floor(elevation * uBands) / uBands;
60
+ float nextBand = floor(elevation * uBands + 1.0) / uBands;
61
+ float mixFactor = smoothstep(bandedElevation, nextBand, elevation);
62
+
63
+ float contourLine = 1.0 - smoothstep(0.0, uContourWidth, abs(mixFactor - 0.5));
64
+
65
+ vec3 finalColor = mix(uBaseColor, uContourColor, contourLine * 0.5);
66
+
67
+ gl_FragColor = vec4(finalColor, 1.0);
68
+ }
69
+ ` ;
70
+
71
+ type TerrainProps = {
72
+ seed : number ;
73
+ heightScale : number ;
74
+ bands : number ;
75
+ baseColor : string ;
76
+ contourColor : string ;
77
+ } ;
78
+
79
+ const Terrain : React . FC < TerrainProps > = ( { seed, heightScale, bands, baseColor, contourColor } ) => {
80
+ const mesh = useRef < THREE . Mesh > ( null ) ; // Explicitly define mesh type
81
+ const prng = useMemo ( ( ) => alea ( seed ) , [ seed ] ) ;
82
+ const noise2D = useMemo ( ( ) => createNoise2D ( prng ) , [ prng ] ) ;
83
+
84
+ const geometry = useMemo ( ( ) => {
85
+ const geo = new THREE . PlaneGeometry ( 20 , 20 , 200 , 200 ) ;
86
+ const positions = geo . attributes . position . array as Float32Array ;
87
+
88
+ for ( let i = 0 ; i < positions . length ; i += 3 ) {
89
+ const x = positions [ i ] ;
90
+ const y = positions [ i + 1 ] ;
91
+ const noise = noise2D ( x * 0.05 , y * 0.05 ) ;
92
+ positions [ i + 2 ] = noise * heightScale ;
93
+ }
94
+
95
+ geo . computeVertexNormals ( ) ;
96
+ return geo ;
97
+ } , [ noise2D , heightScale ] ) ;
98
+
99
+ const material = useMemo ( ( ) => {
100
+ return new THREE . ShaderMaterial ( {
101
+ vertexShader : topographicVertexShader ,
102
+ fragmentShader : topographicFragmentShader ,
103
+ uniforms : {
104
+ uBaseColor : { value : new THREE . Color ( baseColor ) } ,
105
+ uContourColor : { value : new THREE . Color ( contourColor ) } ,
106
+ uBands : { value : bands } ,
107
+ uContourWidth : { value : 0.1 } ,
108
+ } ,
109
+ } ) ;
110
+ } , [ baseColor , contourColor , bands ] ) ;
111
+
112
+ return (
113
+ < mesh ref = { mesh } geometry = { geometry } material = { material } rotation = { [ - Math . PI / 2 , 0 , 0 ] } />
114
+ ) ;
115
+ } ;
116
+
117
+ const TopographicMap = ( ) => {
118
+ return (
119
+ < Canvas >
120
+ < ambientLight />
121
+ < Terrain
122
+ seed = { 42 }
123
+ heightScale = { 1 }
124
+ bands = { 40 }
125
+ baseColor = "#F5F5DC" // Beige
126
+ contourColor = "#8B4513" // SaddleBrown
127
+ />
128
+ </ Canvas >
129
+ ) ;
130
+ } ;
0 commit comments