File size: 9,327 Bytes
e34da73 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | document.addEventListener('DOMContentLoaded', () => {
// Initialize Three.js scene
let scene, camera, renderer, controls;
let currentModel = null;
let isARSupported = false;
let isInARMode = false;
// Model gallery data
const modelData = [
{
id: 'bamboo',
title: 'Bamboo Cluster',
description: 'A peaceful cluster of bamboo stalks',
thumbnail: 'https://static.photos/nature/640x360/1',
path: 'https://cdn.glitch.global/8e8b5d9a-7e4d-4a8b-9e8f-8f8e8f8e8f8e/bamboo.glb'
},
{
id: 'bonsai',
title: 'Zen Bonsai',
description: 'A meticulously crafted bonsai tree',
thumbnail: 'https://static.photos/nature/640x360/2',
path: 'https://cdn.glitch.global/8e8b5d9a-7e4d-4a8b-9e8f-8f8e8f8e8f8e/bonsai.glb'
},
{
id: 'fern',
title: 'Lush Fern',
description: 'A vibrant green fern plant',
thumbnail: 'https://static.photos/nature/640x360/3',
path: 'https://cdn.glitch.global/8e8b5d9a-7e4d-4a8b-9e8f-8f8e8f8e8f8e/fern.glb'
},
{
id: 'lotus',
title: 'Floating Lotus',
description: 'A beautiful water lotus flower',
thumbnail: 'https://static.photos/nature/640x360/4',
path: 'https://cdn.glitch.global/8e8b5d9a-7e4d-4a8b-9e8f-8f8e8f8e8f8e/lotus.glb'
},
{
id: 'palm',
title: 'Tropical Palm',
description: 'A tall tropical palm tree',
thumbnail: 'https://static.photos/nature/640x360/5',
path: 'https://cdn.glitch.global/8e8b5d9a-7e4d-4a8b-9e8f-8f8e8f8e8f8e/palm.glb'
},
{
id: 'sakura',
title: 'Cherry Blossom',
description: 'A delicate cherry blossom tree',
thumbnail: 'https://static.photos/nature/640x360/6',
path: 'https://cdn.glitch.global/8e8b5d9a-7e4d-4a8b-9e8f-8f8e8f8e8f8e/sakura.glb'
}
];
// Check for WebXR support
function checkXRSupport() {
if ('xr' in navigator) {
navigator.xr.isSessionSupported('immersive-ar').then((supported) => {
isARSupported = supported;
document.getElementById('xr-button').style.display = supported ? 'block' : 'none';
});
}
}
// Initialize Three.js scene
function initScene() {
const canvas = document.getElementById('ar-viewport');
// Scene
scene = new THREE.Scene();
// Camera
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 1.6, 3);
// Renderer
renderer = new THREE.WebGLRenderer({
canvas: canvas,
antialias: true,
alpha: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.xr.enabled = true;
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.8);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.6);
directionalLight.position.set(0, 10, 5);
scene.add(directionalLight);
// Controls (for non-AR mode)
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
// Load default model
loadModel(modelData[0].path);
// Check XR support
checkXRSupport();
// Start animation loop
animate();
// Hide loading screen
setTimeout(() => {
document.getElementById('loading').style.display = 'none';
}, 1500);
}
// Load 3D model
function loadModel(path) {
const loader = new THREE.GLTFLoader();
if (currentModel) {
scene.remove(currentModel);
}
loader.load(path, (gltf) => {
currentModel = gltf.scene;
currentModel.scale.set(0.5, 0.5, 0.5);
currentModel.position.set(0, 0, 0);
scene.add(currentModel);
}, undefined, (error) => {
console.error('Error loading model:', error);
});
}
// Animation loop
function animate() {
requestAnimationFrame(animate);
if (!isInARMode) {
controls.update();
}
renderer.render(scene, camera);
}
// Initialize AR session
function startARSession() {
if (!isARSupported) return;
const sessionInit = { optionalFeatures: ['dom-overlay', 'dom-overlay-for-handheld-ar'] };
navigator.xr.requestSession('immersive-ar', sessionInit).then((session) => {
isInARMode = true;
document.getElementById('ar-controls').classList.remove('hidden');
renderer.xr.setSession(session);
// Add reticle for placing objects
const reticle = new THREE.Mesh(
new THREE.RingGeometry(0.15, 0.2, 32).rotateX(-Math.PI / 2),
new THREE.MeshBasicMaterial({ color: 0xffffff })
);
reticle.matrixAutoUpdate = false;
reticle.visible = false;
scene.add(reticle);
// Handle session end
session.addEventListener('end', () => {
isInARMode = false;
document.getElementById('ar-controls').classList.add('hidden');
currentModel.position.set(0, 0, 0);
});
// Handle select events (placing objects)
session.addEventListener('select', () => {
if (currentModel && reticle.visible) {
currentModel.position.setFromMatrixPosition(reticle.matrix);
}
});
});
}
// Event listeners
document.getElementById('xr-button').addEventListener('click', startARSession);
document.getElementById('rotate-btn').addEventListener('click', () => {
if (currentModel) {
currentModel.rotation.y += Math.PI / 4;
}
});
document.getElementById('scale-btn').addEventListener('touchstart', (e) => {
if (e.touches.length === 2 && currentModel) {
const touch1 = e.touches[0];
const touch2 = e.touches[1];
const dist1 = Math.hypot(
touch2.pageX - touch1.pageX,
touch2.pageY - touch1.pageY
);
function handleMove(e) {
const touch1 = e.touches[0];
const touch2 = e.touches[1];
const dist2 = Math.hypot(
touch2.pageX - touch1.pageX,
touch2.pageY - touch1.pageY
);
const scale = dist2 / dist1;
currentModel.scale.set(scale, scale, scale);
}
function handleEnd() {
document.removeEventListener('touchmove', handleMove);
document.removeEventListener('touchend', handleEnd);
}
document.addEventListener('touchmove', handleMove);
document.addEventListener('touchend', handleEnd);
}
});
document.getElementById('place-btn').addEventListener('click', () => {
// In AR mode, this would place the object at the reticle position
if (isInARMode && currentModel) {
currentModel.position.set(0, 0, -1);
}
});
// Populate model gallery
function populateModelGallery() {
const gallery = document.getElementById('model-gallery');
modelData.forEach((model) => {
const card = document.createElement('div');
card.className = 'model-card bg-white dark:bg-gray-800 rounded-xl overflow-hidden shadow-md cursor-pointer transition-all';
card.innerHTML = `
<div class="h-48 overflow-hidden">
<img src="${model.thumbnail}" alt="${model.title}" class="w-full h-full object-cover">
</div>
<div class="p-4">
<h3 class="font-bold text-lg mb-1">${model.title}</h3>
<p class="text-gray-600 dark:text-gray-300 text-sm mb-3">${model.description}</p>
<button class="bg-emerald-500 hover:bg-emerald-600 text-white px-3 py-1 rounded-full text-sm transition-colors">
View in AR
</button>
</div>
`;
card.addEventListener('click', () => loadModel(model.path));
gallery.appendChild(card);
});
}
// Initialize everything
populateModelGallery();
initScene();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
}); |