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 targetPixelBuffer: CVPixelBuffer?;
var textureCache: CVOpenGLESTextureCache?; // var context: EAGLContext?;
var texture: CVOpenGLESTexture? = nil; // var textureCache: CVOpenGLESTextureCache?;
var frameBuffer: GLuint = 0; // 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 {
return
}
render(viewer, 0) render(viewer, 0)
self.registry.textureFrameAvailable(self.textureId!) self.registry.textureFrameAvailable(textureId)
}
} }
public func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? { public func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? {
@@ -96,6 +96,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();
@@ -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,18 +148,35 @@ 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;
switch methodName { switch methodName {
case "addLight": case "addLight":
let args = call.arguments as! Array<Any> let args = call.arguments as! Array<Any>
let entity = add_light( let entity = add_light(
self.viewer, self.viewer,
@@ -168,7 +191,7 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
Float(args[8] as! Double), Float(args[8] as! Double),
args[9] as! Bool) args[9] as! Bool)
result(entity); result(entity);
case "setAnimation": case "setAnimation":
let args = call.arguments as! Array<Any?> let args = call.arguments as! Array<Any?>
let assetPtr = UnsafeMutableRawPointer.init(bitPattern: args[0] as! Int) let assetPtr = UnsafeMutableRawPointer.init(bitPattern: args[0] as! Int)
@@ -180,39 +203,41 @@ 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>
let frameData = (boneAnimations[i][2] as! FlutterStandardTypedData) let frameData = (boneAnimations[i][2] as! FlutterStandardTypedData)
let frameDataNative = UnsafeMutableBufferPointer<Float>.allocate(capacity: Int(frameData.elementCount)) let frameDataNative = UnsafeMutableBufferPointer<Float>.allocate(capacity: Int(frameData.elementCount))
frameData.data.withUnsafeBytes{ (floatPtr: UnsafePointer<Float>) in frameData.data.withUnsafeBytes{ (floatPtr: UnsafePointer<Float>) in
for i in 0...Int(frameData.elementCount - 1) { for i in 0...Int(frameData.elementCount - 1) {
frameDataNative[i] = floatPtr.advanced(by: i).pointee frameDataNative[i] = floatPtr.advanced(by: i).pointee
}
} }
var boneNameArray = UnsafeMutableBufferPointer<UnsafePointer<CChar>?>.allocate(capacity: boneNames.count)
for i in 0...boneNames.count - 1 {
boneNameArray[i] = UnsafePointer(strdup(boneNames[i]))
}
var meshNameArray = UnsafeMutableBufferPointer<UnsafePointer<CChar>?>.allocate(capacity: meshNames.count)
for i in 0...meshNames.count - 1 {
meshNameArray[i] = UnsafePointer(strdup(meshNames[i]))
}
boneAnimStructs[i] = BoneAnimation(
boneNames: boneNameArray.baseAddress,
meshNames:meshNameArray.baseAddress,
data:frameDataNative.baseAddress,
numBones: boneNames.count,
numMeshTargets: meshNames.count
)
} }
var boneNameArray = UnsafeMutableBufferPointer<UnsafePointer<CChar>?>.allocate(capacity: boneNames.count)
for i in 0...boneNames.count - 1 {
boneNameArray[i] = UnsafePointer(strdup(boneNames[i]))
}
var meshNameArray = UnsafeMutableBufferPointer<UnsafePointer<CChar>?>.allocate(capacity: meshNames.count)
for i in 0...meshNames.count - 1 {
meshNameArray[i] = UnsafePointer(strdup(meshNames[i]))
}
boneAnimStructs[i] = BoneAnimation(
boneNames: boneNameArray.baseAddress,
meshNames:meshNameArray.baseAddress,
data:frameDataNative.baseAddress,
numBones: boneNames.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
morphData.data.withUnsafeBytes { (morphDataNative: UnsafePointer<Float>) in morphData.data.withUnsafeBytes { (morphDataNative: UnsafePointer<Float>) in
@@ -344,15 +369,11 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
doRender() doRender()
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)