Sphere Mapping Textures

This forum is currently in read-only mode.
0 favourites
From the Asset Store
A template which allows you to customise the controls of your game.
  • Hi folks!

    Currently I'm coding a GUI-frontend for the Noise Plugin, which i really like to share with you once it's finished. One cool feature I had in mind was to prepare (polar distort) a texture to be correctly mapped on a 3D sphere. My ingredients are a Image Manipulator to read the original pixels from the generated noise and an Array in 2:1 dimension to process the fancy math. Sadly, I can't figure out how to port the following code into construct. Maybe you can help me. <img src="smileys/smiley19.gif" border="0" align="middle">

    This is what I've done so far:

    <img src="http://dl.dropbox.com/u/6200498/loop.png" border="0">

    This is the code I'm trying to translate:

    for (j=0;j<image.height;j++) {
         theta = PI * (j - (image.height-1)/2.0) / (double)(image.height-1);
         for (i=0;i<image.width;i++) {
              phi  = TWOPI * (i - image.width/2.0) / (double)image.width;
              phi2 = phi * cos(theta);
              i2  = phi2 * image.width / TWOPI + image.width/2;
              if (i2 < 0 || i2 > image.width-1) {
                  ?newpixel = red;                         /* Should not happen */
              } else {
                  ?newpixel = imagein[j*image.width+i2];
              }
              imageout[j*image.width+i] = image.newpixel;
         }
    }

    I got it from here:

    http://local.wasp.uwa.edu.au/~pbourke/texture_colour/texturemap

    (Texture map correction for spherical mapping)

    Edit:

    To show you what I'm aiming for:

    <img src="http://dl.dropbox.com/u/6200498/sphere.jpg" border="0">

    The texture on the sphere looks distorted on its poles.

    Maybe I don't necessarily have to go the Get/Set-pixel way and there are other solutions like mesh-distortion a sprite which i copy into a canvas or something like that. I just want to produce neat looking planets <img src="smileys/smiley2.gif" border="0" align="middle">

  • I thought this interesting enough to try out.

    http://dl.dropbox.com/u/5426011/examples10/sphereTex.cap

    The results look pretty bad...

    Either his method is flawed or another method is needed for texture mapping a sphere.

  • Nice job there Rojo.

    I think, in theory, you could cut out both the sprite, and image manipulator. Canvas now has the ability to do distort maps, and Noise2 can generate without having to make an image.

    Then again you could also use color filters on the distort map.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks alot for your replies!

    I agree, Rojo, the formula i googled all day long does not look promising enough. Maybe i was confused by all the mapping techniques that are availabe. Another formula i stumbled upon was the Mercator Projection

    Perhaps this is the place to be.. dunno i'm confused..

    In general you're right newt, but i wanted to physically distort the sprite (texture) because it should be mapped onto a real 3d sphere (via textureSetter). And the whole point of this tool i'm developing is to output an INI which is then loaded into my space-game which should work procedurally. The containg planets/suns consist of 3d-meshes, because i find them easier to handle (e.g. roll, pitch, yaw right away).

    So any ideas on how to push those damn pixels in an appropriate way are more than welcome.

    EDIT:

    I like to illustrate that thing a bit further by pointing to the follow article which shows the progress in photoshop (sigh..)

    spherical mapping

    Basically it's just:

    1) Rectangular to Polar

    2) Polar to Rectangular

    3) Flip Image / Rotate 180 degr.

    4) Rectangular to Polar

    5) Polar to Rectangular

  • The library that Noise uses already has support for polar noise generation. I just didn't add it into the plugin because I never thought it'd be utilized by a Construct user, as it's more of a 3D thing. I can look into adding it to the plugin at some point in the future, but I've got kind of a full plate right now.

    libnoise is actually *much* more powerful than Noise, with all kinds of options and post processing stuff. I only implemented things I thought might be relevant to a Construct user.

  • Hey Arsonide <img src="smileys/smiley1.gif" border="0" align="middle" />

    I think that Noise V2 does it's job pretty well, thx for that! Of course it might be easier to further post process an image than to recode that plugin once more for C1. I can imagine this sort of update for a potential C2 plugin if you're into that one day.

    Edit:

    Then again you could also use color filters on the distort map.

    I didn't get that. Do You think that this is a benefit over meshes?

    Edit2:

    This whole thing is driving me mad. <img src="smileys/smiley11.gif" border="0" align="middle" /> It seems that there isn't a 'perfect' mapping solution, because the poles consist of 1 point each; which is impossible to map.

    Buuut i think your canvas/sprite distortion solution is more elegant. With a little effort i might change your example to have pitch/yaw/roll.

    One more question though:

    Is it possible to increase the distortion mesh quality? I tried to double the X/Y values but it gave me weird effects like little white dots and a whole different "tiling" of the texture. Multisampling is a showstopper also.

    Edit3:

    It's me again... Sadly, distorting a sprite/canvas does not solve the problem of mangled poles. Seems like i have to dive in a bit further and search for some other solutions.

  • OK, i almost got it.. long story short:

    "Lambert cylindrical equal-area projection"

    The technique seems to do the job, hopefully.

    The X-pixels are untouched and Y-pixels get compressed towards the poles via Sin... But as you can see there is still something wrong with the formula. My guess is that there is some PI missing somewhere:

    <img src="http://dl.dropbox.com/u/6200498/cylindrical.png" border="0">

    Please have a look at the cap: sphereTex2.cap

  • "I didn't get that. Do You think that this is a benefit over meshes?"

    Meshes can have color filters applied at the row/column.

    It would be faster than perpixel, but its per vertex instead.

    It really wouldn't work here, unless the texture is really small.

    It would be useful if say you wanted to add lighting.

    As to increasing the quality by basically subdividing it, yes that will add some, but keep in mind there is the hardware limitation, around 64x64.

  • i won't forget your solution and ideas newt, it might come in handy in the future.

    with lucids help in the chat it turned out out that some sort of cosp plays a role in this matter:

    <img src="http://dl.dropbox.com/u/6200498/cosp.jpg" border="0" />

    which is this:

    cosp(0,IM.GetImageHeight,LoopIndex("j")/IM.GetImageHeight)

    but it exaggerates the distortion i tried to work against.

    i can imagine that a sine-interpolation would be needed here.

    something like this:

    Lerp(0,IM.GetImageHeight, Sin(LoopIndex("j")/IM.GetImageHeight))

    But it's just a pixel mess.

  • For sine interpolation:

    Lerp(0,IM.GetImageHeight, (1+Sin(LoopIndex("j")*180/IM.GetImageHeight))/2)

    But that still won't eliminate the distortion at the poles.

    Paul Brouke's method eliminates the pole distortion but causes seams because parts of the image are discarded.

    <img src="http://local.wasp.uwa.edu.au/~pbourke/texture_colour/texturemap/sketch.gif" border="0"> The area in that shape is the only part of the image that is preserved.

    The libnoise website it has some sphere textures that are distorted in a way similar to pbrouke's method but I think that it was generated special for spheres.

    http://libnoise.sourceforge.net/examples/textures/index.html

    Also the Photoshop method just blurs the texture near the poles, it would still have the pole distortion.

  • For sine interpolation:

    Lerp(0,IM.GetImageHeight, (1+Sin(LoopIndex("j")*180/IM.GetImageHeight))/2)

    But that still won't eliminate the distortion at the poles.

    Paul Brouke's method eliminates the pole distortion but causes seams because parts of the image are discarded.

    <img src="http://local.wasp.uwa.edu.au/~pbourke/texture_colour/texturemap/sketch.gif" border="0"> The area in that shape is the only part of the image that is preserved.

    The libnoise website it has some sphere textures that are distorted in a way similar to pbrouke's method but I think that it was generated special for spheres.

    http://libnoise.sourceforge.net/examples/textures/index.html

    Also the Photoshop method just blurs the texture near the poles, it would still have the pole distortion.

    the new formular is not correct either:

    <img src="http://dl.dropbox.com/u/6200498/sinus2.jpg" border="0">

    the examples given from libnoise represent what i'm looking for, because the more we go towards the poles the less detailed the texture has to be. so wrapping a cylinder around it does it imho.

    you're right, paul bourkes approach is be correct, but it is lossy.

    and yeah, screw photoshop. construct is the place to be ;)

  • Here is the projection without distortion at the poles:

    sphere-mapping-01.cap

    However bear in mind that is impossible to distort this texture to fit perfectly a sphere without seems or distortion. If your goal is to achieve a sphere with an uniform noise you will need to generate the noise with the proper detail density across the poles in a way that it wraps perfectly.

  • Not sure if tillable will work with non power of two in Noise2.

    Edit:

    However, you can I think you can stretch the output, and noone would probably be able to tell the difference.

  • Here is the projection without distortion at the poles:

    sphere-mapping-01.cap

    However bear in mind that is impossible to distort this texture to fit perfectly a sphere without seems or distortion. If your goal is to achieve a sphere with an uniform noise you will need to generate the noise with the proper detail density across the poles in a way that it wraps perfectly.

    did you just simplified the formular from page 1? impressive to see that it's possible with less variables!

    although it's pretty cool, i think that the far more simple approach of "lambert cylindrical equal-area projection" (also on page 1) suitable for this case.

    basically it must be something in the direction Rojo posted.

    Not sure if tillable will work with non power of two in Noise2.

    Edit:

    However, you can I think you can stretch the output, and noone would probably be able to tell the difference.

    lol, you're right. the noise itself tiles pretty well, but it won't fit spheres if not power of two ;)

  • Sorry, but I'm not totally following your thinking. If you apply the Lambert cylindrical equal-area projection in that rectangular texture you will only get some distortion near the equator line.

    Like I said earlier, if you want to be able to have a noise texture that properly wraps a sphere without distortion you will need to create an algorithm that generates the noise in that "rhombic" space that Rojo posted, and transform it in a rectangle with the previous example I made (that I think results in an approximation of the Lambert projection).

    Take a look at this

    Also take a look at Sinusoidal Projection.

    If this is you goal then know that is impossible to transform that rectangular texture to fit a sphere without seems or distortions. That texture has more information near the poles than what can fit in a sphere surface. If this is not your goal then I misunderstood the purpose of this topic.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)