使用 CALayers 制作浮雕演示文稿

发布于 2024-11-30 08:07:40 字数 75 浏览 0 评论 0原文

我正在寻找一种使用 CALayers 在 NSView 中显示浮雕图像的方法。有可能吗?如果没有,我如何从 CALayers 创建浮雕?

I'm searching a way to display an Anaglyph Image in a NSView using CALayers. It's possible? If not, how I can create anaglyph from CALayers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉宸 2024-12-07 08:07:40

我用以下代码解决了这个问题:

layerLeft=[[QTCaptureLayer alloc]initWithSession:sessionLeft];
layerRight=[[QTCaptureLayer alloc]initWithSession:sessionRight];
[anaglyphView setWantsLayer:YES];
CALayer *layer = [CALayer layer];
CALayer *containerLeft = [CALayer layer];
CALayer *containerRight = [CALayer layer];
[anaglyphView setLayer:layer];

layer.backgroundColor = CGColorCreateGenericRGB(0,0,0,1);
layer.borderColor=CGColorCreateGenericRGB(1,1,1,1);
layer.borderWidth=4.0;
layer.frame = NSRectToCGRect(NSMakeRect(0, 0, 1000, 1000));

[anaglyphView.layer addSublayer:containerLeft];
[anaglyphView.layer addSublayer:containerRight];
[containerLeft addSublayer:layerLeft];
[containerRight addSublayer:layerRight];

containerLeft.frame=NSRectToCGRect(NSMakeRect(50, 50, 600, 400));
containerRight.frame=NSRectToCGRect(NSMakeRect(50, 50, 600, 400));
layerLeft.frame = NSRectToCGRect(NSMakeRect(0, 0, 600, 400));
layerRight.frame=NSRectToCGRect(NSMakeRect(0, 0, 600, 400));

rightFilter = [CIFilter filterWithName:@"CIColorMatrix"];
leftFilter = [CIFilter filterWithName:@"CIColorMatrix"];
CIFilter *compositeFilter = [CIFilter filterWithName:@"CIDifferenceBlendMode"];


CGFloat zero[4] = {0.0f, 0.0f, 0.0f, 0.0f};
CGFloat r[4] = {1, 0.0f, 0.0f, 0.0f};
CGFloat g[4] = {0.0f, 1, 0.0f, 0.0f};
CGFloat b[4] = {0.0f, 0.0f, 1, 0.0f};
CGFloat a[4] = {0.0f, 0.0f, 0.0f, 1};
[rightFilter setValue:nil forKey:@"inputImage"];  
[rightFilter setValue:[CIVector vectorWithValues:zero count:4] forKey:@"inputRVector"];
[rightFilter setValue:[CIVector vectorWithValues:g count:4] forKey:@"inputGVector"];
[rightFilter setValue:[CIVector vectorWithValues:b count:4] forKey:@"inputBVector"];
[rightFilter setValue:[CIVector vectorWithValues:a count:4] forKey:@"inputAVector"];
[rightFilter setValue:[CIVector vectorWithValues:zero count:4] forKey:@"inputBiasVector"];

[leftFilter setValue:nil forKey:@"inputImage"];
[leftFilter setValue:[CIVector vectorWithValues:r count:4] forKey:@"inputRVector"];
[leftFilter setValue:[CIVector vectorWithValues:zero count:4] forKey:@"inputGVector"];
[leftFilter setValue:[CIVector vectorWithValues:zero count:4] forKey:@"inputBVector"];
[leftFilter setValue:[CIVector vectorWithValues:a count:4] forKey:@"inputAVector"];
[leftFilter setValue:[CIVector vectorWithValues:zero count:4] forKey:@"inputBiasVector"];

layerRight.compositingFilter =rightFilter;
layerLeft.compositingFilter =leftFilter;
containerRight.compositingFilter=compositeFilter;

