Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flashlight #6553

Closed
mramato opened this issue May 2, 2018 · 7 comments · Fixed by #8493
Closed

Flashlight #6553

mramato opened this issue May 2, 2018 · 7 comments · Fixed by #8493

Comments

@mramato
Copy link
Contributor

mramato commented May 2, 2018

When loading arbitrary data in Cesium, it often ends up being night-time at the data location so nothing is lit and the entire scene is washed out. In these cases I don't care about the time and having to turn on and fiddle with the timeline is a pain. I just want to be able to see the details of stuff on the screen.

I've seen other apps offer a "Flashlight" button that puts a light source at the camera location pointing in the camera direction (or in all directions). One hack I thought of was putting the Sun at the camera location, but that proved tricky because the implementation is not very monkey-patchable.

While I know arbitrary light sources are on the long term roadmap, what's the simplest thing (even if it's a temporary workaround done at the app level) that we can do in the meantime?

CC @lilleyse @bagnell

@mramato
Copy link
Contributor Author

mramato commented May 2, 2018

Also CCing @emackey because this sounds like the type of problem I might be able to nerd snipe him on

@lilleyse
Copy link
Contributor

lilleyse commented May 2, 2018

Related PR for models and 3D Tiles - #6160.
Code parked on https://github.com/AnalyticalGraphicsInc/cesium/tree/enable-lighting

Though we would probably want this to be a scene level property.

@emackey
Copy link
Contributor

emackey commented May 2, 2018

Here's a function that will guess a time within +/- 12 hours that any given point on the globe has daylight:

/////////////////////////////////////////////////////////////////////////
var scratchFindDaytimeDateA = new Cesium.JulianDate();
var scratchFindDaytimeMatrixA = new Cesium.Matrix3();
var scratchFindDaytimeCartesianA = new Cesium.Cartesian3();
var scratchFindDaytimeCartesianB = new Cesium.Cartesian3();
var scratchFindDaytimeCartesianC = new Cesium.Cartesian3();

function findDaytimeForPoint(position, currentTime, result) {
    result = result || new Cesium.JulianDate();
    currentTime.clone(result);
    
    var posVector = Cesium.Cartesian3.normalize(position, scratchFindDaytimeCartesianA);
    var highestDot = -2;

    // During a 24-hour span, the Sun's apparent Inertial position doesn't change much.
    var sunInertial = Cesium.Simon1994PlanetaryPositions.computeSunPositionInEarthInertialFrame(currentTime, scratchFindDaytimeCartesianB);

    // Test the ECF position each hour over 24 hours (+/- 12 hours from current time).
    var sampleTime = Cesium.JulianDate.addHours(currentTime, -12, scratchFindDaytimeDateA);
    for (var h = 0; h < 24; ++h) {
        var temeTransform = Cesium.Transforms.computeTemeToPseudoFixedMatrix(sampleTime, scratchFindDaytimeMatrixA);
        var sunPositionWc = Cesium.Matrix3.multiplyByVector(temeTransform, sunInertial, scratchFindDaytimeCartesianC);
        var sunVector = Cesium.Cartesian3.normalize(sunPositionWc, sunPositionWc);
        var dotProduct = Cesium.Cartesian3.dot(posVector, sunVector);
        
        if (dotProduct > highestDot) {
            highestDot = dotProduct;
            sampleTime.clone(result);
        }
        
        Cesium.JulianDate.addHours(sampleTime, 1, sampleTime);
    }
    return result;
}
/////////////////////////////////////////////////////////////////////////

And here's the obligatory Sandcastle Demo. Click any point on the globe to light it up.

Note that because of the limited search space (one day), points near the poles may not get any light if you click them in their local winter.

@mramato
Copy link
Contributor Author

mramato commented May 2, 2018

Awesome @emackey! That seems like a great workaround.

@OmarShehata
Copy link
Contributor

Requested on the forum.

@lilleyse lilleyse mentioned this issue Dec 24, 2019
1 task
@lilleyse
Copy link
Contributor

This type of effect is now possible in #8493.

viewer.scene.light = new Cesium.DirectionalLight({
    direction : new Cesium.Cartesian3(1, 0, 0)
});

viewer.scene.preRender.addEventListener(function(scene, time) {
    viewer.scene.light.direction = Cesium.Cartesian3.clone(scene.camera.directionWC, viewer.scene.light.direction);
});

Sandcastle

@cesium-concierge
Copy link

Congratulations on closing the issue! I found these Cesium forum links in the comments above:

https://groups.google.com/d/msg/cesium-dev/BgRKJ8N6aDI/-avmESUeCgAJ

If this issue affects any of these threads, please post a comment like the following:

The issue at #6553 has just been closed and may resolve your issue. Look for the change in the next stable release of Cesium or get it now in the master branch on GitHub https://github.com/AnalyticalGraphicsInc/cesium.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants