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

Can't generate tube or line #9

Open
seldon1000 opened this issue Dec 22, 2023 · 1 comment
Open

Can't generate tube or line #9

seldon1000 opened this issue Dec 22, 2023 · 1 comment

Comments

@seldon1000
Copy link

Hello 👋🏻

I am trying to build a sort of circle arc, or torus section, using the provided line and tube methods. Here is my sample code:

func points(of radius: Float, from start: Angle2D, to end: Angle2D) -> [SIMD3<Float>] {
    let degrees = Int(abs(start.degrees - end.degrees))
        
    var points = [SIMD3<Float>]()
        
    for i in 0...degrees {
        let angle = Angle2D(degrees: Double(i)).radians
            
        let x = Float(radius * cos(angle))
        let y = Float(radius * sin(angle))
            
        points.append([x, 0, y])
    }
        
    return points
 }

let points = points(of: radius, from: .zero, to: .radians(.pi / 2))
        
guard let mesh = try? RealityGeometry.line(points: points, radius: 1).0 else { return nil }
        
let entity = ModelEntity(mesh: mesh, materials: [PhysicallyBasedMaterial.glassMaterial])

The code runs with no errors and manages to go over the guard line. However, using line or generateTube does not produce any visible entity. Interesting fact: using generatePath with the same points does produce the correct mesh, but path is flat and I need a proper 3D tube-like object.

BTW thank you for this package, it's helping me a lot with my non-existent 3D development skills.

@scoumbe
Copy link
Contributor

scoumbe commented Mar 3, 2024

I have the same use case and I am experiencing the same issue of my created entities being invisible.

extension RealityGeometry {
    /// Create a new torus arc MeshResource 🍩
    /// - Parameters:
    ///   - sides: Number of segments in the toroidal direction (outer edge of the torus).
    ///   - csSides: Number of segments in the poloidal direction (segments in the tube)
    ///   - radius: Distance from the centre of the torus to the centre of the tube.
    ///   - csRadius: Radius of the tube.
    ///   - startDegrees: The starting angle of the arc
    ///   - endDegrees: The ending angle of the arc
    /// - Returns: A new torus `MeshResource`
    public static func generateTorusArc(
        sides: Int, csSides: Int, radius: Float, csRadius: Float, startDegrees: Float, endDegrees: Float
    ) throws -> MeshResource {
        
        // Build a series of points that will serve to build a tube around
        let angleIncs = 360.0 / Float(sides)
        let dToR: Float = .pi / 180
        let radIncs = angleIncs * dToR
        let startRads: Float = startDegrees * dToR
        let endRads: Float = endDegrees * dToR
        let ptRadius = (csRadius + radius)
        
        var i = startRads
        let z: Float = 0.0
        var x: Float
        var y: Float
        var points: [SIMD3<Float>] = []
        
        while i < endRads {
            var point = SIMD3(x: cosf(i) * ptRadius, y: cosf(i) * ptRadius, z: z)
            points.append(point)
            i += radIncs
        }
        
        var results = try generateTube(points: points, radius: csRadius, edges: csSides)
        
        return results
    }
    
}

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

No branches or pull requests

2 participants