Exemple #1
0
    private void UpdateMaterial()
    {
        var targetStarfieldTechnique = "Variant";

        if (starfieldMaterial != null && starfieldTechnique != targetStarfieldTechnique)
        {
            starfieldMaterial = SGT_Helper.DestroyObject(starfieldMaterial);
        }

        // New material?
        if (starfieldMaterial == null)
        {
            starfieldTechnique = targetStarfieldTechnique;
            starfieldMaterial  = SGT_Helper.CreateMaterial("Hidden/SGT/Starfield/" + starfieldTechnique, starfieldRenderQueue);
        }
        else
        {
            SGT_Helper.SetRenderQueue(starfieldMaterial, starfieldRenderQueue);
        }
    }
Exemple #2
0
    public void LateUpdate()     // TODO: Check mesh size
    {
        if (skysphereGameObject == null)
        {
            skysphereGameObject = SGT_Helper.CreateGameObject("Skysphere", gameObject);
        }
        if (skysphereMesh == null)
        {
            skysphereMesh = new SGT_Mesh();
        }
        if (skysphereObserver == null)
        {
            skysphereObserver = SGT_Helper.FindCamera();
        }

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

        UpdateMaterial();
        UpdateShader();

        if (skysphereObserver != null)
        {
            // Stretch to camera's far view frustum
            SGT_Helper.SetLocalScale(skysphereGameObject.transform, skysphereObserver.far * 0.9f);

            // Centre to main camera
            SGT_Helper.SetPosition(skysphereGameObject.transform, skysphereObserver.transform.position);
        }

        skysphereMesh.GameObject          = skysphereGameObject;
        skysphereMesh.HasMeshRenderer     = true;
        skysphereMesh.MeshRendererEnabled = true;
        skysphereMesh.SharedMaterial      = skysphereMaterial;
        skysphereMesh.Update();

#if UNITY_EDITOR == true
        skysphereMesh.HideInEditor();
#endif
    }
    public void Awake()
    {
        var instances = SGT_Helper.FindAll <T>();

        if (instances.Length > 1)
        {
            Debug.LogWarning("An instance of " + typeof(T).Name + " already exists!");
            Debug.Log("Removing other " + (instances.Length - 1) + " instance(s) of " + typeof(T).Name);

            // Destroy other instances
            foreach (var i in instances)
            {
                if (i != this)
                {
                    SGT_Helper.DestroyObject(i);
                }
            }
        }

        instance = (T)this;
    }
Exemple #4
0
    private void UpdateGradient()
    {
        if (atmosphereLightingColour.Modified == true || atmosphereTwilightColour.Modified == true || atmosphereLimbColour.Modified == true)
        {
            lightingTexture = SGT_Helper.DestroyObject(lightingTexture);
        }

        atmosphereLightingColour.Modified = false;
        atmosphereTwilightColour.Modified = false;
        atmosphereLimbColour.Modified     = false;

        if (lightingTexture == null)
        {
            var lightingColours = atmosphereLightingColour.CalculateColours(0.0f, 1.0f, (int)lutSize);
            var twilightColours = atmosphereTwilightColour.CalculateColours(0.0f, 1.0f, (int)lutSize);
            var limbColours     = atmosphereLimbColour.CalculateColours(0.0f, 1.0f, (int)lutSize);

            lightingTexture = SGT_ColourGradient.AllocateTexture((int)lutSize, (int)lutSize);

            for (var y = 0; y < (int)lutSize; y++)
            {
                var limbPixel = limbColours[y];

                for (var x = 0; x < (int)lutSize; x++)
                {
                    var lightingPixel = lightingColours[x];
                    var twilightPixel = twilightColours[x];
                    var colour        = Color.white;

                    colour = SGT_Helper.AlphaBlend(colour, twilightPixel);
                    colour = SGT_Helper.AlphaBlend(colour, limbPixel);
                    colour = colour * lightingPixel;

                    lightingTexture.SetPixel(x, y, colour);
                }
            }

            lightingTexture.Apply();
        }
    }
    public void Regenerate()
    {
        if (modified == true)
        {
            DestroyGeneratedMeshes();

            if (starVariants.Count > 0)
            {
                SGT_Helper.BeginRandomSeed(starfieldSeed);
                {
                    RecalculateCoords();
                    RecalculateWeights();

                    var remainingStars = starfieldStarCount;
                    var starsPerMesh   = SGT_Helper.MeshVertexLimit / 4;
                    var newMeshes      = new MeshList();

                    while (remainingStars > 0)
                    {
                        var starsInMesh = Mathf.Min(remainingStars, starsPerMesh);

                        newMeshes.Add(GenerateStarMesh(starfieldStarCount - remainingStars, starsInMesh));

                        remainingStars -= starsInMesh;
                    }

                    meshes = newMeshes.ToArray();

                    if (starfieldMultiMesh != null)
                    {
                        starfieldMultiMesh.ReplaceAll(meshes);
                        starfieldMultiMesh.Update();
                    }
                }
                SGT_Helper.EndRandomSeed();
            }

            modified = false;
        }
    }
    public void LateUpdate()
    {
        if (coronaGameObject == null)
        {
            coronaGameObject = SGT_Helper.CreateGameObject("Corona", gameObject);
        }
        if (coronaMesh == null)
        {
            coronaMesh = new SGT_Mesh();
        }
        if (coronaObserver == null)
        {
            coronaObserver = SGT_Helper.FindCamera();
        }

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

        if (coronaAutoRegen == true)
        {
            Regenerate();
        }

        UpdateMaterial();
        UpdateShader();
        UpdateCoronaOffset();

        coronaMesh.GameObject          = coronaGameObject;
        coronaMesh.HasMeshRenderer     = true;
        coronaMesh.MeshRendererEnabled = true;
        coronaMesh.SharedMaterial      = coronaMaterial;
        coronaMesh.SharedMesh          = generatedMesh;
        coronaMesh.Update();

#if UNITY_EDITOR == true
        coronaMesh.HideInEditor();
#endif
    }
Exemple #7
0
    private void UpdateRenderQueues()
    {
        if (materials == null)
        {
            materials = new MaterialList();
        }
        if (renderQueues == null)
        {
            renderQueues = new IntList();
        }

        if (materials.Count != renderQueues.Count)
        {
            materials.Clear();
            renderQueues.Clear();
        }

        for (var i = 0; i < materials.Count; i++)
        {
            SGT_Helper.SetRenderQueue(materials[i], renderQueues[i]);
        }
    }
Exemple #8
0
    public void Update()
    {
        if (orbit == true)
        {
            if (Application.isPlaying == true)
            {
                orbitAngle += SGT_Helper.RadiansPerSecond(orbitPeriod) * Time.deltaTime;
            }

            var targetPosition = SGT_Helper.PolarToCartesian(new Vector2(orbitAngle, 0.0f)) * orbitDistance; targetPosition.x *= (1.0f - orbitOblateness);

            SGT_Helper.SetLocalPosition(transform, targetPosition);
        }

        if (rotation == true)
        {
            if (Application.isPlaying == true)
            {
                transform.Rotate(rotationAxis, SGT_Helper.DegreesPerSecond(rotationPeriod) * Time.deltaTime, Space.Self);
            }
        }
    }
    public void Regenerate()
    {
        if (modified == false)
        {
            CheckForModifications();
        }

        if (modified == true)
        {
            modified = false;

            SGT_Helper.DestroyObjects(generatedMeshes);

            SGT_Helper.BeginRandomSeed(ringSeed);
            {
                var remainingAsteroids = ringAsteroidCount;
                var asteroidsPerMesh   = SGT_Helper.MeshVertexLimit / 4;
                var newMeshes          = new MeshList();

                while (remainingAsteroids > 0)
                {
                    var asteroidsInMesh = Mathf.Min(remainingAsteroids, asteroidsPerMesh);

                    newMeshes.Add(GenerateAsteroidMesh(asteroidsInMesh));

                    remainingAsteroids -= asteroidsInMesh;
                }

                generatedMeshes = newMeshes.ToArray();

                if (ringMultiMesh != null)
                {
                    ringMultiMesh.ReplaceAll(generatedMeshes);
                    ringMultiMesh.Update();
                }
            }
            SGT_Helper.EndRandomSeed();
        }
    }
    private void UpdateShader()
    {
        var uniformScale = UniformScale;

        // Update shader variables
        coronaMaterial.SetTexture("coronaTexture", coronaTexture);
        coronaMaterial.SetVector("coronaPosition", transform.position);
        coronaMaterial.SetColor("coronaColour", SGT_Helper.Premultiply(coronaColour));
        coronaMaterial.SetFloat("coronaFalloff", coronaFalloff * coronaFalloff);

        if (cullNear == true)
        {
            coronaMaterial.SetFloat("cullNearOffset", cullNearOffset * uniformScale);
            coronaMaterial.SetFloat("invCullNearLength", SGT_Helper.Reciprocal(cullNearLength * uniformScale));
        }

        if (meshType == Type.Ring)
        {
            coronaMaterial.SetFloat("coronaRadius", meshRadius * uniformScale);
            coronaMaterial.SetFloat("coronaHeight", meshHeight * uniformScale);
        }
    }
    private void UpdateMaterial()
    {
        var targetTechnique = "Variant";

        if (tiled == false)
        {
            targetTechnique += "Stretched";
        }

        if (shadow == true)
        {
            targetTechnique += "Shadow";
        }

        if (lit == true)
        {
            targetTechnique += "Lit";
        }

        if (scattering == true)
        {
            targetTechnique += "Scattering";
        }

        if (ringMaterial == null || technique != targetTechnique)
        {
            SGT_Helper.DestroyObject(ringMaterial);

            technique    = targetTechnique;
            ringMaterial = SGT_Helper.CreateMaterial("Hidden/SGT/Ring/" + technique, renderQueue);

            ringMesh.SharedMaterial = ringMaterial;
        }
        else
        {
            SGT_Helper.SetRenderQueue(ringMaterial, renderQueue);
        }
    }
    public void LateUpdate()
    {
        if (starfieldGameObject == null)
        {
            starfieldGameObject = SGT_Helper.CreateGameObject("Starfield", gameObject);
        }
        if (starfieldMultiMesh == null)
        {
            starfieldMultiMesh = new SGT_MultiMesh();
        }
        if (starfieldCamera == null)
        {
            StarfieldCamera = SGT_Helper.FindCamera();                                          // NOTE: Assigning property
        }
        SGT_Helper.SetParent(starfieldGameObject, gameObject);
        SGT_Helper.SetLayer(starfieldGameObject, gameObject.layer);
        SGT_Helper.SetTag(starfieldGameObject, gameObject.tag);

        if (starfieldAutoRegen == true)
        {
            Regenerate();
        }

        UpdateMaterial();
        UpdateShader();
        UpdateBackground();

        starfieldMultiMesh.GameObject           = starfieldGameObject;
        starfieldMultiMesh.HasMeshRenderers     = true;
        starfieldMultiMesh.MeshRenderersEnabled = true;
        starfieldMultiMesh.SharedMaterial       = starfieldMaterial;
        starfieldMultiMesh.ReplaceAll(meshes);
        starfieldMultiMesh.Update();

#if UNITY_EDITOR == true
        starfieldMultiMesh.HideInEditor();
#endif
    }
    private void UpdateRingRotations()
    {
        if (generatedRotations != null && generatedRotations.Length > 0)
        {
            ringMesh.Resize(generatedRotations.Length);
            ringMesh.Update();

            for (var i = 0; i < generatedRotations.Length; i++)
            {
                var meshRoot = ringMesh.GetMeshGameObject(i);

                if (meshRoot != null)
                {
                    SGT_Helper.SetLocalRotation(meshRoot.transform, generatedRotations[i]);
                }
            }
        }
        else
        {
            ringMesh.Resize(1);
            ringMesh.Update();
        }
    }
Exemple #14
0
    public void Regenerate()
    {
        if (dustModified == false)
        {
            CheckForModifications();
        }

        if (dustModified == true)
        {
            Validate();

            dustModified = false;

            var particles = new ParticleList();

            SGT_Helper.BeginRandomSeed(dustSeed);
            {
                for (var y = 0; y < dustCount; y++)
                {
                    var p    = new Particle();
                    var posX = Random.Range(0.0f, 1.0f);
                    var posY = Random.Range(0.0f, 1.0f);
                    var posZ = Random.Range(0.0f, 1.0f);

                    p.Position = new Vector3(posX, posY, posZ);
                    p.Size     = particleScale;
                    p.Colour   = Color.white * particleColour;
                    p.Angle    = Random.value * 360.0f;

                    particles.Add(p);
                }
            }
            SGT_Helper.EndRandomSeed();

            CreateMesh(particles);
        }
    }
    public Vector3 ForceAtPoint(Vector3 xyz)
    {
        var newForce = transform.position - xyz;

        if (newForce.sqrMagnitude > 0.0f)
        {
            switch (gravitySourceType)
            {
            case GravityType.Linear:
            {
                newForce = newForce.normalized * SGT_Helper.RemapClamped(gravitySourceRadius, gravitySourceRadius + gravitySourceHeight, newForce.magnitude, 0.0f, gravitySourceForce);
            }
            break;

            case GravityType.Exponential:
            {
                newForce = (newForce / newForce.sqrMagnitude) * gravitySourceForce;
            }
            break;
            }
        }

        return(newForce);
    }
    private void MoveShaders(bool sync)
    {
        var dummyMaterial = new Material(Shader.Find("Diffuse"));

        UnityEditorInternal.InternalEditorUtility.SetupShaderMenu(dummyMaterial);

        var shaders = (Shader[])Resources.FindObjectsOfTypeAll(typeof(Shader));

        dummyMaterial = SGT_Helper.DestroyObject(dummyMaterial);

        var includedShaders = sync ? IncludedShaders() : null;

        foreach (var shader in shaders)
        {
            if (shader.name.Contains("Hidden/SGT/") == true)
            {
                var include = true;

                if (sync == true)
                {
                    var index = includedShaders.IndexOf(shader);

                    if (index != -1)
                    {
                        includedShaders.RemoveAt(index);
                    }
                    else
                    {
                        include = false;
                    }
                }

                MoveShader(shader, include);
            }
        }
    }
Exemple #17
0
    public void FixedUpdate()
    {
        if (thrusterPhysics == true)
        {
            if (thrusterPhysicsRigidbody == null)
            {
                thrusterPhysicsRigidbody = SGT_Helper.GetComponentUpwards <Rigidbody>(gameObject);
            }

            if (thrusterPhysicsRigidbody != null && targetThrusterThrottle != 0.0f)
            {
                var force = -transform.forward * thrusterPhysicsForce * targetThrusterThrottle * Time.fixedDeltaTime;

                switch (thrusterPhysicsForceType)
                {
                case ForceType.AddForce: thrusterPhysicsRigidbody.AddForce(force, thrusterPhysicsForceMode); break;

                case ForceType.AddForceAtPosition: thrusterPhysicsRigidbody.AddForceAtPosition(force, transform.position, thrusterPhysicsForceMode); break;

                case ForceType.AddRelativeForce: thrusterPhysicsRigidbody.AddRelativeForce(force, thrusterPhysicsForceMode); break;
                }
            }
        }
    }
    private void UpdateGradient()
    {
        if (atmosphereDensityColour.Modified == true)
        {
            atmosphereTexture = SGT_Helper.DestroyObject(atmosphereTexture);
        }

        atmosphereDensityColour.Modified = false;

        if (atmosphereTexture == null)
        {
            atmosphereDensityColour.Modified = false;

            SGT_Helper.DestroyObject(atmosphereTexture);
            SGT_Helper.DestroyObject(atmosphereSurfaceTexture);

            var density        = atmosphereDensityColour.CalculateColours(1.0f, 0.5f, (int)lutSize);
            var surfaceDensity = atmosphereDensityColour.CalculateColours(0.0f, 0.5f, (int)lutSize);

            atmosphereTexture        = SGT_ColourGradient.AllocateTexture((int)lutSize);
            atmosphereSurfaceTexture = SGT_ColourGradient.AllocateTexture((int)lutSize);

            for (var x = 0; x < (int)lutSize; x++)
            {
                atmosphereTexture.SetPixel(x, 0, density[x]);
            }

            for (var x = 0; x < (int)lutSize; x++)
            {
                atmosphereSurfaceTexture.SetPixel(x, 0, surfaceDensity[x]);
            }

            atmosphereTexture.Apply();
            atmosphereSurfaceTexture.Apply();
        }
    }
