公司网站建设设计方案,苏州市住房城乡建设局网站,seo搜索优化试卷,二级黄冈站首先#xff0c;3D场景中渲染的任何虚拟元素都必须具有网格#xff08;顶点及顶点间的拓扑关系#xff09;#xff0c;没有网格的元素无法利用GPU 进行渲染#xff0c;因此#xff0c;在3D 场景申渲染 3D文字时#xff0c;文字也必须具有网格。在计算机系统中#xff0… 首先3D场景中渲染的任何虚拟元素都必须具有网格顶点及顶点间的拓扑关系没有网格的元素无法利用GPU 进行渲染因此在3D 场景申渲染 3D文字时文字也必须具有网格。在计算机系统中文字以平面点阵的形式存储和表示所以进行3D文字渲染需要将平面点阵转换为3D网格。 在 RealityKit 中开发人员可以程序化地生成立方体、球体、圆柱体等3D虚拟对象这个过程其实就是利用算法生成立方体、球体、圆柱体的网格信息、法线信息、UV坐标信息的过程有了这些基础信息CPU与 GPU 就知道如何将虚拟对象渲染出来。 RealityKit 也提供了根据指定文字自动生成文字网格、法线信息、UV坐标信息的方法 generateText该方法返回 MeshResource 类型对象利用这个对象就可以对文字进行3D 渲染。在 RealityKit 中生成3D文字的典型代码如代码如下所示。
//
// Text3DView.swift
// ARKitDeamo
//
// Created by zhaoquan du on 2024/3/21.
//import SwiftUI
import ARKit
import RealityKit
import Combinestruct Text3DView: View {State var change: String 中文汉字var body: some View {Text3DViewContainer(change: change).overlay(VStack{Spacer()TextField( LocalizedStringKey(), text: $change).foregroundColor(.black).background(Color.white).frame(width:300,height:50).cornerRadius(5).opacity(0.6).offset(y:-330).padding(.bottom, 300)}).navigationTitle(3D文字).edgesIgnoringSafeArea(.all)}
}struct Text3DViewContainer:UIViewRepresentable {var change:String func makeUIView(context: Context) - some ARView {let arView ARView(frame: .zero)context.coordinator.arView arViewlet config ARWorldTrackingConfiguration()config.planeDetection .horizontalcontext.coordinator.createPlane()arView.session.run(config)return arView}func updateUIView(_ uiView: UIViewType, context: Context) {if !change.isEmpty {context.coordinator.chengeText(text: change)}}func makeCoordinator() - Coordinator {Coordinator()}class Coordinator: NSObject {var arView: ARView!var text: String var textEntity: ModelEntity!func createPlane() {let planeAnchor AnchorEntity(plane: .horizontal)let textr MeshResource.generateText(中文汉字,extrusionDepth: 0.05,font: .systemFont(ofSize: 15),containerFrame: .zero,alignment: .left,lineBreakMode: .byWordWrapping)let textMetiral SimpleMaterial(color: .red, isMetallic: true)textEntity ModelEntity(mesh: textr, materials: [textMetiral])textEntity.generateCollisionShapes(recursive: false)planeAnchor.addChild(textEntity)arView.scene.addAnchor(planeAnchor)arView.installGestures(.all, for: textEntity)}func chengeText(text: String) {let planeAnchor AnchorEntity(plane: .horizontal)let textr MeshResource.generateText(text,extrusionDepth: 0.05,font: .systemFont(ofSize: 2),containerFrame: .zero,alignment: .left,lineBreakMode: .byWordWrapping)let textMetiral SimpleMaterial(color: .red, isMetallic: true)textEntity.removeFromParent()textEntity ModelEntity(mesh: textr, materials: [textMetiral])textEntity.generateCollisionShapes(recursive: false)planeAnchor.addChild(textEntity)arView.scene.addAnchor(planeAnchor)arView.installGestures(.all, for: textEntity)}}
}
#Preview {Text3DView()
}从代码可以看到生成3D文字的过程与生成其他程序化虚拟模型对象的过程完全一致唯一区别是生成 3D 文字网格的方法要求设置的参数更多generateText方法原型
static func generateText (_ string: String, extrusionDepth: Float 0. 25, font: MeshResource. Font .systemPont(ofSize: MeshResource. Font. systemFontSize), containerFrame: CGRect CGRect. zero, alignment: CTTextAlignment . left, lineBreakMode: CTLineBreakMode byTruncatingTail) - MeshResource
generateText方法参数众多但实际除了 string 其余参数都可以使用默认值各参数的意义如下表所示。 表11-1 生成3D文字网格的参数属性 参数名 描述 string 需要3D渲染的文字使用内置的systemFont 可以渲染中文汉字与英文字符如果使用其他字体渲染中文汉字需要确保字体支持 extrusionDepth 渲染的文字厚度即在Z轴上的长度以米为单位 font 渲染所用字体渲染中文汉字需要字体支持使用该属性可以指定字体大小。默认使用系统字体 containerFrame 该属性指定文字所占空间尺寸类似于 Word文字排版软件中的文本框指定文字所占尺寸当指定该值时如果文字渲染超出该尺寸则会以 lineBreakMode 属性指定的方式截断。默认为00会以最合适的大小包裹所有文字 alignment 文字在 containerFrame 中的对齐方式可以为 center居中对齐、justified分散对齐、left左对齐、natural两端对齐、right右对齐之一该属性会影响缩放、旋转3D文字时的定位点 lineBreakMode 文字超出 containerFrame 范围时的截断方式可以 byWordWrapping以单词/汉字为单位显示超出部分不显示、byCharWrapping以字符/汉字单位显示超出部分不显示、byClipping剪切与containerFrame 尺寸一致的内容长度后半部分被截断、byTruncatingHead前面文字被截断用省略号显示、byTruncatingTail后面文字被截断用省略号显示、byTruncatingMiddle两端文字保留中间文字被省略用省略号显示之一 generateText方法生成的文字 3D网格可以与其他程序化虚拟模型对象一样被赋子材质包括纹理也可以使用 ARAnchor 将其固定到场景中。 在 RealityKit 中生成的文字 3D网格不可修改因此无法通过网格修改的方式更新谊染的3D文字果需要更新已生成的3D文字则只能重新生成新的文字3D网格。 上述代码我们直接使用 changeText方法重新生成新的文字3D网格然后重新生成 textEntity 实体更新渲染的3D文字。在实际开发中也可以通过扩展extension Entity 或者 ModelEntity 类添加更新 3D文字的方法达到更方便使用的目的。