Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ライブ配信アプリのアイテム再生をMetalで実装する事になった話

noppefoxwolf
September 02, 2018

 ライブ配信アプリのアイテム再生をMetalで実装する事になった話

noppefoxwolf

September 02, 2018
Tweet

More Decks by noppefoxwolf

Other Decks in Technology

Transcript

  1. ᶃ"71MBZFSͰ࠶ੜ͢Δ let url = Bundle.main.url(forResource: "item_effect", withExtension: "mov")! let asset

    = AVURLAsset(url: url) let item = AVPlayerItem(asset: asset) let player = AVPlayer(playerItem: item) let playerLayer = AVPlayerLayer(player: player) playerLayer.backgroundColor = UIColor.clear.cgColor view.layer.addSubLayer(playerLayer) player.play() JPTEDC TIBSF 
  2. ᶄ6**NBHF7JFXͰ࠶ੜ͢Δ let imageView = UIImageView() let images = (0...300).compactMap({ UIImage(named:

    "\($0).png") }) imageView.animationImages = images imageView.startAnimating() JPTEDC TIBSF 
  3. ϑΥʔϚοτ ಁա ѹॖ཰ σίʔυෛՙ BQOH ˓ .# º XFCQ ˓

    .# º TG XFCN ˓ ,# º NQ º .# ˓ I "1/(͸༰ྔͷ࣌఺Ͱෆ࠾༻ͩͬͨͷͰະଌఆɻ8&#.͸σίʔμ͕ແͦ͞͏ͳͷͰະଌఆ WQXJUIBMQIB JPTEDC TIBSF 
  4. $*'JMUFSΛ࢖ͬͨ߹੒ let filter = CIFilter(name: "CIBlendWithMask")! filter.setValue(baseCIImage, forKey: kCIInputImageKey) filter.setValue(alphaCIImage,

    forKey: kCIInputMaskImageKey) let filteredImage = filter.outputImage! let image = UIImage(ciImage: filteredImage) imageView.image = image JPTEDC TIBSF 
  5. 0QFO(-&4Λ࢖ͬͨ߹੒ͱඳը varying lowp vec2 textureCoordinate; uniform sampler2D videoFrame; void main()

    { // ϕʔεͷ৭ͷx lowp float x = textureCoordinate.x / 2.0; // ϕʔε৭ͷऔಘ lowp vec2 baseCoord = vec2(x, textureCoordinate.y); lowp vec4 baseColor = texture2D(videoFrame, baseCoord); // alpha஋ͷऔಘ lowp vec2 alphaCoord = vec2(x + 0.5, textureCoordinate.y); lowp vec4 alphaColor = texture2D(videoFrame, alphaCoord); // RGB͸ϕʔε৭ɺA͸alpha஋Λར༻͢Δ gl_FragColor = vec4(baseColor.r, baseColor.g, baseColor.b, alphaColor.r); } JPTEDC TIBSF 
  6. .FUBMΛ࢖ͬͯΈͯײͨ͡ར఺ let desc = MTLRenderPipelineDescriptor() desc.vertexFunction = library.makeFunction(name: vertexFunctionName) desc.fragmentFunction

    = library.makeFunction(name: fragmentFunctionName) desc.colorAttachments[0].pixelFormat = pixelFormat 4XJGU0CK$Ͱ΋ॻ͖΍͍͢Ϟμϯͳ*' ίϯύΠϧ࣌ʹ໰୊͕ൃ֮͠΍͍͢ɻ JPTEDC TIBSF