跪拜 Guibai
← All articles
Frontend · Backend · AI Programming

A 3D Watermelon Built with Three.js and Procedural Canvas Textures

By AI编程实验室 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Procedural Canvas textures sidestep asset pipelines for small 3D web scenes, keeping the bundle light and every visual parameter tweakable in code. The iteration log — removing a slice, killing fog, tuning a radial gradient — is a concrete map of how to turn a generic 3D model into a specific, convincing object.

Summary

A single interactive 3D watermelon scene, built with Three.js and Vite, renders a ripe, chilled half-melon without any external texture files. The rind's irregular stripes and the flesh's radial gradient are both generated at runtime on HTML Canvas elements and mapped onto the geometry as CanvasTextures. The scene supports mouse and touch drag rotation, a subtle auto-rotate idle state, and an optional ambient soundscape triggered by a button.

The most instructive part of the build was the iterative refinement away from an uncanny first draft. An extra slice was removed because it distracted from the main subject. The rind was rebuilt to replace uniform stripes with procedurally varied widths, curves, and shades. Flesh colors were adjusted through several radial-gradient passes to eliminate a pale, unripe look and a hard plastic-like edge between the red and white. Disabling a scene-wide fog effect that washed out contrast was the final step that made the model read as clean and cold.

This workflow demonstrates that for stylized 3D web pieces, code-generated textures offer more control than downloaded images. The entire visual identity — stripe irregularity, flesh saturation, droplet transparency — stays adjustable in a few lines of JavaScript, and the page loads with zero texture requests.

Takeaways
A flattened hemisphere forms the watermelon body: sphere geometry below, a circular plane for the cut flesh on top.
Rind stripes are generated on a Canvas with varying widths, curves, and shades to avoid uniform, artificial-looking repetition.
Flesh color uses a radial gradient that transitions from deep red through light red and pale yellow-white to the green edge, eliminating a hard plastic-like boundary.
Seeds are scaled spheres; condensation droplets use a physical material with high transparency and low roughness.
Removing a scene-wide fog effect restored the watermelon's color saturation, contrast, and clean silhouette.
Interaction maps pointer-down and move events directly to horizontal rotation and pitch, with a slight auto-rotation when idle.
An optional ambient sound layer is toggled via a button using the Web Audio API.
Conclusions

User feedback like "looks unripe" or "stripes are all the same" maps directly to material, texture, and lighting parameters, making visual iteration a technical rather than purely artistic problem.

Canvas-generated textures give stylized 3D projects more control than downloaded images because color, proportion, and randomness stay adjustable in code without re-exporting assets.

The difference between a generic 3D model and a convincing object often comes down to removing effects — an extra slice, a fog pass — rather than adding more detail.

Concepts & terms
CanvasTexture
A Three.js texture created from an HTML Canvas element, allowing textures to be generated programmatically with the Canvas 2D API instead of loaded from image files.
Radial gradient
A gradient that radiates outward from a center point, used here on a Canvas to smoothly blend the watermelon's deep red flesh into the lighter rind without a hard edge.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