Exemplo n.º 1
0
 public void CopySurfaceMultiMeshInto(SGT_SurfaceMultiMesh target)
 {
     if (target != null && surfaceMesh != null)
     {
         surfaceMesh.CopyMeshesInto(target);
     }
 }
Exemplo n.º 2
0
    public void Awake()
    {
        switch (FindAwakeState())
        {
        case AwakeState.AwakeOriginal:
        {
            if (sourceSurfaceMesh == null)
            {
                sourceSurfaceMesh = new SGT_SurfaceMultiMesh();

                SendMessage("CopySurfaceMultiMeshInto", sourceSurfaceMesh, SendMessageOptions.DontRequireReceiver);

                modified = true;
            }
        }
        break;

        case AwakeState.AwakeDuplicate:
        {
            generatedSurfaceMesh = null;
            modified             = true;
        }
        break;

        case AwakeState.AwakeAgain:
        {
        }
        break;
        }
    }
 public void CopySurfaceMultiMeshInto(SGT_SurfaceMultiMesh target)
 {
     if (target != null && surfaceMesh != null)
     {
         surfaceMesh.CopyMeshesInto(target);
     }
 }
Exemplo n.º 4
0
    public void SetSurfaceConfiguration(SGT_SurfaceConfiguration newConfiguration)
    {
        if (sourceSurfaceMesh == null)
        {
            sourceSurfaceMesh = new SGT_SurfaceMultiMesh();
        }

        sourceSurfaceMesh.Configuration = newConfiguration;
    }
	public void LateUpdate()
	{
		if (oblatenessGameObject == null) oblatenessGameObject = SGT_Helper.CreateGameObject("Oblateness", gameObject);
		if (surfaceGameObject    == null) surfaceGameObject    = SGT_Helper.CreateGameObject("Surface", oblatenessGameObject);
		if (atmosphereGameObject == null) atmosphereGameObject = SGT_Helper.CreateGameObject("Atmosphere", oblatenessGameObject);
		if (atmosphereMesh       == null) atmosphereMesh       = new SGT_Mesh();
		if (starObserver         == null) starObserver         = SGT_Helper.FindCamera();
		if (surfaceTexture       == null) surfaceTexture       = new SGT_SurfaceTexture();
		if (surfaceMultiMesh     == null) surfaceMultiMesh     = new SGT_SurfaceMultiMesh();
		
		SGT_Helper.SetParent(oblatenessGameObject, gameObject);
		SGT_Helper.SetLayer(oblatenessGameObject, gameObject.layer);
		SGT_Helper.SetTag(oblatenessGameObject, gameObject.tag);
		
		SGT_Helper.SetParent(surfaceGameObject, oblatenessGameObject);
		SGT_Helper.SetLayer(surfaceGameObject, oblatenessGameObject.layer);
		SGT_Helper.SetTag(surfaceGameObject, oblatenessGameObject.tag);
		
		SGT_Helper.SetParent(atmosphereGameObject, oblatenessGameObject);
		SGT_Helper.SetLayer(atmosphereGameObject, oblatenessGameObject.layer);
		SGT_Helper.SetTag(atmosphereGameObject, oblatenessGameObject.tag);
		
		if (atmosphereDensityColour == null)
		{
			atmosphereDensityColour = new SGT_ColourGradient(false, true);
			atmosphereDensityColour.AddColourNode(new Color(1.0f, 1.0f, 0.0f, 1.0f), 0.0f);
			atmosphereDensityColour.AddColourNode(new Color(1.0f, 0.5f, 0.0f, 1.0f), 0.5f).Locked = true;
			atmosphereDensityColour.AddColourNode(new Color(1.0f, 0.0f, 0.0f, 1.0f), 1.0f);
		}
		
		UpdateTransform();
		UpdateGradient();
		UpdateMaterial();
		UpdateShader();
		
		surfaceMultiMesh.GameObject           = surfaceGameObject;
		surfaceMultiMesh.HasMeshRenderers     = true;
		surfaceMultiMesh.MeshRenderersEnabled = true;
		surfaceMultiMesh.MeshCollidersEnabled = true;
		surfaceMultiMesh.SetSharedMaterials(surfaceMaterials);
		surfaceMultiMesh.Update(gameObject.layer, gameObject.tag);
		
		atmosphereMesh.GameObject          = atmosphereGameObject;
		atmosphereMesh.HasMeshRenderer     = true;
		atmosphereMesh.MeshRendererEnabled = true;
		atmosphereMesh.SharedMaterial      = atmosphereMaterial;
		atmosphereMesh.Update();
		
#if UNITY_EDITOR == true
		SGT_Helper.HideGameObject(oblatenessGameObject);
		
		atmosphereMesh.HideInEditor();
		surfaceMultiMesh.HideInEditor();
#endif
	}
Exemplo n.º 6
0
    public void SetSurfaceMultiMesh(SGT_SurfaceMultiMesh newSurfaceMultiMesh)
    {
        if (newSurfaceMultiMesh != null)
        {
            if (surfaceMesh == null)
            {
                surfaceMesh = new SGT_SurfaceMultiMesh();
            }

            newSurfaceMultiMesh.CopyMeshesInto(surfaceMesh);
        }
    }
Exemplo n.º 7
0
    public void SetSurfaceConfiguration(SGT_SurfaceConfiguration newConfiguration)
    {
        if (surfaceMesh == null)
        {
            surfaceMesh = new SGT_SurfaceMultiMesh();
        }

        if (newConfiguration != surfaceMesh.Configuration)
        {
            surfaceMesh.Configuration = newConfiguration;
        }
    }
Exemplo n.º 8
0
    public void Update()
    {
        if (sourceSurfaceMesh == null)
        {
            sourceSurfaceMesh = new SGT_SurfaceMultiMesh();
        }

        if (displacementAutoRegen == true)
        {
            Regenerate();
        }
    }
