Move the sun of a procedural sky
Updated
The cheap procedural Sky, with a sun disc: the script steps the sun’s elevation from morning to a low sunset and back, raising the haze near the horizon, and aims the Sun light along sunDirection.
Watch the sky redden as the sun sinks to the horizon, then rise again. Drag to orbit the camera, scroll to zoom.
Move the sun of a procedural sky
#pragma lifecycle(startup, update)
// Each sky change recomputes the sky, so update it only twice a second.let sky;let sun;let sunLight;let wait = 0;
function startup() { sky = scene.findEntity(scene.rootId).getComponent(SkyComponent); sun = findEntity('Sun'); sunLight = sun.getComponent(DirectionalLightComponent);}
function update(dt, time) { wait -= dt; if (wait > 0) return; wait = 0.5; const low = (1 - Math.cos((time / 20) * M_TAU)) / 2; // 0 at noon-ish, 1 at sunset sky.setProps({ elevation: 25 - low * 23, turbidity: 2 + low * 8, rayleigh: 3 + low }); sun.lookAt(sun.position.clone().add(sky.sunDirection)); sunLight.setProps({ intensity: 0.4 + (1 - low) * 1.8, color: low > 0.6 ? '#ffb27a' : '#ffffff' }); if (Math.abs(low - 1) < 0.01 || low < 0.01) log(`Sun at ${Math.round(sky.props.elevation)}°`);}Open it in XR Creator
Section titled “Open it in XR Creator”Download the project file below, then import it in XR Hub with Projects › New › Import, as described in Projects and templates. It opens in XR Creator with the same scene you see running above, each script already attached to its Behavior entity. From there you can edit the scripts, adjust components and their props, preview your changes, and deploy the result as a world of your own.
ProceduralSky.xrproj