Get sun color from sky material?

Is it possible to get the sun color from the sky material somehow?

I’m trying to match the diffuse color of a directional shadow caster light (the Sun) with the sky material sun color (i.e. warm golden light at sunrise and sunset versus cool white light at noon) so I can create dynamic “time of day” lighting and shadow simulations.

Hi @inteja

I checked the documentation but didn’t find something useful for you use case.

I would try to get the material textures:
https://doc.babylonjs.com/api/classes/babylon.material#getrendertargettextures

And then read pixels using RenderTargetTexture - Babylon.js Documentation to get the computed sky color.

Do you know a better solution @Deltakosh ?

2 Likes

Hi guys. Here’s a skyMaterial PG with a “cycling inclination” to play-with. Be patient. :slight_smile:

https://www.babylonjs-playground.com/#4R1H1U#3

Line 21 - I report the sphere’s reflectionProbe._renderTargetTexture to console… but just once… and it’s mostly black at that time.

In real time, and using Cedric’s interesting idea, Integra will probably want to sample some pixel colors about… what? Every 5 minutes? (dependent upon many factors). Interesting.

BJS.Tools.createCCDchip() :wink: (Same as RTT/imageBuffer-work, you say? yeah, I guess so).

1 Like

Thanks for the suggestions @Cedric and @Wingnut!

I tried Cedrics approach to color the scene fog. Used a small reflection probe, actually 4 pixels should be fine, too. Sometimes the fog lights up though.

var rp = new ReflectionProbe("skyReflection", 16, scene)
rp.renderList.push(skyBox);
pixels = rp.cubeTexture.readPixels(0,0)
// i take the first pixel of the reflection probe texture for fog color.
// since pixels are stored as buffer array, first pixel are first 4 values of array [r,g,b,a....]
scene.fogColor = new Color3(pixels[0]/255, pixels[1]/255, pixels[2]/255)

hope it helps some other people.

Edit: Maybe it is necessary to assign the cubeTexture to some object material in the scene. My reflection probe texture was only updating, when this object was visible. Assigning the cubeTexture to scene.environmentTexture does not seem to be enough.

4 Likes

Thanks for that hint! I was about to give up in my attempt :slight_smile:

You can use camera.customRenderTargets.push(renderTargetTexture); to ensure the target texture is updated, as noted in Optimizing Your Scene | Babylon.js Documentation .

I am then able to do: scene.environmentTexture = envReflectionProbe.cubeTexture;.

Also remember that this will affect PBRMaterials lighting and their reflections. You may want to tweak
material.environmentIntensity (default is 1.0) if, like me, you are mixing PBR and non PBR materials which react differently to light and you had changed it previously.

1 Like