- Home /
How to create a spherical skybox???
https://www.youtube.com/watch?v=H7-3dZTERf0 I was watching this video by sebastian lague and at 3:51 he shows to planets with different spheres that work as skyboxes.. how is this achieved???
Answer by Bunny83 · Dec 01, 2016 at 09:38 PM
That's just a sphere with inward looking faces. A normal sphere mesh (like the one that comes with Unity) has it's faces point outwards so when you view the sphere from the inside you wouldn't see anything. When the faces are reverted it's the opposite So from outside you see through the faces but from the inside you can see them. That's why you see the back of the sphere when you're outside the sphere.
edit
I quickly made a simple script that inverts all faces of a mesh at runtime. Just attach the script to a sphere GameObject. It can invert the faces as well as the normal vectors (so any lighting calculation would be correct). It also works on a cube or any other mesh.
//MeshInverter.cs
using UnityEngine;
public class MeshInverter : MonoBehaviour
{
public bool InvertFaces = true;
public bool InvertNormals = true;
void Start ()
{
var mf = GetComponent<MeshFilter>();
if (mf != null)
{
var m = Instantiate(mf.sharedMesh);
Process(m);
mf.sharedMesh = m;
}
var smr = GetComponent<SkinnedMeshRenderer>();
if (smr != null)
{
var m = Instantiate(smr.sharedMesh);
Process(m);
smr.sharedMesh = m;
}
}
private void Process(Mesh m)
{
int subMeshes = m.subMeshCount;
for (int i = 0; i < subMeshes; i++)
{
if (InvertFaces)
{
var type = m.GetTopology(i);
var indices = m.GetIndices(i);
if (type == MeshTopology.Quads)
{
for (int n = 0; n < indices.Length; n += 4)
{
int tmp = indices[n];
indices[n] = indices[n + 3];
indices[n + 3] = tmp;
tmp = indices[n + 1];
indices[n + 1] = indices[n + 2];
indices[n + 2] = tmp;
}
}
else if (type == MeshTopology.Triangles)
{
for (int n = 0; n < indices.Length; n += 3)
{
int tmp = indices[n];
indices[n] = indices[n + 1];
indices[n + 1] = tmp;
}
}
m.SetIndices(indices, type, i);
}
}
if (InvertNormals)
{
var normals = m.normals;
for (int n = 0; n < normals.Length; n++)
normals[n] = -normals[n];
m.normals = normals;
}
}
}
What would you think of achieving the same effect with shader? Just by adding the Cull front directive to the shader diplicate.
Well it depends on the kind of shader. If the shader uses any kind of lighting you have to flip the normals as well (I mean in the shader). However it makes much more sense to just use a proper model in the first place as this allows you to use any shader you want.
If the shader is an unlit shader then yes, using just cull front would be enough when using a normal sphere.