{
  "name": "lake",
  "title": "Lake",
  "type": "registry:component",
  "description": "Procedural animated-shader water surface (no textures); place its surface at or below ground level.",
  "dependencies": [
    "@react-three/fiber@^9.6.1",
    "@runek/core@^0.12.0",
    "three@^0.184.0"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "Lake.tsx",
      "content": "import { useFrame } from '@react-three/fiber'\nimport { useWorld, type Vec3 } from '@runek/core'\nimport { useMemo, useRef } from 'react'\nimport * as THREE from 'three'\n\nexport interface LakeProps {\n  position?: Vec3\n  rotation?: Vec3\n  /** Water surface `[width, depth]`, in units. */\n  size?: [number, number]\n  /** Defaults to the world palette's `waterDeep` slot. */\n  colorDeep?: string\n  /** Defaults to the world palette's `waterShallow` slot. */\n  colorShallow?: string\n  /** Direction the sun glint comes from; pair with your Sky's `sunPosition`. */\n  sunPosition?: Vec3\n  waveHeight?: number\n  waveSpeed?: number\n  segments?: number\n}\n\nconst VERTEX = /* glsl */ `\n  uniform float uTime;\n  uniform float uWaveHeight;\n  uniform float uWaveSpeed;\n  varying vec3 vWorldPos;\n  varying vec3 vNormal;\n  varying float vWave;\n\n  float waveH(vec2 q, float t) {\n    float w1 = sin(q.x * 0.6 + t) * cos(q.y * 0.4 + t * 0.8);\n    float w2 = sin(q.x * 0.2 - t * 0.7) * sin(q.y * 0.8 + t);\n    float w3 = sin((q.x + q.y) * 1.5 + t * 1.6) * 0.25;\n    return (w1 + w2 + w3) * 0.5;\n  }\n\n  void main() {\n    float t = uTime * uWaveSpeed;\n    vec3 p = position;\n    float h = waveH(p.xy, t);\n    vWave = h;\n    p.z += h * uWaveHeight;\n\n    // finite-difference normal so the light reacts to the waves\n    float eps = 0.35;\n    float hx = waveH(position.xy + vec2(eps, 0.0), t);\n    float hy = waveH(position.xy + vec2(0.0, eps), t);\n    vec3 n = normalize(vec3(-(hx - h) * uWaveHeight / eps, -(hy - h) * uWaveHeight / eps, 1.0));\n    vNormal = normalize(mat3(modelMatrix) * n);\n\n    vWorldPos = (modelMatrix * vec4(p, 1.0)).xyz;\n    gl_Position = projectionMatrix * viewMatrix * vec4(vWorldPos, 1.0);\n  }\n`\n\nconst FRAGMENT = /* glsl */ `\n  uniform vec3 uColorDeep;\n  uniform vec3 uColorShallow;\n  uniform vec3 uSunDir;\n  varying vec3 vWorldPos;\n  varying vec3 vNormal;\n  varying float vWave;\n\n  void main() {\n    vec3 n = normalize(vNormal);\n    vec3 viewDir = normalize(cameraPosition - vWorldPos);\n\n    float m = smoothstep(-1.0, 1.0, vWave);\n    vec3 col = mix(uColorDeep, uColorShallow, m);\n\n    // sky tint at grazing angles\n    float fresnel = pow(1.0 - max(dot(n, viewDir), 0.0), 3.0);\n    col = mix(col, vec3(0.62, 0.78, 0.9), fresnel * 0.55);\n\n    // sun glint\n    vec3 r = reflect(-uSunDir, n);\n    float glint = pow(max(dot(r, viewDir), 0.0), 70.0);\n    col += vec3(1.0, 0.95, 0.85) * glint * 0.7;\n\n    // crest brighten, as before\n    col += pow(max(m - 0.6, 0.0), 2.0) * 0.4;\n\n    gl_FragColor = vec4(col, 0.78 + fresnel * 0.18);\n  }\n`\n\n/** Procedural animated water (no textures), decorative (no collider). The local\n *  origin is the water surface: place it at or below the surrounding ground so it\n *  fills a depression rather than floating (CONTRACT §10). */\nexport function Lake({\n  position,\n  rotation = [0, 0, 0],\n  size = [20, 20],\n  colorDeep,\n  colorShallow,\n  sunPosition = [80, 30, 40],\n  waveHeight = 0.12,\n  waveSpeed = 1,\n  segments = 64,\n}: LakeProps) {\n  const { unit, palette, ground } = useWorld()\n  const deep = colorDeep ?? palette.waterDeep\n  const shallow = colorShallow ?? palette.waterShallow\n  // Surface sits at the world ground baseline by default; explicit position wins.\n  const pos = position ?? [0, ground, 0]\n  const w = size[0] * unit\n  const d = size[1] * unit\n  const matRef = useRef<THREE.ShaderMaterial>(null)\n\n  const uniforms = useMemo(\n    () => ({\n      uTime: { value: 0 },\n      uWaveHeight: { value: waveHeight * unit },\n      uWaveSpeed: { value: waveSpeed },\n      uColorDeep: { value: new THREE.Color(deep) },\n      uColorShallow: { value: new THREE.Color(shallow) },\n      uSunDir: { value: new THREE.Vector3(...sunPosition).normalize() },\n    }),\n    [waveHeight, waveSpeed, deep, shallow, sunPosition, unit],\n  )\n\n  useFrame((_, delta) => {\n    if (matRef.current) matRef.current.uniforms.uTime.value += delta\n  })\n\n  return (\n    <mesh position={pos} rotation={[-Math.PI / 2 + rotation[0], rotation[1], rotation[2]]}>\n      <planeGeometry args={[w, d, segments, segments]} />\n      <shaderMaterial\n        ref={matRef}\n        uniforms={uniforms}\n        vertexShader={VERTEX}\n        fragmentShader={FRAGMENT}\n        transparent\n      />\n    </mesh>\n  )\n}\n",
      "type": "registry:component"
    }
  ]
}
