simd provides types and functions for small vector and matrix computations.

Posts under simd tag

31 Posts

Post

Replies

Boosts

Views

Activity

RealityKit_DirectionalLight_Question
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
1
0
259
Feb ’26
Float8 and Float16 "Reserved_Name__Do_not_use"
I am developing a macOS terminal app, running on an M4 Pro, and using Metal. I am not able use float8 or float16, both reporting Variable has incomplete type 'float16' (aka '__Reserved_Name__Do_not_use_float16'). Based on the system I should be able to use these. Either it is because it is also compiling to Intel, which they are not allowed, or something else. Either way I have not been able to figure out how to get past this. IIs there a compiler setting I need to set to make this work? if so which one and what setting do I need? I only want to run this on M processes, on the latest version of OS so not interested in Intel version or backward compatibility.
Topic: Graphics & Games SubTopic: Metal Tags:
4
0
288
Aug ’25
hyperthreading with arm64
Hi, I am curious about if hyperthreading is enabled/disabled on my macbook pro M1 or M4. Howto figure out? I am using macOS 15.5. Further, I develop a multi-threaded audio sequencer that creates threads per instrument. I use vector operations to increase performance. I recognized lowering synchronization rate from 250 Hz to 60 Hz gives additional performance advantages. Howto programmatically check if Hyperthreading is enabled/disabled and howto enable/disable it programmatically? After some research I found sysctl() and nvram SMTDisable=%01. https://support.apple.com/en-us/101870 Can anyone provide me an Objective C example? regards, Joël
2
0
329
Jul ’25
Invert a 3x3 matrix in a Metal compute kernel
I searched the Metal Shading Language Specification Version 3.0 document, however I cannot see any function for inverting a matrix. Is there really no function in Metal for inverting a matrix? I often need to this in linear equations and have so far resorted to writing the necessary function each time, most of the time just copy-and-pasting code. inverse exists in SIMD and GLSL, so why not in Metal? It seems so unexpected that this function does not exist that I am almost certain I have just overlooked something obvious. I even tried 1 / M, to no avail.
2
1
2k
Feb ’25
Issue storing SIMD3<Float> (and other simd’s) in a SwiftData model
I'm encountering an issue when trying to store a SIMD3<Float> in a SwiftData model. Since SIMD3<Float> already conforms to Codable, I expected it to work. However, attempting to store a single SIMD3<Float> crashes with the following error: Fatal error: Unexpected property within Persisted Struct/Enum: Builtin.Vec4xFPIEEE32 Interestingly, storing an array of vectors, [SIMD3<Float>], works perfectly fine. The issue only arises when trying to store a single SIMD3<Float>. I’m not looking for a workaround (I can break the vector into individual floats in a custom codable struct to get by) , but I’d like to understand why storing a codable SIMD3<Float> in SwiftData results in this crash. Is this a limitation of SwiftData, or is there something I’m missing about how vectors are handled? Any insights would be greatly appreciated!
3
1
886
Sep ’24
Placing object in AR without hittest or plane
Hi, i want to place a object in 3d world space without the use of hittest or plane detection in ios swift code. Suggest the best method. Now, I take the camera center matrix and use simd_mul to place the object, it works but the object gets placed at the centre of the mobile screen. I want to select the x and y positino on the screen 2d coordinate and place the object. I tried using the unprojectpoint function, to get the AR scene world coordinate of the point i touch on the mobile screen. I get the x, y,z values, they are very close to the values from camera center matrix. When i try to replace the unprojectpoint values in the cameracenter matrix, i dont see a difference in the location of the placed object. The below code always place object from center screen with specified depth, But i need to place object in user specified position(x,y) of the screen with depth. 2D pixel coordinate system of the renderer to the 3D world coordinate system of the scene. /* Create a transform with a translation of 0.2 meters in front of the camera. */ var translation = matrix_identity_float4x4 translation.columns.3.z = -0.2 let transform = simd_mul(view.session.currentFrame.camera.transform, translation) Refer from : [https://developer.apple.com/documentation/arkit/arskview/providing_2d_virtual_content_with_spritekit) The code i used for replacing the camera center matrix with the unprojectpoint is let vpWithZ = SCNVector3(x: 100.0, y: 100.0, z: -1.0) let worldPoint = sceneView.unprojectPoint(vpWithZ) var translation = matrix_identity_float4x4 translation.columns.3.z = Float(Depth) var translation2 = sceneView.session.currentFrame!.camera.transform translation2.columns.3.x = worldPoint.x translation2.columns.3.y = worldPoint.y translation2.columns.3.z = worldPoint.z let new_transform = simd_mul(translation2, translation) /* add object name you wanted in your project*/ let sphere = SCNSphere(radius: 0.03) let objectNode = SCNNode(geometry: sphere) objectNode.position = SCNVector3(x: transform.columns.3.x, y: transform.columns.3.y, z: transform.columns.3.z) The below image shows outline of my idea.
0
0
870
Apr ’24
sims_4x4 data
I'm trying to decipher this roomplan api data output. Dimensions: the simd3 is width and height and I think thickness in meters but the spacial information is not clear to me and I can't find any info on it. an insights on this? *** CAPTURED ROOM OBJECTS *** Walls: Identifier: 9F4BEEBB-990C-42F3-A0BC-912E1F770877 Dimensions: SIMD3(5.800479, 2.4299998, 0.0) Category: wall Transform: SIMD4(0.25821668, 0.0, 0.966087, 0.0) SIMD4(0.0, 1.0, 0.0, 0.0) SIMD4(-0.966087, 0.0, 0.2582167, 0.0) SIMD4(2.463065, -0.27346277, -0.5366996, 1.0) Identifier: A72544F5-068D-4F19-8FA4-60C1331003E3 Dimensions: SIMD3(2.28993, 2.4299998, 0.0) Category: wall Transform: SIMD4(0.966087, 0.0, -0.2582166, 0.0) SIMD4(0.0, 1.0, 0.0, 0.0) SIMD4(0.25821656, 0.0, 0.966087, 0.0) SIMD4(0.608039, -0.27346277, -3.0429342, 1.0)
1
1
878
Mar ’24
simd_quatd from to. Difference between vectors by quaternion
let dir = Vector3D(x: 0.21074744760506547, y: 0.38871720406617527, z: -1.0819520200824684).uniformlyScaled(by: 1) let vd = Vector3D(vector: [0,0,-1]) let rot = Rotation3D(position: Point3D(x: 0, y: 0, z: 0),target: Point3D(dir)) print(vd.rotated(by: simd_quatd(from: vd.normalized.vector , to: dir.normalized.vector))) let r = simd.simd_quaternion(simd_float3(vd), simd_float3(dir)) print(vd.rotated(by: Rotation3D(simd_quatd(from: vd.normalized.vector , to: dir.normalized.vector)))) result (x: 0.18030816736048502, y: 0.33257288514358785, z: -0.9256804204747842) (x: 0.18030816736048502, y: 0.33257288514358785, z: -0.9256804204747842) (x: 0.1439359315016178, y: 0.2654854115377888, z: -0.953309993592521) (x: 0.0, y: 0.0, z: 1.0) Expect (x: 0.21074744760506547, y: 0.38871720406617527, z: -1.0819520200824684) Why it is not follow expect value?
0
0
508
Nov ’23
View and Projection Matrix in Metal for a First Person Camera
So I'm trying to make a simple scene with some geometry of sorts and a movable camera. So far I've been able to render basic geometry in 2D alongside transforming set geometry using matrices. Following this I moved on to the Calculating Primitive Visibility Using Depth Testing Sample ... also smooth sailing. Then I had my first go at transforming positions between different coordinate spaces. I didn't get quite far with my rather blurry memory from OpenGL, all dough when I compared my view and projection matrix with the ones from the OpenGL glm::lookAt() and glm::perspective() functions there seemed to be no fundamental differences. Figuring Metal doing things differently I browsed the Metal Sample Code library for a sample containing a first-person camera. The only one I could find was Rendering Terrain Dynamically with Argument Buffers. Luckily it contained code for calculating view and projection matrices, which seemed to differ from my code. But I still have problems Problem Description When positioning the camera right in front of the geometry the view as well as the projection matrix produce seemingly accurate results: Camera Positon(0, 0, 1); Camera Directio(0, 0, -1) When moving further away though, parts of the scene are being wrongfully culled. Notably the ones farther away from the camera: Camera Position(0, 0, 2); Camera Direction(0, 0, -1) Rotating the Camera also produces confusing results: Camera Position: (0, 0, 1); Camera Direction: (cos(250°), 0, sin(250°)), yes I converted to radians My Suspicions The Projection isn't converting the vertices from view space to Normalised Device Coordinates correctly. Also when comparing two first two images, the lower part of the triangle seems to get bigger as the camera moves away which also doesn't appear to be right. Obviously the view matrix is also not correct as I'm pretty sure what's describe above isn't supposed to happen. Code Samples MainShader.metal #include <metal_stdlib> #include <Shared/Primitives.h> #include <Shared/MainRendererShared.h> using namespace metal; struct transformed_data {     float4 position [[position]];     float4 color; }; vertex transformed_data vertex_shader(uint vertex_id [[vertex_id]],                                       constant _vertex *vertices [[buffer(0)]],                                       constant _uniforms& uniforms [[buffer(1)]]) {     transformed_data output;     float3 dir = {0, 0, -1};     float3 inEye = float3{ 0, 0, 1 }; // position     float3 inTo = inEye + dir; // position + direction     float3 inUp = float3{ 0, 1, 0};          float3 z = normalize(inTo - inEye);     float3 x = normalize(cross(inUp, z));     float3 y = cross(z, x);     float3 t = (float3) { -dot(x, inEye), -dot(y, inEye), -dot(z, inEye) };     float4x4 viewm = float4x4(float4 { x.x, y.x, z.x, 0 },                               float4 { x.y, y.y, z.y, 0 },                               float4 { x.z, y.z, z.z, 0 },                               float4 { t.x, t.y, t.z, 1 });          float _nearPlane = 0.1f;     float _farPlane = 100.0f;     float _aspectRatio = uniforms.viewport_size.x / uniforms.viewport_size.y;     float va_tan = 1.0f / tan(0.6f * 3.14f * 0.5f);     float ys = va_tan;     float xs = ys / _aspectRatio;     float zs = _farPlane / (_farPlane - _nearPlane);     float4x4 projectionm = float4x4((float4){ xs,  0,  0, 0},                                     (float4){  0, ys,  0, 0},                                     (float4){  0,  0, zs, 1},                                     (float4){  0,  0, -_nearPlane * zs, 0 } );          float4 projected = (projectionm*viewm) * float4(vertices[vertex_id].position,1);     vector_float2 viewport_dim = vector_float2(uniforms.viewport_size);     output.position = vector_float4(0.0, 0.0, 0.0, 1.0);     output.position.xy = projected.xy / (viewport_dim / 2);     output.position.z = projected.z;     output.color = vertices[vertex_id].color;     return output; } fragment float4 fragment_shader(transformed_data in [[stage_in]]) {return in.color;} These are the vertices definitions let triangle_vertices = [_vertex(position: [ 480.0, -270.0, 1.0], color: [1.0, 0.0, 0.0, 1.0]),                          _vertex(position: [-480.0, -270.0, 1.0], color: [0.0, 1.0, 0.0, 1.0]),                          _vertex(position: [   0.0,  270.0, 0.0], color: [0.0, 0.0, 1.0, 1.0])] // TO-DO: make this use 4 vertecies and 6 indecies let quad_vertices = [_vertex(position: [ 480.0,  270.0, 0.5], color: [0.5, 0.5, 0.5, 1.0]),                      _vertex(position: [ 480.0, -270.0, 0.5], color: [0.5, 0.5, 0.5, 1.0]),                      _vertex(position: [-480.0, -270.0, 0.5], color: [0.5, 0.5, 0.5, 1.0]),                      _vertex(position: [-480.0,  270.0, 0.5], color: [0.5, 0.5, 0.5, 1.0]),                      _vertex(position: [ 480.0,  270.0, 0.5], color: [0.5, 0.5, 0.5, 1.0]),                      _vertex(position: [-480.0, -270.0, 0.5], color: [0.5, 0.5, 0.5, 1.0])] This is the initialisation code of the depth stencil descriptor and state _view.depthStencilPixelFormat = MTLPixelFormat.depth32Float _view.clearDepth = 1.0 // other render initialisation code let depth_stencil_descriptor = MTLDepthStencilDescriptor() depth_stencil_descriptor.depthCompareFunction = MTLCompareFunction.lessEqual depth_stencil_descriptor.isDepthWriteEnabled = true; depth_stencil_state = try! _view.device!.makeDepthStencilState(descriptor: depth_stencil_descriptor)! So if you have any idea on why its not working or have some code of your own that's working or know of any public samples containing a working first-person camera, feel free to help me out. Thank you in advance! (please ignore any spelling or similar mistakes, english is not my primary language)
3
1
3.9k
Aug ’23
anchor points from 2dtexture via vertex shader
I'm trying to cast the positions of points from an image to the location of a SCNNode using SCNNodeRendererDelegate during an ARSession. I have the node in the scene and I am sending uniforms to the renderer using the arguments from renderNode(_ node: SCNNode, renderer: SCNRenderer, arguments: [String : Any]) let modelTransform = arguments["kModelTransform"] as! SCNMatrix4         let viewTransform = arguments["kViewTransform"] as! SCNMatrix4         let modelViewTransform = arguments["kModelViewTransform"] as! SCNMatrix4         let modelViewProjectionTransform = arguments["kModelViewProjectionTransform"] as! SCNMatrix4         let projectionTransform = arguments["kProjectionTransform"] as! SCNMatrix4         let normalsTransform = arguments["kNormalTransform"] as! SCNMatrix4 In the vertex shader, I calculate the normals using intrinsics = session.currentFrame!.camera.intrinsics in uint2 pos; // specified in pixel coordinates, normalizing?     pos.y = vertexID / depthTexture.get_width();     pos.x = vertexID % depthTexture.get_width();          float depthMultiplier = 100.0f;     float depth = depthTexture.read(pos).x * depthMultiplier;          float xrw = (pos.x - cameraIntrinsics[2][0]) * depth / cameraIntrinsics[0][0];     float yrw = (pos.y - cameraIntrinsics[2][1]) * depth / cameraIntrinsics[1][1];          float4 xyzw = { xrw, yrw, depth, 1.f }; My goal is to calculate the clip space position for each vertex using the node uniforms. So I've been multiplying the model view projection matrix a number of ways, but almost every time, the points are either skewed on the image plane, or if projected properly, don't adhere to the position of the modelTransform that I pass in (ie. when I raycast out and get a transform, set the node there, then use the node's renderer callback to grab its transform to pass into the vertex shader). What transform matrices should I multiply by the vertex? I am using the session.currentFrame?.camera to get the camera.viewMatrix(for: .portrait). But should I use the viewTransform matrix from the node instead? I also get the projection matrix from camera.projectionMatrix(for: .portrait, viewportSize: renderer.currentViewport.size, zNear: CGFloat(znear) , zFar: CGFloat(zfar)). But should I use the projectionTransform from the node, or what about the modelViewProjectionTransform from the node? Could I just multiply nodeUniforms.modelViewProjectionTransform * xyzw in the shader? If you need more clarification about what I am trying to do, let me know! Thanks
0
0
977
Dec ’22
Convert SceneKit/matrix/transform code into pure Metal/MetalKit
Hi, I'm trying to convert my SceneKit code into pure Metal/MetalKit and I feel confused about this code and how I should rewrite it. I've made the code simple for this case with some pseudocode. The part where it says "SceneKit magic" is where I feel lost. The SCNNode is just an empty placeholder just as it is – and it works for the calculations it is doing inside. Any help and pointers are much appreciated :-) class Scene { let ball = Ball() let scnNode = SCNNode() var quaternionAngleVelocity = float2(0, 0) func update() { // pseudocode, the user press the up key quaternionAngleVelocity.x -= 3 // pseudocode, the user press the down key quaternionAngleVelocity.x += 3 // pseudocode, the user press the left key quaternionAngleVelocity.y -= 3 // pseudocode, the user press the right key quaternionAngleVelocity.y += 3 ... // apply friction quaternionAngleVelocity *= (1 - 0.05) // i.e. decrease with 5% // rotate let newQuaternionAxisX = simd_quatf(angle: quaternionAngleVelocity.x.degreesToRadians, axis: simd_float3(x: 1, y: 0, z: 0)) let newQuaternionAxisY = simd_quatf(angle: quaternionAngleVelocity.y.degreesToRadians, axis: simd_float3(x: 0, y: 1, z: 0)) let finalQuaternion = simd_normalize(newQuaternionAxisX * newQuaternionAxisY) // SceneKit magic…? let matrix4 = SCNMatrix4Mult(scnNode.worldTransform, SCNMatrix4(simd_float4x4(finalQuaternion))) scnNode.transform = matrix4 ball.quaternion = scnNode.simdWorldOrientation } } class Ball { var position: float3 = [0, 0, 0] var rotation: float3 = [0, 0, 0] { didSet { let rotationMatrix = float4x4(rotation: rotation) quaternion = simd_quatf(rotationMatrix) } } var scale: float3 = [1, 1, 1] var forwardVector: float3 { return normalize([sin(rotation.y), 0, cos(rotation.y)]) } var rightVector: float3 { return [forwardVector.z, forwardVector.y, -forwardVector.x] } var upVector: float3 { return [0, forwardVector.z, 0] } var quaternion = simd_quatf() var modelMatrix: float4x4 { let translateMatrix = float4x4(translation: position) // let rotateMatrix = float4x4(rotation: rotation) let rotateMatrix = float4x4(quaternion) let scaleMatrix = float4x4(scaling: scale) return (translateMatrix * rotateMatrix * scaleMatrix) } var worldTransform: float4x4 { return modelMatrix } }
3
0
1.8k
Oct ’22
Can't define the matrix_double3x3 in a c header
I tried to define the matrix_double3x3 properties but the compiler don't allow me to do. I can define the matrix_float3x3 or matrix_half3x3 but not for the matrix_double3x3. But I saw the matrix_types.h, it has typedef simd_double3x3 matrix_double3x3 My goal is to send the matrix_double3x3 to Metal for computing the rectified image. Environment Xcode 14.0 (14A309) Destination: macOS (Minimum Deployments: 12.3) macOS 12.6 (21G115)
1
0
2.2k
Sep ’22
Parenting a Metal rendered point cloud to an ARAnchor
Hi there! I have accomplished rendering the point cloud via a metal texture with depth. I plugged in gestures to manipulate the object over the top of the camera feed. I am able to investigate up close it like any volumetric point cloud. However, I am trying to anchor it to an ARAnchor so that I can move around my physical space and investigate the stationary cloud. I have an ARSession running, as well as a custom Renderer that handles Metal. I think it comes down to getting the final view matrix, which is then set into the renderEncoder renderEncoder.setVertexBytes(&finalViewMatrix, length: MemoryLayout.size(ofValue: finalViewMatrix), index: 0) I believe I can solve the anchor issue by doing the correct matrix math. To do this, I guess that I need the world matrix that the anchor is in. Then also the local model matrix of the anchor I would use to multiply with the model matrix of the point cloud, thus parenting it. Then I could multiply the projection matrix and view matrix with the model matrix. Does this sound like a sound way to go about the issue? I have already tried many methods and haven't quite achieved it, especially when moving the physical device forwards and backwards -- it moves with the device. Thank you!
0
0
1.4k
Sep ’22
RealityKit_DirectionalLight_Question
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
Replies
1
Boosts
0
Views
259
Activity
Feb ’26
NEON vector comparaison
hi, Is it possible to compare two vectors and get a boolean answer? example : uint642_t a; uint642_t b; .../... if(a == b) .../... how to do it ? thank
Replies
3
Boosts
0
Views
984
Activity
Oct ’25
Can't find arm_neon.h in macOS 15.6.1
I can't find the arm_neon.h header file in macOS 15.6.1 This header file defines NEON intrinsics.
Replies
1
Boosts
0
Views
1.7k
Activity
Sep ’25
What's wrong with simd_inverse?
When using simd_inverse functions, the same input yields different results on different devices. On the Mac mini with M4 pro and M3 ultra, the result is But on the Mac mini of M2 ultra, the result is
Replies
2
Boosts
0
Views
1.1k
Activity
Aug ’25
Float8 and Float16 "Reserved_Name__Do_not_use"
I am developing a macOS terminal app, running on an M4 Pro, and using Metal. I am not able use float8 or float16, both reporting Variable has incomplete type 'float16' (aka '__Reserved_Name__Do_not_use_float16'). Based on the system I should be able to use these. Either it is because it is also compiling to Intel, which they are not allowed, or something else. Either way I have not been able to figure out how to get past this. IIs there a compiler setting I need to set to make this work? if so which one and what setting do I need? I only want to run this on M processes, on the latest version of OS so not interested in Intel version or backward compatibility.
Topic: Graphics & Games SubTopic: Metal Tags:
Replies
4
Boosts
0
Views
288
Activity
Aug ’25
hyperthreading with arm64
Hi, I am curious about if hyperthreading is enabled/disabled on my macbook pro M1 or M4. Howto figure out? I am using macOS 15.5. Further, I develop a multi-threaded audio sequencer that creates threads per instrument. I use vector operations to increase performance. I recognized lowering synchronization rate from 250 Hz to 60 Hz gives additional performance advantages. Howto programmatically check if Hyperthreading is enabled/disabled and howto enable/disable it programmatically? After some research I found sysctl() and nvram SMTDisable=%01. https://support.apple.com/en-us/101870 Can anyone provide me an Objective C example? regards, Joël
Replies
2
Boosts
0
Views
329
Activity
Jul ’25
Invert a 3x3 matrix in a Metal compute kernel
I searched the Metal Shading Language Specification Version 3.0 document, however I cannot see any function for inverting a matrix. Is there really no function in Metal for inverting a matrix? I often need to this in linear equations and have so far resorted to writing the necessary function each time, most of the time just copy-and-pasting code. inverse exists in SIMD and GLSL, so why not in Metal? It seems so unexpected that this function does not exist that I am almost certain I have just overlooked something obvious. I even tried 1 / M, to no avail.
Replies
2
Boosts
1
Views
2k
Activity
Feb ’25
When will ARM SVE be available on APPLE chips?
hi Question is in the title.. do we already know the length of the vectors? best regards
Replies
1
Boosts
0
Views
781
Activity
Oct ’24
Issue storing SIMD3<Float> (and other simd’s) in a SwiftData model
I'm encountering an issue when trying to store a SIMD3<Float> in a SwiftData model. Since SIMD3<Float> already conforms to Codable, I expected it to work. However, attempting to store a single SIMD3<Float> crashes with the following error: Fatal error: Unexpected property within Persisted Struct/Enum: Builtin.Vec4xFPIEEE32 Interestingly, storing an array of vectors, [SIMD3<Float>], works perfectly fine. The issue only arises when trying to store a single SIMD3<Float>. I’m not looking for a workaround (I can break the vector into individual floats in a custom codable struct to get by) , but I’d like to understand why storing a codable SIMD3<Float> in SwiftData results in this crash. Is this a limitation of SwiftData, or is there something I’m missing about how vectors are handled? Any insights would be greatly appreciated!
Replies
3
Boosts
1
Views
886
Activity
Sep ’24
Placing object in AR without hittest or plane
Hi, i want to place a object in 3d world space without the use of hittest or plane detection in ios swift code. Suggest the best method. Now, I take the camera center matrix and use simd_mul to place the object, it works but the object gets placed at the centre of the mobile screen. I want to select the x and y positino on the screen 2d coordinate and place the object. I tried using the unprojectpoint function, to get the AR scene world coordinate of the point i touch on the mobile screen. I get the x, y,z values, they are very close to the values from camera center matrix. When i try to replace the unprojectpoint values in the cameracenter matrix, i dont see a difference in the location of the placed object. The below code always place object from center screen with specified depth, But i need to place object in user specified position(x,y) of the screen with depth. 2D pixel coordinate system of the renderer to the 3D world coordinate system of the scene. /* Create a transform with a translation of 0.2 meters in front of the camera. */ var translation = matrix_identity_float4x4 translation.columns.3.z = -0.2 let transform = simd_mul(view.session.currentFrame.camera.transform, translation) Refer from : [https://developer.apple.com/documentation/arkit/arskview/providing_2d_virtual_content_with_spritekit) The code i used for replacing the camera center matrix with the unprojectpoint is let vpWithZ = SCNVector3(x: 100.0, y: 100.0, z: -1.0) let worldPoint = sceneView.unprojectPoint(vpWithZ) var translation = matrix_identity_float4x4 translation.columns.3.z = Float(Depth) var translation2 = sceneView.session.currentFrame!.camera.transform translation2.columns.3.x = worldPoint.x translation2.columns.3.y = worldPoint.y translation2.columns.3.z = worldPoint.z let new_transform = simd_mul(translation2, translation) /* add object name you wanted in your project*/ let sphere = SCNSphere(radius: 0.03) let objectNode = SCNNode(geometry: sphere) objectNode.position = SCNVector3(x: transform.columns.3.x, y: transform.columns.3.y, z: transform.columns.3.z) The below image shows outline of my idea.
Replies
0
Boosts
0
Views
870
Activity
Apr ’24
sims_4x4 data
I'm trying to decipher this roomplan api data output. Dimensions: the simd3 is width and height and I think thickness in meters but the spacial information is not clear to me and I can't find any info on it. an insights on this? *** CAPTURED ROOM OBJECTS *** Walls: Identifier: 9F4BEEBB-990C-42F3-A0BC-912E1F770877 Dimensions: SIMD3(5.800479, 2.4299998, 0.0) Category: wall Transform: SIMD4(0.25821668, 0.0, 0.966087, 0.0) SIMD4(0.0, 1.0, 0.0, 0.0) SIMD4(-0.966087, 0.0, 0.2582167, 0.0) SIMD4(2.463065, -0.27346277, -0.5366996, 1.0) Identifier: A72544F5-068D-4F19-8FA4-60C1331003E3 Dimensions: SIMD3(2.28993, 2.4299998, 0.0) Category: wall Transform: SIMD4(0.966087, 0.0, -0.2582166, 0.0) SIMD4(0.0, 1.0, 0.0, 0.0) SIMD4(0.25821656, 0.0, 0.966087, 0.0) SIMD4(0.608039, -0.27346277, -3.0429342, 1.0)
Replies
1
Boosts
1
Views
878
Activity
Mar ’24
simd_quatd from to. Difference between vectors by quaternion
let dir = Vector3D(x: 0.21074744760506547, y: 0.38871720406617527, z: -1.0819520200824684).uniformlyScaled(by: 1) let vd = Vector3D(vector: [0,0,-1]) let rot = Rotation3D(position: Point3D(x: 0, y: 0, z: 0),target: Point3D(dir)) print(vd.rotated(by: simd_quatd(from: vd.normalized.vector , to: dir.normalized.vector))) let r = simd.simd_quaternion(simd_float3(vd), simd_float3(dir)) print(vd.rotated(by: Rotation3D(simd_quatd(from: vd.normalized.vector , to: dir.normalized.vector)))) result (x: 0.18030816736048502, y: 0.33257288514358785, z: -0.9256804204747842) (x: 0.18030816736048502, y: 0.33257288514358785, z: -0.9256804204747842) (x: 0.1439359315016178, y: 0.2654854115377888, z: -0.953309993592521) (x: 0.0, y: 0.0, z: 1.0) Expect (x: 0.21074744760506547, y: 0.38871720406617527, z: -1.0819520200824684) Why it is not follow expect value?
Replies
0
Boosts
0
Views
508
Activity
Nov ’23
View and Projection Matrix in Metal for a First Person Camera
So I'm trying to make a simple scene with some geometry of sorts and a movable camera. So far I've been able to render basic geometry in 2D alongside transforming set geometry using matrices. Following this I moved on to the Calculating Primitive Visibility Using Depth Testing Sample ... also smooth sailing. Then I had my first go at transforming positions between different coordinate spaces. I didn't get quite far with my rather blurry memory from OpenGL, all dough when I compared my view and projection matrix with the ones from the OpenGL glm::lookAt() and glm::perspective() functions there seemed to be no fundamental differences. Figuring Metal doing things differently I browsed the Metal Sample Code library for a sample containing a first-person camera. The only one I could find was Rendering Terrain Dynamically with Argument Buffers. Luckily it contained code for calculating view and projection matrices, which seemed to differ from my code. But I still have problems Problem Description When positioning the camera right in front of the geometry the view as well as the projection matrix produce seemingly accurate results: Camera Positon(0, 0, 1); Camera Directio(0, 0, -1) When moving further away though, parts of the scene are being wrongfully culled. Notably the ones farther away from the camera: Camera Position(0, 0, 2); Camera Direction(0, 0, -1) Rotating the Camera also produces confusing results: Camera Position: (0, 0, 1); Camera Direction: (cos(250°), 0, sin(250°)), yes I converted to radians My Suspicions The Projection isn't converting the vertices from view space to Normalised Device Coordinates correctly. Also when comparing two first two images, the lower part of the triangle seems to get bigger as the camera moves away which also doesn't appear to be right. Obviously the view matrix is also not correct as I'm pretty sure what's describe above isn't supposed to happen. Code Samples MainShader.metal #include <metal_stdlib> #include <Shared/Primitives.h> #include <Shared/MainRendererShared.h> using namespace metal; struct transformed_data {     float4 position [[position]];     float4 color; }; vertex transformed_data vertex_shader(uint vertex_id [[vertex_id]],                                       constant _vertex *vertices [[buffer(0)]],                                       constant _uniforms& uniforms [[buffer(1)]]) {     transformed_data output;     float3 dir = {0, 0, -1};     float3 inEye = float3{ 0, 0, 1 }; // position     float3 inTo = inEye + dir; // position + direction     float3 inUp = float3{ 0, 1, 0};          float3 z = normalize(inTo - inEye);     float3 x = normalize(cross(inUp, z));     float3 y = cross(z, x);     float3 t = (float3) { -dot(x, inEye), -dot(y, inEye), -dot(z, inEye) };     float4x4 viewm = float4x4(float4 { x.x, y.x, z.x, 0 },                               float4 { x.y, y.y, z.y, 0 },                               float4 { x.z, y.z, z.z, 0 },                               float4 { t.x, t.y, t.z, 1 });          float _nearPlane = 0.1f;     float _farPlane = 100.0f;     float _aspectRatio = uniforms.viewport_size.x / uniforms.viewport_size.y;     float va_tan = 1.0f / tan(0.6f * 3.14f * 0.5f);     float ys = va_tan;     float xs = ys / _aspectRatio;     float zs = _farPlane / (_farPlane - _nearPlane);     float4x4 projectionm = float4x4((float4){ xs,  0,  0, 0},                                     (float4){  0, ys,  0, 0},                                     (float4){  0,  0, zs, 1},                                     (float4){  0,  0, -_nearPlane * zs, 0 } );          float4 projected = (projectionm*viewm) * float4(vertices[vertex_id].position,1);     vector_float2 viewport_dim = vector_float2(uniforms.viewport_size);     output.position = vector_float4(0.0, 0.0, 0.0, 1.0);     output.position.xy = projected.xy / (viewport_dim / 2);     output.position.z = projected.z;     output.color = vertices[vertex_id].color;     return output; } fragment float4 fragment_shader(transformed_data in [[stage_in]]) {return in.color;} These are the vertices definitions let triangle_vertices = [_vertex(position: [ 480.0, -270.0, 1.0], color: [1.0, 0.0, 0.0, 1.0]),                          _vertex(position: [-480.0, -270.0, 1.0], color: [0.0, 1.0, 0.0, 1.0]),                          _vertex(position: [   0.0,  270.0, 0.0], color: [0.0, 0.0, 1.0, 1.0])] // TO-DO: make this use 4 vertecies and 6 indecies let quad_vertices = [_vertex(position: [ 480.0,  270.0, 0.5], color: [0.5, 0.5, 0.5, 1.0]),                      _vertex(position: [ 480.0, -270.0, 0.5], color: [0.5, 0.5, 0.5, 1.0]),                      _vertex(position: [-480.0, -270.0, 0.5], color: [0.5, 0.5, 0.5, 1.0]),                      _vertex(position: [-480.0,  270.0, 0.5], color: [0.5, 0.5, 0.5, 1.0]),                      _vertex(position: [ 480.0,  270.0, 0.5], color: [0.5, 0.5, 0.5, 1.0]),                      _vertex(position: [-480.0, -270.0, 0.5], color: [0.5, 0.5, 0.5, 1.0])] This is the initialisation code of the depth stencil descriptor and state _view.depthStencilPixelFormat = MTLPixelFormat.depth32Float _view.clearDepth = 1.0 // other render initialisation code let depth_stencil_descriptor = MTLDepthStencilDescriptor() depth_stencil_descriptor.depthCompareFunction = MTLCompareFunction.lessEqual depth_stencil_descriptor.isDepthWriteEnabled = true; depth_stencil_state = try! _view.device!.makeDepthStencilState(descriptor: depth_stencil_descriptor)! So if you have any idea on why its not working or have some code of your own that's working or know of any public samples containing a working first-person camera, feel free to help me out. Thank you in advance! (please ignore any spelling or similar mistakes, english is not my primary language)
Replies
3
Boosts
1
Views
3.9k
Activity
Aug ’23
How to enable BFloat16 data type?
When compile source with __bf16 variable, it throw exception, "__bf16 is not supported on this target". The compiler is xcode build-in clang-1400.0.29.202. Is Apple silicon support bf16 type?
Replies
2
Boosts
0
Views
3.1k
Activity
Mar ’23
anchor points from 2dtexture via vertex shader
I'm trying to cast the positions of points from an image to the location of a SCNNode using SCNNodeRendererDelegate during an ARSession. I have the node in the scene and I am sending uniforms to the renderer using the arguments from renderNode(_ node: SCNNode, renderer: SCNRenderer, arguments: [String : Any]) let modelTransform = arguments["kModelTransform"] as! SCNMatrix4         let viewTransform = arguments["kViewTransform"] as! SCNMatrix4         let modelViewTransform = arguments["kModelViewTransform"] as! SCNMatrix4         let modelViewProjectionTransform = arguments["kModelViewProjectionTransform"] as! SCNMatrix4         let projectionTransform = arguments["kProjectionTransform"] as! SCNMatrix4         let normalsTransform = arguments["kNormalTransform"] as! SCNMatrix4 In the vertex shader, I calculate the normals using intrinsics = session.currentFrame!.camera.intrinsics in uint2 pos; // specified in pixel coordinates, normalizing?     pos.y = vertexID / depthTexture.get_width();     pos.x = vertexID % depthTexture.get_width();          float depthMultiplier = 100.0f;     float depth = depthTexture.read(pos).x * depthMultiplier;          float xrw = (pos.x - cameraIntrinsics[2][0]) * depth / cameraIntrinsics[0][0];     float yrw = (pos.y - cameraIntrinsics[2][1]) * depth / cameraIntrinsics[1][1];          float4 xyzw = { xrw, yrw, depth, 1.f }; My goal is to calculate the clip space position for each vertex using the node uniforms. So I've been multiplying the model view projection matrix a number of ways, but almost every time, the points are either skewed on the image plane, or if projected properly, don't adhere to the position of the modelTransform that I pass in (ie. when I raycast out and get a transform, set the node there, then use the node's renderer callback to grab its transform to pass into the vertex shader). What transform matrices should I multiply by the vertex? I am using the session.currentFrame?.camera to get the camera.viewMatrix(for: .portrait). But should I use the viewTransform matrix from the node instead? I also get the projection matrix from camera.projectionMatrix(for: .portrait, viewportSize: renderer.currentViewport.size, zNear: CGFloat(znear) , zFar: CGFloat(zfar)). But should I use the projectionTransform from the node, or what about the modelViewProjectionTransform from the node? Could I just multiply nodeUniforms.modelViewProjectionTransform * xyzw in the shader? If you need more clarification about what I am trying to do, let me know! Thanks
Replies
0
Boosts
0
Views
977
Activity
Dec ’22
Convert SceneKit/matrix/transform code into pure Metal/MetalKit
Hi, I'm trying to convert my SceneKit code into pure Metal/MetalKit and I feel confused about this code and how I should rewrite it. I've made the code simple for this case with some pseudocode. The part where it says "SceneKit magic" is where I feel lost. The SCNNode is just an empty placeholder just as it is – and it works for the calculations it is doing inside. Any help and pointers are much appreciated :-) class Scene { let ball = Ball() let scnNode = SCNNode() var quaternionAngleVelocity = float2(0, 0) func update() { // pseudocode, the user press the up key quaternionAngleVelocity.x -= 3 // pseudocode, the user press the down key quaternionAngleVelocity.x += 3 // pseudocode, the user press the left key quaternionAngleVelocity.y -= 3 // pseudocode, the user press the right key quaternionAngleVelocity.y += 3 ... // apply friction quaternionAngleVelocity *= (1 - 0.05) // i.e. decrease with 5% // rotate let newQuaternionAxisX = simd_quatf(angle: quaternionAngleVelocity.x.degreesToRadians, axis: simd_float3(x: 1, y: 0, z: 0)) let newQuaternionAxisY = simd_quatf(angle: quaternionAngleVelocity.y.degreesToRadians, axis: simd_float3(x: 0, y: 1, z: 0)) let finalQuaternion = simd_normalize(newQuaternionAxisX * newQuaternionAxisY) // SceneKit magic…? let matrix4 = SCNMatrix4Mult(scnNode.worldTransform, SCNMatrix4(simd_float4x4(finalQuaternion))) scnNode.transform = matrix4 ball.quaternion = scnNode.simdWorldOrientation } } class Ball { var position: float3 = [0, 0, 0] var rotation: float3 = [0, 0, 0] { didSet { let rotationMatrix = float4x4(rotation: rotation) quaternion = simd_quatf(rotationMatrix) } } var scale: float3 = [1, 1, 1] var forwardVector: float3 { return normalize([sin(rotation.y), 0, cos(rotation.y)]) } var rightVector: float3 { return [forwardVector.z, forwardVector.y, -forwardVector.x] } var upVector: float3 { return [0, forwardVector.z, 0] } var quaternion = simd_quatf() var modelMatrix: float4x4 { let translateMatrix = float4x4(translation: position) // let rotateMatrix = float4x4(rotation: rotation) let rotateMatrix = float4x4(quaternion) let scaleMatrix = float4x4(scaling: scale) return (translateMatrix * rotateMatrix * scaleMatrix) } var worldTransform: float4x4 { return modelMatrix } }
Replies
3
Boosts
0
Views
1.8k
Activity
Oct ’22
How works "convert" function from RealityFoundation package
very an entity in RealityKit has a method called: convert(::). Actually for me is very interesting how this method works which the following signature: convert(direction: from:) Can someone describe which formula to use to calculate this?
Replies
0
Boosts
0
Views
1.1k
Activity
Oct ’22
Can't define the matrix_double3x3 in a c header
I tried to define the matrix_double3x3 properties but the compiler don't allow me to do. I can define the matrix_float3x3 or matrix_half3x3 but not for the matrix_double3x3. But I saw the matrix_types.h, it has typedef simd_double3x3 matrix_double3x3 My goal is to send the matrix_double3x3 to Metal for computing the rectified image. Environment Xcode 14.0 (14A309) Destination: macOS (Minimum Deployments: 12.3) macOS 12.6 (21G115)
Replies
1
Boosts
0
Views
2.2k
Activity
Sep ’22
How to copy SIMD float4x4 to vertex shader?
How can I copy a SIMD float4x4 matrix to a vertex shader by using the setVertexBytes:length:atIndex message of a render command encoder?
Replies
1
Boosts
0
Views
2.2k
Activity
Sep ’22
Parenting a Metal rendered point cloud to an ARAnchor
Hi there! I have accomplished rendering the point cloud via a metal texture with depth. I plugged in gestures to manipulate the object over the top of the camera feed. I am able to investigate up close it like any volumetric point cloud. However, I am trying to anchor it to an ARAnchor so that I can move around my physical space and investigate the stationary cloud. I have an ARSession running, as well as a custom Renderer that handles Metal. I think it comes down to getting the final view matrix, which is then set into the renderEncoder renderEncoder.setVertexBytes(&finalViewMatrix, length: MemoryLayout.size(ofValue: finalViewMatrix), index: 0) I believe I can solve the anchor issue by doing the correct matrix math. To do this, I guess that I need the world matrix that the anchor is in. Then also the local model matrix of the anchor I would use to multiply with the model matrix of the point cloud, thus parenting it. Then I could multiply the projection matrix and view matrix with the model matrix. Does this sound like a sound way to go about the issue? I have already tried many methods and haven't quite achieved it, especially when moving the physical device forwards and backwards -- it moves with the device. Thank you!
Replies
0
Boosts
0
Views
1.4k
Activity
Sep ’22