Может ли кто-нибудь подсказать мне, как добиться этого эффекта, или поделиться рабочей демонстрацией или фрагмент кода?
Я использую Swift для разработки. Буду признателен за любую помощь.
Вот базовая настройка моего ARSCNView с размещением узлов:
Код: Выделить всё
import UIKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
let configuration = ARWorldTrackingConfiguration()
sceneView.session.run(configuration)
sceneView.delegate = self
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
sceneView.addGestureRecognizer(tapGesture)
}
@objc func handleTap(_ gesture: UITapGestureRecognizer) {
let location = gesture.location(in: sceneView)
let hitResults = sceneView.hitTest(location, types: .featurePoint)
if let hitResult = hitResults.first {
let sphere = SCNSphere(radius: 0.05)
sphere.firstMaterial?.diffuse.contents = UIColor.blue
let node = SCNNode(geometry: sphere)
node.position = SCNVector3(hitResult.worldTransform.columns.3.x,
hitResult.worldTransform.columns.3.y,
hitResult.worldTransform.columns.3.z)
sceneView.scene.rootNode.addChildNode(node)
}
}
}
Я изучал пользовательские фрагментные шейдеры с помощью Metal, но не уверен, как применить их только к камере. кормить, сохраняя при этом узлы незатронутыми. Большинство примеров влияют на весь вид.
это файл SceneFilterTechnique.metal, который я использовал для создания оттенков серого
< div class="snippet">
Код: Выделить всё
#include
using namespace metal;
typedef struct {
float4 renderedCoordinate [[position]];
float2 textureCoordinate;
} TextureMappingVertex;
vertex TextureMappingVertex mapTexture(unsigned int vertex_id [[ vertex_id ]]) {
float4x4 renderedCoordinates = float4x4(float4( -1.0, -1.0, 0.0, 1.0 ),
float4( 1.0, -1.0, 0.0, 1.0 ),
float4( -1.0, 1.0, 0.0, 1.0 ),
float4( 1.0, 1.0, 0.0, 1.0 ));
float4x2 textureCoordinates = float4x2(float2( 0.0, 1.0 ),
float2( 1.0, 1.0 ),
float2( 0.0, 0.0 ),
float2( 1.0, 0.0 ));
TextureMappingVertex outVertex;
outVertex.renderedCoordinate = renderedCoordinates[vertex_id];
outVertex.textureCoordinate = textureCoordinates[vertex_id];
return outVertex;
}
fragment half4 displayTexture(TextureMappingVertex mappingVertex [[ stage_in ]],
texture2d texture [[ texture(0) ]]) {
constexpr sampler s(address::clamp_to_edge, filter::linear);
float4 color = texture.sample(s, mappingVertex.textureCoordinate);
float grayscale = (color.r + color.g + color.b) / 3.0;
return half4(grayscale, grayscale, grayscale, color.a);
}
поэтому, когда я пытаюсь добавить это в
sceneView.technique = filterTechnique
показывает белый экран
Это файл списка свойств
с тем же именем, что и у металлического файла
Код: Выделить всё
sequence
apply_filter
passes
apply_filter
metalVertexShader
mapTexture
metalFragmentShader
displayTexture
draw
DRAW_QUAD
inputs
scene
COLOR
outputs
color
COLOR
поэтому у меня есть функция для создания техники
Код: Выделить всё
private func makeTechnique(fromPlistNamed plistName: String) -> SCNTechnique {
guard let url = Bundle.main.url(forResource: plistName, withExtension: "plist") else {
fatalError("\(plistName).plist does not exist in the main bundle")
}
guard let dictionary = NSDictionary(contentsOf: url) as? [String: Any] else {
fatalError("Failed to parse \(plistName).plist as a dictionary")
}
guard let technique = SCNTechnique(dictionary: dictionary) else {
fatalError("Failed to initialize a technique using \(plistName).plist")
}
return technique
}
и я добавляю эту технику в SceneView как Follow
Код: Выделить всё
let filterTechnique = makeTechnique(fromPlistNamed: "SceneFilterTechnique")
sceneView.technique = filterTechnique
Я тоже прикрепил выходное изображение

Подробнее здесь: https://stackoverflow.com/questions/793 ... lored-in-s