Exemplo n.º 9
0
    public void CopyMeshesInto(SGT_SurfaceMultiMesh target)
    {
        if (target != null && multiMeshes != null)
        {
            var surfaceCount = Count;

            target.Configuration = configuration;

            for (var i = 0; i < surfaceCount; i++)
            {
                var s = GetMultiMesh(i);
                var t = target.GetMultiMesh(i);

                if (s != null && t != null)
                {
                    s.CopyMeshesInto(t);
                }
            }
        }
    }
Exemplo n.º 10
0
    public static bool MeshesIdentical(SGT_SurfaceMultiMesh a, SGT_SurfaceMultiMesh b)
    {
        if (a == null && b != null)
        {
            return(false);
        }
        if (a != null && b == null)
        {
            return(false);
        }
        if (a != null && b != null)
        {
            if (a.multiMeshes == null && b.multiMeshes != null)
            {
                return(false);
            }
            if (a.multiMeshes != null && b.multiMeshes == null)
            {
                return(false);
            }
            if (a.multiMeshes != null && b.multiMeshes != null)
            {
                if (a.multiMeshes.Length != b.multiMeshes.Length)
                {
                    return(false);
                }

                for (var i = 0; i < a.multiMeshes.Length; i++)
                {
                    if (SGT_MultiMesh.MeshesIdentical(a.multiMeshes[i], b.multiMeshes[i]) == false)
                    {
                        return(false);
                    }
                }
            }
        }

        return(true);
    }
    public static SGT_SurfaceMultiMesh SurfaceMultiMeshField(string handle, string tooltip, SGT_SurfaceMultiMesh field, bool required = false)
    {
        if (CanDraw == true)
        {
            switch (field.Configuration)
            {
                case SGT_SurfaceConfiguration.Sphere:
                {
                    MultiMeshField(handle, tooltip, field.GetMultiMesh(0), required);
                }
                break;
                case SGT_SurfaceConfiguration.Cube:
                {
                    var rectP = ReserveField(handle, tooltip);
                    var rectN = ReserveField();

                    var pX = SGT_RectHelper.HorizontalSlice(rectP, 0.0f / 3.0f, 1.0f / 3.0f);
                    var pY = SGT_RectHelper.HorizontalSlice(rectP, 1.0f / 3.0f, 2.0f / 3.0f);
                    var pZ = SGT_RectHelper.HorizontalSlice(rectP, 2.0f / 3.0f, 3.0f / 3.0f);

                    var nX = SGT_RectHelper.HorizontalSlice(rectN, 0.0f / 3.0f, 1.0f / 3.0f);
                    var nY = SGT_RectHelper.HorizontalSlice(rectN, 1.0f / 3.0f, 2.0f / 3.0f);
                    var nZ = SGT_RectHelper.HorizontalSlice(rectN, 2.0f / 3.0f, 3.0f / 3.0f);

                    MultiMeshFieldWithLabel(pX, "X+", 25, field.GetMultiMesh(CubemapFace.PositiveX), required);
                    MultiMeshFieldWithLabel(pY, "Y+", 25, field.GetMultiMesh(CubemapFace.PositiveY), required);
                    MultiMeshFieldWithLabel(pZ, "Z+", 25, field.GetMultiMesh(CubemapFace.PositiveZ), required);

                    MultiMeshFieldWithLabel(nX, "X-", 25, field.GetMultiMesh(CubemapFace.NegativeX), required);
                    MultiMeshFieldWithLabel(nY, "Y-", 25, field.GetMultiMesh(CubemapFace.NegativeY), required);
                    MultiMeshFieldWithLabel(nZ, "Z-", 25, field.GetMultiMesh(CubemapFace.NegativeZ), required);
                }
                break;
            }
        }

        return field;
    }
    private System.Collections.IEnumerator Update_Coroutine()
    {
        if (patchIndices == null)
        {
            RebuildPatchIndices();
        }

        sideCombinedMeshes = new Mesh[6][];

        // Main loop
        for (;;)
        {
            if (sides != null)
            {
                var sgtVector3 = new SGT_FillVector3();
                SendMessage("FillSurfaceObserverPosition", sgtVector3, SendMessageOptions.DontRequireReceiver);
                surfaceObserverPosition = sgtVector3.Vector3;

                var splitList = new PatchList();

                // Begin budgeting
                budgetUsage.Reset();
                budgetUsage.Start();

                // Update all patches
                for (var i = 0; i < sides.Length; i++)
                {
                    UpdatePatch(sides[i], surfaceObserverPosition, splitList);

                    /* Defer operation? */ if (OverBudgetCheck("Update Patches") == true){yield return new WaitForEndOfFrame(); budgetUsage.Reset(); budgetUsage.Start();}
                }

                // Split patches?
                if (splitList.Count > 0)
                {
                    var splitCount = 0;

                    for (var i = 0; i < splitList.Count; i++)
                    {
                        splitCount += 1;

                        SplitPatch(splitList[i]);

                        if (splitCount == maxSplitsPerFrame)
                        {
                            splitCount = 0;

                            /* Defer operation? */ if (OverBudgetCheck("Split Patches") == true){yield return new WaitForEndOfFrame(); budgetUsage.Reset(); budgetUsage.Start();}
                        }
                    }
                }

                // Rebuild mesh?
                if (rebuild == true)
                {
                    rebuild = false;

                    var totalSurfaces         = SGT_SurfaceConfiguration_.SurfaceCount(surfaceConfiguration);
                    var newSideCombinedMeshes = new Mesh[6][];

                    for (var surfaceIndex = 0; surfaceIndex < totalSurfaces; surfaceIndex++)
                    {
                        var buildList = new PatchList();

                        // Begin budgeting
                        budgetUsage.Reset();
                        budgetUsage.Start();

                        switch (surfaceConfiguration)
                        {
                            case SGT_SurfaceConfiguration.Sphere:
                            {
                                for (var i = 0; i < 6; i++)
                                {
                                    BuildPatch(sides[i], buildList);

                                    /* Defer operation? */ if (OverBudgetCheck("Build Patches") == true){yield return new WaitForEndOfFrame(); budgetUsage.Reset(); budgetUsage.Start();}
                                }
                            }
                            break;
                            case SGT_SurfaceConfiguration.Cube:
                            {
                                BuildPatch(sides[surfaceIndex], buildList);

                                /* Defer operation? */ if (OverBudgetCheck("Build Patches") == true){yield return new WaitForEndOfFrame(); budgetUsage.Reset(); budgetUsage.Start();}
                            }
                            break;
                        }

                        // Stitch patches
                        var stitchCount = 0;

                        for (var i = 0; i < buildList.Count; i++)
                        {
                            stitchCount += 1;

                            StitchPatch(buildList[i]);

                            if (stitchCount == maxStitchesPerFrame)
                            {
                                stitchCount = 0;

                                /* Defer operation? */ if (OverBudgetCheck("Sitch Patches") == true){yield return new WaitForEndOfFrame(); budgetUsage.Reset(); budgetUsage.Start();}
                            }
                        }

                        var combinedMeshesList = new MeshList();
                        var combineFrom        = 0;
                        var patchesPerMesh     = verticesPerMesh / ((patchResolution + 1) * (patchResolution + 1));

                        // Create sub meshes
                        for (var i = 0; i < buildList.Count; i++)
                        {
                            var combineCount = (i + 1) - combineFrom;

                            if (combineCount == patchesPerMesh || i == (buildList.Count - 1))
                            {
                                var combinedMesh = CombinePatches(buildList, combineFrom, combineFrom + combineCount);

                                combinedMeshesList.Add(combinedMesh);

                                combineFrom = i + 1;

                                /* Defer operation? */ if (OverBudgetCheck("Combine Patches") == true){yield return new WaitForEndOfFrame(); budgetUsage.Reset(); budgetUsage.Start();}
                            }
                        }

                        var combinedMeshes = combinedMeshesList.ToArray();

                        // Append index to name?
                        if (combinedMeshes.Length > 1)
                        {
                            for (var i = 0; i < combinedMeshes.Length; i++)
                            {
                                combinedMeshes[i].name = "(" + (i + 1) + "/" + combinedMeshes.Length + ") " + combinedMeshes[i].name;
                            }
                        }

                        newSideCombinedMeshes[surfaceIndex] = combinedMeshes;
                    }

                    // Delete old meshes and swap
                    for (var i = 0; i < 6; i++)
                    {
                        SGT_Helper.DestroyObjects(sideCombinedMeshes[i]);
                    }

                    sideCombinedMeshes = newSideCombinedMeshes;

                    var generatedSurfaceMultiMesh = new SGT_SurfaceMultiMesh();

                    generatedSurfaceMultiMesh.Configuration = surfaceConfiguration;

                    for (var i = 0; i < totalSurfaces; i++)
                    {
                        generatedSurfaceMultiMesh.ReplaceAll(sideCombinedMeshes[i], i);
                    }

                    SendMessage("SetSurfaceMultiMesh", generatedSurfaceMultiMesh, SendMessageOptions.DontRequireReceiver);

                    SendMessage("TessellationFinished", this, SendMessageOptions.DontRequireReceiver);
                }
            }

            if (minUpdateInterval > 0.0f)
            {
                yield return new WaitForSeconds(minUpdateInterval);
            }
            else
            {
                yield return new WaitForEndOfFrame();
            }
        }
    }
