shadows-that-look-right
БесплатноБез исполняемых скриптовНе проверенGet crisp, stable, correctly placed shadows in three.js: enable the shadow map, fit the directional light's shadow camera to the scene bounds, pick a map…
от nirholas★ 289 звёзд на GitHubОб этом скилле
Shadows that look right in three.js
Targets three.js r187 ([email protected]). Every runnable block is executed in Chromium against
a real CC0 model before this plugin ships.
A shadow needs four switches on, and missing any one gives no shadow and no error:
renderer.shadowMap.enabled = truelight.castShadow = true(onlyDirectionalLight,SpotLightandPointLightcast)mesh.castShadow = trueon every caster. A glTF model is a tree, so traverse it.mesh.receiveShadow = trueon every receiver, including the ground.
After that, almost every ugly shadow is the shadow camera being the wrong size. A
DirectionalLight renders its shadow through an orthographic camera that defaults to a
10 x 10 unit box. Larger scenes get clipped shadows; smaller ones waste resolution and look
blocky. Fit it to what casts.
1. A complete, fitted setup
fitShadowCamera sizes the light's orthographic shadow camera to a bounding sphere of the
casters, so the shadow is as sharp as the map size allows and never clipped. Re-run it when
the scene's extent changes.
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.toneMapping = THREE.AgXToneMapping;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap; // the default; soft edges come from shadow.radius
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9dde3);
scene.environment = new THREE.PMREMGenerator(renderer).fromScene(new RoomEnvironment(), 0.04).texture;
scene.environmentIntensity = 0.5; // fill light; the sun below draws the shadow
const gltf = await new GLTFLoader().loadAsync('model.glb');
const model = gltf.scene;
model.traverse((obj) => {
if (obj.isMesh) {
obj.castShadow = true;
obj.receiveShadow = true; // self-shadowing: arms shade the seat
}
});
scene.add(model);
// Stand the model on y = 0 whatever its authored origin.
const box = new THREE.Box3().setFromObject(model);
model.position.y -= box.min.y;
box.setFromObject(model);
const sphere = box.getBoundingSphere(new THREE.Sphere());
const ground = new THREE.Mesh(
new THREE.CircleGeometry(sphere.radius * 4, 64),
new THREE.MeshStandardMaterial({ color: 0xbfc5cc, roughness: 0.9 }),
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const sun = new THREE.DirectionalLight(0xffffff, 3);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.radius = 4; // blur in shadow-map texels
scene.add(sun, sun.target); // the target must be in the scene for its position to count
function fitShadowCamera(light, bounds, direction) {
const { center, radius } = bounds;
light.target.position.copy(center);
light.position.copy(center).addScaledVector(direction.clone().normalize(), radius * 3);
const cam = light.shadow.camera;
cam.left = cam.bottom = -radius;
cam.right = cam.top = radius;
cam.near = radius; // the light sits 3 radii away, so the casters span 2r to 4r
cam.far = radius * 5;
cam.updateProjectionMatrix();
// Bias in world units scales with the scene, so derive it from the texel size.
const texel = (2 * radius) / light.shadow.mapSize.x;
light.shadow.bias = -0.0005;
light.shadow.normalBias = texel * 1.5;
}
fitShadowCamera(sun, sphere, new THREE.Vector3(1, 2, 1.2));
const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, sphere.radius / 50, sphere.radius * 50);
camera.position.copy(sphere.center).add(new THREE.Vector3(-0.6, 0.45, 1).normalize().multiplyScalar(sphere.radius * 3.2));
camera.lookAt(sphere.center);
// Press H to see the shadow camera's box; everything that casts must sit inside it.
const helper = new THREE.CameraHelper(sun.shadow.camera);
helper.visible = false;
scene.add(helper);
window.addEventListener('keydown', (e) => {
if (e.key.toLowerCase() === 'h') helper.visible = !helper.visible;
});
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
renderer.setAnimationLoop(() => renderer.render(scene, camera));
PCFSoftShadowMap was removed in r187: setting it logs a warning and falls back to
PCFShadowMap, which now samples a rotated disk and softens with shadow.radius. Code that
used PCFSoftShadowMap for soft edges should switch to PCFShadowMap plus radius.
2. Diagnose by symptom
| Symptom | Cause | Fix |
|---|---|---|
| No shadow at all | One of the four switches is off, or the light is a HemisphereLight/AmbientLight (they cannot cast) |
Check all four; traverse glTF scenes for castShadow |
| Sha |
Установить shadows-that-look-right в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл shadows-that-look-right?
Get crisp, stable, correctly placed shadows in three.js: enable the shadow map, fit the directional light's shadow camera to the scene bounds, pick a map size, tune bias and normalBias against shadow acne and peter-panning, soften with shadow.radius, catch shadows on an invisible ground with ShadowMaterial, and freeze the shadow map for static scenes. Use when shadows are missing, blocky, pixelated, striped (acne), detached from the object (peter-panning), cut off at an edge, flickering, too dark, or too slow on mobile, or when the user asks how to add shadows or a ground shadow under a model.
Как установить скилл shadows-that-look-right?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл shadows-that-look-right запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
от Anthropicaccess
Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropicconfigure
Set up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
от Anthropicaccess
Manage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropic