Grain texture usage

Did someone figure out the algorithm behind cameraGrainTexture usage like SceneKit does it? I work with raw Metal and can't figure how to use it properly.

Replies

Without going into full detail, here is a general overview of *a* way that you can simulate camera grain on a scene using the cameraGrainTexture:


  • In your camera+scene compositing pass, you will want to set the cameraGrainTexture as a fragment texture. You will also want to set the cameraGrainIntensity as a uniform, and it is probably a good idea to have some way to psuedo-randomly animate the cameraGrainTexture each frame, 2 random values as frame-uniforms to act as texture offsets may suffice. Also make sure that the depth and color attachments of your scene rendering pass has been bound to this compositing pass.
  • Use the image plane's texture coordinates to sample your texture, you should scale these texture coordinates by some value (which you should change depending on the device's screen size). Remember to offset the texture coordinates using your texture offset values. Your sampler's address mode should be repeat.
  • Remember, the cameraGrainTexture is a texture3D, so when you sample it you should use the cameraGrainIntensity as the z-value, which will ensure that you get an appropriate camera grain texture.
  • Sample the scene color texture.
  • Sample the depth texture and conduct a depth test, (assuming you are using the default clear depth of 1), if depth < 1, you should apply your camera grain. Use mix to apply the camera grain to your scene color.
  • Composite the scene color (which now has a simulated camera grain) with your camera color texture.

Thank you for the response. Those are the things, that I figured already and I expected some details, or at least some sort of hint towards a paper or an article. The problem is that I can't find a proper blending algorithm, using that input data and sampling strategy, that would feel natural.