Exemple #19
0
    private void Validate()
    {
        if (dust == null)
        {
            dust = SGT_Helper.CreateGameObject("Dust", this);
        }
        if (dustMesh == null)
        {
            dustMesh = new SGT_Mesh();
        }
        if (dustMaterial == null)
        {
            dustMaterial = SGT_Helper.CreateMaterial("Hidden/SGT/Dust/" + dustTechnique);
        }

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

        var finalColour = SGT_Helper.Premultiply(particleColour);

        dustMaterial.SetTexture("dustTexture", particleTexture);
        dustMaterial.SetFloat("dustRadius", dustRadius);
        dustMaterial.SetColor("particleColour", finalColour);
        dustMaterial.SetFloat("particleFadeInDistance", dustRadius / particleFadeInDistance);
        dustMaterial.SetFloat("particleFadeOutDistance", dustRadius / particleFadeOutDistance);

        SGT_Helper.SetRenderQueue(dustMaterial, dustRenderQueue);

        dustMesh.GameObject          = dust;
        dustMesh.HasMeshRenderer     = true;
        dustMesh.MeshRendererEnabled = SGT_Helper.IsBlack(finalColour) == false;
        dustMesh.SharedMesh          = generatedMesh;
        dustMesh.SharedMaterial      = dustMaterial;
        dustMesh.Update();
    }
    private void UpdateGradient()
    {
        // See if any gradient has been modified
        if (planetLighting.Modified == true)
        {
            planetLighting.Modified = false;

            surfaceLightingTexture   = SGT_Helper.DestroyObject(surfaceLightingTexture);
            atmosphereTexture        = SGT_Helper.DestroyObject(atmosphereTexture);
            atmosphereSurfaceTexture = SGT_Helper.DestroyObject(atmosphereSurfaceTexture);
            cloudsLightingTexture    = SGT_Helper.DestroyObject(cloudsLightingTexture);
        }

        if (atmosphereTwilightColour.Modified == true)
        {
            atmosphereTwilightColour.Modified = false;

            atmosphereTexture        = SGT_Helper.DestroyObject(atmosphereTexture);
            atmosphereSurfaceTexture = SGT_Helper.DestroyObject(atmosphereSurfaceTexture);
            cloudsLightingTexture    = SGT_Helper.DestroyObject(cloudsLightingTexture);
        }

        if (atmosphereDensityColour.Modified == true)
        {
            atmosphereDensityColour.Modified = false;

            atmosphereTexture        = SGT_Helper.DestroyObject(atmosphereTexture);
            atmosphereSurfaceTexture = SGT_Helper.DestroyObject(atmosphereSurfaceTexture);
        }

        if (cloudsLimbColour.Modified == true)
        {
            cloudsLimbColour.Modified = false;

            cloudsLightingTexture = SGT_Helper.DestroyObject(cloudsLightingTexture);
        }

        // Build colour arrays
        var lighting = surfaceLightingTexture == null || atmosphereTexture == null || atmosphereSurfaceTexture == null || cloudsLightingTexture == null?planetLighting.CalculateColours(0.0f, 1.0f, (int)planetLutSize) : null;

        var twilight = atmosphereTexture == null || atmosphereSurfaceTexture == null?atmosphereTwilightColour.CalculateColours(0.0f, 1.0f, (int)planetLutSize) : null;

        var cloudsTwilight = cloudsLightingTexture == null?atmosphereTwilightColour.CalculateColours(cloudsTwilightOffset, cloudsTwilightOffset + 1.0f, (int)planetLutSize) : null;

        var density = atmosphereTexture == null?atmosphereDensityColour.CalculateColours(1.0f, 0.5f, (int)planetLutSize) : null;

        var surfaceDensity = atmosphereSurfaceTexture == null?atmosphereDensityColour.CalculateColours(0.0f, 0.5f, (int)planetLutSize) : null;

        var cloudsLimb = cloudsLightingTexture == null?cloudsLimbColour.CalculateColours(0.0f, 1.0f, (int)planetLutSize) : null;

        // Rebuild textures
        if (surfaceLightingTexture == null)
        {
            updateShader          |= ShaderFlags.SurfaceTextureLighting;
            surfaceLightingTexture = SGT_ColourGradient.AllocateTexture((int)planetLutSize);

            for (var x = 0; x < (int)planetLutSize; x++)
            {
                surfaceLightingTexture.SetPixel(x, 0, lighting[x]);
            }

            surfaceLightingTexture.anisoLevel = 8;
            surfaceLightingTexture.filterMode = FilterMode.Trilinear;
            surfaceLightingTexture.Apply();
        }

        if (atmosphereTexture == null)
        {
            updateShader     |= ShaderFlags.AtmosphereTexture;
            atmosphereTexture = SGT_ColourGradient.AllocateTexture((int)planetLutSize, (int)planetLutSize);

            for (var y = 0; y < (int)planetLutSize; y++)
            {
                var densityColour = density[y];

                for (var x = 0; x < (int)planetLutSize; x++)
                {
                    var lightingColour = lighting[x];
                    //var twilightColour   = twilight[x];
                    lightingColour.a = Mathf.Lerp(atmosphereNightOpacity, 1.0f, lightingColour.grayscale);
                    //var colour           = SGT_Helper.AlphaBlend(densityColour, twilightColour) * lightingColour;

                    var twilightColour = SGT_Helper.AlphaBlend(Color.white, twilight[x]);
                    var colour         = SGT_Helper.BlendRGB(densityColour * lightingColour, twilightColour, SGT_Helper.ChannelBlendMode.Multiply);

                    atmosphereTexture.SetPixel(x, y, SGT_Helper.PreventZeroRGB(colour));
                    atmosphereTexture.filterMode = FilterMode.Trilinear;
                }
            }

            atmosphereTexture.Apply();
        }

        if (atmosphereSurfaceTexture == null)
        {
            updateShader            |= ShaderFlags.SurfaceTextureAtmosphere;
            atmosphereSurfaceTexture = SGT_ColourGradient.AllocateTexture((int)planetLutSize, (int)planetLutSize);

            for (var y = 0; y < (int)planetLutSize; y++)
            {
                var densityColour = surfaceDensity[y];

                for (var x = 0; x < (int)planetLutSize; x++)
                {
                    var lightingColour = lighting[x];
                    var twilightColour = twilight[x];
                    lightingColour.a = Mathf.Lerp(atmosphereNightOpacity, 1.0f, lightingColour.grayscale);
                    var colour = SGT_Helper.AlphaBlend(densityColour, twilightColour) * lightingColour;

                    atmosphereSurfaceTexture.SetPixel(x, y, SGT_Helper.PreventZeroRGB(colour));
                    atmosphereSurfaceTexture.filterMode = FilterMode.Trilinear;
                }
            }

            atmosphereSurfaceTexture.Apply();
        }

        if (cloudsLightingTexture == null)
        {
            updateShader         |= ShaderFlags.CloudsTextureLighting;
            cloudsLightingTexture = SGT_ColourGradient.AllocateTexture((int)planetLutSize, (int)planetLutSize);

            for (var y = 0; y < (int)planetLutSize; y++)
            {
                var limb = cloudsLimb[y];

                for (var x = 0; x < (int)planetLutSize; x++)
                {
                    var colour = lighting[x];

                    if (atmosphere == true)
                    {
                        colour *= SGT_Helper.AlphaBlend(limb, cloudsTwilight[x]);
                    }
                    else
                    {
                        colour *= limb;
                    }

                    cloudsLightingTexture.SetPixel(x, y, colour);
                    cloudsLightingTexture.filterMode = FilterMode.Trilinear;
                }
            }

            cloudsLightingTexture.Apply();
        }
    }
    private void UpdateShader()
    {
        var starDirection       = planetLightSource != null ? (planetLightSource.transform.position - transform.position).normalized : Vector3.up;
        var lightSourcePosition = planetLightSource != null ? planetLightSource.transform.position : Vector3.zero;
        var transformPosition   = transform.position;
        var uniformScale        = UniformScale;
        var observerPosition    = planetObserver != null ? planetObserver.transform.position : Vector3.zero;
        var distance            = (observerPosition - transformPosition).magnitude;

        float maxSurfaceDepth, maxAtmosphereDepth;

        SGT_Helper.CalculateHorizonAtmosphereDepth(surfaceRadius * uniformScale, AtmosphereRadius * uniformScale, distance, out maxSurfaceDepth, out maxAtmosphereDepth);

        // Common shader variables
        SGT_ShaderHelper.SetVector(surfaceMaterials, "centrePosition", transformPosition);
        SGT_ShaderHelper.SetVector(surfaceMaterials, "starDirection", starDirection);

        if (surfaceTextureNormal != null)
        {
            var starDirectionM = surfaceGameObject.transform.worldToLocalMatrix.MultiplyVector(starDirection).normalized;

            SGT_ShaderHelper.SetVector(surfaceMaterials, "starDirectionM", starDirectionM);
        }

        if (atmosphereMaterial != null)
        {
            var altitudeUnclamped = distance - surfaceRadius * uniformScale;
            var altitude          = Mathf.Clamp(altitudeUnclamped, 0.0f, atmosphereHeight * uniformScale);
            var atmoAlt           = SGT_Helper.RemapClamped(atmosphereHeight * uniformScale * atmosphereSkyAltitude, atmosphereHeight * uniformScale, altitude, 0.0f, 1.0f);

            SGT_ShaderHelper.SetFloat(surfaceMaterials, "maxDepth", maxSurfaceDepth * (1.0f - atmosphereFog));             // FOG

            atmosphereMaterial.SetFloat("maxDepth", maxAtmosphereDepth);
            atmosphereMaterial.SetVector("centrePosition", transformPosition);
            atmosphereMaterial.SetVector("starDirection", starDirection);
            atmosphereMaterial.SetFloat("atmosphereFalloff", Mathf.SmoothStep(atmosphereFalloffInside, atmosphereFalloffOutside, atmoAlt));

            if (atmosphereScattering == true)
            {
                SGT_ShaderHelper.SetVector(surfaceMaterials, "starPosition", lightSourcePosition);

                atmosphereMaterial.SetVector("starPosition", lightSourcePosition);
            }
        }

        if (cloudsMaterials != null)
        {
            SGT_ShaderHelper.SetVector(cloudsMaterials, "centrePosition", transformPosition);
            SGT_ShaderHelper.SetVector(cloudsMaterials, "starDirection", starDirection);
        }

        if (shadow == true)
        {
            var shadowMatrix = Matrix4x4.identity;

            switch (shadowCasterType)
            {
            case SGT_ShadowOccluder.Planet:
            {
                if (shadowCasterGameObject != null)
                {
                    shadowMatrix = SGT_Helper.CalculateSphereShadowMatrix(ShadowCasterRadiusOuter, shadowCasterGameObject.transform.position, lightSourcePosition, transformPosition, uniformScale);
                }
            }
            break;

            case SGT_ShadowOccluder.Ring:
            {
                shadowMatrix = SGT_Helper.CalculateCircleShadowMatrix(ShadowCasterRadiusOuter, transform.position, transform.up, lightSourcePosition, transformPosition, uniformScale);
            }
            break;
            }

            SGT_ShaderHelper.SetMatrix(surfaceMaterials, "shadowMatrix", shadowMatrix);

            if (atmosphereMaterial != null)
            {
                atmosphereMaterial.SetMatrix("shadowMatrix", shadowMatrix);
            }
            if (cloudsMaterials != null)
            {
                SGT_ShaderHelper.SetMatrix(cloudsMaterials, "shadowMatrix", shadowMatrix);
            }
        }

        // Uncommon shader variables
        if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.ShadowSettings) == true)
        {
            var cutoff       = ShadowCasterRadiusInner / ShadowCasterRadiusOuter;
            var invCutoff    = SGT_Helper.Reciprocal(1.0f - cutoff);
            var shadowValues = new Vector3(cutoff, invCutoff, 1.0f + ShadowCasterRadiusOuter);

            SGT_ShaderHelper.SetVector(surfaceMaterials, "shadowValues", shadowValues);

            if (atmosphereMaterial != null)
            {
                atmosphereMaterial.SetVector("shadowValues", shadowValues);
            }
            if (cloudsMaterials != null)
            {
                SGT_ShaderHelper.SetVector(cloudsMaterials, "shadowValues", shadowValues);
            }
        }

        if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.SurfaceTextureDay) == true)
        {
            for (var i = 0; i < surfaceMaterials.Length; i++)
            {
                surfaceMaterials[i].SetTexture("dayTexture", surfaceTextureDay.GetTexture((CubemapFace)i));
            }
        }

        if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.SurfaceTextureNight) == true)
        {
            for (var i = 0; i < surfaceMaterials.Length; i++)
            {
                surfaceMaterials[i].SetTexture("nightTexture", surfaceTextureNight.GetTexture((CubemapFace)i));
            }
        }

        if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.SurfaceTextureNormal) == true)
        {
            for (var i = 0; i < surfaceMaterials.Length; i++)
            {
                surfaceMaterials[i].SetTexture("normalTexture", surfaceTextureNormal.GetTexture((CubemapFace)i));
            }
        }

        if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.SurfaceTextureSpecular) == true)
        {
            for (var i = 0; i < surfaceMaterials.Length; i++)
            {
                surfaceMaterials[i].SetTexture("specularTexture", surfaceTextureSpecular.GetTexture((CubemapFace)i));
            }
        }

        if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.SurfaceSpecularSettings) == true)
        {
            SGT_ShaderHelper.SetFloat(surfaceMaterials, "specularPower", surfaceSpecularPower * surfaceSpecularPower);
        }

        if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.SurfaceTextureDetail) == true)
        {
            SGT_ShaderHelper.SetTexture(surfaceMaterials, "detailTexture", surfaceTextureDetail);
            SGT_ShaderHelper.SetFloat(surfaceMaterials, "detailRepeat", surfaceDetailRepeat);
        }

        if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.SurfaceTextureLighting) == true)
        {
            SGT_ShaderHelper.SetTexture(surfaceMaterials, "lightingTexture", surfaceLightingTexture);
        }

        if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.SurfaceTextureShadow) == true)
        {
            SGT_ShaderHelper.SetTexture(surfaceMaterials, "shadowTexture", shadowTextureSurface);
        }

        if (atmosphereMaterial != null)
        {
            SGT_ShaderHelper.SetFloat(surfaceMaterials, "atmosphereRadius", AtmosphereRadius * uniformScale);
            SGT_ShaderHelper.SetFloat(surfaceMaterials, "atmosphereFalloff", atmosphereFalloffSurface);
            SGT_ShaderHelper.SetFloat(surfaceMaterials, "surfaceRadius", surfaceRadius * uniformScale);
            SGT_ShaderHelper.SetFloat(surfaceMaterials, "atmosphereHeight", atmosphereHeight * uniformScale);

            if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.SurfaceTextureAtmosphere) == true)
            {
                SGT_ShaderHelper.SetTexture(surfaceMaterials, "atmosphereTexture", atmosphereSurfaceTexture);
            }

            if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.AtmosphereTexture) == true)
            {
                atmosphereMaterial.SetTexture("atmosphereTexture", atmosphereTexture);
            }

            if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.AtmosphereTextureShadow) == true)
            {
                atmosphereMaterial.SetTexture("shadowTexture", shadowTextureAtmosphere);
            }

            if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.ScatteringSettings) == true)
            {
                var mie  = -(1.0f - 1.0f / Mathf.Pow(10.0f, atmosphereScatteringMie * 5.0f));
                var mie4 = new Vector4(mie * 2.0f, 1.0f - mie * mie, 1.0f + mie * mie, 1.5f);
                var ray2 = new Vector2(0.0f, atmosphereScatteringRayleigh);

                SGT_ShaderHelper.SetVector(surfaceMaterials, "rayleighValues", ray2);

                atmosphereMaterial.SetVector("mieValues", mie4);
                atmosphereMaterial.SetVector("rayleighValues", ray2);
            }
        }

        if (cloudsMaterials != null)
        {
            if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.CloudsTextureShadow) == true)
            {
                SGT_ShaderHelper.SetTexture(cloudsMaterials, "shadowTexture", shadowTextureClouds);
            }

            if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.CloudsTexture) == true)
            {
                for (var i = 0; i < cloudsMaterials.Length; i++)
                {
                    cloudsMaterials[i].SetTexture("cloudsTexture", cloudsTexture.GetTexture((CubemapFace)i));
                }
            }

            if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.CloudsTextureLighting) == true)
            {
                SGT_ShaderHelper.SetTexture(cloudsMaterials, "lightingTexture", cloudsLightingTexture);
            }

            if (SGT_Helper.FlagIsSet(updateShader, ShaderFlags.CloudsFalloffSettings) == true)
            {
                SGT_ShaderHelper.SetFloat(cloudsMaterials, "falloff", cloudsFalloff * cloudsFalloff);
            }
        }

        updateShader = 0;
    }
Exemple #22
0
    public void LateUpdate()
    {
        if (oblatenessGameObject == null)
        {
            oblatenessGameObject = SGT_Helper.CreateGameObject("Oblateness", gameObject);
        }
        if (atmosphereGameObject == null)
        {
            atmosphereGameObject = SGT_Helper.CreateGameObject("Atmosphere", oblatenessGameObject);
        }
        if (atmosphereMesh == null)
        {
            atmosphereMesh = new SGT_Mesh();
        }
        if (gasGiantLightSource == null)
        {
            gasGiantLightSource = SGT_LightSource.Find();
        }
        if (gasGiantObserver == null)
        {
            gasGiantObserver = SGT_Helper.FindCamera();
        }

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

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

        if (atmosphereLightingColour == null)
        {
            atmosphereLightingColour = new SGT_ColourGradient(false, false);
            atmosphereLightingColour.AddColourNode(Color.black, 0.4f);
            atmosphereLightingColour.AddColourNode(Color.white, 0.7f);
        }

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

        if (atmosphereLimbColour == null)
        {
            atmosphereLimbColour = new SGT_ColourGradient(false, true);
            atmosphereLimbColour.AddAlphaNode(0.0f, 0.75f);
            atmosphereLimbColour.AddAlphaNode(1.0f, 1.0f);
            atmosphereLimbColour.AddColourNode(Color.blue, 0.4f);
        }

        if (shadowAutoUpdate == true)
        {
            switch (shadowType)
            {
            case SGT_ShadowOccluder.Ring:
            {
                var fill = new SGT_FillRingDimensions();

                SendMessage("FillRingDimensions", fill, SendMessageOptions.DontRequireReceiver);

                shadowRadius = fill.Radius;
                shadowWidth  = fill.Width;
            }
            break;

            case SGT_ShadowOccluder.Planet:
            {
                if (shadowGameObject != null)
                {
                    var fill = new SGT_FillFloat();

                    shadowGameObject.SendMessage("FillShadowRadius", fill, SendMessageOptions.DontRequireReceiver);

                    shadowRadius = fill.Float;
                }
            }
            break;
            }
        }

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

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

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

        atmosphereMesh.HideInEditor();
#endif
    }
Exemple #23
0
    private void CreateMesh(ParticleList particles)
    {
        SGT_Helper.DestroyObject(generatedMesh);

        particleCount = particles.Count;
        generatedMesh = new Mesh();

        var positions = new Vector3[particleCount * 4];
        var indices   = new int[particleCount * 6];
        var uv0s      = new Vector2[particleCount * 4];
        var uv1s      = new Vector2[particleCount * 4];
        var normals   = new Vector3[particleCount * 4];
        var colours   = new Color[particleCount * 4];

        for (var i = 0; i < particleCount; i++)
        {
            var particle = particles[i];

            var i0 = i * 6;
            var i1 = i0 + 1;
            var i2 = i1 + 1;
            var i3 = i2 + 1;
            var i4 = i3 + 1;
            var i5 = i4 + 1;

            var v0 = i * 4;
            var v1 = v0 + 1;
            var v2 = v1 + 1;
            var v3 = v2 + 1;

            // Index data
            indices[i0] = v0;
            indices[i1] = v1;
            indices[i2] = v2;
            indices[i3] = v3;
            indices[i4] = v2;
            indices[i5] = v1;

            var right = SGT_Helper.Rotate(Vector2.right * SGT_Helper.InscribedBox, particle.Angle);
            var up    = SGT_Helper.Rotate(Vector2.up * SGT_Helper.InscribedBox, particle.Angle);
            var uv1   = new Vector2(particle.Size * 0.5f, 0.0f);

            // Write star values into vertex data
            positions[v0] = particle.Position;
            positions[v1] = particle.Position;
            positions[v2] = particle.Position;
            positions[v3] = particle.Position;

            normals[v0] = SGT_Helper.NewVector3(-right + up, 0.0f);
            normals[v1] = SGT_Helper.NewVector3(right + up, 0.0f);
            normals[v2] = SGT_Helper.NewVector3(-right - up, 0.0f);
            normals[v3] = SGT_Helper.NewVector3(right - up, 0.0f);

            colours[v0] = particle.Colour;
            colours[v1] = particle.Colour;
            colours[v2] = particle.Colour;
            colours[v3] = particle.Colour;

            uv0s[v0] = new Vector2(0.0f, 1.0f);
            uv0s[v1] = new Vector2(1.0f, 1.0f);
            uv0s[v2] = new Vector2(0.0f, 0.0f);
            uv0s[v3] = new Vector2(1.0f, 0.0f);

            uv1s[v0] = uv1;
            uv1s[v1] = uv1;
            uv1s[v2] = uv1;
            uv1s[v3] = uv1;
        }

        generatedMesh.name      = "Dust";
        generatedMesh.vertices  = positions;
        generatedMesh.normals   = normals;
        generatedMesh.colors    = colours;
        generatedMesh.uv        = uv0s;
        generatedMesh.uv2       = uv1s;
        generatedMesh.triangles = indices;
        generatedMesh.bounds    = new Bounds(Vector3.zero, new Vector3(100000000.0f, 100000000.0f, 100000000.0f));         // NOTE: using infinity here causes an error

        dustMesh.SharedMesh = generatedMesh;
        dustMesh.Update();
    }
    public override void OnInspector()
    {
        var mbs = (SGT_MonoBehaviour[])Object.FindObjectsOfType(typeof(SGT_MonoBehaviour));

        SGT_EditorGUI.HelpBox("This tool allows you to cleanup any hidden game objects that have been orphaned by SGT.", MessageType.Info);
        SGT_EditorGUI.HelpBox("If a game object is still visible after showing it, then it's still in use by SGT.", MessageType.Info);

        SGT_EditorGUI.Separator();

        if (SGT_EditorGUI.Button("Show All Hidden Game Objects") == true)
        {
            foreach (var mb in mbs)
            {
                if (mb != null)
                {
                    foreach (Transform t in mb.transform)
                    {
                        if (Check(t) == true)
                        {
                            t.hideFlags = 0;
                        }
                    }
                }
            }

            Repaint();
        }

        SGT_EditorGUI.Separator();

        if (SGT_EditorGUI.Button("Delete All Hidden Game Objects") == true)
        {
            foreach (var mb in mbs)
            {
                if (mb != null)
                {
                    foreach (Transform t in mb.transform)
                    {
                        if (Check(t) == true)
                        {
                            SGT_Helper.DestroyGameObject(t);
                        }
                    }
                }
            }

            Repaint();
        }

        SGT_EditorGUI.Separator();

        var totalHiddenCount = 0;

        for (var i = 0; i < mbs.Length; i++)
        {
            var mb = mbs[i];

            if (mb != null)
            {
                var hiddenCount = 0;

                foreach (Transform t in mb.transform)
                {
                    if (Check(t) == true)
                    {
                        hiddenCount      += 1;
                        totalHiddenCount += 1;
                    }
                }

                if (hiddenCount > 0)
                {
                    SGT_EditorGUI.BeginGroup("SGT Component (" + mb.name + ")");
                    {
                        for (var j = 0; j < mb.transform.childCount; j++)
                        {
                            var t = mb.transform.GetChild(j);

                            if (Check(t) == true)
                            {
                                if (SGT_EditorGUI.TextWithButton("Hidden GameObject (" + t.name + ")", "Show", 50.0f) == true)
                                {
                                    t.hideFlags = 0;

                                    GUI.changed = true;
                                    EditorUtility.SetDirty(mb);
                                    EditorApplication.RepaintHierarchyWindow();
                                    Repaint();
                                }
                            }
                        }
                    }
                    SGT_EditorGUI.EndGroup();

                    SGT_EditorGUI.Separator();
                }
            }
        }

        if (totalHiddenCount == 0)
        {
            SGT_EditorGUI.HelpBox("This scene contains no hidden children of SGT components.", MessageType.Info);
        }
    }
Exemple #25
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);
            }
        }
    }
    public void Regenerate()
    {
        if (modified == false)
        {
            CheckForModifications();
        }

        if (modified == true)
        {
            modified = false;

            SGT_Helper.BeginRandomSeed(meshSeed);
            {
                Quaternion[] rotations = null;

                switch (meshAlignment)
                {
                case Alignment.Billboard:
                {
                    rotations    = new Quaternion[1];
                    rotations[0] = Quaternion.identity;
                }
                break;

                case Alignment.AxisAlined:
                {
                    coronaGameObject.transform.localRotation = Quaternion.identity;

                    rotations    = new Quaternion[3];
                    rotations[0] = Quaternion.LookRotation(Vector3.right);
                    rotations[1] = Quaternion.LookRotation(Vector3.up);
                    rotations[2] = Quaternion.LookRotation(Vector3.forward);
                }
                break;

                case Alignment.Random:
                {
                    coronaGameObject.transform.localRotation = Quaternion.identity;

                    rotations = new Quaternion[meshPlaneCount];

                    for (var i = 0; i < rotations.Length; i++)
                    {
                        rotations[i] = Random.rotation;
                    }
                }
                break;
                }

                // Replace mesh
                SGT_Helper.DestroyObject(generatedMesh);

                switch (meshType)
                {
                case Type.Plane:
                {
                    GeneratePlane(rotations);
                }
                break;

                case Type.Ring:
                {
                    GenerateRing(rotations);
                }
                break;
                }

                if (coronaMesh != null)
                {
                    coronaMesh.SharedMesh = generatedMesh;
                }
            }
            SGT_Helper.EndRandomSeed();

            UpdateCoronaOffset();
        }
    }
    private void GenerateRing(Quaternion[] rotations)
    {
        var positions       = new Vector3[rotations.Length * (meshSegments * 2 + 2)];
        var indices         = new int[rotations.Length * meshSegments * 6];
        var uv0             = new Vector2[rotations.Length * (meshSegments * 2 + 2)];
        var vStep           = 1.0f / meshSegments;
        var angleStep       = (Mathf.PI * 2.0f) / meshSegments;
        var meshRadiusOuter = (meshRadius + meshHeight) * SGT_Helper.PolygonOuterBoundScale(angleStep);

        // Write index data
        {
            var indicesIndex = 0;
            var vertexIndex  = 0;

            for (var r = 0; r < rotations.Length; r++)
            {
                for (var i = 0; i < meshSegments; i++)
                {
                    var vertexIndexNext = vertexIndex + 2;

                    // Index data
                    indices[indicesIndex + 0] = vertexIndex + 0;
                    indices[indicesIndex + 1] = vertexIndex + 1;
                    indices[indicesIndex + 2] = vertexIndexNext + 1;
                    indices[indicesIndex + 3] = vertexIndex + 0;
                    indices[indicesIndex + 4] = vertexIndexNext + 1;
                    indices[indicesIndex + 5] = vertexIndexNext + 0;

                    indicesIndex += 6;
                    vertexIndex  += 2;
                }

                vertexIndex += 2;
            }
        }

        // Write vertex data
        {
            var vertexIndex = 0;

            for (var r = 0; r < rotations.Length; r++)
            {
                var rot = rotations[r];

                for (var i = 0; i < meshSegments + 1; i++)
                {
                    // Calculate stuff
                    var angle     = angleStep * i;
                    var direction = new Vector3(Mathf.Sin(angle), Mathf.Cos(angle), 0.0f);

                    // Write vertex data
                    positions[vertexIndex + 0] = rot * (direction * meshRadius);
                    positions[vertexIndex + 1] = rot * (direction * meshRadiusOuter);

                    uv0[vertexIndex + 0] = new Vector2(vStep * i, 0.0f);
                    uv0[vertexIndex + 1] = new Vector2(vStep * i, 1.0f);

                    vertexIndex += 2;
                }
            }
        }

        generatedMesh           = new Mesh();
        generatedMesh.vertices  = positions;
        generatedMesh.triangles = indices;
        generatedMesh.uv        = uv0;
        generatedMesh.RecalculateBounds();
        generatedMesh.RecalculateNormals();
    }
Exemple #28
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
    }
Exemple #29
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
    }
Exemple #30
0
    private void UpdateShader()
    {
        var position            = transform.position;
        var lightSourcePosition = SGT_Helper.GetPosition(ringLightSource);
        var uniformScale        = UniformScale;
        var starPositionRaw     = ringGameObject.transform.worldToLocalMatrix.MultiplyPoint(lightSourcePosition);
        var oldCameraRotation   = ringCameraRotation; if (ringCamera != null)
        {
            ringCameraRotation = ringCamera.transform.rotation;
        }
        var cameraRotationDelta = Quaternion.Inverse(oldCameraRotation) * ringCameraRotation;

        ringCameraRoll = SGT_Helper.Wrap(ringCameraRoll - cameraRotationDelta.eulerAngles.z, 0.0f, 360.0f);

        var roll = Quaternion.Euler(new Vector3(0.0f, 0.0f, ringCameraRoll));

        ringMaterial.SetTexture("dayTexture", asteroidTextureDay);
        ringMaterial.SetTexture("nightTexture", asteroidTextureNight);
        ringMaterial.SetTexture("bumpTexture", asteroidTextureHeight);
        ringMaterial.SetVector("starPositionRaw", SGT_Helper.NewVector4(starPositionRaw, 1.0f));
        ringMaterial.SetVector("centrePosition", position);
        ringMaterial.SetFloat("ringHeight", ringHeight);
        ringMaterial.SetMatrix("cameraRoll", SGT_MatrixHelper.Rotation(roll));

        if (shadow == true)
        {
            var shadowRatio = 0.0f;
            var shadowScale = 0.0f;

            if (shadowWidth > 0.0f)
            {
                shadowRatio = ShadowInnerRadius / ShadowOuterRadius;
                shadowScale = 1.0f / (1.0f - shadowRatio);
            }

            ringMaterial.SetColor("umbraColour", shadowUmbraColour);
            ringMaterial.SetColor("penumbraColour", shadowPenumbraColour);
            ringMaterial.SetFloat("shadowRatio", shadowRatio);
            ringMaterial.SetFloat("shadowScale", shadowScale);

            if (ShadowInnerRadius > 0.0f)
            {
                var direction = (position - lightSourcePosition).normalized;
                var r         = Quaternion.FromToRotation(direction, Vector3.forward);
                var s         = SGT_Helper.NewVector3(1.0f / (ShadowOuterRadius * uniformScale));

                var shadowT = SGT_MatrixHelper.Translation(-position);
                var shadowR = SGT_MatrixHelper.Rotation(r);
                var shadowS = SGT_MatrixHelper.Scaling(s);

                var shadowMatrix = shadowS * shadowR * shadowT;

                ringMaterial.SetMatrix("shadowMatrix", shadowMatrix);
            }
        }

        if (spin == true)
        {
            ringMaterial.SetFloat("spinRateMax", spinRateMax);
        }
    }