How to create a sphere at the Sun's location?

I’m trying to create a big gold sphere to represent the Sun but it’s not working. I see the Earth but no other objects. Here’s my code:

Corrections and recommendations would be appreciated.

Hi Daniel,

I’m not able to debug your code right now, but first try placing a label at the position you think the sun is at. This way it will be big enough that you know you’ll see it. I suspect the 1,000 meters used now will be hard to spot.

Also, the Moon implementation may have some code of interest.

Patrick

Don’t forget to ask Cesium to render stuff that far out.

viewer.scene.camera.frustum.far = 1e12;

–Ed.

The next attempt to put a giant volume at the location of the Sun starts with the Sandcastle volumes example.
A Cesium Inspector was added and when I click on view frustum, it indicates two objects, i.e., Earth and Moon.
The far wall of the viewing frustum is set to 1e12. The viewer is set to track and select the entity but the camera shows the Earth.

Please let me know whether I’m using the Cesium.Simon1994PlanetaryPositions.computeSunPositionInEarthInertialFrame function correctly.

I changed the call to the compute Sun position to:
var pointInSpace = new Cesium.Cartesian3(1.0,1.0,1.0);
pointInSpace = Cesium.Simon1994PlanetaryPositions.computeSunPositionInEarthInertialFrame(julianDate, pointInSpace) ;

It did not help.

On the Web Console there is an error message:
TypeError: n.NegativePiToPi is not a function Cesium.js:388

I upgraded to Cesium-1.3 and I’m still getting the n.NegativePiToPi is not a function error at Cesium.js line 388.

Hi Dan,

Give this a try with the latest copy of Cesium.

–Ed.

<script>

    var solarRadiusInMeters = 6.955e8;

    var viewer = new Cesium.Viewer('cesiumContainer');

    viewer.scene.camera.frustum.far = 1e12;            //Move the far wall of the viewing frustum.
    viewer.extend(Cesium.viewerCesiumInspectorMixin);  //Add Cesium Inspector

    var scene = viewer.scene;
    scene.skyBox.show = false;                        // Turn off the sky box
    scene.sun.show = false;                           // Don't show the default Sun

    // Turn on sun lighting of the globe.
    scene.globe.enableLighting = true;

    // Create a Yellow rim-lit material.
    var material = Cesium.Material.fromType(Cesium.Material.RimLightingType);
    material.uniforms.color = Cesium.Color.YELLOW;

    // Create Sun graphics primitive.
    var sunEllipsoid = viewer.scene.primitives.add(new Cesium.EllipsoidPrimitive({
        center: new Cesium.Cartesian3(),  // For now, place the Sun at the origin.
        radii: new Cesium.Cartesian3(solarRadiusInMeters, solarRadiusInMeters, solarRadiusInMeters),
        material: material
    }));

    // Allocate "new" variables outside of the render loop when possible, to reduce garbage collection.
    var sunPosition = new Cesium.Cartesian3();
    var icrfToFixedScratch = new Cesium.Matrix3();

    // Update the camera and the Sun with each animation frame.
    function icrf(scene, time) {
        if (scene.mode !== Cesium.SceneMode.SCENE3D) {
            return;
        }

        var icrfToFixed = Cesium.Transforms.computeIcrfToFixedMatrix(time, icrfToFixedScratch);
        if (Cesium.defined(icrfToFixed)) {
            // Update the camera with the new ICRF rotation
            scene.camera.transform = Cesium.Matrix4.fromRotationTranslation(icrfToFixed);

            // Compute Sun position in Inertial.
            Cesium.Simon1994PlanetaryPositions.computeSunPositionInEarthInertialFrame(time, sunPosition);

            // Transform Sun position from Inertial to Fixed.
            Cesium.Matrix3.multiplyByVector(icrfToFixed, sunPosition, sunPosition);

            // Update the Sun's modelMatrix to move it from the origin to its new position for this animation frame.
            sunEllipsoid.modelMatrix = Cesium.Matrix4.fromRotationTranslation(Cesium.Matrix3.IDENTITY, sunPosition);
        }
    }
    scene.preRender.addEventListener(icrf);

</script>

Ed -

Thanks for the reply. I copied the Javascript into a local copy of Sandcastle running on Cesium 1.3 and received the following error:

An error occurred while rendering. Rendering has stopped.

DeveloperError: translation is required.
DeveloperError@http://localhost:8080/Source/Core/DeveloperError.js:43:13
Matrix4.fromRotationTranslation@http://localhost:8080/Source/Core/Matrix4.js:302:13
icrf@http://localhost:8080/Apps/Sandcastle/templates/bucket.html:44:17
Event.prototype.raiseEvent@http://localhost:8080/Source/Core/Event.js:146:17
render@http://localhost:8080/Source/Scene/Scene.js:1353:9
Scene.prototype.render@http://localhost:8080/Source/Scene/Scene.js:1410:13
CesiumWidget.prototype.render@http://localhost:8080/Source/Widgets/CesiumWidget/CesiumWidget.js:600:13
render@http://localhost:8080/Source/Widgets/CesiumWidget/CesiumWidget.js:70:25

The entire viewing area of Sandcastle was yellow with a little black spot in the middle.