Exemplo n.º 13
0
    public void Regenerate()
    {
        if (modified == false)
        {
            CheckForModifications();
        }

        if (modified == true)
        {
            if (displacementTexture != null && sourceSurfaceMesh != null)
            {
                if (generatedSurfaceMesh == null)
                {
                    generatedSurfaceMesh = new SGT_SurfaceMultiMesh();
                }

                // Destroy old meshes
                generatedSurfaceMesh.DestroyAllMeshes();

                sourceSurfaceMesh.CopyMeshesInto(generatedSurfaceMesh);

                var surfaceCount = generatedSurfaceMesh.Count;

                for (var i = 0; i < surfaceCount; i++)
                {
                    var multiMesh = generatedSurfaceMesh.GetMultiMesh(i);

                    if (multiMesh != null)
                    {
                        for (var j = 0; j < multiMesh.Count; j++)
                        {
                            var mesh = multiMesh.GetSharedMesh(j);

                            if (mesh != null)
                            {
                                mesh           = SGT_Helper.CloneObject(mesh);
                                mesh.hideFlags = HideFlags.DontSave;

                                var positions      = mesh.vertices;
                                var uv0s           = mesh.uv;
                                var newPositions   = new Vector3[positions.Length];
                                var displacementUV = Vector2.zero;

                                for (var k = 0; k < positions.Length; k++)
                                {
                                    var texture  = (Texture2D)null;
                                    var position = positions[k];

                                    switch (displacementTexture.Configuration)
                                    {
                                    case SGT_SurfaceConfiguration.Sphere:
                                    {
                                        texture = displacementTexture.GetTexture2D(0);

                                        if (texture != null)
                                        {
                                            displacementUV = useUV == true ? uv0s[k] : SGT_Helper.CartesianToPolarUV(position);

                                            if (clamp == true)
                                            {
                                                displacementUV = SGT_Helper.ClampCollapseV(displacementUV, SGT_Helper.PixelUV(texture));
                                            }
                                        }
                                    }
                                    break;

                                    case SGT_SurfaceConfiguration.Cube:
                                    {
                                        texture = displacementTexture.GetTexture2D(position);

                                        if (texture != null)
                                        {
                                            displacementUV = useUV == true ? uv0s[k] : SGT_Helper.CubeUV(position, true);

                                            if (clamp == true)
                                            {
                                                displacementUV = SGT_Helper.ClampUV(displacementUV, SGT_Helper.PixelUV(texture));
                                            }
                                        }
                                    }
                                    break;
                                    }

                                    var displacement = texture != null?texture.GetPixelBilinear(displacementUV.x, displacementUV.y).r : 0.0f;

                                    newPositions[k] = position * Mathf.Lerp(scaleMin, scaleMax, displacement);
                                }

                                mesh.name    += "(Displaced)";
                                mesh.vertices = newPositions;
                                mesh.bounds   = new Bounds(mesh.bounds.center, mesh.bounds.size * scaleMax);
                                mesh.RecalculateNormals();

                                multiMesh.SetSharedMesh(mesh, j);
                            }
                        }
                    }
                }

                MarkAsUnmodified();

                SendMessage("SetSurfaceMultiMesh", generatedSurfaceMesh, SendMessageOptions.DontRequireReceiver);
            }
        }
    }
Exemplo n.º 14
0
    public void SetSurfaceConfiguration(SGT_SurfaceConfiguration newConfiguration)
    {
        if (surfaceMesh == null) surfaceMesh = new SGT_SurfaceMultiMesh();

        if (newConfiguration != surfaceMesh.Configuration)
        {
            surfaceMesh.Configuration = newConfiguration;
        }
    }
Exemplo n.º 15
0
    public void LateUpdate()
    {
        if (surfaceGameObject      == null) surfaceGameObject      = SGT_Helper.CreateGameObject("Surface", gameObject);
        if (atmosphereGameObject   == null) atmosphereGameObject   = SGT_Helper.CreateGameObject("Atmosphere", gameObject);
        if (cloudsGameObject       == null) cloudsGameObject       = SGT_Helper.CreateGameObject("Clouds", gameObject);
        if (surfaceMesh            == null) surfaceMesh            = new SGT_SurfaceMultiMesh();
        if (surfaceTextureDay      == null) surfaceTextureDay      = new SGT_SurfaceTexture();
        if (surfaceTextureNight    == null) surfaceTextureNight    = new SGT_SurfaceTexture();
        if (surfaceTextureNormal   == null) surfaceTextureNormal   = new SGT_SurfaceTexture();
        if (surfaceTextureSpecular == null) surfaceTextureSpecular = new SGT_SurfaceTexture();
        if (planetObserver         == null) planetObserver         = SGT_Helper.FindCamera();
        if (planetLightSource      == null) planetLightSource      = SGT_LightSource.Find();

        SGT_Helper.SetParent(surfaceGameObject, gameObject);
        SGT_Helper.SetLayer(surfaceGameObject, gameObject.layer);
        SGT_Helper.SetTag(surfaceGameObject, gameObject.tag);

        SGT_Helper.SetParent(atmosphereGameObject, gameObject);
        SGT_Helper.SetLayer(atmosphereGameObject, gameObject.layer);
        SGT_Helper.SetTag(atmosphereGameObject, gameObject.tag);

        SGT_Helper.SetParent(cloudsGameObject, gameObject);
        SGT_Helper.SetLayer(cloudsGameObject, gameObject.layer);
        SGT_Helper.SetTag(cloudsGameObject, gameObject.tag);

        surfaceTextureDay.Configuration      = surfaceMesh.Configuration;
        surfaceTextureNight.Configuration    = surfaceMesh.Configuration;
        surfaceTextureNormal.Configuration   = surfaceMesh.Configuration;
        surfaceTextureSpecular.Configuration = surfaceMesh.Configuration;

        if (surfaceTextureDay.Modified      == true) { updateShader |= ShaderFlags.SurfaceTextureDay;      surfaceTextureDay.Modified      = false; }
        if (surfaceTextureNight.Modified    == true) { updateShader |= ShaderFlags.SurfaceTextureNight;    surfaceTextureNight.Modified    = false; }
        if (surfaceTextureNormal.Modified   == true) { updateShader |= ShaderFlags.SurfaceTextureNormal;   surfaceTextureNormal.Modified   = false; }
        if (surfaceTextureSpecular.Modified == true) { updateShader |= ShaderFlags.SurfaceTextureSpecular; surfaceTextureSpecular.Modified = false; }

        if (atmosphere == true)
        {
            if (atmosphereMesh == null) atmosphereMesh = new SGT_Mesh();
        }
        else
        {
            if (atmosphereMesh != null) atmosphereMesh.Clear();
        }

        if (clouds == true)
        {
            if (cloudsMesh    == null) cloudsMesh    = new SGT_SurfaceMultiMesh();
            if (cloudsTexture == null) cloudsTexture = new SGT_SurfaceTexture();

            if (cloudsTexture.Modified == true) { updateShader |= ShaderFlags.CloudsTexture; cloudsTexture.Modified = false; }
        }
        else
        {
            if (cloudsTexture != null) cloudsTexture = null;
            if (cloudsMesh    != null) cloudsMesh.Clear();
        }

        if (planetLighting == null)
        {
            planetLighting = new SGT_ColourGradient(false, false);
            planetLighting.AddColourNode(Color.black, 0.45f);
            planetLighting.AddColourNode(Color.white, 0.55f);
        }

        if (atmosphereTwilightColour == null)
        {
            atmosphereTwilightColour = new SGT_ColourGradient(false, true);
            atmosphereTwilightColour.AddAlphaNode(1.0f, 0.45f);
            atmosphereTwilightColour.AddAlphaNode(0.0f, 0.65f);
            atmosphereTwilightColour.AddColourNode(new Color(1.0f, 0.19f, 0.0f, 1.0f), 0.5f);
        }

        if (atmosphereDensityColour == null)
        {
            atmosphereDensityColour = new SGT_ColourGradient(false, true);
            atmosphereDensityColour.AddColourNode(new Color(0.17f, 0.53f, 0.85f), 0.2f);
            atmosphereDensityColour.AddColourNode(Color.white, 0.5f).Locked = true;
            atmosphereDensityColour.AddColourNode(new Color(0.17f, 0.51f, 1.0f), 1.0f);
        }

        if (cloudsLimbColour == null)
        {
            cloudsLimbColour = new SGT_ColourGradient(false, true);
            cloudsLimbColour.AddColourNode(Color.white, 0.5f);
        }

        // Rotate?
        if (Application.isPlaying == true)
        {
            if (cloudsRotationPeriod != 0.0f)
            {
                cloudsGameObject.transform.Rotate(0.0f, SGT_Helper.DegreesPerSecond(cloudsRotationPeriod) * Time.smoothDeltaTime, 0.0f);
            }
        }

        // Update scales
        SGT_Helper.SetLocalScale(surfaceGameObject.transform, surfaceRadius);

        if (atmosphere == true)
        {
            SGT_Helper.SetLocalScale(atmosphereGameObject.transform, AtmosphereRadius);

            // Point atmosphere at camera
            if (planetObserver != null)
            {
                var observerPosition  = planetObserver.transform.position;
                var observerDirection = (observerPosition - gameObject.transform.position).normalized;

                SGT_Helper.SetUp(atmosphereGameObject.transform, observerDirection);
            }
        }

        if (clouds == true)
        {
            SGT_Helper.SetLocalScale(cloudsGameObject.transform, CloudsRadius);

            UpdateCloudsOffset();
        }

        UpdateGradient();
        UpdateTechnique();
        UpdateShader();

        surfaceMesh.GameObject           = surfaceGameObject;
        surfaceMesh.HasMeshRenderers     = true;
        surfaceMesh.MeshRenderersEnabled = true;
        surfaceMesh.MeshCollidersEnabled = true;
        surfaceMesh.SetSharedMaterials(surfaceMaterials);
        surfaceMesh.Update(gameObject.layer, gameObject.tag);

        if (atmosphere == true)
        {
            atmosphereMesh.GameObject          = atmosphereGameObject;
            atmosphereMesh.HasMeshRenderer     = true;
            atmosphereMesh.MeshRendererEnabled = true;
            atmosphereMesh.SharedMaterial      = atmosphereMaterial;
            atmosphereMesh.Update();
        }

        if (clouds == true)
        {
            cloudsMesh.GameObject           = cloudsGameObject;
            cloudsMesh.HasMeshRenderers     = true;
            cloudsMesh.MeshRenderersEnabled = true;
            cloudsMesh.SetSharedMaterials(cloudsMaterials);
            cloudsMesh.Update(gameObject.layer, gameObject.tag);
        }

        #if UNITY_EDITOR == true
        surfaceMesh.HideInEditor();
        if (atmosphereMesh != null) atmosphereMesh.HideInEditor();
        if (cloudsMesh     != null) cloudsMesh.HideInEditor();

        SGT_Helper.HideGameObject(atmosphereGameObject);
        SGT_Helper.HideGameObject(cloudsGameObject);
        #endif
    }
