Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Xerophyte
Mar 17, 2008

This space intentionally left blank

Raenir Salazar posted:

Basically I just want to (in Unity) generate procedural Perlin/Simplex noise to a texture to use for real-time terrain generation (I tried using DOTS/Unity's Jobs system but 5k by 2k was an unacceptable 10 seconds).

To from there get it so that the displayed material on the plane doesn't squash/stretch when I change the scale of the plane to something that isn't square (1408, 1, 512) I did this:

code:
				o.uv = v.texcoord - float4(0.5, 0.5, 0, 0);
				o.uv.x = o.uv.x * _Scale.x;
				o.uv.y = o.uv.y * _Scale.y;
I was looking into "world space coordinates" because as far as I knew the points for the noise was determined by the object vertices, but with the above and subsequent changes the need for this change no longer is needed. Basically if I made a plane that's wider than a square I wanted the generated noise to "continue".

Where _Scale is something like 2.75, 1 in the case of 1408 by 512 to make sure the noise/material displayed to the plane isn't being stretched.

But now I want to be able to both zoom and pan (zoom is controlled by _Frequency), but I can't get that to work.

[...]

But no matter what I try I can't seem to solve the parallax without also breaking the zoom being centered.

I don't really need 3D or 4D noise because I'm likely just going to use a fall off map to insure the map is surrounded by water, so I don't really need it to be able to wrap around.

Right, OK. I'm kind of avoiding doing a deep dive into that particular simplex noise implementation, because it should be irrelevant to your problem and it seems very Unity-specific. I would again be deeply surprised if Unity does not have a better solution to your problem than "edit this shader" but I don't know Unity. Someone who does could probably give a better answer.

This said, the "parallax" you're talking about sounds like what happens when you scale or translate the different octaves in simplex noise independently. I think inoise is called for each octave in that implementation, so if you insert a fixed transform there then the transforms will only be correct for at most one octave of the noise and you get the effect that the different layers of the noise move independently.


For texture transforms, you're on the right track by the sounds of it. You can always do those without touching anything about the details of the texture generation or even knowing what type of texture you are using. You don't need or want to touch noise generation parameters like frequency or bandwidth to zoom or pan, any more than you'd need to edit an image-based texture in paint to zoom or pan.

If you have a texture coordinate p, and you want to translate it so the texture origin is centered on a point p0, you can do the transform p = p + p0.
If you want to zoom in by a factor of k, you can do the transform p = p * k.

Transforming the texture lookup coordinate prior to look-up works for any texture, procedural or image, of any dimensions. So just do that things wherever you're specifying the o.uv = v.texcoord - float4(0.5, 0.5, 0, 0); texture coordinate, I assume the vertex shader. You'd end up with something like:

code:
// Assuming we have the uniforms:
// uniform float2 _Scale;
// uniform float2 _Translate;
// uniform float2 _OutputSize;
// Your Uniforms May Vary

// Change the extents from [0,1] -> [-0.5,0.5] as before
o.uv = v.texcoord - float4(0.5, 0.5, 0, 0);

// Translate to center the texture on a new point
o.uv = o.uv + _Translate;

// Zoom in or out on the new center
o.uv = o.uv * _Scale;

// Correct for blitting to an image with a non-square aspect
float aspectRatio = _OutputSize.x / _OutputSize.y;
o.uv.x = o.uv.x * aspectRatio;
This will look different in your case, I don't know how Unity does texture transforms. As you noticed you can bake the aspect ratio into the _Scale parameter, for instance. In general a texture transform can be expressed with a homogeneous coordinates and a matrix like any other affine transform.


I suggested 3D noise because I figured you were texturing 3D data. I'm not sure what you mean by wrap-around, but it sounds unrelated to texture dimension. Since you're texturing a 2D image, use a 2D texture.

Adbot
ADBOT LOVES YOU

Xerophyte
Mar 17, 2008

This space intentionally left blank
You can apply an offset to gl_FragDepth based on gl_FrontFacing in the fragment shader. That might limit the early z tests, depending on how you're offsetting, and I don't think you can offset by the same amount as glPolygonOffset (which I've never used).

In general you will not be able to do this in the vertex shader by just looking at the post-transform normal. A given vertex can be a part of both a front-facing and a back-facing triangle, and a vertex normal can and almost certainly will be back-facing from some perspectives even if it is a part of a front-facing triangle. Offsetting the vertex by the normal still works if you know you have nice inputs with vertex data that lets you, of course, but not for arbitrary input.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply