3D Parametrik Geometri (Küre, Silindir, Torus, Dalga Mesh), Phong Speküler Işıklandırma ve 3D Raycaster Motoru

📦 _db_core PAKET #11 ✨ Blinn-Phong Speküler Aydınlatma 🎯 3D Raycasting & Nesne Seçim Motoru

3D Parametrik Geometri (Küre, Silindir, Torus, Dalga Mesh), Phong Speküler Işıklandırma ve 3D Raycaster Motoru

Sıfır harici kütüphane ile küresel koordinatlardan 3B Küre (Sphere), 3B Torus (Halka), Silindir/Piston ve Dinamik Sinüs Yüzeyi üreten; yüzey normallerini Ambient + Diffuse + Specular (Blinn-Phong) modeliyle aydınlatan ve fare/dokunma ile 3B uzaydaki düğümleri yakalayan (3D Raycasting) mühendislik çekirdeği.

Mod: Küre + 3D Atom Düğümleri | Sürükle: 360° Orbit 🎯 3D Raycast: Bir 3B düğüme veya yüzeye tıklayın (X: 0.0, Y: 0.0, Z: 0.0)

1. Parametrik 3D Küre ve 3D Torus Mesh Üretici Fonksiyonları

Küresel ve toroidal denklemler kullanılarak enlem (latBands) ve boylam (lonBands) adımlarından dörtgen (quad) yüzeyler ve analitik yüzey normalleri üretilir.

// Parametrik 3B Küre (Sphere) ve 3B Torus (Halka) Mesh Üreticisi
function generateSphereMesh(radius = 110, latBands = 14, lonBands = 20) {
  const vertices = [], faces = [];
  for (let lat = 0; lat <= latBands; lat++) {
    const theta = (lat * Math.PI) / latBands;
    const sinT = Math.sin(theta), cosT = Math.cos(theta);
    for (let lon = 0; lon <= lonBands; lon++) {
      const phi = (lon * 2 * Math.PI) / lonBands;
      const nx = Math.cos(phi) * sinT, ny = cosT, nz = Math.sin(phi) * sinT;
      vertices.push({ x: radius * nx, y: radius * ny, z: radius * nz, nx, ny, nz });
    }
  }
  for (let lat = 0; lat < latBands; lat++) {
    for (let lon = 0; lon < lonBands; lon++) {
      const a = lat * (lonBands + 1) + lon;
      const b = a + lonBands + 1;
      faces.push([a, b, b + 1, a + 1]);
    }
  }
  return { vertices, faces };
}

function generateTorusMesh(R = 95, r = 38, segsU = 22, segsV = 14) {
  const vertices = [], faces = [];
  for (let i = 0; i <= segsU; i++) {
    const u = (i * 2 * Math.PI) / segsU;
    const cosU = Math.cos(u), sinU = Math.sin(u);
    for (let j = 0; j <= segsV; j++) {
      const v = (j * 2 * Math.PI) / segsV;
      const cosV = Math.cos(v), sinV = Math.sin(v);
      const x = (R + r * cosV) * cosU;
      const y = r * sinV;
      const z = (R + r * cosV) * sinU;
      vertices.push({ x, y, z, nx: cosV * cosU, ny: sinV, nz: cosV * sinU });
    }
  }
  for (let i = 0; i < segsU; i++) {
    for (let j = 0; j < segsV; j++) {
      const a = i * (segsV + 1) + j, b = a + segsV + 1;
      faces.push([a, b, b + 1, a + 1]);
    }
  }
  return { vertices, faces };
}

2. Blinn-Phong Speküler Aydınlatma Modeli (Ambient + Diffuse + Specular)

Metalik, cam veya organik 3B yüzey hissi oluşturmak için yüzey normali $\vec{N}$, ışık yönü $\vec{L}$ ve yarı-açı vektörü $\vec{H} = \text{normalize}(\vec{L} + \vec{V})$ üzerinden parlak yansıma (Specular Highlight) hesaplanır.

// Blinn-Phong 3B Yüzey Renk ve Parlama Hesaplayıcısı
function computePhongColor(normal, lightDir, baseRGB, shininess = 28) {
  const dotNL = Math.max(0, normal.x * lightDir.x + normal.y * lightDir.y + normal.z * lightDir.z);
  const ambient = 0.22;
  const diffuse = 0.68 * dotNL;
  // Kamera bakış yönü V = (0, 0, -1) için Halfway vektörü H = normalize(L + V)
  const hx = lightDir.x, hy = lightDir.y, hz = lightDir.z - 1;
  const hLen = Math.hypot(hx, hy, hz) || 1;
  const dotNH = Math.max(0, (normal.x * hx + normal.y * hy + normal.z * hz) / hLen);
  const specular = Math.pow(dotNH, shininess) * 0.85;

  const r = Math.min(255, Math.round(baseRGB[0] * (ambient + diffuse) + 255 * specular));
  const g = Math.min(255, Math.round(baseRGB[1] * (ambient + diffuse) + 255 * specular));
  const b = Math.min(255, Math.round(baseRGB[2] * (ambient + diffuse) + 255 * specular));
  return `rgb(${r},${g},${b})`;
}

3. 3D Raycasting & Ekran Noktasından 3D Küre/Yüzey Yakalama

Kullanıcının 2B kanvas üzerinde tıkladığı (screenX, screenY) koordinatından 3B sahnedeki dönüştürülmüş nesnelerin izdüşüm yarıçapları ve derinlik (z) sırası taranarak en öndeki 3B nesne seçilir.

// Ekran Koordinatından 3B Küre / Düğümve Yüzey Hit-Test (Raycasting)
function raycastSphere3D(screenX, screenY, projectedNodes) {
  let bestHit = null;
  let minDepth = Infinity;
  for (const node of projectedNodes) {
    const dx = screenX - node.sx;
    const dy = screenY - node.sy;
    const dist = Math.hypot(dx, dy);
    if (dist <= node.screenRadius * 1.25 && node.wz < minDepth) {
      minDepth = node.wz;
      bestHit = node;
    }
  }
  return bestHit; // Seçilen 3B nesnenin (x, y, z) ve normal verisini döndürür
}
🌐 Parametrik 3B Primitifler Küre, Torus, Silindir/Piston ve zaman-bağımlı $z = f(x,y,t)$ dalga yüzeyleri.
💎 Speküler Parlama Blinn-Phong yarı-vektör hesabı ile gerçekçi metalik/atomik parlama noktası.
🎯 İnteraktif 3D Seçim Tıklanan 3B atom düğümünü veya yüzey poligonunu derinlik öncelikli yakalar.

Yorumlar

Yorum Gönder

Opromt · Ücretsiz yayınlarımız sizler için , Kopyala, yapıştır, kullan. ← Ana Sayfa