Exemplo n.º 16
0
    public static SGT_SurfaceMultiMesh SurfaceMultiMeshField(string handle, string tooltip, SGT_SurfaceMultiMesh field, bool required = false)
    {
        if (CanDraw == true)
        {
            switch (field.Configuration)
            {
            case SGT_SurfaceConfiguration.Sphere:
            {
                MultiMeshField(handle, tooltip, field.GetMultiMesh(0), required);
            }
            break;

            case SGT_SurfaceConfiguration.Cube:
            {
                var rectP = ReserveField(handle, tooltip);
                var rectN = ReserveField();

                var pX = SGT_RectHelper.HorizontalSlice(rectP, 0.0f / 3.0f, 1.0f / 3.0f);
                var pY = SGT_RectHelper.HorizontalSlice(rectP, 1.0f / 3.0f, 2.0f / 3.0f);
                var pZ = SGT_RectHelper.HorizontalSlice(rectP, 2.0f / 3.0f, 3.0f / 3.0f);

                var nX = SGT_RectHelper.HorizontalSlice(rectN, 0.0f / 3.0f, 1.0f / 3.0f);
                var nY = SGT_RectHelper.HorizontalSlice(rectN, 1.0f / 3.0f, 2.0f / 3.0f);
                var nZ = SGT_RectHelper.HorizontalSlice(rectN, 2.0f / 3.0f, 3.0f / 3.0f);

                MultiMeshFieldWithLabel(pX, "X+", 25, field.GetMultiMesh(CubemapFace.PositiveX), required);
                MultiMeshFieldWithLabel(pY, "Y+", 25, field.GetMultiMesh(CubemapFace.PositiveY), required);
                MultiMeshFieldWithLabel(pZ, "Z+", 25, field.GetMultiMesh(CubemapFace.PositiveZ), required);

                MultiMeshFieldWithLabel(nX, "X-", 25, field.GetMultiMesh(CubemapFace.NegativeX), required);
                MultiMeshFieldWithLabel(nY, "Y-", 25, field.GetMultiMesh(CubemapFace.NegativeY), required);
                MultiMeshFieldWithLabel(nZ, "Z-", 25, field.GetMultiMesh(CubemapFace.NegativeZ), required);
            }
            break;
            }
        }

        return(field);
    }
    private System.Collections.IEnumerator Update_Coroutine()
    {
        if (patchIndices == null)
        {
            RebuildPatchIndices();
        }

        sideCombinedMeshes = new Mesh[6][];

        // Main loop
        for (;;)
        {
            if (sides != null)
            {
                var sgtVector3 = new SGT_FillVector3();
                SendMessage("FillSurfaceObserverPosition", sgtVector3, SendMessageOptions.DontRequireReceiver);
                surfaceObserverPosition = sgtVector3.Vector3;

                var splitList = new PatchList();

                // Begin budgeting
                budgetUsage.Reset();
                budgetUsage.Start();

                // Update all patches
                for (var i = 0; i < sides.Length; i++)
                {
                    UpdatePatch(sides[i], surfaceObserverPosition, splitList);

                    /* Defer operation? */ if (OverBudgetCheck("Update Patches") == true)
                    {
                        yield return(new WaitForEndOfFrame()); budgetUsage.Reset(); budgetUsage.Start();
                    }
                }

                // Split patches?
                if (splitList.Count > 0)
                {
                    var splitCount = 0;

                    for (var i = 0; i < splitList.Count; i++)
                    {
                        splitCount += 1;

                        SplitPatch(splitList[i]);

                        if (splitCount == maxSplitsPerFrame)
                        {
                            splitCount = 0;

                            /* Defer operation? */ if (OverBudgetCheck("Split Patches") == true)
                            {
                                yield return(new WaitForEndOfFrame()); budgetUsage.Reset(); budgetUsage.Start();
                            }
                        }
                    }
                }

                // Rebuild mesh?
                if (rebuild == true)
                {
                    rebuild = false;

                    var totalSurfaces         = SGT_SurfaceConfiguration_.SurfaceCount(surfaceConfiguration);
                    var newSideCombinedMeshes = new Mesh[6][];

                    for (var surfaceIndex = 0; surfaceIndex < totalSurfaces; surfaceIndex++)
                    {
                        var buildList = new PatchList();

                        // Begin budgeting
                        budgetUsage.Reset();
                        budgetUsage.Start();

                        switch (surfaceConfiguration)
                        {
                        case SGT_SurfaceConfiguration.Sphere:
                        {
                            for (var i = 0; i < 6; i++)
                            {
                                BuildPatch(sides[i], buildList);

                                /* Defer operation? */ if (OverBudgetCheck("Build Patches") == true)
                                {
                                    yield return(new WaitForEndOfFrame()); budgetUsage.Reset(); budgetUsage.Start();
                                }
                            }
                        }
                        break;

                        case SGT_SurfaceConfiguration.Cube:
                        {
                            BuildPatch(sides[surfaceIndex], buildList);

                            /* Defer operation? */ if (OverBudgetCheck("Build Patches") == true)
                            {
                                yield return(new WaitForEndOfFrame()); budgetUsage.Reset(); budgetUsage.Start();
                            }
                        }
                        break;
                        }

                        // Stitch patches
                        var stitchCount = 0;

                        for (var i = 0; i < buildList.Count; i++)
                        {
                            stitchCount += 1;

                            StitchPatch(buildList[i]);

                            if (stitchCount == maxStitchesPerFrame)
                            {
                                stitchCount = 0;

                                /* Defer operation? */ if (OverBudgetCheck("Sitch Patches") == true)
                                {
                                    yield return(new WaitForEndOfFrame()); budgetUsage.Reset(); budgetUsage.Start();
                                }
                            }
                        }

                        var combinedMeshesList = new MeshList();
                        var combineFrom        = 0;
                        var patchesPerMesh     = verticesPerMesh / ((patchResolution + 1) * (patchResolution + 1));

                        // Create sub meshes
                        for (var i = 0; i < buildList.Count; i++)
                        {
                            var combineCount = (i + 1) - combineFrom;

                            if (combineCount == patchesPerMesh || i == (buildList.Count - 1))
                            {
                                var combinedMesh = CombinePatches(buildList, combineFrom, combineFrom + combineCount);

                                combinedMeshesList.Add(combinedMesh);

                                combineFrom = i + 1;

                                /* Defer operation? */ if (OverBudgetCheck("Combine Patches") == true)
                                {
                                    yield return(new WaitForEndOfFrame()); budgetUsage.Reset(); budgetUsage.Start();
                                }
                            }
                        }

                        var combinedMeshes = combinedMeshesList.ToArray();

                        // Append index to name?
                        if (combinedMeshes.Length > 1)
                        {
                            for (var i = 0; i < combinedMeshes.Length; i++)
                            {
                                combinedMeshes[i].name = "(" + (i + 1) + "/" + combinedMeshes.Length + ") " + combinedMeshes[i].name;
                            }
                        }

                        newSideCombinedMeshes[surfaceIndex] = combinedMeshes;
                    }

                    // Delete old meshes and swap
                    for (var i = 0; i < 6; i++)
                    {
                        SGT_Helper.DestroyObjects(sideCombinedMeshes[i]);
                    }

                    sideCombinedMeshes = newSideCombinedMeshes;

                    var generatedSurfaceMultiMesh = new SGT_SurfaceMultiMesh();

                    generatedSurfaceMultiMesh.Configuration = surfaceConfiguration;

                    for (var i = 0; i < totalSurfaces; i++)
                    {
                        generatedSurfaceMultiMesh.ReplaceAll(sideCombinedMeshes[i], i);
                    }

                    SendMessage("SetSurfaceMultiMesh", generatedSurfaceMultiMesh, SendMessageOptions.DontRequireReceiver);

                    SendMessage("TessellationFinished", this, SendMessageOptions.DontRequireReceiver);
                }
            }

            if (minUpdateInterval > 0.0f)
            {
                yield return(new WaitForSeconds(minUpdateInterval));
            }
            else
            {
                yield return(new WaitForEndOfFrame());
            }
        }
    }
