fix resizing on iOS

This commit is contained in:
Nick Fisher
2022-12-20 11:30:40 +08:00
parent 0568713371
commit 19fcc8ac53

View File

@@ -1,6 +1,5 @@
import Flutter import Flutter
import UIKit import UIKit
import OpenGLES.ES3
import GLKit import GLKit
public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture { public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture {
@@ -12,12 +11,12 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
var width: Double = 0 var width: Double = 0
var height: Double = 0 var height: Double = 0
var context: EAGLContext?;
var targetPixelBuffer: CVPixelBuffer?;
var textureCache: CVOpenGLESTextureCache?;
var texture: CVOpenGLESTexture? = nil;
var frameBuffer: GLuint = 0;
var targetPixelBuffer: CVPixelBuffer?;
// var context: EAGLContext?;
// var textureCache: CVOpenGLESTextureCache?;
// var texture: CVOpenGLESTexture? = nil;
// var frameBuffer: GLuint = 0;
var pixelBufferAttrs = [ var pixelBufferAttrs = [
kCVPixelBufferPixelFormatTypeKey: NSNumber(value: kCVPixelFormatType_32BGRA), kCVPixelBufferPixelFormatTypeKey: NSNumber(value: kCVPixelFormatType_32BGRA),
@@ -83,10 +82,11 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
} }
@objc func doRender() { @objc func doRender() {
if(viewer != nil && _rendering) { guard _rendering == true, let textureId = self.textureId, let viewer = viewer else {
render(viewer, 0) return
self.registry.textureFrameAvailable(self.textureId!)
} }
render(viewer, 0)
self.registry.textureFrameAvailable(textureId)
} }
public func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? { public func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? {
@@ -97,6 +97,10 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
return Unmanaged.passRetained(targetPixelBuffer!); return Unmanaged.passRetained(targetPixelBuffer!);
} }
public func onTextureUnregistered(texture:FlutterTexture) {
print("Texture unregistered")
}
public static func register(with registrar: FlutterPluginRegistrar) { public static func register(with registrar: FlutterPluginRegistrar) {
let _messenger = registrar.messenger(); let _messenger = registrar.messenger();
messenger = _messenger; messenger = _messenger;
@@ -124,6 +128,8 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
private func initialize(width:Int32, height:Int32) { private func initialize(width:Int32, height:Int32) {
print("Initializing with size \(width)x\(height)")
createPixelBuffer(width:Int(width), height:Int(height)) createPixelBuffer(width:Int(width), height:Int(height))
self.textureId = self.registry.register(self) self.textureId = self.registry.register(self)
@@ -142,12 +148,29 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
unsafeBitCast(targetPixelBuffer!, to: UnsafeMutableRawPointer.self), unsafeBitCast(targetPixelBuffer!, to: UnsafeMutableRawPointer.self),
UInt32(width), UInt32(height)) UInt32(width), UInt32(height))
update_viewport_and_camera_projection(self.viewer!, Int32(width), Int32(height), 1.0); update_viewport_and_camera_projection(self.viewer!, width, height, 1.0);
createDisplayLink() createDisplayLink()
} }
private func resize(width:Int32, height:Int32) {
print("Resizing to size \(width)x\(height)")
if(self.textureId != nil) {
self.registry.unregisterTexture(self.textureId!)
}
createPixelBuffer(width: Int(width), height:Int(height))
create_swap_chain(
self.viewer,
unsafeBitCast(targetPixelBuffer!, to: UnsafeMutableRawPointer.self),
UInt32(width), UInt32(height))
update_viewport_and_camera_projection(self.viewer!, width, height, 1.0);
self.textureId = self.registry.register(self)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let methodName = call.method; let methodName = call.method;
@@ -180,7 +203,7 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
let numBoneAnimations = boneAnimations.count let numBoneAnimations = boneAnimations.count
var boneAnimStructs = UnsafeMutableBufferPointer<BoneAnimation>.allocate(capacity: numBoneAnimations) var boneAnimStructs = UnsafeMutableBufferPointer<BoneAnimation>.allocate(capacity: numBoneAnimations)
if numBoneAnimations > 0 {
for i in 0...numBoneAnimations - 1 { for i in 0...numBoneAnimations - 1 {
let boneNames = boneAnimations[i][0] as! Array<String> let boneNames = boneAnimations[i][0] as! Array<String>
let meshNames = boneAnimations[i][1] as! Array<String> let meshNames = boneAnimations[i][1] as! Array<String>
@@ -212,6 +235,8 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
numMeshTargets: meshNames.count numMeshTargets: meshNames.count
) )
} }
}
let numFrames = args[4] as! Int let numFrames = args[4] as! Int
let frameLenInMs = args[5] as! Double let frameLenInMs = args[5] as! Double
@@ -345,14 +370,10 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
result("OK") result("OK")
case "resize": case "resize":
let args = call.arguments as! Array<Double> let args = call.arguments as! Array<Double>
let width = Int(args[0]) let width = Int32(args[0])
let height = Int(args[1]) let height = Int32(args[1])
createPixelBuffer(width: width, height:height) resize(width:width, height:height)
create_swap_chain( result(self.textureId)
self.viewer,
unsafeBitCast(targetPixelBuffer!, to: UnsafeMutableRawPointer.self),
UInt32(width), UInt32(height))
result("OK")
case "rotateStart": case "rotateStart":
let args = call.arguments as! Array<Any> let args = call.arguments as! Array<Any>
grab_begin(self.viewer, Float(args[0] as! Double), Float(args[1] as! Double), false) grab_begin(self.viewer, Float(args[0] as! Double), Float(args[1] as! Double), false)