Odd, that would mean that “sunPosition” is undefined in the “icrf” function. Of course I tested this (against master) and didn’t see that problem here. You could try wrapping that last statement inside an “if (Cesium.defined(sunPosition))” check, but I don’t understand why such a check would be needed, since we already check the rotation matrix a few lines before.

I submitted a pull request, if anyone else wants to test it:

https://github.com/AnalyticalGraphicsInc/cesium/pull/2288

–Ed.

Over at

If you copy the code between //Sandcastle_Begin and //Sandcastle_End then paste it to

It works! The star-field doesn’t show up though, just the Sun and Moon. I like the fact that it is astronomically correct, just like the Moon. You actually have to travel about 150 GigaMeters from the Earth to get to the Sun. By default you can only see 50 MegaMeters, but this code changes the far clip plane. Also the Sun is displayed with it’s actual 1.4 GigaMeter diameter (contrast that with Earth’s 0.012 GigaMeter diameter!) It could use a texture though, the Moon has one.

change
sunEllipsoid.modelMatrix = Cesium.Matrix4.fromRotationTranslation(Cesium.Matrix3.IDENTITY, sunPosition, sunModelMatrixScratch);

to

sunEllipsoid.modelMatrix = Cesium.Matrix4.fromRotationTranslation(Cesium.Matrix3.IDENTITY, {x:1000000000,y:0,z:0}, sunModelMatrixScratch);

To really get a feel of how large the sun is! Instead of 150 GigaMeters away from the Earth the Sun is now a mere 1 GigaMeter away from Earth (center to center that is, since the Sun has about a 0.7 GigaMeter radius its about 300 MegaMeters away from the surface, which is why the Moon is sometimes inside the Sun as it has a 384 MegaMeter orbit.) Cesium can be a useful tool to study the Solar System!

I tried running it on 1.37 sandcastle and I get
An error occurred while rendering. Rendering has stopped.
undefined
TypeError: Cannot set property transform of #<Camera> which has only a getter
TypeError: Cannot set property transform of #<Camera> which has only a getter
    at icrf (<anonymous>:34:32)
    at Event.raiseEvent (https://cesiumjs.org/Cesium/Source/Core/Event.js:142:30)
    at render (https://cesiumjs.org/Cesium/Source/Scene/Scene.js:2623:26)
    at Scene.render (https://cesiumjs.org/Cesium/Source/Scene/Scene.js:2702:13)
    at CesiumWidget.render (https://cesiumjs.org/Cesium/Source/Widgets/CesiumWidget/CesiumWidget.js:686:25)
    at render (https://cesiumjs.org/Cesium/Source/Widgets/CesiumWidget/CesiumWidget.js:71:32)

Hi Bhawna,

Can you provide the code snippet you ran?

Thanks,

Gabby

Hey Gabby,
I ran this on 1.37 sandcastle.

//Sandcastle_Begin
var solarRadiusInMeters = 6.955e8;
var viewer = new Cesium.Viewer(‘cesiumContainer’);
viewer.scene.camera.frustum.far = 1e12; //Move the far wall of the viewing frustum.
var scene = viewer.scene;
scene.skyBox.show = false; // Turn off the sky box
scene.sun.show = false; // Don’t show the default Sun
// Turn on sun lighting of the globe.
scene.globe.enableLighting = true;
// Create a Yellow rim-lit material.
var material = Cesium.Material.fromType(Cesium.Material.RimLightingType);
material.uniforms.color = Cesium.Color.YELLOW;
// Create Sun graphics primitive.
var sunEllipsoid = scene.primitives.add(new Cesium.EllipsoidPrimitive({
center: new Cesium.Cartesian3(), // For now, place the Sun at the origin.
radii: new Cesium.Cartesian3(solarRadiusInMeters, solarRadiusInMeters, solarRadiusInMeters),
material: material
}));
// Allocate “new” variables outside of the render loop when possible, to reduce garbage collection.
var sunPosition = new Cesium.Cartesian3();
var icrfToFixedScratch = new Cesium.Matrix3();
var sunModelMatrixScratch = new Cesium.Matrix4();
// Update the camera and the Sun with each animation frame.
function icrf(scene, time) {
if (scene.mode !== Cesium.SceneMode.SCENE3D) {
return;
}
var icrfToFixed = Cesium.Transforms.computeIcrfToFixedMatrix(time, icrfToFixedScratch);
if (Cesium.defined(icrfToFixed)) {
// Update the camera with the new ICRF rotation
scene.camera.transform = Cesium.Matrix4.fromRotationTranslation(icrfToFixed);
// Compute Sun position in Inertial.
Cesium.Simon1994PlanetaryPositions.computeSunPositionInEarthInertialFrame(time, sunPosition);
// Transform Sun position from Inertial to Fixed.
Cesium.Matrix3.multiplyByVector(icrfToFixed, sunPosition, sunPosition);
// Update the Sun’s modelMatrix to move it from the origin to its new position for this animation frame.
sunEllipsoid.modelMatrix = Cesium.Matrix4.fromRotationTranslation(Cesium.Matrix3.IDENTITY, sunPosition, sunModelMatrixScratch);
}
}
scene.preRender.addEventListener(icrf);
//Sandcastle_End

The offending line is

scene.camera.transform = Cesium.Matrix4.fromRotationTranslation(icrfToFixed);

You can’t set the camera’s transform directly. Take a look at the camera documentation for the methods you can use, particularly lookAtTransform.

Thanks!

Gabby