Page 3 of 8

Posted: Tue Jun 12, 2007 8:06 pm
by Oogst
I am working on a number of variations now, including adding some form of furniture, varying the sizes of the rooms, varying the lighting per room and varying the textures per room. Some of these require a form of randomness, where I can map en 3D position to a random number. How can I do this in a shader? I saw an implementation using perlin noise that required 70 instructions (!) for the noise alone, which seems way too much. A 3D texture does not seem very proper, because it would require quite a large size to work and sizes of 3D textures explode quickly. What approach would you folks advice?

The very specific requirements I am looking for right now is to generate either a 0 or a 1 from a 3D integer coordinate to decide whether a room is lit or not. I tried using sin in combination with step for this, but that gave repetitions all the time.

Posted: Tue Jun 12, 2007 8:53 pm
by Kencho
Hmmm... a cheap method to create some randomness? I think that multiplying the coords by a very large prime, adding another prime, and then using modulus (by another large prime) might work fine, or at least not create such obvious repetitions.

Posted: Tue Jun 12, 2007 9:04 pm
by Oogst
Maybe a stupid question, but how can I do modulo in a shader model 2.x shader?

Posted: Tue Jun 12, 2007 9:17 pm
by Kencho
I don't really know :? :oops:

Posted: Tue Jun 12, 2007 9:30 pm
by jjp
As far as I remember one of the articles in GPU Gems 2 shows how to do 3d-noise with about 50 instructions..

Posted: Tue Jun 12, 2007 9:56 pm
by inneractive
I have to admit that your technique looks very strange and unrealistic for now. It's probably been mentioned in the thread that glass has reflective properties. Perhaps you will need to do another pass that blends the interior with exterior environment reflection to get a more realistic look.

Posted: Wed Jun 13, 2007 12:59 am
by Prezadent
A link to the newest demo really needs to be kept in the first post.

Posted: Wed Jun 13, 2007 3:42 am
by deficite
This is wonderful! We sure have some bright minds here at OGRE :D

Posted: Wed Jun 13, 2007 4:33 am
by dartsman
wow, this could be used for some awesome city scenes for sure :)

awesome work!

can't wait to try the demo, and for some more screenshots ;)

Posted: Wed Jun 13, 2007 7:48 am
by Shadow007
What you need is a hash function to get int f(x,y,z) = hash(x + hash(y + hash(z)));

You would get 32 bits to choose from :)


Now as a hash, you can use some kind of pseudoRNG (similar to what Kencho suggests).

You can also have a look at the following link :Long Period Hash Functions for Procedural Texturing. http://www.cs.kuleuven.ac.be/~graphics/ ... lTexturing

Oogst wrote:Maybe a stupid question, but how can I do modulo in a shader model 2.x shader?
Wouldn't that be a % b = a - (floor(a/b) * b) ?
There may be easier possibilities though.

Posted: Wed Jun 13, 2007 8:00 am
by Oogst
inneractive wrote:I have to admit that your technique looks very strange and unrealistic for now. It's probably been mentioned in the thread that glass has reflective properties. Perhaps you will need to do another pass that blends the interior with exterior environment reflection to get a more realistic look.
Some of the images already do that! It is just that the reflection is set to pretty weak to show the effect of Interior Mapping more clearly for demoing purposes. (is that de-moing or demo-ing?) I think I should also make the balance between reflection and interior depend on the angle with the glass, to make a sort of fresnel-effect.
Shadow007 wrote:What you need is a hash function to get int f(x,y,z) = hash(x + hash(y + hash(z)));

You would get 32 bits to choose from :)

Now as a hash, you can use some kind of pseudoRNG (similar to what Kencho suggests).

You can also have a look at the following link :Long Period Hash Functions for Procedural Texturing. http://www.cs.kuleuven.ac.be/~graphics/ ... lTexturing
That sounds interesting, but also very complex. Also, shader model 2 does not let me look at individual bits. I have not had a detailed look at the Kencho-paper now, though, so I will look at that as soon as I have the time.
Shadow007 wrote:Wouldn't that be a % b = a - (floor(a/b) * b) ?
There may be easier possibilities though.
Doh! I should have been able to think of that myself! :o

Posted: Wed Jun 13, 2007 9:36 am
by inneractive
Oogst wrote: Some of the images already do that! It is just that the reflection is set to pretty weak to show the effect of Interior Mapping more clearly for demoing purposes. (is that de-moing or demo-ing?) I think I should also make the balance between reflection and interior depend on the angle with the glass, to make a sort of fresnel-effect.
Oh okay, sorry I missed that. I think this will be great for city scenes. A fresnel effect would be really nice.

Posted: Wed Jun 13, 2007 9:51 am
by Shadow007
Found an other source for what you want :
http://www.csee.umbc.edu/~olano/s2005c37/ch04.pdf

I've got some source code :

Code: Select all

// vertex to fragment communication for noise shaders
varying vec3 Nin;
// 2D noise texture
uniform sampler2D ntex;
modulus for random hash
const float modulus = 61;
void
main()
{
// integer and fractional components of input
float fracArg = fract(modulus*Nin.z);
float intArg = floor(modulus*Nin.z);
// hash z & z+1 to get offsets for noise slices
vec2 hash = mod(intArg,modulus);
hash.y = hash.y+1;
hash = mod(hash*hash,modulus);
hash = hash/modulus;
// look up noise and blend slices
vec2 g0, g1;
g0 = texture2D(ntex, vec2(Nin.x,Nin.y+hash.x)).ra*2-1;
g1 = texture2D(ntex, vec2(Nin.x,Nin.y+hash.y)).ra*2-1;
float noise = mix( g0.x+g0.y*fracArg,
g1.x+g1.y*(fracArg-1),
smoothstep(0,1,fracArg));
// combine with lighting
gl_FragColor = (noise*.5+.5)*gl_Color;
}
you don't need to do the "noise steps" ... just check the result with < or > ...

