5

In the node editor, I am looking for a way to get a random number (in [0,1)) per ray. The object info node's random output just gives a random number per object, the number never changes. I am looking for a way to get a new number every time the shader is sampled.

I am aware of this question, however that is not quite what I am looking for.

PGmath
  • 25,106
  • 17
  • 103
  • 199
  • Just curious.. What do you want this for? – gandalf3 Jul 11 '15 at 00:50
  • try combining some ray outputs with some math to produce some thing that seems random. outputs like ray length,view vector and camera_uv coordinates... – Chebhou Jul 11 '15 at 01:15
  • @Chebhou The problem is that I need it to give me a different value every time I sample the shader, even in the same spot. To get a static random value I could use a noise texture with a huge scale. – PGmath Jul 11 '15 at 01:18
  • 1
    @PGmath Oh I see , i don't think there's a variable related to the sample number AFAIK , check if OSL can provide such a thing – Chebhou Jul 11 '15 at 01:22
  • @gandalf3 Well, the thing that brought it to mind at the moment was trying to more properly roughen a fake glossy reflection. (Blurring does work mostly but I was looking for another way.) But I have wished there was such a feature in the past though for other reasons, mostly having to do with various tricks to speed up render times/reduce noise. – PGmath Jul 11 '15 at 20:39

2 Answers2

4

There's enough randomness in the Position from the Geometry node. It will almost never be exactly the same for two samples, but you have to apply a hash function to convert it to a random number in the [0,1) range.

The simplest solution would be to use a Voronoi or Noise Texture node with a very large Scale.

Another solution is to make your own hash function using math nodes, replicating for example this GLSL code.

brecht
  • 7,471
  • 30
  • 48
0

I think you have to combine as many inputs as possible for Noise texture with big scale: window coordinates, per-object random, object local and global coordinates, normal vector, frame number, etc, to get higher enthropy, and maybe this way you will get something close to what you want.

Daniil Romanov
  • 715
  • 3
  • 8
  • I don't think this will work, I need a new random number *per sample*, i.e. every time the surface is sampled a new number is generated. – PGmath Jul 11 '15 at 20:37
  • Ok, I understand. I mean, you have to gather many possible input factors where at least some can change between frames, inside pixel, etc, to get high enough enthropy for one noise texture with pretty big scale, so every ray will meet new value. Also I forget about one more thing: make seed driver-controlled and put random function there to get random rays for every frame and even render launch link. BTW: I tried to use this driver in shader - didn't work. – Daniil Romanov Jul 11 '15 at 20:51
  • I'm not quite sure what you mean, but I think I mostly get it. But I'm not really interested in frames/animation, what I am looking for is something that will give me a new number every time a surface is sampled, within a frame. – PGmath Jul 12 '15 at 13:36
  • BTW, A sample is where cycles shoots a ray from the camera at a pixel and it bounces through the scene to determine what color that pixel should be. – PGmath Jul 12 '15 at 13:38
  • Of course. When you put on surface very fine-grain noise, then every ray will get own random value, combine it with that random-seed driver and all rays became more random every time you render even same single frame. – Daniil Romanov Jul 12 '15 at 14:17
  • But, according to my understanding of what you are saying, within a single render of a single frame the same spot on a mesh will always have the same number. The point of my question is to get a new number each time the same spot on the mesh is sampled within a single render. – PGmath Jul 12 '15 at 16:38
  • BTW, as of 2.75 you don't need to randomize the seed with a driver, there is a button to pick a new seed per frame. But this doesn't apply to my question as I am not rendering an animation – PGmath Jul 12 '15 at 16:42
  • It's not about animation only, with this driver you will get new random number absolutely for every render launch even for same frame number. But unfortunately this driver don't work with material nodes. – Daniil Romanov Jul 12 '15 at 18:04