- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
题
你如何定义 custom geometry from vertex data 上的 Material ,以便它呈现与“典型”SCNNodes 相同的效果?
细节
在这个场景中有
import UIKit
import SceneKit
import PlaygroundSupport
// create a scene view with an empty scene
var sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 600, height: 600))
var scene = SCNScene()
sceneView.scene = scene
sceneView.backgroundColor = UIColor(white: 0.75, alpha: 1.0)
sceneView.allowsCameraControl = true
PlaygroundPage.current.liveView = sceneView
let directionalLightNode: SCNNode = {
let n = SCNNode()
n.light = SCNLight()
n.light!.type = SCNLight.LightType.directional
n.light!.color = UIColor(white: 0.75, alpha: 1.0)
return n
}()
directionalLightNode.simdPosition = simd_float3(0,5,0) // Above the scene
directionalLightNode.simdOrientation = simd_quatf(angle: -90 * Float.pi / 180.0, axis: simd_float3(1,0,0)) // pointing down
scene.rootNode.addChildNode(directionalLightNode)
// a camera
var cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.simdPosition = simd_float3(0,0,5)
scene.rootNode.addChildNode(cameraNode)
添加蓝色和红色球体
// ----------------------------------------------------
// Example creating SCNSphere Nodes directly
// Sphere 1
let sphere1 = SCNSphere(radius: 0.3)
let sphere1Material = SCNMaterial()
sphere1Material.diffuse.contents = UIColor.red
sphere1Material.lightingModel = .physicallyBased
sphere1.materials = [sphere1Material]
let sphere1Node = SCNNode(geometry: sphere1)
sphere1Node.simdPosition = simd_float3(-2,0,0)
// Sphere2
let sphere2 = SCNSphere(radius: 0.3)
let sphere2Material = SCNMaterial()
sphere2Material.diffuse.contents = UIColor.blue
sphere2Material.lightingModel = .physicallyBased
sphere2.materials = [sphere2Material]
let sphere2Node = SCNNode(geometry: sphere2)
sphere2Node.simdPosition = simd_float3(-1,0,0)
scene.rootNode.addChildNode(sphere1Node)
scene.rootNode.addChildNode(sphere2Node)
// ----------------------------------------------------
// Example creating SCNGeometry using vertex data
struct Vertex {
let x: Float
let y: Float
let z: Float
let r: Float
let g: Float
let b: Float
}
let vertices: [Vertex] = [
Vertex(x: 0.0, y: 0.0, z: 0.0, r: 1.0, g: 0.0, b: 0.0),
Vertex(x: 1.0, y: 0.0, z: 0.0, r: 0.0, g: 0.0, b: 1.0)
]
let vertexData = Data(
bytes: vertices,
count: MemoryLayout<Vertex>.size * vertices.count
)
let positionSource = SCNGeometrySource(
data: vertexData,
semantic: SCNGeometrySource.Semantic.vertex,
vectorCount: vertices.count,
usesFloatComponents: true,
componentsPerVector: 3,
bytesPerComponent: MemoryLayout<Float>.size,
dataOffset: 0,
dataStride: MemoryLayout<Vertex>.size
)
let colorSource = SCNGeometrySource(
data: vertexData,
semantic: SCNGeometrySource.Semantic.color,
vectorCount: vertices.count,
usesFloatComponents: true,
componentsPerVector: 3,
bytesPerComponent: MemoryLayout<Float>.size,
dataOffset: MemoryLayout<Float>.size * 3,
dataStride: MemoryLayout<Vertex>.size
)
let elements = SCNGeometryElement(
data: nil,
primitiveType: .point,
primitiveCount: vertices.count,
bytesPerIndex: MemoryLayout<Int>.size
)
elements.pointSize = 100
elements.minimumPointScreenSpaceRadius = 100
elements.maximumPointScreenSpaceRadius = 100
let spheres = SCNGeometry(sources: [positionSource, colorSource], elements: [elements])
let sphereNode = SCNNode(geometry: spheres)
let sphereMaterial = SCNMaterial()
sphereMaterial.lightingModel = .physicallyBased
spheres.materials = [sphereMaterial]
sphereNode.simdPosition = simd_float3(0,0,0)
scene.rootNode.addChildNode(sphereNode)
normals
现在显示颜色,但在所有方向(即没有阴影)。
SCNSphere()
第三点指向我的
VertexData
,两者都使用相同的RGB值,但
VertexData
中的黑色物体看起来太“轻”
let vertices: [Vertex] = [
Vertex(x: 0.0, y: 0.0, z: 0.0, r: 1.0, g: 0.0, b: 0.0),
Vertex(x: 1.0, y: 0.0, z: 0.0, r: 0.0, g: 0.0, b: 1.0),
Vertex(x: 0.0, y: 1.0, z: 0.0, r: 0.07, g: 0.11, b: 0.12)
]
let vertexData = Data(
bytes: vertices,
count: MemoryLayout<Vertex>.size * vertices.count
)
let normals = Array(repeating: SCNVector3(1,1,1), count: vertices.count)
let normalSource = SCNGeometrySource(normals: normals)
///
///
let spheres = SCNGeometry(
sources: [
positionSource,
normalSource,
colorSource
],
elements: [elements]
)
最佳答案
根据documentation ,制作自定义几何图形需要 3 个步骤。
SCNGeometrySource
包含 3D 形状的顶点。 SCNGeometryElement
包含一个索引数组,显示顶点如何连接。 SCNGeometrySource
来源和 SCNGeometryElement
成SCNGeometry
. let vertices: [Vertex] = [ /// what's `r`, `g`, `b` for btw?
Vertex(x: 0.0, y: 0.0, z: 0.0, r: 1.0, g: 0.0, b: 0.0),
Vertex(x: 1.0, y: 0.0, z: 0.0, r: 0.0, g: 0.0, b: 1.0)
]
这将形成一条线...
let vertices: [Vertex] = [
Vertex(x: 0.0, y: 0.0, z: 0.0, r: 1.0, g: 0.0, b: 0.0), /// vertex 0
Vertex(x: 1.0, y: 0.0, z: 0.0, r: 0.0, g: 0.0, b: 1.0), /// vertex 1
Vertex(x: 1.0, y: 0.0, z: -0.5, r: 0.0, g: 0.0, b: 1.0), /// vertex 2
Vertex(x: 0.0, y: 1.0, z: 0.0, r: 0.0, g: 0.0, b: 1.0), /// vertex 3
]
vertices
进入
Data
,然后使用
init(data:semantic:vectorCount:usesFloatComponents:componentsPerVector:bytesPerComponent:dataOffset:dataStride:)
初始化程序。
let vertexData = Data(
bytes: vertices,
count: MemoryLayout<Vertex>.size * vertices.count
)
let positionSource = SCNGeometrySource(
data: vertexData,
semantic: SCNGeometrySource.Semantic.vertex,
vectorCount: vertices.count,
usesFloatComponents: true,
componentsPerVector: 3,
bytesPerComponent: MemoryLayout<Float>.size,
dataOffset: 0,
dataStride: MemoryLayout<Vertex>.size
)
这是非常先进和复杂的。使用
init(vertices:)
更容易.
let verticesConverted = vertices.map { SCNVector3($0.x, $0.y, $0.z) } /// convert to `[SCNVector3]`
let positionSource = SCNGeometrySource(vertices: verticesConverted)
现在您已经获得了
SCNGeometrySource
, 是时候了
第 2 步 — 通过
SCNGeometryElement
连接顶点.在您当前的代码中,您使用
init(data:primitiveType:primitiveCount:bytesPerIndex:)
,然后传入
nil
...
let elements = SCNGeometryElement(
data: nil,
primitiveType: .point,
primitiveCount: vertices.count,
bytesPerIndex: MemoryLayout<Int>.size
)
如果数据本身是
nil
,SceneKit 如何知道如何连接顶点?但无论如何,还有一个更简单的初始化程序:
init(indices:primitiveType:)
.这需要一个数组
FixedWidthInteger
,每个代表一个顶点回到你的
positionSource
.
FixedWidthInteger
表示?好吧,还记得你是怎么通过的
verticesConverted
,
SCNVector3
的数组, 至
positionSource
? SceneKit 看到每个
FixedWidthInteger
作为索引并使用它访问
verticesConverted
.
UInt16
应该没问题(它符合
FixedWidthInteger
)。
/// pairs of 3 indices, each representing a vertex
let indices: [UInt16] = [
0, 1, 3, /// front triangle
1, 2, 3, /// right triangle
2, 0, 3, /// back triangle
3, 0, 2, /// left triangle
0, 2, 1 /// bottom triangle
]
let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)
这里的顺序非常具体。默认情况下,SceneKit 只渲染三角形的正面,为了区分正面和背面,它依赖于您的排序。基本规则是:
逆时针意思是前面。
SCNGeometrySource
和
SCNGeometryElement
.
let geometry = SCNGeometry(sources: [positionSource], elements: [element])
就是这样!现在您的
SCNGeometrySource
和
SCNGeometryElement
设置正确,
lightingModel
将正常工作。
/// add some color
let material = SCNMaterial()
material.diffuse.contents = UIColor.orange
material.lightingModel = .physicallyBased
geometry.materials = [material]
/// add the node
let node = SCNNode(geometry: geometry)
scene.rootNode.addChildNode(node)
SCNGeometrySource
s。第二个是用 SCNGeometrySource.Semantic.color
添加颜色, 对?我使用的更简单的初始化程序, init(vertices:)
, 默认为 .vertex
.如果你想要每个顶点的颜色或其他东西,你可能需要回到 init(data:semantic:vectorCount:usesFloatComponents:componentsPerVector:bytesPerComponent:dataOffset:dataStride:)
. sceneView.autoenablesDefaultLighting = true
为了更好的照明elements.pointSize = 100
elements.minimumPointScreenSpaceRadius = 100
elements.maximumPointScreenSpaceRadius = 100
... 那么 2D 圆将是你能得到的最好的。
pointSize
documentation :
SceneKit can render each point as a small 2D surface that always faces the camera. By applying a texture or custom shader to that surface, you can efficiently render many small objects at once.
.physicallyBased
照明不起作用(
.constant
会,但就是这样)。最好用许多小三角形制作球体,例如上面答案中的金字塔。这也是 Apple 对其内置几何体所做的,包括
SCNSphere
.
let sphere = SCNSphere(radius: 1)
let sphereMaterial = SCNMaterial()
sphereMaterial.diffuse.contents = UIColor.purple
sphereMaterial.fillMode = .lines /// add this to see the triangles
sphereMaterial.lightingModel = .physicallyBased
sphere.materials = [sphereMaterial]
let sphereNode = SCNNode(geometry: sphere)
scene.rootNode.addChildNode(sphereNode)
关于swift - 自定义 SCNGeometry 节点上的基于物理的照明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69240185/
我正在尝试在 SceneKit 中创建自定义几何形状,结果我的形状很好。唯一的问题是它是黑色的,即使我改变了颜色,它仍然是黑色的。我已经在 SceneKit 中测试了其他原始形状,结果是正常的。我以为
我制作了这个方法来抓取 UIImage 并将其转换为 3D 模型。然而,这意味着我添加了很多节点。我在想也许可以通过将所有几何图形添加到单个节点中来优化它。有办法实现吗? 这是代码 static in
试图弄清楚我如何创建一个多边形作为原始类型的 SCNGeometry,我的目标是添加多边形节点作为球体节点的子节点,并使其看起来像 map 套件的 MKPolygon,like in this exa
题 你如何定义 custom geometry from vertex data 上的 Material ,以便它呈现与“典型”SCNNodes 相同的效果? 细节 在这个场景中有 定向灯 使用 ph
我正在从网络服务器下载 collada 文件,并希望以编程方式映射纹理,但 SCNGeometry 为零。有人可以提出建议吗?下面是示例代码。 let url = URL(string: "h
我正在尝试使用 kvo 来观察节点的几何形状,因为 Scenekit 不提供某种回调来让我知道节点的详细程度何时已更新。这样做的目的是我的节点有空闲动画,当细节级别更改为最详细版本以外的任何内容时,我
我正在创建自定义 SCNGeometry。首先,我将其开发为平面。它会显示,我可以将颜色应用到它的漫反射内容,但如果我尝试应用任何东西,例如 UIImage 或 CALayer,它会显示为白色。 代码
我正在尝试创建自定义 SCNPlane可以具有任何多边形形式的类,因为常规 SCNPlane 只能是矩形。 我试图通过扩展 SCNGeometry 来实现这一点.下面是我使用的代码: extensio
这个问题在这里已经有了答案: Stroke Width with a SceneKit line primitive type (2 个答案) 关闭 5 年前。 我创建了一条线,但无法增加它的粗细。
我在 iOS 上使用 SceneKit,并且我有一个几何体要渲染为线框。所以基本上我只想画线,所以没有纹理。 我发现我可以使用 SCNMaterial 的 shaderModifiers 属性来完成此
好像我想通过组合一些 SCNBox 和 SCNPyramid 等来创建一些自定义形状,我可以通过设置正确的位置和几何形状将它们放在一起。然而我就是找不到一种方法将它们组合成一个可以在物理世界中修改或
我想将法线映射添加到 SCNGeometry。我有一个法线贴图图像 (PNG) 和第二组 UV 坐标,作为 GeometrySource 注入(inject)。 如果可能的话,我最喜欢的解决方案是为受
我使用 UIView 作为 SCNPlane 的漫反射 Material ,这样我就可以在我的 3D 场景中显示 2D 菜单。 这如我所料: var node:SCNNode = // A child
我在大学里接到了一个任务,要为 SCNGeometry Octahedron 添加一张图片作为纹理。这是我在 Swift 中的第一个项目。 对于带有 UIImage 类的 UIKit 有很多建议,但我
我想弄清楚如何在屏幕上显示一个 SCNScylinder,其中只有一个边框/描边/边缘可见。我场景中的一切都工作正常,我正在考虑将清晰的颜色应用于 specular.contents 是否需要使用 S
我正在尝试创建一个具有自定义形状的平面形式的自定义 SCNGeometry,它可以放置在 ARKit session 中。我在以下方法中使用选项 SCNGeometryPrimitiveTypePol
Apple 最近发布了 Model I/O Framework,可以理解,文档当前为 quite sparse许多类(class)仍未完全记录。 Model I/O talk建议在 SceneKit
我正在创建 AR 应用程序(Xcode 10.1、Swift 4.2.1)。 我想将 USDZ 3D 对象加载到一个空 SceneKit 的场景中,然后将其作为 MDL 网格进行处理。 这是我的代码:
我正在尝试将一个简单的文本标签放在一个(希望是所有类型的)内置 SCNGeometry 形状上,因为它们在屏幕上移动。我最接近成功的是通过 .firstMaterial.diffuse.content
将图像作为 Material 添加到预定义的 3D 对象(例如 Swift 中的立方体)对我来说效果很好。现在我正在尝试将图像作为 Material 添加到在运行时动态创建的多边形中。 我希望这样的事
我是一名优秀的程序员,十分优秀!