Home Unreal Engine

HLSL Random Int

admin
Offline / Send Message
System admin
So I know there are a lot of good programmer hlsl guys here because Ive read a lot of the threads about custom HLSL stuff for Unreal.

I was hoping to make a custom node for a random Int. Some of the stuff Ive been reading suggests using a random tex coordinate on a bw tex2d which seems like it could work but I have no idea how to implement this in Unreal.

Any help would be fantastic! :)

Replies

  • sprunghunt
    Options
    Offline / Send Message
    sprunghunt polycounter
    Google "perlin noise" for some hints on how to generate random seeming numbers.
  • Xoliul
    Options
    Offline / Send Message
    Xoliul polycounter lvl 14
    HLSL can't do random numbers by itself. You either need to write your own seeded semi-random function (not hard), and then feed the seed with a number. The good thing is that is deterministic (the same every time for the same seed).
    The perlin noise thing is much more complex, it gets it's random numbers from a noise texture, BUT this texture depends on uncompressed texture data, something UDK doesn't like.

    Either way, you will need the customcode node, and UDK really isn't the best environment for this. I'd suggest you just try to work with pre-generated cloud textures that are offset with world coordinates for example, much less hassle and can give good results in most cases.
  • Ace-Angel
    Options
    Offline / Send Message
    Ace-Angel polycounter lvl 12
    From what I recall, and correct me if I'm wrong, for certain specific Int types of variables, you need to have SM4.0 workspace, something which UDK stopped providing ages ago (hence, cutting people away from some awesome code), so you need a DX11 (again, I could be royally wrong).

    Secondly, a true random generator burns too many instructions to be worth, I think it was Vailias that said he ran out of instruction space for his noise function, and had to resort to a texture sampler to do most of he grease work, same info I keep on finding with google. So even if you were successful, you wouldn't be able to do much with it in the fist place, and I don't think UDK allows Material-Per-Material sampling to save instruction space.

    The best solution I can think of is what Xoliul said, with a per channel noise. You could have one texture, with a noise texture in each one, and afterwards, you could then create a set of 'random' numbers (manual numbers you put up), and use those with a time offset to cycle through them while doing the same for the noise channels, that way you have 'somewhat' of a random thing going on where two sets of times are molding for one texture.

    I tried doing this not too long ago, but I was going for something more spiffy, and was trying to do more than simple Sin and Tan function, but didn't work. Sure someone would know a better way of doing this.

    Or, you simply use World Space and Texture Coordinaes, and tabulate those.
  • Vailias
    Options
    Offline / Send Message
    Vailias polycounter lvl 18
    Yeah.. it was me. Purely programatic perlin noise ran up over 512 instructions due the the pseudo-random permutation, among other things.
    It became workable when I dumped the 512 number random seed array to a 1d texture lookup.
    I did find another way to get pseudo-random noisy behavior via a texturless GLSL Perlin Simplex Noise implementation someone did and posted on the web, but still super heavy. Well over 120 instructions just for a single pass noise function.

    Frankly even Ken Perlin has resorted to texture lookups for real-time implementations of his algorithm.. he's not the best at explaining things though, so his examples are good, but hard to follow. :)


    Xoliul and Sprunghunt have some good advice though, as the permute-permute-permute approach in Perlin's algorithm is useful for pseudo-random generation.
    Without knowing what effect you're after its hard to give correct and concise direction though.
  • Xoliul
    Options
    Offline / Send Message
    Xoliul polycounter lvl 14
    The only advantage of actual 3D perlin noise from a lookup texture, is as far as I know, that you get seamless texturing from it (ignores UV seams).
    If that isn't (much of) a concern, just go for pregenerated noise textures in UV space. Multiple UV channels can help you almost completely circumvent the UV seam issue.
  • sprunghunt
    Options
    Offline / Send Message
    sprunghunt polycounter
    Xoliul wrote: »
    HLSL can't do random numbers by itself. You either need to write your own seeded semi-random function (not hard), and then feed the seed with a number. The good thing is that is deterministic (the same every time for the same seed).
    The perlin noise thing is much more complex, it gets it's random numbers from a noise texture, BUT this texture depends on uncompressed texture data, something UDK doesn't like.

    Either way, you will need the customcode node, and UDK really isn't the best environment for this. I'd suggest you just try to work with pre-generated cloud textures that are offset with world coordinates for example, much less hassle and can give good results in most cases.

    There is a "noise()" function in HLSL specifically for generating perlin noise. But it doesn't work in UDK because of the context it's being used in. It's only for generating proceedural textures offline. It might work with the custom texture node?

    However here's a fast implementation of perlin noise by ken perlin himself.

    http://mrl.nyu.edu/~perlin/noise/

    You'll notice he's not actually randomly generating numbers. Rather he's referencing an array of previously generated numbers. Which is what I was suggesting earlier. I'm not sure if it's better than using a texture. I guess there's going to be faster data access?
  • Xoliul
    Options
    Offline / Send Message
    Xoliul polycounter lvl 14
    That intrinsic one is pretty much useless, since it only does offline texture filling as you say, which requires application framework support. On top of that, it would only be in UV space, so none of the 3D-seamless benefits. Doing Filter > Render > Clouds in PS is the exact same thing.
Sign In or Register to comment.