Files
snapcad-studio/apps/web/src/components/snapcad/TerrainGeneratorModal.tsx
T

304 lines
11 KiB
TypeScript
Raw Normal View History

"use client";
import React, { useState, useId } from "react";
import {
Mountain,
X,
Sparkles,
Layers,
Check,
Sliders,
} from "lucide-react";
import { createLocalId } from "@/lib/localIds";
import type { WorkplaneShape, ArchitecturalTextureId } from "@/types/sketchforge";
export interface TerrainGeneratorModalProps {
isOpen: boolean;
onClose: () => void;
onAddShape: (shape: WorkplaneShape) => void;
}
type TerrainPreset = "hills" | "slope" | "terrace" | "valley";
export const TerrainGeneratorModal: React.FC<TerrainGeneratorModalProps> = ({
isOpen,
onClose,
onAddShape,
}) => {
const baseId = useId();
const [preset, setPreset] = useState<TerrainPreset>("hills");
const [width, setWidth] = useState(200);
const [depth, setDepth] = useState(200);
const [maxHeight, setMaxHeight] = useState(18);
const [texture, setTexture] = useState<ArchitecturalTextureId>("grass");
if (!isOpen) return null;
const handleGenerate = () => {
// Generate grid vertices (e.g. 16x16 grid for clean performance)
const segments = 16;
const halfW = width / 2;
const halfD = depth / 2;
const positions: number[] = [];
const normals: number[] = [];
// Height function based on preset
const getHeight = (u: number, v: number) => {
// u, v in [0, 1]
if (preset === "hills") {
return (
Math.sin(u * Math.PI * 2) * Math.cos(v * Math.PI * 2) * maxHeight * 0.5 +
Math.sin(u * Math.PI * 4 + v * 2) * (maxHeight * 0.25) +
maxHeight * 0.5
);
}
if (preset === "slope") {
return u * maxHeight + Math.sin(v * Math.PI * 3) * (maxHeight * 0.15);
}
if (preset === "valley") {
const distFromCenter = Math.abs(u - 0.5) * 2;
return distFromCenter * distFromCenter * maxHeight;
}
// terrace
const stepped = Math.floor(u * 4) / 4;
return stepped * maxHeight;
};
const getNormal = (u: number, v: number) => {
const eps = 0.01;
const hL = getHeight(Math.max(0, u - eps), v);
const hR = getHeight(Math.min(1, u + eps), v);
const hD = getHeight(u, Math.max(0, v - eps));
const hU = getHeight(u, Math.min(1, v + eps));
const dx = (hR - hL) / (2 * eps * width);
const dz = (hU - hD) / (2 * eps * depth);
const len = Math.sqrt(dx * dx + 1 + dz * dz);
return [-dx / len, 1 / len, -dz / len];
};
// Build triangles
for (let i = 0; i < segments; i++) {
for (let j = 0; j < segments; j++) {
const u0 = i / segments;
const u1 = (i + 1) / segments;
const v0 = j / segments;
const v1 = (j + 1) / segments;
const x0 = -halfW + u0 * width;
const x1 = -halfW + u1 * width;
const z0 = -halfD + v0 * depth;
const z1 = -halfD + v1 * depth;
const y00 = getHeight(u0, v0);
const y10 = getHeight(u1, v0);
const y01 = getHeight(u0, v1);
const y11 = getHeight(u1, v1);
const n00 = getNormal(u0, v0);
const n10 = getNormal(u1, v0);
const n01 = getNormal(u0, v1);
const n11 = getNormal(u1, v1);
// Triangle 1: (0,0), (1,0), (0,1)
positions.push(x0, y00, z0, x1, y10, z0, x0, y01, z1);
normals.push(...n00, ...n10, ...n01);
// Triangle 2: (1,0), (1,1), (0,1)
positions.push(x1, y10, z0, x1, y11, z1, x0, y01, z1);
normals.push(...n10, ...n11, ...n01);
}
}
const terrainShape: WorkplaneShape = {
id: createLocalId("terrain"),
name:
preset === "hills"
? "Dalgalı Tepe Arazisi"
: preset === "slope"
? "Eğimli Arsa"
: preset === "valley"
? "Vadi Arazisi"
: "Teraslı Arsa",
kind: "mesh",
color: texture === "grass" ? "#22c55e" : "#64748b",
texture,
tag: "Arazi / Topoğrafya",
x: 0,
z: 0,
elevation: 0,
size: Math.max(width, depth),
width,
depth,
height: Math.max(5, maxHeight),
rotation: 0,
importedMesh: {
positions,
normals,
baseWidth: width,
baseDepth: depth,
baseHeight: maxHeight,
triangleCount: positions.length / 9,
sourceFormat: "json",
},
};
onAddShape(terrainShape);
onClose();
};
return (
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4 backdrop-blur-sm"
role="dialog"
aria-modal="true"
aria-labelledby="terrain-title"
>
<div className="relative flex max-h-[90vh] w-full max-w-md flex-col rounded-2xl border border-slate-200 bg-white shadow-2xl dark:border-slate-800 dark:bg-slate-900">
{/* Header */}
<div className="flex items-center justify-between border-b border-slate-200 px-6 py-4 dark:border-slate-800">
<div className="flex items-center gap-3">
<div className="flex h-10 w-10 items-center justify-center rounded-xl bg-emerald-500/10 text-emerald-600 dark:bg-emerald-500/20 dark:text-emerald-400">
<Mountain className="h-5 w-5" />
</div>
<div>
<h2
id="terrain-title"
className="text-lg font-bold text-slate-900 dark:text-white"
>
Arazi &amp; Topoğrafya Oluşturucu
</h2>
<p className="text-xs text-slate-500 dark:text-slate-400">
Binanızı kot farkı ve eğimli araziye oturtun
</p>
</div>
</div>
<button
type="button"
onClick={onClose}
className="rounded-lg p-2 text-slate-400 transition-colors hover:bg-slate-100 dark:hover:bg-slate-800"
aria-label="Kapat"
>
<X className="h-5 w-5" />
</button>
</div>
{/* Content */}
<div className="flex-1 space-y-4 overflow-y-auto px-6 py-4 text-sm">
{/* Preset Selection */}
<div>
<label className="mb-2 block text-xs font-semibold uppercase tracking-wider text-slate-500 dark:text-slate-400">
Arazi Tipi
</label>
<div className="grid grid-cols-2 gap-2">
{[
{ id: "hills", label: "Dalgalı Tepe", desc: "Doğal yükseltiler" },
{ id: "slope", label: "Eğimli Arsa", desc: "Tek yöne meyil" },
{ id: "terrace", label: "Teraslı Plato", desc: "Kademeli kotlar" },
{ id: "valley", label: "Vadi / Çukur", desc: "Ortası alçak" },
].map((item) => (
<button
key={item.id}
type="button"
onClick={() => setPreset(item.id as TerrainPreset)}
className={`flex flex-col items-start rounded-xl border p-2.5 text-left transition-all ${
preset === item.id
? "border-emerald-500 bg-emerald-50/70 text-emerald-900 dark:bg-emerald-950/40 dark:text-emerald-200"
: "border-slate-200 hover:border-slate-300 dark:border-slate-800 dark:hover:border-slate-700"
}`}
>
<span className="font-semibold text-xs">{item.label}</span>
<span className="text-[10px] text-slate-400">{item.desc}</span>
</button>
))}
</div>
</div>
{/* Sliders */}
<div className="space-y-3 rounded-xl border border-slate-100 bg-slate-50/60 p-3.5 dark:border-slate-800 dark:bg-slate-900/40">
<div className="flex items-center justify-between">
<span className="text-xs text-slate-600 dark:text-slate-300">Genişlik &amp; Derinlik</span>
<span className="font-mono text-xs font-semibold">{width} × {depth} mm</span>
</div>
<input
type="range"
min="100"
max="350"
step="10"
value={width}
onChange={(e) => {
const val = Number(e.target.value);
setWidth(val);
setDepth(val);
}}
className="w-full accent-emerald-600"
/>
<div className="flex items-center justify-between pt-1">
<span className="text-xs text-slate-600 dark:text-slate-300">Tepe / Kot Yüksekliği</span>
<span className="font-mono text-xs font-semibold">{maxHeight} mm</span>
</div>
<input
type="range"
min="4"
max="45"
step="1"
value={maxHeight}
onChange={(e) => setMaxHeight(Number(e.target.value))}
className="w-full accent-emerald-600"
/>
</div>
{/* Texture */}
<div>
<label className="mb-2 block text-xs font-semibold uppercase tracking-wider text-slate-500 dark:text-slate-400">
Yüzey Kaplaması
</label>
<div className="flex gap-2">
<button
type="button"
onClick={() => setTexture("grass")}
className={`flex-1 rounded-xl border py-2 text-xs font-medium transition-all ${
texture === "grass"
? "border-emerald-500 bg-emerald-500 text-white shadow-sm"
: "border-slate-200 dark:border-slate-700"
}`}
>
🌱 Çim &amp; Doğa
</button>
<button
type="button"
onClick={() => setTexture("concrete")}
className={`flex-1 rounded-xl border py-2 text-xs font-medium transition-all ${
texture === "concrete"
? "border-emerald-500 bg-emerald-500 text-white shadow-sm"
: "border-slate-200 dark:border-slate-700"
}`}
>
🧱 Toprak &amp; Beton
</button>
</div>
</div>
</div>
{/* Footer */}
<div className="flex items-center justify-end gap-2 border-t border-slate-200 bg-slate-50 px-6 py-3.5 dark:border-slate-800 dark:bg-slate-950/50">
<button
type="button"
onClick={onClose}
className="rounded-xl px-4 py-2 text-xs font-semibold text-slate-600 hover:bg-slate-100 dark:text-slate-300 dark:hover:bg-slate-800"
>
İptal
</button>
<button
type="button"
onClick={handleGenerate}
className="flex items-center gap-1.5 rounded-xl bg-emerald-600 px-5 py-2 text-xs font-semibold text-white shadow-sm hover:bg-emerald-500 active:scale-95"
>
<Sparkles className="h-4 w-4" />
<span>Araziyi Oluştur</span>
</button>
</div>
</div>
</div>
);
};