My application calculates three distinct Meesus Double [x, y, z] Radian values to light a sphere in RealityKit with DirectionalLight. It is my understanding that I must use (simd_quatf) for each radian value to properly light the sphere in the view. The code correctly [orientates] the sphere with the combined (simd_quatf) DirectionalLight in the view, but the illumination (Z-axis) fails to properly illuminate the sphere with the expected result, compared to associated Meesus web page images. For the moment, I do not know how to correct the (Z-axis). Curious for a suggestion ... :]
// Location values.
let theLatitude: Double = 51.13107260
let theLongitude: Double = -114.01127910
let currentDate: Date = Date()
struct TheCalculatedMoonPhaseTest_ContentView: View {
var body: some View {
VStack {
if #available(macOS 15.0, *) {
RealityView { content in
let moonSphere_Entity = Entity.createSphere(radius: 0.90, color: .black)
moonSphere.Entity.name = "MoonSphere"
moonSphere.Entity.position = SIMD3<Float>(x: 0, y: 0, z: 0)
content.add(moonSphere.Entity)
let sunLight_Entity = createDirectionalLight(latitude: theLatitude, longitude: theLongitude, date: currentDate)
content.add(sunLight_Entity)
} // End of [RealityView]
} else {
// Earlier version required.
} // End of [if #available(macOS 15.0, *)]
} // End of [VStack]
.background(Color.black)
} // End of [var body: some View]
// MARK: - 🟠🟠🟠🟠 [SET THE BACKGROUND COLOUR] 🟠🟠🟠🟠
var backgroundColor: Color = Color.init(.black)
// MARK: - 🟠🟠🟠🟠 [CREATE THE DIRECTIONAL LIGHT FOR THE SPHERE] 🟠🟠🟠🟠
func createDirectionalLight(latitude: Double, longitude: Double, date: Date) -> Entity {
let directionalLight = DirectionalLight()
directionalLight.light.color = .white
directionalLight.light.intensity = 1000000
directionalLight.shadow = DirectionalLightComponent.Shadow()
directionalLight.shadow?.maximumDistance = 5
directionalLight.shadow?.depthBias = 1
// MARK: 🟠🟠🟠🟠 Retrieve the [MEESUS MOON AGE VALUES] from the [CONSTANT FOLDER] 🟠🟠🟠🟠
let theMeesusMoonAge_LunarAgeDaysValue = 25.90567592898601
if theMeesusMoonAge_LunarAgeDaysValue >= 23.10 && theMeesusMoonAge_LunarAgeDaysValue < (29.530588853 - 1.00) {
let someCalculatedX_WestEastRadian: Float = Float(1.00)
// Identify the sphere’s DirectionalLight Tilt Angle (Y) radian value ::
// Note :: The following Tilt Angle is corrected to [Zenith] with the [MeesusCalculatedTilt_Angle] minus the [MeesusCalculatedPar_Angle].
let someCalculatedY_TiltAngleRadian: Float = Float(1.3396086)
// Identify the sphere’s DirectionalLight Illumination (Z) radian Value ::
// Note :: The Meesus calculated illumination fraction is converted to degrees, then converted to a radian value.
let someCalculatedZ_IlluminationAngleRadian: Float = Float(0.45176168630244457) // <=== 14.3800% Illumination.
// Define rotation angles in radians for X, Y, and Z axes.
let x_Radians = someCalculatedX_WestEastRadian
let y_Radians = someCalculatedY_TiltAngleRadian
let z_Radians = someCalculatedZ_IlluminationAngleRadian
// Identify and separate the quaternion [simd_quatf] for each Radian.
let q_X = simd_quatf(angle: x_Radians, axis: SIMD3<Float>(1, 0, 0))
let q_Y = simd_quatf(angle: y_Radians, axis: SIMD3<Float>(0, 1, 0))
let q_Z = simd_quatf(angle: z_Radians, axis: SIMD3<Float>(0, 0, 1))
// Apply and combine the rotations, where order matters.
let combinedRotation = q_Z * q_Y * q_X
// Identify the [Combined Rotation].
// The [MyMoonMeesus] :: [WANING CRESCENT] calculated [combinedRotation] :: simd_quatf(real: 0.73715997, imag: SIMD3<Float>(0.24427173, 0.61516714, -0.13599981)) ° Radians
// Normalize the [combinedRotation].
let theNormalizesRotation = simd_normalize(combinedRotation)
// Identify the [Normalized Combined Rotation].
// The [MyMoonMeesus] :: [WANING CRESCENT] calculated [normalizedRotation] :: simd_quatf(real: 0.73715997, imag: SIMD3<Float>(0.24427173, 0.61516714, -0.13599981)) ° Radians
// Assume the [theNormalizesRotation] appears reversed.
let theCorrectedRotation = theNormalizesRotation.inverse
// Identify the [Reversed Combined Rotation].
// The [MyMoonMeesus] :: [WANING CRESCENT] calculated [correctedRotation] :: simd_quatf(real: 0.73715997, imag: SIMD3<Float>(-0.24427173, -0.61516714, 0.13599981)) ° Radians
// Apply the [Corrected Rotation] to the entity.
directionalLight.transform.rotation *= theCorrectedRotation
// Add the [directionalLight] to the scene ::
let anchor = AnchorEntity()
anchor.addChild(directionalLight)
} // End of [if theMeesusMoonAge_LunarAgeDaysValue >= 23.10 && theMeesusMoonAge_LunarAgeDaysValue < (29.530588853 - 1.00)]
return directionalLight
} // End of [func createDirectionalLight(latitude: Double, longitude: Double, date: Date) -> Entity]
} // End of [struct TheCalculatedMoonPhaseTest_ContentView: View]
// MARK: 🟠🟠🟠🟠 [ENTITY HELPER EXTENSION] 🟠🟠🟠🟠
extension Entity {
static func createSphere(radius: Float, color: NSColor) -> Entity {
let mesh = MeshResource.generateSphere(radius: radius)
var material = PhysicallyBasedMaterial()
material.baseColor = .init(tint: color)
let modelComponent = ModelComponent(mesh: mesh, materials: [material])
let entity = Entity()
entity.components.set(modelComponent)
entity.components.set(Transform())
return entity
} // End of [static func createSphere(radius: Float, color: NSColor) -> Entity]
} // End of [extension Entity]
// Application Image :: Calgary
// Website Image :: timeanddate
// mooncalc.org