Search Unity

'Skybox' for geometry (AKA 3D skybox)

Discussion in 'Wish List' started by noradninja, Dec 28, 2007.

  1. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    What I would LOVE (and may already be doable, I don't know), would be 'skyboxed' geometry. Source does this...basically, you can create geometry that is displayed in from of the skybox, but no matter where you are in the level, you never actually get any closer to is from a visual perspective (just like a skybox, it always appears to be an infinite distance from the player). You could use it to do things like make building geometry so that you appear to be in a much larger location that where you can actually go within a level.

    See http://developer.valvesoftware.com/wiki/3D_Skybox for an explanation, and below for some screenshots:






    The circled areas are skyboxed geometry.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Just use layers and two cameras...the first, "normal", camera doesn't render the 3D skybox, and moves and turns as normal. The second camera renders only the 3D skybox but not the rest of the geometry, and turns with the player's movements but doesn't move (or moves at a slower rate).

    --Eric
     
  3. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    Would you mind throwing a *simple* sample together? Something I can open in Unity and dissect?

    This sounds waaaay easier than what you have to do in Source...if so, then, again, freaking go Unity!
     
  4. defmech

    defmech

    Joined:
    Feb 24, 2007
    Posts:
    506
    This is something I wondered about when I first started using Unity. It's such a trivial effect to achieve in Source and UE(which are much less friendly to use in general), but oddly absent in Unity. I figured it out how to do it, basically was Eric described, but I lack the scripting skill and time to get it done. I'll be so happy if a simple demo of this makes its way to the wiki. It will be just another bulletpoint to the list of what makes Unity so awesome :D
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    As it happens, I did a little test scene since this was interesting me, so I might as well upload it. ;) The caveat being that I haven't actually seen Source in action, so maybe this isn't completely right. The areas of interest in this case are the culling mask settings on the two cameras, the clear flag settings on both cameras, the layers that the objects are assigned to (basically, 3DSkybox for anything that should be rendered by the skybox camera), and the scripts on the skybox camera. You can set the Ratio variable to 0 on the CopyMove script (or just get rid of the script) if you want the skybox camera to stay completely still (aside from rotation) like a regular skybox, or have it be a given fraction of the real camera's movement to give a little more sense of depth. You can't really use the same render settings for both cameras if you want fog, since the scale is different, so the SetFog script sets a different fog distance for the skybox camera.

    --Eric
     

    Attached Files:

  6. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    If you happen to have Pro, Camera.RenderToCubemap() is very useful.

    You could use it to take a picture of a complex scene when the level starts, then delete the original background objects and just use the cubemap as your skybox. This is obviously a lot cheaper than rendering the entire complex scene!
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Actually you can do that with Indie. You only need Pro if you're using rendertextures to do real-time updating cubemaps.

    However, the problem with doing this here is that you basically end up with just a normal skybox, and lose most of the advantages of using a 3D skybox.

    --Eric
     
  8. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Really? I assumed that this was the same thing since a texture is still being rendered.... Oh well. :)

    True. Still, if the background is complex and most of it never changes, it's certainly more efficient to render it once into a skybox, then use a separate camera to render any elements which do change over the top of it, as you suggested.
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Well, the terrain engine works on Indie too, and that uses rendertextures for the billboard trees. :) So between that and cubemaps, Indie isn't completely left out of the texture renderin' goodness.

    See the link in the first message for some reasons for a 3D skybox. Of course you're right that a texture is going to be faster, but that's not really the aim here. It's basically another way of doing LOD for geometry, so it's faster than fully-detailed models, but slower than a 2D skybox.

    --Eric
     
  10. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    Eric- Thanks a lot for the sample file, this will help me a lot. The unfortunate thing is, I am no coder, so while I understand very well the concepts behind how this sort of thing should work, I am a content guy, and writing the code to implement this sort of thing is kind of beyond me. As it was, I had a hard time wrapping my head around MEL scripting in Uni; I suppose I am just too right-brained :wink:

    As for the cubemap rendering, this is of course useful, but the idea here is to have the skybox be 3D for depth purposes; real time updating skyboxes would of course solve this, but being that I am having to fund the licensing myself, Indie is the only version of Unity that I can afford (for now). I imagine when I can come up with the scratch to upgrade, I will test this to see if it is cheaper resource wise once I can go Pro.
     
  11. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    It would appear that the file you uploaded is missing some things, namely the meshes for the ground and houses, and I think the third person camera controller script?
     

    Attached Files:

  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Just tested from the .zip file and it's working here. In your screenshot it's missing the Mouselook script from the Standard Assets folder, but that's definitely included, so I'm not sure what's going on. Apparently you don't have that Standard Assets folder at all (the texture on the wall is wrong too plus there's no skybox). I assume the Library folder is also missing which is why the models aren't working. How are you opening the project?

    Actually no, that would be quite slow. You'd still have to render all the geometry, plus render it to some huge textures. You'd only want to use real-time updating cubemaps for quite small textures, like the reflection on an object. Neil was talking about rendering just once to a static cubemap, which is still a cool thing to do (and would be fast), but it's not really a substitute for a 3D skybox.

    --Eric
     
  13. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    Ok, I got it figured out, I was merely opening the Unity file you sent, as opposed to creating a new project and bringing in the Standard assets.

    Thanks for the clarification on the cubemaps, it seems I misunderstood what Neil was saying.
     
  14. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    I don't know what I am doing wrong here...no matter what, the House and Ground meshes don't load. The Blender files are listed in the Project view, but there are no meshes, and in the Game view, there is no ground plane and no material on the walls, not to mention that the House mesh in the House prefab says missing mesh, and no matter how I open this up, the skybox camera says the three scripts are missing, they don't attach to the camera and I have to manually add them....I hate to ask, but can you walk me, step by step, thru the process for opening this? It seems so silly, but I have to be doing something wrong here and to be honest I think I am making this harder than it should be. Hate to bug you, but I am frustrating myself.

    EDIT: I have it seeming to load properly (all the assets load now) by opening the 3DSkybox test folder via Open Project (which I think is where I was doing things wrong...) but now...LOL... the only thing in the Scene view is a camera....am I just failing hard at life here or what?
     

    Attached Files:

  15. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's OK, I found the whole project/scene thing a bit confusing at first too. Fortunately the next part is easy...open the "Test" scene, either with File -> Open... or (faster), just double-click on it in the Project view.

    Basically, scenes are like what you normally think of as files that you open/save, but they are only valid for the projects they belong to--you can't open a scene file in another project, so I guess that's what was going wrong. Projects are like big meta-files (files that contain files), that can also be open/saved. In the title bar of the Unity window, it first lists the open scene, and then the open project. If you open a project without loading a scene too, then you just get the generic empty "untitled" scene. If you quit out of a project in Unity, it will remember which scene you were working on last when you launch it again, but if you open a new project, you'll get the "untitled" scene first.

    --Eric
     
  16. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    Thanks a lot. Got it working now. I suppose I am too used to the scene/project paradigm that Maya uses (where you open the scene file and the project assets just come along with it).

    This is really slick and is exactly what I was looking for...briefly, I need to ask a couple of questions just to clarify a few points:

    -Which camera is the image skybox (the sky texture) attached to/rendered from?

    -Is the geometry for the 3D skybox in the same scale as the rest of the world geometry or is it scaled down?


    Code (csharp):
    1. var object : Transform;
    2.  
    3. function Update () {
    4.     transform.rotation = object.rotation;
    5. }
    If I wanted to adjust the rotation value of the 3D skybox (to delay it to enhance the parallaxing effect, say, by subtracting a value from transform.rotation), where would I edit the CopyRotation.js script to do this? Basically, I would like the value of Transform to be transform.rotation = object.rotation-value where value is the amount of delay.
     
  17. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's on the SkyboxCamera. If you look at the clear flags, you can see it's set to Skybox. Compare that to the clear flags on the Main Camera on the FPS controller, which is set to Depth Only. This way the stuff on the Main Camera always renders on top, since the Depth is 1. The Depth on the SkyboxCamera is 0, and higher numbers render on top of lower numbers.

    It's scaled down. If you switch the Scene view to top and zoom out a ways, you can see the two setups side by side. (I wish Unity would store the scene camera settings per-project, instead of one preference for everything.)

    Another benefit of 3D skyboxes is better depth precision, since you can have relatively small far clip plane values for both cameras (in this small test project, they can probably go all the way down to 100 or so). Even though the apparent view limit is much greater.

    That's a bit tough, since you can't just subtract quaternions like you can with Vector3s. I'm lousy with quaternions. ;) Also, wouldn't that result in the rotations of the two cameras getting out of whack over time, so eventually they'd be pointing in completely wrong directions relative to each other?

    --Eric
     
  18. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    Thats funny, dimensionally they (the terrains) appear to be the same scale; the 'houses' are scaled down, is that what you meant?
    EDIT: took the clip plane down to 200, that seems to work fine. 100 is too low, started clipping the geometry in the distance. Of course, this is all dependent on your fog distance and the size of your level geometry, but its a really good suggestion for optimization purposes.
    You know, I didn't think of this, but this seems to make a lot of sense. Over time, the values would be so disparate that the perspective would be totally wrong. Maybe I need to take a further look at HL2 and see if they do this on their skyboxes or not; I seem to think they do, but I really can't recall.
     

    Attached Files:

  19. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The terrains are the same size (and in fact the same mesh), but different scales. The "skybox" terrain is representing a much larger area, hence it's also a different scale as much as the houses are. Also the skybox terrain has a different material on it with a larger tiling value, so it looks more like it's farther away when it really isn't.

    For fun times, try changing the Ratio value on the CopyMove script (in the Inspector) to something like .7. Whee, looks like cheesy blue screen effects in old sci-fi flicks where the perspectives were horribly mismatched. ;) It's possible this is what HL2 is doing instead of rotation differentials, except with a much lower ratio value, of course.

    --Eric
     
  20. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    I am really sorry, but I'm kind of confused. Attached are two images with the scale values for each mesh; they appear the same. Am I missing something here? Is the scale of the skybox geometry set elsewhere (not in the mesh transform)? Also, what is the scale difference (are we talking something similar to Source, where skybox geometry is at say 1/16th scale)?
    Actually, I dropped this value to 0.1, and it works just like the effect in HL2, and the perspective seems to be naturally different enough to look realistic and add to the depth of the distant objects without ever throwing the perspective totally off whack. Thanks for the pointer on this, it was really the final bit I was looking for to really polish the effect.
     

    Attached Files:

  21. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The scale difference is 1/8th...if you look at the SkyboxHouses empty (which the skybox houses are children of), you can see the scale there is .125. You can have it be whatever you like, of course. The terrain mesh is 80x80 meters, and since the size is the same for the skybox terrain, but I arbitrarily decided on 1/8th scale there, that means it's representing an area of 640x640 meters. If it makes it easier, make the SkyboxGround object be a child of the SkyboxHouses empty and then take another look at its scale. I understand your confusion, but I'm not sure how to explain it better. :)

    --Eric
     
  22. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    This works out perfectly. Thanks again for all your help with this, I really appreciate it.