Exemplo n.º 18
0
    public void LateUpdate()
    {
        if (surfaceGameObject == null)
        {
            surfaceGameObject = SGT_Helper.CreateGameObject("Surface", gameObject);
        }
        if (atmosphereGameObject == null)
        {
            atmosphereGameObject = SGT_Helper.CreateGameObject("Atmosphere", gameObject);
        }
        if (cloudsGameObject == null)
        {
            cloudsGameObject = SGT_Helper.CreateGameObject("Clouds", gameObject);
        }
        if (surfaceMesh == null)
        {
            surfaceMesh = new SGT_SurfaceMultiMesh();
        }
        if (surfaceTextureDay == null)
        {
            surfaceTextureDay = new SGT_SurfaceTexture();
        }
        if (surfaceTextureNight == null)
        {
            surfaceTextureNight = new SGT_SurfaceTexture();
        }
        if (surfaceTextureNormal == null)
        {
            surfaceTextureNormal = new SGT_SurfaceTexture();
        }
        if (surfaceTextureSpecular == null)
        {
            surfaceTextureSpecular = new SGT_SurfaceTexture();
        }
        if (planetObserver == null)
        {
            planetObserver = SGT_Helper.FindCamera();
        }
        if (planetLightSource == null)
        {
            planetLightSource = SGT_LightSource.Find();
        }

        SGT_Helper.SetParent(surfaceGameObject, gameObject);
        SGT_Helper.SetLayer(surfaceGameObject, gameObject.layer);
        SGT_Helper.SetTag(surfaceGameObject, gameObject.tag);

        SGT_Helper.SetParent(atmosphereGameObject, gameObject);
        SGT_Helper.SetLayer(atmosphereGameObject, gameObject.layer);
        SGT_Helper.SetTag(atmosphereGameObject, gameObject.tag);

        SGT_Helper.SetParent(cloudsGameObject, gameObject);
        SGT_Helper.SetLayer(cloudsGameObject, gameObject.layer);
        SGT_Helper.SetTag(cloudsGameObject, gameObject.tag);

        surfaceTextureDay.Configuration      = surfaceMesh.Configuration;
        surfaceTextureNight.Configuration    = surfaceMesh.Configuration;
        surfaceTextureNormal.Configuration   = surfaceMesh.Configuration;
        surfaceTextureSpecular.Configuration = surfaceMesh.Configuration;

        if (surfaceTextureDay.Modified == true)
        {
            updateShader |= ShaderFlags.SurfaceTextureDay;      surfaceTextureDay.Modified = false;
        }
        if (surfaceTextureNight.Modified == true)
        {
            updateShader |= ShaderFlags.SurfaceTextureNight;    surfaceTextureNight.Modified = false;
        }
        if (surfaceTextureNormal.Modified == true)
        {
            updateShader |= ShaderFlags.SurfaceTextureNormal;   surfaceTextureNormal.Modified = false;
        }
        if (surfaceTextureSpecular.Modified == true)
        {
            updateShader |= ShaderFlags.SurfaceTextureSpecular; surfaceTextureSpecular.Modified = false;
        }

        if (atmosphere == true)
        {
            if (atmosphereMesh == null)
            {
                atmosphereMesh = new SGT_Mesh();
            }
        }
        else
        {
            if (atmosphereMesh != null)
            {
                atmosphereMesh.Clear();
            }
        }

        if (clouds == true)
        {
            if (cloudsMesh == null)
            {
                cloudsMesh = new SGT_SurfaceMultiMesh();
            }
            if (cloudsTexture == null)
            {
                cloudsTexture = new SGT_SurfaceTexture();
            }

            if (cloudsTexture.Modified == true)
            {
                updateShader |= ShaderFlags.CloudsTexture; cloudsTexture.Modified = false;
            }
        }
        else
        {
            if (cloudsTexture != null)
            {
                cloudsTexture = null;
            }
            if (cloudsMesh != null)
            {
                cloudsMesh.Clear();
            }
        }

        if (planetLighting == null)
        {
            planetLighting = new SGT_ColourGradient(false, false);
            planetLighting.AddColourNode(Color.black, 0.45f);
            planetLighting.AddColourNode(Color.white, 0.55f);
        }

        if (atmosphereTwilightColour == null)
        {
            atmosphereTwilightColour = new SGT_ColourGradient(false, true);
            atmosphereTwilightColour.AddAlphaNode(1.0f, 0.45f);
            atmosphereTwilightColour.AddAlphaNode(0.0f, 0.65f);
            atmosphereTwilightColour.AddColourNode(new Color(1.0f, 0.19f, 0.0f, 1.0f), 0.5f);
        }

        if (atmosphereDensityColour == null)
        {
            atmosphereDensityColour = new SGT_ColourGradient(false, true);
            atmosphereDensityColour.AddColourNode(new Color(0.17f, 0.53f, 0.85f), 0.2f);
            atmosphereDensityColour.AddColourNode(Color.white, 0.5f).Locked = true;
            atmosphereDensityColour.AddColourNode(new Color(0.17f, 0.51f, 1.0f), 1.0f);
        }

        if (cloudsLimbColour == null)
        {
            cloudsLimbColour = new SGT_ColourGradient(false, true);
            cloudsLimbColour.AddColourNode(Color.white, 0.5f);
        }

        // Rotate?
        if (Application.isPlaying == true)
        {
            if (cloudsRotationPeriod != 0.0f)
            {
                cloudsGameObject.transform.Rotate(0.0f, SGT_Helper.DegreesPerSecond(cloudsRotationPeriod) * Time.smoothDeltaTime, 0.0f);
            }
        }

        // Update scales
        SGT_Helper.SetLocalScale(surfaceGameObject.transform, surfaceRadius);

        if (atmosphere == true)
        {
            SGT_Helper.SetLocalScale(atmosphereGameObject.transform, AtmosphereRadius);

            // Point atmosphere at camera
            if (planetObserver != null)
            {
                var observerPosition  = planetObserver.transform.position;
                var observerDirection = (observerPosition - gameObject.transform.position).normalized;

                SGT_Helper.SetUp(atmosphereGameObject.transform, observerDirection);
            }
        }

        if (clouds == true)
        {
            SGT_Helper.SetLocalScale(cloudsGameObject.transform, CloudsRadius);

            UpdateCloudsOffset();
        }

        UpdateGradient();
        UpdateTechnique();
        UpdateShader();

        surfaceMesh.GameObject           = surfaceGameObject;
        surfaceMesh.HasMeshRenderers     = true;
        surfaceMesh.MeshRenderersEnabled = true;
        surfaceMesh.MeshCollidersEnabled = true;
        surfaceMesh.SetSharedMaterials(surfaceMaterials);
        surfaceMesh.Update(gameObject.layer, gameObject.tag);

        if (atmosphere == true)
        {
            atmosphereMesh.GameObject          = atmosphereGameObject;
            atmosphereMesh.HasMeshRenderer     = true;
            atmosphereMesh.MeshRendererEnabled = true;
            atmosphereMesh.SharedMaterial      = atmosphereMaterial;
            atmosphereMesh.Update();
        }

        if (clouds == true)
        {
            cloudsMesh.GameObject           = cloudsGameObject;
            cloudsMesh.HasMeshRenderers     = true;
            cloudsMesh.MeshRenderersEnabled = true;
            cloudsMesh.SetSharedMaterials(cloudsMaterials);
            cloudsMesh.Update(gameObject.layer, gameObject.tag);
        }

#if UNITY_EDITOR == true
        surfaceMesh.HideInEditor();
        if (atmosphereMesh != null)
        {
            atmosphereMesh.HideInEditor();
        }
        if (cloudsMesh != null)
        {
            cloudsMesh.HideInEditor();
        }

        SGT_Helper.HideGameObject(atmosphereGameObject);
        SGT_Helper.HideGameObject(cloudsGameObject);
#endif
    }
