multimodalart's picture
multimodalart HF Staff
Upload folder using huggingface_hub
19c0aab verified
Raw History Blame Contribute Delete
18.7 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Sakura Bonsai</title>
<style>
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #eee4d8;
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.info {
position: fixed;
top: 22px;
left: 24px;
z-index: 2;
color: #5b3c45;
pointer-events: none;
user-select: none;
}
.info h1 {
margin: 0 0 6px;
font-size: clamp(20px, 3vw, 30px);
font-weight: 650;
letter-spacing: -0.03em;
}
.info p {
margin: 0;
font-size: 13px;
opacity: 0.7;
}
.reset {
position: fixed;
right: 20px;
bottom: 20px;
z-index: 3;
border: 0;
border-radius: 999px;
padding: 10px 16px;
color: #5b3c45;
background: rgba(255, 255, 255, 0.72);
backdrop-filter: blur(10px);
box-shadow: 0 6px 20px rgba(74, 45, 53, 0.12);
font: inherit;
font-size: 13px;
cursor: pointer;
}
.reset:hover {
background: rgba(255, 255, 255, 0.92);
}
</style>
</head>
<body>
<div class="info">
<h1>Sakura Bonsai</h1>
<p>Drag to orbit · Scroll to zoom · Double-click to reset</p>
</div>
<button class="reset" id="reset">Reset view</button>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, scene, and camera
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee4d8);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xfff7ef, 0x8e8378, 2.1));
const sun = new THREE.DirectionalLight(0xfff0df, 3.2);
sun.position.set(-4, 8, 5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -6;
sun.shadow.camera.right = 6;
sun.shadow.camera.top = 7;
sun.shadow.camera.bottom = -2;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 20;
sun.shadow.bias = -0.0007;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xffc1d3, 1.1);
fill.position.set(5, 4, -4);
scene.add(fill);
// ------------------------------------------------------------
// Helpers
// ------------------------------------------------------------
const materials = {
cream: new THREE.MeshStandardMaterial({
color: 0xcbb89e,
roughness: 0.83,
metalness: 0.02
}),
terracotta: new THREE.MeshStandardMaterial({
color: 0xb86f58,
roughness: 0.9
}),
rim: new THREE.MeshStandardMaterial({
color: 0x9e5548,
roughness: 0.88
}),
soil: new THREE.MeshStandardMaterial({
color: 0x4c332b,
roughness: 1
}),
bark: new THREE.MeshStandardMaterial({
color: 0x694638,
roughness: 0.98
}),
barkLight: new THREE.MeshStandardMaterial({
color: 0x8a5b43,
roughness: 0.96
}),
petal: new THREE.MeshStandardMaterial({
color: 0xf08eb1,
roughness: 0.78
}),
floor: new THREE.MeshStandardMaterial({
color: 0xd8c9b9,
roughness: 1
}),
stone: new THREE.MeshStandardMaterial({
color: 0x9a958a,
roughness: 1
})
};
function addBox(parent, size, position, material, rotationY = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.rotation.y = rotationY;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addBeam(parent, start, end, width, material) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const beam = new THREE.Mesh(
new THREE.BoxGeometry(width, length, width),
material
);
beam.position.copy(start).add(end).multiplyScalar(0.5);
beam.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
beam.castShadow = true;
beam.receiveShadow = true;
parent.add(beam);
return beam;
}
function randomBetween(min, max) {
return min + Math.random() * (max - min);
}
// ------------------------------------------------------------
// Ground and pot
// ------------------------------------------------------------
addBox(
scene,
new THREE.Vector3(30, 0.12, 30),
new THREE.Vector3(0, -0.08, 0),
materials.floor
);
const pot = new THREE.Group();
scene.add(pot);
addBox(
pot,
new THREE.Vector3(1.85, 0.26, 1.42),
new THREE.Vector3(0, 0.13, 0),
materials.terracotta
);
addBox(
pot,
new THREE.Vector3(2.18, 0.86, 1.55),
new THREE.Vector3(0, 0.68, 0),
materials.cream
);
addBox(
pot,
new THREE.Vector3(2.22, 0.15, 1.58),
new THREE.Vector3(0, 0.68, 0),
materials.terracotta
);
addBox(
pot,
new THREE.Vector3(2.52, 0.18, 1.82),
new THREE.Vector3(0, 1.18, 0),
materials.rim
);
addBox(
pot,
new THREE.Vector3(2.31, 0.1, 1.64),
new THREE.Vector3(0, 1.3, 0),
materials.soil
);
// Small decorative pot corners.
for (const x of [-1.13, 1.13]) {
for (const z of [-0.82, 0.82]) {
addBox(
pot,
new THREE.Vector3(0.2, 0.2, 0.2),
new THREE.Vector3(x, 1.24, z),
materials.rim
);
}
}
// Pebbles beside the pot.
addBox(
scene,
new THREE.Vector3(0.42, 0.22, 0.34),
new THREE.Vector3(1.72, 0.08, 0.58),
materials.stone,
-0.25
);
addBox(
scene,
new THREE.Vector3(0.32, 0.18, 0.28),
new THREE.Vector3(-1.65, 0.05, 0.74),
materials.stone,
0.35
);
// ------------------------------------------------------------
// Voxel trunk and branches
// ------------------------------------------------------------
const tree = new THREE.Group();
tree.position.y = 1.32;
scene.add(tree);
const trunkPieces = [
[-0.02, 0.12, 0, 0.43, 0.43],
[0.02, 0.56, 0.02, 0.4, 0.4],
[-0.06, 0.99, 0.04, 0.38, 0.39],
[0.02, 1.41, 0.02, 0.36, 0.36],
[-0.12, 1.82, -0.03, 0.33, 0.33],
[0.04, 2.22, 0.05, 0.29, 0.29]
];
for (const [x, y, z, w, h] of trunkPieces) {
addBox(
tree,
new THREE.Vector3(w, h, w),
new THREE.Vector3(x, y, z),
((x + y) % 1 < 0) ? materials.bark : materials.barkLight
);
}
const branchEnds = [
new THREE.Vector3(-1.2, 2.34, 0.16),
new THREE.Vector3(1.08, 2.78, -0.16),
new THREE.Vector3(-1.42, 3.02, -0.2),
new THREE.Vector3(0.28, 3.31, 0.23),
new THREE.Vector3(-0.78, 3.47, -0.35),
new THREE.Vector3(1.34, 3.18, 0.28)
];
addBeam(
tree,
new THREE.Vector3(0, 1.48, 0.02),
branchEnds[0],
0.25,
materials.bark
);
addBeam(
tree,
new THREE.Vector3(-0.03, 1.84, 0.01),
branchEnds[1],
0.23,
materials.barkLight
);
addBeam(
tree,
new THREE.Vector3(-0.08, 1.86, -0.02),
branchEnds[2],
0.22,
materials.bark
);
addBeam(
tree,
new THREE.Vector3(-0.08, 2.16, 0.03),
branchEnds[3],
0.2,
materials.barkLight
);
addBeam(
tree,
new THREE.Vector3(-0.05, 2.3, 0.02),
branchEnds[4],
0.17,
materials.bark
);
addBeam(
tree,
new THREE.Vector3(0.02, 2.36, 0.03),
branchEnds[5],
0.18,
materials.barkLight
);
// ------------------------------------------------------------
// Voxel leaf blocks underneath the blossoms
// ------------------------------------------------------------
const foliageMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.93
});
const foliageCount = 64;
const foliageMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
foliageMaterial,
foliageCount
);
foliageMesh.castShadow = true;
foliageMesh.receiveShadow = true;
tree.add(foliageMesh);
const dummy = new THREE.Object3D();
const foliageColors = [0x81966a, 0x94a879, 0x758e63, 0xa4b384];
for (let i = 0; i < foliageCount; i++) {
const angle = randomBetween(0, Math.PI * 2);
const radius = Math.sqrt(Math.random());
const x = Math.cos(angle) * radius * 1.58;
const z = Math.sin(angle) * radius * 1.02;
const y = 3.15 + randomBetween(-0.82, 0.62);
const size = randomBetween(0.28, 0.68);
dummy.position.set(x, y, z);
dummy.rotation.set(
randomBetween(-0.18, 0.18),
randomBetween(0, Math.PI),
randomBetween(-0.18, 0.18)
);
dummy.scale.set(size, size * randomBetween(0.65, 1.05), size);
dummy.updateMatrix();
foliageMesh.setMatrixAt(i, dummy.matrix);
foliageMesh.setColorAt(
i,
new THREE.Color(foliageColors[Math.floor(Math.random() * foliageColors.length)])
);
}
foliageMesh.instanceMatrix.needsUpdate = true;
foliageMesh.instanceColor.needsUpdate = true;
// ------------------------------------------------------------
// Instanced voxel blossoms
// ------------------------------------------------------------
const blossomClusters = 52;
const petalsPerCluster = 5;
const blossomMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.72
});
const blossoms = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
blossomMaterial,
blossomClusters * petalsPerCluster
);
blossoms.castShadow = true;
blossoms.receiveShadow = true;
tree.add(blossoms);
const blossomColors = [
0xef7fa6,
0xf29db8,
0xf6b4c8,
0xffc4d5,
0xffdbe4
];
let blossomIndex = 0;
for (let cluster = 0; cluster < blossomClusters; cluster++) {
const angle = randomBetween(0, Math.PI * 2);
const radius = Math.sqrt(Math.random());
const centerX = Math.cos(angle) * radius * 1.5;
const centerZ = Math.sin(angle) * radius * 0.96;
const centerY = 3.1 + randomBetween(-0.92, 0.68);
const clusterScale = randomBetween(0.78, 1.25);
for (let petal = 0; petal < petalsPerCluster; petal++) {
const petalOffset = new THREE.Vector3();
if (petal === 0) {
petalOffset.set(0, 0.02, 0);
} else {
const direction = (petal - 1) * Math.PI * 0.5 + randomBetween(-0.2, 0.2);
petalOffset.set(
Math.cos(direction) * 0.13,
randomBetween(-0.025, 0.035),
Math.sin(direction) * 0.13
);
}
dummy.position.set(
centerX + petalOffset.x,
centerY + petalOffset.y,
centerZ + petalOffset.z
);
dummy.rotation.set(
randomBetween(-0.22, 0.22),
randomBetween(0, Math.PI),
randomBetween(-0.22, 0.22)
);
dummy.scale.set(
randomBetween(0.18, 0.25) * clusterScale,
randomBetween(0.13, 0.19) * clusterScale,
randomBetween(0.18, 0.25) * clusterScale
);
dummy.updateMatrix();
blossoms.setMatrixAt(blossomIndex, dummy.matrix);
blossoms.setColorAt(
blossomIndex,
new THREE.Color(
blossomColors[Math.floor(Math.random() * blossomColors.length)]
)
);
blossomIndex++;
}
}
blossoms.instanceMatrix.needsUpdate = true;
blossoms.instanceColor.needsUpdate = true;
// ------------------------------------------------------------
// Falling petals
// ------------------------------------------------------------
const fallingPetals = [];
const petalGeometry = new THREE.BoxGeometry(0.16, 0.035, 0.23);
for (let i = 0; i < 26; i++) {
const petal = new THREE.Mesh(petalGeometry, materials.petal);
petal.castShadow = true;
petal.position.set(
randomBetween(-1.8, 1.8),
randomBetween(1.2, 5.4),
randomBetween(-1.4, 1.4)
);
petal.rotation.set(
randomBetween(0, Math.PI),
randomBetween(0, Math.PI),
randomBetween(0, Math.PI)
);
petal.userData = {
speed: randomBetween(0.22, 0.48),
driftX: randomBetween(-0.1, 0.1),
driftZ: randomBetween(-0.08, 0.08),
spin: randomBetween(-1.4, 1.4)
};
scene.add(petal);
fallingPetals.push(petal);
}
// ------------------------------------------------------------
// Custom camera controls: orbit, zoom, and keyboard support
// ------------------------------------------------------------
const controls = {
theta: 0.55,
phi: 1.12,
radius: 8.6,
target: new THREE.Vector3(0, 2.15, 0),
dragging: false,
previousX: 0,
previousY: 0
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x + controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
function resetCamera() {
controls.theta = 0.55;
controls.phi = 1.12;
controls.radius = 8.6;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
controls.dragging = true;
controls.previousX = event.clientX;
controls.previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!controls.dragging) return;
const dx = event.clientX - controls.previousX;
const dy = event.clientY - controls.previousY;
controls.previousX = event.clientX;
controls.previousY = event.clientY;
controls.theta -= dx * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.005,
0.32,
1.48
);
updateCamera();
});
function stopDragging(event) {
controls.dragging = false;
renderer.domElement.style.cursor = "grab";
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("pointerleave", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius + event.deltaY * 0.008,
4.2,
15
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
document.getElementById("reset").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
const step = 0.1;
if (event.key === "ArrowLeft") controls.theta += step;
if (event.key === "ArrowRight") controls.theta -= step;
if (event.key === "ArrowUp") controls.phi = Math.max(0.32, controls.phi - step);
if (event.key === "ArrowDown") controls.phi = Math.min(1.48, controls.phi + step);
if (event.key === "+" || event.key === "=") {
controls.radius = Math.max(4.2, controls.radius - 0.4);
}
if (event.key === "-") {
controls.radius = Math.min(15, controls.radius + 0.4);
}
if (event.key === "r" || event.key === "R") resetCamera();
updateCamera();
});
renderer.domElement.style.cursor = "grab";
updateCamera();
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
for (const petal of fallingPetals) {
petal.position.y -= petal.userData.speed * delta;
petal.position.x += petal.userData.driftX * delta;
petal.position.z += petal.userData.driftZ * delta;
petal.rotation.x += petal.userData.spin * delta;
petal.rotation.y += petal.userData.spin * 0.65 * delta;
petal.rotation.z += petal.userData.spin * 0.4 * delta;
if (petal.position.y < 0.03) {
petal.position.set(
randomBetween(-1.8, 1.8),
randomBetween(4.5, 5.6),
randomBetween(-1.4, 1.4)
);
}
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>