Posted: Wed Jun 13, 2007 4:01 pm
by Shadow007
Of course, the easiest solution would be to use a "Noise" 2D Texture.To get a 3D result, you can do : ((h2d(x,y) + z)% 61)^2

Posted: Sun Jun 24, 2007 7:30 pm
by Oogst
(I am sorry for not reacting any earlier, but I had some school work to do and had no time to try these things until now.)
Shadow007 wrote:Of course, the easiest solution would be to use a "Noise" 2D Texture.To get a 3D result, you can do : ((h2d(x,y) + z)% 61)^2
Hmm, that one looks really simple, but I do not get how it can work. What is that "h2d"-thing? Do you mean a texture read there? In that case the formula seems wrong, because the texture will result in a float between 0 and 1, which will be almost fully cancelled out by the much larger value in z.

As an alternative, I tried something really stupid that kind of works:

Code: Select all

float calculateBinaryNoise(float3 position, sampler2D noiseTexture, float threshold)
{
	float3 noises = float3(tex2D(noiseTexture, float2(position.x, position.y) / 32).r,
									tex2D(noiseTexture, float2(position.z, position.x) / 32).r,
									tex2D(noiseTexture, float2(position.y, position.z) / 32).r);
	float result = noises.x + noises.y + noises.z;
	result /= 3;
	result = step(result, threshold);
	return result;
}
It does not seem to be perfectly noisy, though, and it requires three texture reads, which seems like quite a lot. The result of switching of lights is kind of fun, however:

Image

I have made a button to increase the threshold, thus slowly turning on all the lights in the building. That looks funky. Can't wait to do that in an entire city and make a timelapse of the sun lowering and the city turning on.

Posted: Mon Jun 25, 2007 11:09 am
by Shadow007
Oogst wrote:
Shadow007 wrote:To get a 3D result, you can do : ((h2d(x,y) + z)% 61)^2
Hmm, that one looks really simple, but I do not get how it can work. What is that "h2d"-thing? Do you mean a texture read there? In that case the formula seems wrong, because the texture will result in a float between 0 and 1, which will be almost fully cancelled out by the much larger value in z.
Of course you're right. you can then scale that one to :

Code: Select all

(floor((h2d(x,y) * 61)+ floor(z))% 61)^2
That result then needs to be re-moded to something like 59 or 67.

Posted: Tue Jun 26, 2007 10:43 am
by Oogst
Would that formula not give a very regular increase over z?

Posted: Tue Jun 26, 2007 12:55 pm
by Shadow007
Yes, it would ... I once again forgot to apply an operator :
apply a %67 after the square operation...

Posted: Tue Jun 26, 2007 1:06 pm
by Oogst

Code: Select all

((floor((h2d(x,y) * 61)+ floor(z))% 61)^2) % 61
At least it looks pretty complicated now... :shock:

I cannot try it here at school, but I will try this new one soon. :)

Posted: Tue Jun 26, 2007 2:02 pm
by Shadow007
Perhaps you can try with not "flooring" the intermediary results.
It may be complicated, but it's only 1 texture read ...

And I guess you can save a few instructions at the end by not remultiplying the "rest" by 61 at the last mod (you need a value between 0-1 right ?)

BTW I find the "On/Off" interior mapping quite convincing :)
Did you get any results with the "furniture ?"

Posted: Wed Jun 27, 2007 7:59 am
by Oogst
I don't have time to work on it this week, but I already have the way the furniture and animated characters are going to work on paper. I just need to code it, but I am again too busy with school to work on it this week. I will have a demo of these things in a couple of weeks, though. And I am writing a paper on the whole topic as well.

Posted: Wed Jun 27, 2007 8:13 am
by Shadow007
Take your time :))

Just a note : going through the paper that suggests the (h(x,y) + z)mod M
http://www.csee.umbc.edu/~olano/papers/mNoise.pdf,
I found out that I forgot one "cool" thing about it : Using 4 channels of colors, you can get 4 independent values out of 1 sequence of operations.

So, you can have lit/unlit, furniture, people, doors ... with 1 texture lookup and the following mod operations ...

Posted: Wed Jun 27, 2007 8:14 am
by JRowe47
How expensive would render to texture be, to show actors moving around inside and so on? That would be awesome, if feasible... silhouettes on shades, an ogre looking out the window from his or her desk... objects in a room?

Really neat, can't wait to read your paper :)

I worked on something similar for a game company.

Posted: Wed Jun 27, 2007 8:24 am
by Lee04
Super great work!

I worked on something similar for a game company.
It also had an "old glass" effect on the windows.

They did a game set in New York, still the TD killed this and said we already have so many materials for the engine (they used Unreal 3 engine) why are you doing this? He asked...

Instead they modeled all windows...

And then they wondered why it was so slow....rendering.

Posted: Wed Jun 27, 2007 8:50 am
by Shadow007
JRowe47 wrote:How expensive would render to texture be, to show actors moving around inside and so on? That would be awesome, if feasible... silhouettes on shades, an ogre looking out the window from his or her desk... objects in a room?
Moving around would be quite difficult I think :)


JRowe47 wrote:Really neat, can't wait to read your paper :)
Seconded :)

BTW it seems I forgot to tell you how great I find the "preview" you did a few days ago (cf the image upper in this page).