Exemplo n.º 19
0
    public void LateUpdate()
    {
        if (oblatenessGameObject == null)
        {
            oblatenessGameObject = SGT_Helper.CreateGameObject("Oblateness", gameObject);
        }
        if (surfaceGameObject == null)
        {
            surfaceGameObject = SGT_Helper.CreateGameObject("Surface", oblatenessGameObject);
        }
        if (atmosphereGameObject == null)
        {
            atmosphereGameObject = SGT_Helper.CreateGameObject("Atmosphere", oblatenessGameObject);
        }
        if (atmosphereMesh == null)
        {
            atmosphereMesh = new SGT_Mesh();
        }
        if (starObserver == null)
        {
            starObserver = SGT_Helper.FindCamera();
        }
        if (surfaceTexture == null)
        {
            surfaceTexture = new SGT_SurfaceTexture();
        }
        if (surfaceMultiMesh == null)
        {
            surfaceMultiMesh = new SGT_SurfaceMultiMesh();
        }

        SGT_Helper.SetParent(oblatenessGameObject, gameObject);
        SGT_Helper.SetLayer(oblatenessGameObject, gameObject.layer);
        SGT_Helper.SetTag(oblatenessGameObject, gameObject.tag);

        SGT_Helper.SetParent(surfaceGameObject, oblatenessGameObject);
        SGT_Helper.SetLayer(surfaceGameObject, oblatenessGameObject.layer);
        SGT_Helper.SetTag(surfaceGameObject, oblatenessGameObject.tag);

        SGT_Helper.SetParent(atmosphereGameObject, oblatenessGameObject);
        SGT_Helper.SetLayer(atmosphereGameObject, oblatenessGameObject.layer);
        SGT_Helper.SetTag(atmosphereGameObject, oblatenessGameObject.tag);

        if (atmosphereDensityColour == null)
        {
            atmosphereDensityColour = new SGT_ColourGradient(false, true);
            atmosphereDensityColour.AddColourNode(new Color(1.0f, 1.0f, 0.0f, 1.0f), 0.0f);
            atmosphereDensityColour.AddColourNode(new Color(1.0f, 0.5f, 0.0f, 1.0f), 0.5f).Locked = true;
            atmosphereDensityColour.AddColourNode(new Color(1.0f, 0.0f, 0.0f, 1.0f), 1.0f);
        }

        UpdateTransform();
        UpdateGradient();
        UpdateMaterial();
        UpdateShader();

        surfaceMultiMesh.GameObject           = surfaceGameObject;
        surfaceMultiMesh.HasMeshRenderers     = true;
        surfaceMultiMesh.MeshRenderersEnabled = true;
        surfaceMultiMesh.MeshCollidersEnabled = true;
        surfaceMultiMesh.SetSharedMaterials(surfaceMaterials);
        surfaceMultiMesh.Update(gameObject.layer, gameObject.tag);

        atmosphereMesh.GameObject          = atmosphereGameObject;
        atmosphereMesh.HasMeshRenderer     = true;
        atmosphereMesh.MeshRendererEnabled = true;
        atmosphereMesh.SharedMaterial      = atmosphereMaterial;
        atmosphereMesh.Update();

#if UNITY_EDITOR == true
        SGT_Helper.HideGameObject(oblatenessGameObject);

        atmosphereMesh.HideInEditor();
        surfaceMultiMesh.HideInEditor();
#endif
    }
Exemplo n.º 20
0
	public void SetSurfaceMultiMesh(SGT_SurfaceMultiMesh newSurfaceMultiMesh)
	{
		if (newSurfaceMultiMesh != null)
		{
			if (surfaceMultiMesh == null) surfaceMultiMesh = new SGT_SurfaceMultiMesh();
			
			newSurfaceMultiMesh.CopyMeshesInto(surfaceMultiMesh);
		}
	}