其中anaglyphView是一个NSView,layerLeft和layerRight是两个QTCapureLayer。
我为每个通道(左/右)创建了 2 个图层,一层用于颜色过滤,另一层用于复合过滤。我不知道 CIDifferenceBlendMode 复合过滤器是否最适合制作浮雕,但它似乎有效。

I resolved the problem with the following code:

layerLeft=[[QTCaptureLayer alloc]initWithSession:sessionLeft];
layerRight=[[QTCaptureLayer alloc]initWithSession:sessionRight];
[anaglyphView setWantsLayer:YES];
CALayer *layer = [CALayer layer];
CALayer *containerLeft = [CALayer layer];
CALayer *containerRight = [CALayer layer];
[anaglyphView setLayer:layer];

layer.backgroundColor = CGColorCreateGenericRGB(0,0,0,1);
layer.borderColor=CGColorCreateGenericRGB(1,1,1,1);
layer.borderWidth=4.0;
layer.frame = NSRectToCGRect(NSMakeRect(0, 0, 1000, 1000));

[anaglyphView.layer addSublayer:containerLeft];
[anaglyphView.layer addSublayer:containerRight];
[containerLeft addSublayer:layerLeft];
[containerRight addSublayer:layerRight];

containerLeft.frame=NSRectToCGRect(NSMakeRect(50, 50, 600, 400));
containerRight.frame=NSRectToCGRect(NSMakeRect(50, 50, 600, 400));
layerLeft.frame = NSRectToCGRect(NSMakeRect(0, 0, 600, 400));
layerRight.frame=NSRectToCGRect(NSMakeRect(0, 0, 600, 400));

rightFilter = [CIFilter filterWithName:@"CIColorMatrix"];
leftFilter = [CIFilter filterWithName:@"CIColorMatrix"];
CIFilter *compositeFilter = [CIFilter filterWithName:@"CIDifferenceBlendMode"];


CGFloat zero[4] = {0.0f, 0.0f, 0.0f, 0.0f};
CGFloat r[4] = {1, 0.0f, 0.0f, 0.0f};
CGFloat g[4] = {0.0f, 1, 0.0f, 0.0f};
CGFloat b[4] = {0.0f, 0.0f, 1, 0.0f};
CGFloat a[4] = {0.0f, 0.0f, 0.0f, 1};
[rightFilter setValue:nil forKey:@"inputImage"];  
[rightFilter setValue:[CIVector vectorWithValues:zero count:4] forKey:@"inputRVector"];
[rightFilter setValue:[CIVector vectorWithValues:g count:4] forKey:@"inputGVector"];
[rightFilter setValue:[CIVector vectorWithValues:b count:4] forKey:@"inputBVector"];
[rightFilter setValue:[CIVector vectorWithValues:a count:4] forKey:@"inputAVector"];
[rightFilter setValue:[CIVector vectorWithValues:zero count:4] forKey:@"inputBiasVector"];

[leftFilter setValue:nil forKey:@"inputImage"];
[leftFilter setValue:[CIVector vectorWithValues:r count:4] forKey:@"inputRVector"];
[leftFilter setValue:[CIVector vectorWithValues:zero count:4] forKey:@"inputGVector"];
[leftFilter setValue:[CIVector vectorWithValues:zero count:4] forKey:@"inputBVector"];
[leftFilter setValue:[CIVector vectorWithValues:a count:4] forKey:@"inputAVector"];
[leftFilter setValue:[CIVector vectorWithValues:zero count:4] forKey:@"inputBiasVector"];

layerRight.compositingFilter =rightFilter;
layerLeft.compositingFilter =leftFilter;
containerRight.compositingFilter=compositeFilter;

Where anaglyphView is a NSView and layerLeft and layerRight are two QTCapureLayer.
I created 2 layers for each channel (left/right), one for color filtering and the other for composite filtering. I don't know if CIDifferenceBlendMode composite filter is the best for making anaglyph, but it seems to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文