- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试实现一个 Metal 支持的绘图应用程序,其中通过沿路径重复冲压纹理正方形在 MTKView 上绘制笔触。我遇到的问题是,虽然每个画笔图章都正确显示了纹理的不透明度,但重叠的方 block 不会产生值(value),而是会相互覆盖。在下面的标题中,每个图章都是一个带有 alpha 分量的纹理圆圈
我有一种感觉,因为所有的图章都是一次渲染的,所以渲染器没有办法“建立”值。然而,我对我的 Metal 知识有点不了解,所以我希望有人能给我指出正确的方向。
以下是进一步的相关信息:
对于单个笔刷笔划,所有几何体都存储在一个数组 vertexArrayBrush3DMesh 中,该数组包含所有正方形图章(每个正方形由 2 个三角形组成)。每个顶点的坐标的 z 值为 0.0,这意味着它们都占据相同的 3d“平面”。这可能是个问题吗? (我测试了放置随机 z 值,但我没有看到行为的视觉差异)
下面是我的 renderPipeline 设置。请注意,“.isBlendingEnabled = true”和“.alphaBlendingOperation = .add”都被注释掉了,因为它们对解决我的问题没有影响
// 5a. Define render pipeline settings
let renderPipelineDescriptor = MTLRenderPipelineDescriptor()
renderPipelineDescriptor.vertexFunction = vertexProgram
renderPipelineDescriptor.sampleCount = self.sampleCount
renderPipelineDescriptor.colorAttachments[0].pixelFormat = self.colorPixelFormat
//renderPipelineDescriptor.colorAttachments[0].isBlendingEnabled = true
//renderPipelineDescriptor.colorAttachments[0].alphaBlendOperation = .add
renderPipelineDescriptor.fragmentFunction = fragmentProgram
请注意,将以下属性添加到 renderPassDescriptor 确实对设置整个 Canvas 的透明度有影响
// ensure canvas is transparent
renderPassDescriptor?.colorAttachments[0].loadAction = .clear
renderPassDescriptor?.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 0)
下面是我执行渲染的代码部分。
func metalRenderColoredMesh(){
// the key to this routine is that it operates on a prepopulated array of points stored in vertexArrayBrush3DMesh
// whenever we want to render the current mesh, this routine gets called from draw()
if vertexArrayBrush3DMesh.count > 1 { // we must have more than 2 points to be able to draw a line
// 6. Set buffer size of objects to be drawn
let dataSize = vertexArrayBrush3DMesh.count * MemoryLayout<Vertex3DColor>.stride // apple recommendation size of the vertex data in bytes
let vertexBuffer: MTLBuffer = device!.makeBuffer(bytes: vertexArrayBrush3DMesh, length: dataSize, options: [])! // create a new buffer on the GPU
let renderPassDescriptor: MTLRenderPassDescriptor? = self.currentRenderPassDescriptor
let samplerState: MTLSamplerState? = defaultSampler(device: self.device!)
let texture = MetalTexture(resourceName: "opaqueRound", ext: "png", mipmaped: true)
texture.loadTexture(device: device!, commandQ: commandQueue, flip: true)
// If the renderPassDescriptor is valid, begin the commands to render into its drawable
if renderPassDescriptor != nil {
// ensure canvas is transparent
renderPassDescriptor?.colorAttachments[0].loadAction = .clear
renderPassDescriptor?.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 0)
// Create a new command buffer for each tessellation pass
let commandBuffer: MTLCommandBuffer? = commandQueue.makeCommandBuffer()
// 7a. Create a renderCommandEncoder four our renderPipeline
let renderCommandEncoder: MTLRenderCommandEncoder? = commandBuffer?.makeRenderCommandEncoder(descriptor: renderPassDescriptor!)
renderCommandEncoder?.label = "Render Command Encoder"
renderCommandEncoder?.setRenderPipelineState(renderPipeline!)
renderCommandEncoder?.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
renderCommandEncoder?.setFragmentTexture(texture.texture, index: 0)
renderCommandEncoder?.setFragmentSamplerState(samplerState, index: 0)
// most important below: we tell the GPU to draw a set of triangles, based on the vertex buffer. Each triangle consists of three vertices, starting at index 0 inside the vertex buffer, and there are vertexCount/3 triangles total
renderCommandEncoder?.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: vertexArrayBrush3DMesh.count)
///////////renderCommandEncoder?.popDebugGroup()
renderCommandEncoder?.endEncoding() // finalize renderEncoder set up
commandBuffer?.present(self.currentDrawable!) // needed to make sure the new texture is presented as soon as the drawing completes
// 7b. Render to pipeline
commandBuffer?.commit() // commit and send task to gpu
} // end of if renderPassDescriptor
} // end of if vertexArrayBrush3DMesh.count > 1 }// end of func metalRenderColoredMesh()
2017/01/17更新
在实现@warrenm 提供的建议后,我的笔触看起来更有希望了。
但是,结果出现了一些新的问题/问题。
我不确定 Metal 如何处理 > 1 的加法颜色值。它们是否会限制在 1?这是我在笔画的饱和部分看到的振铃的原因吗?
由于我实现的贝塞尔曲线采样的不规则性质,笔触的某些区域看起来有些不规则。为了让这种印记方法发挥作用,我必须想出一种方法将印记均匀地分布在整个笔画上。
最佳答案
您的混合因素需要一些改进。默认情况下,即使启用了混合,片段着色器的输出也会替换颜色缓冲区的当前内容(请注意,我在这里忽略了深度缓冲区,因为这可能无关紧要)。
您当前拥有的混合方程式是:
cdst′ = 1 * csrc + 0 * cdst
对于经典的 source-over 合成,你想要的更像是:
cdst′ = αsrc * csrc + (1 - αsrc) * c< sub>dst
renderPipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha
renderPipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
renderPipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha
renderPipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
对于您的特定用例,您可能想要使用加法混合,其中新的片段值只是添加到已经存在的片段中:
cdst′ = 1 * csrc + 1 * cdst
renderPipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .one
renderPipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .one
renderPipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .one
renderPipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .one
您是否真的需要添加混合取决于您所追求的确切效果。当值小于 1 时,它会产生一种“累积”的绘画效果,但一旦累积颜色值超过 1,它也会饱和,这可能并不令人愉快。另一方面,加法混合是可交换的,这意味着您不必关心绘制笔触的顺序。
(在前面的讨论中,我忽略了预乘 alpha,在绘制非不透明图像时绝对必须考虑到这一点。您可以阅读所有关于粗糙细节的信息 here。)
关于swift - 在 MTKView 上重复冲压纹理的透明度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48276449/
我有一个未定义数量的显示上下文,每个都将显示一个纹理。当我调用 glGenTextures 时,我会在所有显示上下文中返回相同的名称。这会起作用吗?即使它们具有相同的名称,它们仍会存储和显示不同的纹理
我在 SVG 中看到过:文本填充是图像而不是颜色;我一直想知道使用 CSS3 是否可以实现这样的事情。 我浏览了整个网络,到目前为止只找到了基本上将图像覆盖在文本上的解决方法(请参阅 this ,这对
我是 WebGL 的新手。 :)我知道顶点数据和纹理不应该经常更新,但是当它们确实发生变化时,首选哪个:- 通过调用 gl.deleteBuffer 销毁先前的缓冲区 (static_draw) 并创
我需要将 GL_RGBA32F 作为内部格式,但我在 OpenGL ES 实现中没有得到它。相反,我只得到 GL_FLOAT 作为纹理数据类型。 OES_texture_float 规范没有说明里面的
当我执行某些几何体的渲染时,我可以在控制台中看到此警告: THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter sho
我正在尝试使用阴影贴图实现阴影,因此我需要将场景渲染到单独的帧缓冲区(纹理)。我无法让它正常工作,因此在剥离我的代码库后,我留下了一组相对简单的指令,这些指令应该将场景渲染到纹理,然后简单地渲染纹理。
我在 XNA 中使用带有自定义着色器的标准 .fbx 导入器。当我使用 BasicEffect 时,.fbx 模型被 UV 正确包裹并且纹理正确。但是,当我使用我的自定义效果时,我必须将纹理作为参数加
如果我创建一个 .PNG 1024 x 1024 的纹理并在中间画一个 124 x 124 的圆,它周围是空的,它使用的 RAM 量是否与我画一个 124 x 的圆一样124 x 124 空间上的 1
我试图在 Android 中绘制一个地球仪,为此我使用了 OpenGL。然而,为了让它更容易理解,我将从制作一个简单的 3D 立方体开始。我使用 Blender 创建我的 3D 对象(立方体),并在我
文本本身的背景图像层是否有任何 JS/CSS 解决方案? 示例 最佳答案 检查这个http://lea.verou.me/2012/05/text-masking-the-standards-way/
非功能代码: if sprite.texture == "texture" { (code) } 当 Sprite 具有特定纹理时,我正在尝试访问 Sprite 的纹理以运行代码。目前纹理仅在我的
我正在尝试学习适用于 iOS 的 SceneKit 并超越基本形状。我对纹理的工作原理有点困惑。在示例项目中,平面是一个网格,并对其应用了平面 png 纹理。你如何“告诉”纹理如何包裹到物体上?在 3
基本上, 这有效: var expl1 = new THREE.ImageUtils.loadTexture( 'images/explodes/expl1.png' ); this.material
我正在尝试将各种场景渲染为一组纹理,每个场景都有自己的纹理到应该绘制的位置...... 问题: 创建 512 个 FBO,每个 FBO 绑定(bind)了 512 个纹理,这有多糟糕。只使用一个 FB
我正在使用文本 protobuf 文件进行系统配置。 我遇到的一个问题是序列化的 protobuf 格式不支持注释。 有没有办法解决? 我说的是文本序列化数据格式,而不是方案定义。 这个问题是有人在某
我想将我的 3D 纹理的初始化从 CPU 移到 GPU。作为测试,我编写了一个着色器将所有体素设置为一个常数值,但纹理根本没有修改。我如何使它工作? 计算着色器: #version 430 layou
我可以像这样用 JavFX 制作一个矩形: Rectangle node2 = RectangleBuilder.create() .x(-100) .
我在 iPhone 上遇到了 openGL 问题,我确信一定有一个简单的解决方案! 当我加载纹理并显示它时,我得到了很多我认为所谓的“色带”,其中颜色,特别是渐变上的颜色,似乎会自动“优化”。 只是为
假设我有一个域类 class Profile{ String name byte[] logo } 和一个 Controller : class ImageController {
我正在开发一款使用 SDL 的 2D 游戏。由于某些系统的 CPU 较弱而 GPU 较强,因此除了普通的 SDL/软件之外,我还有一个使用 OpenGL 的渲染器后端。 渲染器界面的简化版本如下所示:
我是一名优秀的程序员,十分优秀!