SceneKit - Draw 3D Parabola

前端 未结 2 1486
野的像风
野的像风 2020-12-23 12:55

I\'m given three points and need to draw a smooth 3D parabola. The trouble is that curved line is choppy and has some weird divots in it

相关标签:
2条回答
  • 2020-12-23 13:22

    I don't think your table has enough points, assuming the renderer is connecting them with straight line segments. On top of this, the thickness and dashing of the line make it difficult to see that. Try getting a smooth curve with a thin solid line first.

    If you want to animate the progression of the curve, as if it were showing the flight of a projectile, it will probably be easiest to just write your function for the motion: y = k*x^2, and just render from x=0 to x=T for increasing values of T.

    0 讨论(0)
  • 2020-12-23 13:36

    I'd recommend using SCNShape to create your parabola. To start, you'll need to represent your parabola as a Bézier curve. You can use UIBezierPath for that. For animation, I personally find shader modifiers are a nice fit for cases like this.

    The Parabola

    Watch out, though — you probably want a path that represents just the open stroke of the arc. If you do something like this:

    let path = UIBezierPath()
    path.moveToPoint(CGPointZero)
    path.addQuadCurveToPoint(CGPoint(x: 100, y: 0), controlPoint: CGPoint(x: 50, y: 200))
    

    You'll get a filled-in parabola, like this (seen in 2D in the debugger quick look, then extruded in 3D with SCNShape):

    filled parabolafilled parabola in 3D

    To create a closed shape that's just the arc, you'll need to trace back over the curve, a little bit away from the original:

    let path = UIBezierPath()
    path.moveToPoint(CGPointZero)
    path.addQuadCurveToPoint(CGPoint(x: 100, y: 0), controlPoint: CGPoint(x: 50, y: 200))
    path.addLineToPoint(CGPoint(x: 99, y: 0))
    path.addQuadCurveToPoint(CGPoint(x: 1, y: 0), controlPoint: CGPoint(x: 50, y: 198))
    

    open parabolaopen parabola in 3D

    That's better.

    ... in Three-Dee!

    How to actually make it 3D? Just make an SCNShape with the extrusion depth you like:

    let shape = SCNShape(path: path, extrusionDepth: 10)
    

    And set it in your scene:

    shape.firstMaterial?.diffuse.contents = SKColor.blueColor()
    let shapeNode = SCNNode(geometry: shape)
    shapeNode.pivot = SCNMatrix4MakeTranslation(50, 0, 0)
    shapeNode.eulerAngles.y = Float(-M_PI_4)
    root.addChildNode(shapeNode)
    

    Here I'm using a pivot to make the shape rotate around the major axis of the parabola, instead of the y = 0 axis of the planar Bézier curve. And making it blue. Also, root is just a shortcut I made for the view's scene's root node.

    Animating

    The shape of the parabola doesn't really need to change through your animation — you just need a visual effect that progressively reveals it along its x-axis. Shader modifiers are a great fit for that, because you can make the animated effect per-pixel instead of per-vertex and do all the expensive work on the GPU.

    Here's a shader snippet that uses a progress parameter, varying from 0 to 1, to set opacity based on x-position:

    // declare a variable we can set from SceneKit code
    uniform float progress;
    // tell SceneKit this shader uses transparency so we get correct draw order
    #pragma transparent
    // get the position in model space
    vec4 mPos = u_inverseModelViewTransform * vec4(_surface.position, 1.0);
    // a bit of math to ramp the alpha based on a progress-adjusted position
    _surface.transparent.a = clamp(1.0 - ((mPos.x + 50.0) - progress * 200.0) / 50.0, 0.0, 1.0);
    

    Set that as a shader modifier for the Surface entry point, and then you can animate the progress variable:

    let modifier = "uniform float progress;\n #pragma transparent\n vec4 mPos = u_inverseModelViewTransform * vec4(_surface.position, 1.0);\n _surface.transparent.a = clamp(1.0 - ((mPos.x + 50.0) - progress * 200.0) / 50.0, 0.0, 1.0);"
    shape.shaderModifiers = [ SCNShaderModifierEntryPointSurface: modifier ]
    shape.setValue(0.0, forKey: "progress")
    
    SCNTransaction.begin()
    SCNTransaction.setAnimationDuration(10)
    shape.setValue(1.0, forKey: "progress")
    SCNTransaction.commit()
    

    animating parabola

    Further Considerations

    Here's the whole thing in a form you can paste into a (iOS) playground. A few things left as exercises to the reader, plus other notes:

    • Factor out the magic numbers and make a function or class so you can alter the size/shape of your parabola. (Remember that you can scale SceneKit nodes relative to other scene elements, so they don't have to use the same units.)

    • Position the parabola relative to other scene elements. If you take away my line that sets the pivot, the shapeNode.position is the left end of the parabola. Change the parabola's length (or scale it), then rotate it around its y-axis, and you can make the other end line up with some other node. (For you to fire ze missiles at?)

    • I threw this together with Swift 2 beta, but I don't think there's any Swift-2-specific syntax in there — porting back to 1.2 if you need to deploy soon should be straightforward.

    • If you also want to do this on OS X, it's a bit trickier — there, SCNShape uses NSBezierPath, which unlike UIBezierPath doesn't support quadratic curves. Probably an easy way out would be to fake it with an elliptical arc.

    0 讨论(0)
提交回复
热议问题