drawing line using vertex shader & fragment shader in spritkit in swift

夙愿已清 提交于 2019-12-23 01:07:08

问题


I need to draw a line between two spritnodes while hand is pointing from one node to another with rendering or animation. The line drawing is working for me as I follow this link Bobjt's answer, Animate Path Drawing in SpriteKit . I have used concept of fragment shader and vertex shader here..

I have added animatestroke.fsh file and apply the following code .. But the handplayer animation and line drawing speed is mismatching some times. my line rendering i have given increment by

strokeLengthFloat +=   0.01

And handplayer duration is 1.5:

let moveTosecondObject = SKAction.move(to:  handmovepoint, duration:  1.5 )

Here is my implementation code:

variable declaration

var handplayer = SKSpriteNode()
var firstObject = SKSpriteNode()
var secondObject = SKSpriteNode()
var startTrigger: Bool = false

let strokeSizeFactor = CGFloat( 2.0 )
var strokeShader: SKShader!
var strokeLengthUniform: SKUniform!
var _strokeLengthFloat: Float = 0.0
var strokeLengthKey: String!
var strokeLengthFloat: Float {
    get {
        return _strokeLengthFloat
    }
    set( newStrokeLengthFloat ) {
        _strokeLengthFloat = newStrokeLengthFloat
        strokeLengthUniform.floatValue = newStrokeLengthFloat
    }
}

Loading method

   override func didMove(to view: SKView) {
   handplayer = SKSpriteNode(imageNamed: "HandImage.png")
    handplayer.size.width = handplayer.size.width * 0.5
    handplayer.size.height = handplayer.size.height * 0.5
    handplayer.position.x =  firstObject.position 
    self.addChild(handplayer)
    UpdatePosition()
   }

    func UpdatePosition(){


       //Line drawing part
          strokeLengthKey = "u_current_percentage"
          strokeLengthUniform = SKUniform( name: strokeLengthKey, float: 0.0 )
          let uniforms: [SKUniform] = [strokeLengthUniform]
          strokeShader = shaderWithFilename( "animateStroke", fileExtension: "fsh", uniforms: uniforms )
    strokeLengthFloat = 0.0

           let cameraNode = SKCameraNode()
           self.camera = cameraNode

          let lineStartPosition = CGPoint(x:firstObject.frame.origin.x+firstObject.size.width/2,y:firstObject.frame.origin.y-firstObject.size.height*0.2)

           let lineEndPosition = CGPoint(x:secondObject.frame.origin.x+secondObject.size.width/2,y:secondObject.frame.origin.y+secondObject.size.height*1.2)

         let path = CGMutablePath()
         path.move(to: CGPoint(x:  lineStartPosition.x, y:  lineStartPosition.y))
         path.addLine(to: CGPoint(x: : lineEndPosition.x, y:  lineEndPosition.y), transform: CGAffineTransform.identity)

         let strokeWidth = 1.0 * strokeSizeFactor
         let shapeNode = SKShapeNode( path: path )
         shapeNode.lineWidth = strokeWidth
         shapeNode.lineCap = .square
         shapeNode.addChild( cameraNode )
         shapeNode.strokeShader = strokeShader
         shapeNode.calculateAccumulatedFrame()
         self.addChild( shapeNode )


    }

     // center the camera
     cameraNode.position = CGPoint( x: self.frame.size.width/2.0, y: self.frame.size.height/2.0 )


     **Handplayer Rendering part**
       let handmovepoint = CGPoint(x: secondObject.position.x , y: secondObject.position.y + handplayer.size.height*0.1 )

       let moveTosecondObject = SKAction.move(to:  handmovepoint, duration:  1.5 )
    let delay = SKAction.wait(forDuration: 1)
    handplayer.run(SKAction.sequence([ delay ]), completion: {() -> Void in
        self.startTrigger = true

        self.handplayer.run(SKAction.sequence([ moveTosecondObject ]), completion: {() -> Void in
            //self.handplayer.alpha = 0.0


       })

 }
    //line rendering part
    override func update(_ currentTime: TimeInterval) {

    if(startTrigger){

        strokeLengthFloat +=   0.01 

    }

    if startTrigger == true{

        if strokeLengthFloat > 1.0 {
           //once line drawing completed 
             self.startTrigger = false

            print("Line drawing ended")
            self.handplayer.alpha = 0.0
            self.handplayer.removeFromParent()


            }

           }
         }

animatestroke.fsh class

        void main()
       {
       if ( u_path_length == 0.0 ) {
              gl_FragColor = vec4( 0.0, 0.0, 1.0, 1.0 ); // draw blue // this is an error
        } else if ( v_path_distance / u_path_length <= u_current_percentage ) {
              gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 ); // draw red
        } else {
              gl_FragColor = vec4( 0.0, 0.0, 0.0, 0.0 ); // draw nothing
        }
  }

How could I fix this? Or If anyone have other idea for implementhing this scenario share your Idea.

来源:https://stackoverflow.com/questions/53405562/drawing-line-using-vertex-shader-fragment-shader-in-spritkit-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!