Skip to content

Instantly share code, notes, and snippets.

@finia2NA
Created June 8, 2025 18:34
Show Gist options
  • Select an option

  • Save finia2NA/0ba17eb2c4b9908e7b4afbe2138d3d62 to your computer and use it in GitHub Desktop.

Select an option

Save finia2NA/0ba17eb2c4b9908e7b4afbe2138d3d62 to your computer and use it in GitHub Desktop.
Simple sphere env mapping in three.js
import './style.css';
import * as THREE from 'three';
// Load environment texture
const texture = new THREE.TextureLoader().load('/test.jpg');
// Set up scene and camera
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
// Camera at origin
camera.position.set(0, 0, 0);
// Renderer
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Sphere geometry and basic material
const geometry = new THREE.SphereGeometry(5, 64, 64);
const material = new THREE.MeshBasicMaterial({ map: texture, side: THREE.DoubleSide });
// Create mesh and add to scene
const sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);
let isDragging = false;
let previousMousePosition = { x: 0, y: 0 };
// Add event listeners for mouse interaction
document.addEventListener('mousedown', (event) => {
isDragging = true;
previousMousePosition = { x: event.clientX, y: event.clientY };
});
document.addEventListener('mousemove', (event) => {
if (isDragging) {
const deltaX = event.clientX - previousMousePosition.x;
const deltaY = event.clientY - previousMousePosition.y;
sphere.rotation.y += deltaX * 0.01;
sphere.rotation.x += deltaY * 0.01;
previousMousePosition = { x: event.clientX, y: event.clientY };
}
});
document.addEventListener('mouseup', () => {
isDragging = false;
});
// Animation loop
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
// Handle resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment