GVKun编程网logo

使用粒子系统爆炸3D文本(Swift – SceneKit)(3d粒子爆炸怎么用)

1

在本文中,我们将为您详细介绍使用粒子系统爆炸3D文本(Swift–SceneKit)的相关知识,并且为您解答关于3d粒子爆炸怎么用的疑问,此外,我们还会提供一些关于3d地图Scenekit上的指南针、

在本文中,我们将为您详细介绍使用粒子系统爆炸3D文本(Swift – SceneKit)的相关知识,并且为您解答关于3d粒子爆炸怎么用的疑问,此外,我们还会提供一些关于3d 地图 Scenekit 上的指南针、ARKit + SceneKit:使用重建的物理场景?、iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts、ios – Swift Scenekit – 居中SCNText – getBoundingBoxMin:Max问题的有用信息。

本文目录一览:

使用粒子系统爆炸3D文本(Swift – SceneKit)(3d粒子爆炸怎么用)

使用粒子系统爆炸3D文本(Swift – SceneKit)(3d粒子爆炸怎么用)

我正在使用 Swift SceneKit制作一些爆炸的3D文本.这是文字:

let text = SCNText(string: "Exploding Text",extrusionDepth: 5)
let node = SCNNode(geometry: SCNText)
scene.rootNode.addChildNode(node)

这是我的粒子系统:

let exp = SCNParticleSystem()
exp.loops = false
exp.birthRate = 5000
exp.emissionDuration = 0.01
exp.spreadingAngle = 180
exp.particleDiesOnCollision = true
exp.particleLifeSpan = 0.5
exp.particleLifeSpanVariation = 0.3
exp.particleVeLocity = 500
exp.particleVeLocityVariation = 3
exp.particleSize = 0.05
exp.stretchFactor = 0.05
exp.particleColor = UIColor.blueColor()
scene.addParticleSystem(exp,withTransform: SCNMatrix4MakeRotation(0,0))

现在,粒子从文本中心的单个点发出.有没有办法将粒子锁定到文本的表面,然后运行系统来模拟爆炸文本?

如果没有,这可以用任何其他几何对象如立方体完成吗?

解决方法

您可以使用指定发射器形状

particleSystem.emitterShape = aGeometry;

然后指定“birthLocation”
SCNParticleBirthLocationSurface
要么
SCNParticleBirthLocationVertex

从文本/立方体的表面或顶点发射

3d 地图 Scenekit 上的指南针

3d 地图 Scenekit 上的指南针

如何解决3d 地图 Scenekit 上的指南针

我想要一个像 Google 地球这样的指南针。我正在使用 Scenekit 创建一个带有围绕它移动的相机的地球。球体位于 (0,0) 处的固定位置。我使用四元数移动相机,将新方向应用于空节点。 scene scheme

我想在这样的指南针中显示相机相对于北极的方向 compass Behavior

我试过用向上向量计算角度,但我得到了错误的值。

  1. let worldUp = earthOrbitalCamera.orbitalNode.worldUp
  2. let angle = atan2(worldUp.y,worldUp.x)

通过这个角度,我更新了针的位置。问题是我得到了错误的值。

例如,相机与极北对齐,针指向西 40 度。

任何帮助将不胜感激。谢谢。

解决方法

如果球形模型对齐以使其北极始终指向世界空间中的 +Y 方向,则以下代码可能会起作用。

  1. let worldUp = earthOrbitalCamera.orbitalNode.worldUp
  2. let worldRight = earthOrbitalCamera.orbitalNode.worldRight
  3. let angle = atan2(worldRight.y,worldUp.y)

ARKit + SceneKit:使用重建的物理场景?

ARKit + SceneKit:使用重建的物理场景?

如何解决ARKit + SceneKit:使用重建的物理场景??

我将 ARKit 与 SceneKit 结合使用,并希望让我的 3D 对象与具有激光雷达传感器 (config.sceneReconstruction = .mesh) 的设备创建的 reconstructed scene 进行物理交互。例如,让虚拟球从重建场景的几何形状反弹。

在 RealityKit 中,这似乎可以使用 sceneUnderstanding

arView.environment.sceneUnderstanding.options.insert(.physics)

如何在使用 SceneKit 时达到同样的效果?

解决方法

据我所知,没有使用 SceneKit 启用此功能的内置支持。但是,您可以使用 ARKit 创建的 ARMeshAnchor 轻松组合自定义解决方案。

首先,配置 ARKit 以启用场景重建:

let config = ARWorldTrackingConfiguration()
if ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh) {
    config.sceneReconstruction = .mesh
} else {
    // Handle device that doesn''t support scene reconstruction
}

// and enable physics visualization for debugging
sceneView.debugOptions = [.showPhysicsShapes]

然后在您的 ARSCNViewDelegate 中使用 renderer nodeFor 为新创建的 ARMeshAnchor 实例创建一个 Scenekit 节点:

func renderer(_ renderer: SCNSceneRenderer,nodeFor anchor: ARAnchor) -> SCNNode? {
    guard let meshAnchor = anchor as? ARMeshAnchor else {
        return nil
    }

    let geometry = createGeometryFromAnchor(meshAnchor: meshAnchor)

    // Optionally hide the node from rendering as well
    geometry.firstMaterial?.colorBufferWriteMask = []

    let node = SCNNode(geometry: geometry)

    // Make sure physics apply to the node
    // You must used concavePolyhedron here!
    node.physicsBody = SCNPhysicsBody(type: .static,shape: SCNPhysicsShape(geometry: geometry,options: [.type: SCNPhysicsShape.ShapeType.concavePolyhedron]))

    return node
}

// Taken from https://developer.apple.com/forums/thread/130599
func createGeometryFromAnchor(meshAnchor: ARMeshAnchor) -> SCNGeometry {
    let meshGeometry = meshAnchor.geometry
    let vertices = meshGeometry.vertices
    let normals = meshGeometry.normals
    let faces = meshGeometry.faces
    
    // use the MTL buffer that ARKit gives us
    let vertexSource = SCNGeometrySource(buffer: vertices.buffer,vertexFormat: vertices.format,semantic: .vertex,vertexCount: vertices.count,dataOffset: vertices.offset,dataStride: vertices.stride)
    
    let normalsSource = SCNGeometrySource(buffer: normals.buffer,vertexFormat: normals.format,semantic: .normal,vertexCount: normals.count,dataOffset: normals.offset,dataStride: normals.stride)
    // Copy bytes as we may use them later
    let faceData = Data(bytes: faces.buffer.contents(),count: faces.buffer.length)
    
    // create the geometry element
    let geometryElement = SCNGeometryElement(data: faceData,primitiveType: toSCNGeometryPrimitiveType(faces.primitiveType),primitiveCount: faces.count,bytesPerIndex: faces.bytesPerIndex)
    
    return SCNGeometry(sources: [vertexSource,normalsSource],elements: [geometryElement])
}

最后,每当 ARSCNViewDelegate renderer didUpdate: 函数中重建的几何体发生变化时,更新场景节点:

func renderer(_ renderer: SCNSceneRenderer,didUpdate node: SCNNode,for anchor: ARAnchor) {
    guard let meshAnchor = anchor as? ARMeshAnchor else {
        return
    }
    
    let geometry = SCNGeometry.fromAnchor(meshAnchor: meshAnchor,setColors: false)
    geometry.firstMaterial?.colorBufferWriteMask = []

    node.geometry = geometry

    node.physicsBody!.physicsShape = SCNPhysicsShape(geometry: geometry,options: [.type: SCNPhysicsShape.ShapeType.concavePolyhedron])
}

您在 SceneKit 中创建的任何物理对象现在都应该能够与重建的场景交互:

iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>

iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts

如何解决iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>

我不断收到来自随机用户的随机崩溃报告。不幸的是,我无法定期重现这一点。用户说崩溃是在 discussionViewController 中随机发生的。所有崩溃报告都有类似的内容:

0   libswiftCore.dylib              0x00000001a53face4 swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::incrementSlow(swift::RefCountBitsT<(swift::RefCountInlinedness)1>,unsigned int) + 60 (atomic:1003)
1   libswiftCore.dylib              0x00000001a53c59e0 swift_retain + 124 (RefCount.h:813)
2   libswiftCore.dylib              0x00000001a5401d60 swift_bridgeObjectRetain + 56 (SwiftObject.mm:585)
3   APPNAME                             0x0000000102b59734 closure #1 in discussionViewController.fetchPostData() + 7916

这是完整的崩溃日志和崩溃的线程:

Hardware Model:      iphone11,6
Process:             APPNAME [11770]
Path:                /private/var/containers/Bundle/Application/.../APPNAME.app/APPNAME
Identifier:          ----
Version:             62 (62)
AppStoretools:       12E262
AppVariant:          1:iphone11,6:13
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           ---- [1824]


Date/Time:           2021-06-17 12:07:01.4346 +1000
Launch Time:         2021-06-17 12:06:56.4993 +1000
OS Version:          iPhone OS 14.6 (18F72)
Release Type:        User
Baseband Version:    3.04.01
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x8000000000000010 -> 0x0000000000000010 (possible pointer authentication failure)
VM Region Info: 0x10 is not in any region.  Bytes before following region: 4339515376
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                   102a7c000-102a94000 [   96K] r-x/r-x SM=COW  ...APPNAME.app/APPNAME

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL,Code 0xb
Terminating Process: exc handler [11770]
Triggered by Thread:  3


Thread 3 name:
Thread 3 Crashed:
0   libswiftCore.dylib              0x00000001a53face4 swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::incrementSlow(swift::RefCountBitsT<(swift::RefCountInlinedness)1>,unsigned int) + 60 (atomic:1003)
1   libswiftCore.dylib              0x00000001a53c59e0 swift_retain + 124 (RefCount.h:813)
2   libswiftCore.dylib              0x00000001a5401d60 swift_bridgeObjectRetain + 56 (SwiftObject.mm:585)
3   APPNAME                             0x0000000102b59734 closure #1 in discussionViewController.fetchPostData() + 7916
4   APPNAME                             0x0000000102ad09d4 thunk for @escaping @callee_guaranteed (@guaranteed Data?,@guaranteed NSURLResponse?,@guaranteed Error?) -> () + 132 (<compiler-generated>:0)
5   CFNetwork                       0x00000001a1b0a3dc __40-[__NSURLSessionLocal taskForClassInfo:]_block_invoke + 540 (LocalSession.mm:687)
6   CFNetwork                       0x00000001a1b1c768 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 244 (LocalSessionTask.mm:584)
7   libdispatch.dylib               0x00000001a10d1a84 _dispatch_call_block_and_release + 32 (init.c:1466)
8   libdispatch.dylib               0x00000001a10d381c _dispatch_client_callout + 20 (object.m:559)
9   libdispatch.dylib               0x00000001a10db004 _dispatch_lane_serial_drain + 620 (inline_internal.h:2557)
10  libdispatch.dylib               0x00000001a10dbc34 _dispatch_lane_invoke + 456 (queue.c:3862)
11  libdispatch.dylib               0x00000001a10e64bc _dispatch_workloop_worker_thread + 764 (queue.c:6589)
12  libsystem_pthread.dylib         0x00000001ed04a7a4 0x1ed047000 + 14244
13  libsystem_pthread.dylib         0x00000001ed05174c 0x1ed047000 + 42828

我已验证 discussionViewController.fetchPostData() 不会强制解开任何可选选项,没有 try! 并且在任何地方都使用 [weak self]self?。该函数非常大,所以我很难缩小崩溃发生的范围。

ios – Swift Scenekit – 居中SCNText – getBoundingBoxMin:Max问题

ios – Swift Scenekit – 居中SCNText – getBoundingBoxMin:Max问题

享受SCNText上的alignmentMode选项.谷歌搜索周围看起来像alignmentMode和containerFrame有问题.我发现的替代方案建议使用获取边界框功能来查找文本大小,然后相应地手动调整.很棒,除了我不能让功能工作.当我试图得到两个向量时,我得到一个错误:

‘SCNVector3’无法转换为’UnsafeMutablePointer< SCNVector3>” 我在几何和节点上得到了它.代码示例如下

func setCounterValue(counterValue:Int) {

    var v1 = SCNVector3(x: 0,y: 0,z: 0)
    var v2 = SCNVector3(x: 0,z: 0)


    _counterValue = counterValue

    let newText = SCNText(string: String(format: "%06d",counterValue),extrusionDepth:sDepth)
    newText.font = UIFont (name: "Arial",size: 3)
    newText.firstMaterial!.diffuse.contents = UIColor.whiteColor()
    newText.firstMaterial!.specular.contents = UIColor.whiteColor()

    newText.getBoundingBoxMin(v1,max: v2)

    _textNode = SCNNode(geometry: newText)
    _textNode.getBoundingBoxMin(v1,max: v2)

}

任何建议都非常感谢.

解决方法

好的,所以我的最终代码解决方案如下:
func setCounterValue(counterValue:Int) {

    var v1 = SCNVector3(x: 0,z: 0)

    _textNode.removeFromParentNode()
    _counterValue = counterValue

    let newText = SCNText(string: String(format: "%08d",size: 3)
    newText.firstMaterial!.diffuse.contents = UIColor.whiteColor()
    newText.firstMaterial!.specular.contents = UIColor.whiteColor()

    _textNode = SCNNode(geometry: newText)
    _textNode.getBoundingBoxMin(&v1,max: &v2)

    let dx:Float = Float(v1.x - v2.x)/2.0
    let dy:Float = Float(v1.y - v2.y)
    _textNode.position = SCNVector3Make(dx,dy,Float(sDepth/2))

    node.addChildNode(_textNode)

}

我留下了几个全局变量,但应该有意义.

谢谢大家的帮助.

我们今天的关于使用粒子系统爆炸3D文本(Swift – SceneKit)3d粒子爆炸怎么用的分享已经告一段落,感谢您的关注,如果您想了解更多关于3d 地图 Scenekit 上的指南针、ARKit + SceneKit:使用重建的物理场景?、iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts、ios – Swift Scenekit – 居中SCNText – getBoundingBoxMin:Max问题的相关信息,请在本站查询。

本文标签: