Esempio n. 1
0
    private void UpdateShader()
    {
        var uniformScale        = UniformScale;
        var invScale            = SGT_MatrixHelper.Scaling(SGT_Helper.Reciprocal(new Vector3(1.0f, 1.0f - gasGiantOblateness, 1.0f)));
        var rot                 = SGT_MatrixHelper.Rotation(transform.rotation);
        var invRot              = SGT_MatrixHelper.Rotation(Quaternion.Inverse(transform.rotation));
        var ellipsoid2sphere    = rot * invScale * invRot;
        var sphere2ellipsoid    = ellipsoid2sphere.inverse;
        var lightSourcePosition = SGT_Helper.GetPosition(gasGiantLightSource);

        maxDepth = (gasGiantEquatorialRadius * uniformScale) / (atmosphereDensity * atmosphereDensity);

        // Update shader variables
        atmosphereMaterial.SetTexture("dayTexture", atmosphereDayTexture);
        atmosphereMaterial.SetTexture("nightTexture", atmosphereNightTexture);
        atmosphereMaterial.SetTexture("lightingTexture", lightingTexture);
        atmosphereMaterial.SetMatrix("ellipsoid2sphere", ellipsoid2sphere);
        atmosphereMaterial.SetVector("centrePosition", transform.position);
        atmosphereMaterial.SetVector("starDirection", GasGiantLightSourceDirection);
        atmosphereMaterial.SetFloat("falloff", atmosphereDensityFalloff * atmosphereDensityFalloff);
        atmosphereMaterial.SetFloat("maxDepth", maxDepth);

        if (shadow == true)
        {
            var cutoff       = ShadowCasterRadiusInner / ShadowCasterRadiusOuter;
            var invCutoff    = SGT_Helper.Reciprocal(1.0f - cutoff);
            var shadowValues = new Vector3(cutoff, invCutoff, 1.0f + ShadowCasterRadiusOuter);
            var shadowMatrix = Matrix4x4.identity;

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

                shadowMatrix *= SGT_MatrixHelper.Translation(transform.position);                         // This is to remove one instruction in the VS
                shadowMatrix *= sphere2ellipsoid;
            }
            break;

            case SGT_ShadowOccluder.Planet:
            {
                if (shadowGameObject != null)
                {
                    shadowMatrix = SGT_Helper.CalculateSphereShadowMatrix(ShadowCasterRadiusOuter, shadowGameObject.transform.position, lightSourcePosition, transform.position, uniformScale);

                    shadowMatrix *= SGT_MatrixHelper.Translation(transform.position);                             // This is to remove one instruction in the VS
                }
            }
            break;
            }

            atmosphereMaterial.SetTexture("shadowTexture", shadowTexture);
            atmosphereMaterial.SetMatrix("shadowMatrix", shadowMatrix);
            atmosphereMaterial.SetVector("shadowValues", shadowValues);
        }
    }
    private void UpdateShader()
    {
        var position            = transform.position;
        var lightSourcePosition = SGT_Helper.GetPosition(ringLightSource);
        var uniformScale        = UniformScale;
        var starPositionRaw     = ringGameObject.transform.worldToLocalMatrix.MultiplyPoint(lightSourcePosition);

        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);

        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);
        }
    }
Esempio n. 3
0
    public void LateUpdate()
    {
        if (debrisCentre == null)
        {
            debrisCentre = SGT_Helper.FindCameraGameObject();
        }

        var newCentre = SGT_Helper.GetPosition(debrisCentre);

        velocity = transform.InverseTransformDirection((newCentre - oldCentre).normalized);

        UpdateDebris();

        oldCentre = newCentre;
    }
Esempio n. 4
0
    private void Validate()
    {
        if (nebula == null)
        {
            nebula = SGT_Helper.CreateGameObject("Nebula", this);
        }
        if (nebulaMesh == null)
        {
            nebulaMesh = new SGT_Mesh();
        }
        if (nebulaMaterial == null)
        {
            nebulaMaterial = SGT_Helper.CreateMaterial("Hidden/SGT/Nebula/" + nebulaTechnique);
        }
        if (nebulaCamera == null)
        {
            NebulaCamera = SGT_Helper.FindCamera();                                   // NOTE: Assigning property
        }
        SGT_Helper.SetParent(nebula, gameObject);
        SGT_Helper.SetLayer(nebula, gameObject.layer);
        SGT_Helper.SetTag(nebula, gameObject.tag);

        var sideOrTop         = Mathf.Abs(Vector3.Dot((SGT_Helper.GetPosition(nebulaCamera) - transform.position).normalized, transform.up));
        var finalColour       = SGT_Helper.Premultiply(Color.Lerp(particleSideColour, particleTopColour, sideOrTop));
        var oldCameraRotation = nebulaCameraRotation; if (nebulaCamera != null)
        {
            nebulaCameraRotation = nebulaCamera.transform.rotation;
        }
        var cameraRotationDelta = Quaternion.Inverse(oldCameraRotation) * nebulaCameraRotation;

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

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

        nebulaMaterial.SetColor("particleColour", finalColour);
        nebulaMaterial.SetTexture("particleTexture", particleTexture);
        nebulaMaterial.SetFloat("particleFadeInDistance", particleFadeInDistance);
        nebulaMaterial.SetMatrix("cameraRoll", SGT_MatrixHelper.Rotation(roll));

        SGT_Helper.SetRenderQueue(nebulaMaterial, nebulaRenderQueue);

        nebulaMesh.GameObject          = nebula;
        nebulaMesh.HasMeshRenderer     = true;
        nebulaMesh.MeshRendererEnabled = SGT_Helper.IsBlack(finalColour) == false;
        nebulaMesh.SharedMesh          = generatedMesh;
        nebulaMesh.SharedMaterial      = nebulaMaterial;
        nebulaMesh.Update();
    }
    private void StepScale()
    {
        if (parent == null)
        {
            parent = SGT_Helper.GetComponentUpwards <SGT_DebrisSpawner>(gameObject);
        }

        if (parent != null)
        {
            var position = SGT_Helper.GetPosition(parent.DebrisCentre);
            var distance = (position - transform.position).magnitude;
            var scaleMul = 1.0f - SGT_Helper.RemapClamped(parent.DebrisContainerInnerRadius, parent.DebrisContainerRadius, distance, 0.0f, 1.0f);

            SGT_Helper.SetLocalScale(transform, SGT_Helper.NewVector3(scale * scaleMul));
        }
    }
Esempio n. 6
0
    private void UpdateTransform()
    {
        var oblatenessScale       = new Vector3(1.0f, 1.0f - surfaceOblateness, 1.0f);
        var surfaceScale          = SGT_Helper.NewVector3(SurfaceEquatorialRadius);
        var atmosphereScale       = SGT_Helper.NewVector3(AtmosphereEquatorialRadius);
        var starObserverPosition  = SGT_Helper.GetPosition(starObserver);
        var starObserverDirection = (starObserverPosition - atmosphereGameObject.transform.position).normalized;

        SGT_Helper.SetLocalScale(oblatenessGameObject.transform, oblatenessScale);
        SGT_Helper.SetLocalScale(surfaceGameObject.transform, surfaceScale);
        SGT_Helper.SetLocalScale(atmosphereGameObject.transform, atmosphereScale);

        if (atmosphereGameObject.transform.up != starObserverDirection)
        {
            atmosphereGameObject.transform.up = starObserverDirection;

            SGT_Helper.UpdateNonOrthogonalTransform(atmosphereGameObject.transform);
        }
    }
Esempio n. 7
0
    private void UpdateShader()
    {
        var uniformScale     = UniformScale;
        var centrePosition   = transform.position;
        var observerPosition = SGT_Helper.GetPosition(starObserver);

        var distance   = AtmosphereHeightAtPoint(observerPosition);
        var altitude01 = SGT_Helper.Remap(0.0f, 1.0f, distance, atmosphereSkyAltitude, 1.0f);

        var invScale         = SGT_MatrixHelper.Scaling(SGT_Helper.Reciprocal(new Vector3(1.0f, 1.0f - surfaceOblateness, 1.0f)));
        var rot              = SGT_MatrixHelper.Rotation(transform.rotation);
        var invRot           = SGT_MatrixHelper.Rotation(Quaternion.Inverse(transform.rotation));
        var ellipsoid2sphere = rot * invScale * invRot;

        var rel  = ellipsoid2sphere.MultiplyPoint(observerPosition - centrePosition);
        var dist = rel.magnitude;

        float maxSurfaceDepth, maxAtmosphereDepth;

        SGT_Helper.CalculateHorizonAtmosphereDepth(surfaceRadius * uniformScale, AtmosphereEquatorialRadius * uniformScale, dist, out maxSurfaceDepth, out maxAtmosphereDepth);

        // Update shader variables
        for (var i = 0; i < surfaceMaterials.Length; i++)
        {
            surfaceMaterials[i].SetTexture("surfaceTexture", surfaceTexture.GetTexture((CubemapFace)i));
        }

        SGT_ShaderHelper.SetTexture(surfaceMaterials, "atmosphereTexture", atmosphereSurfaceTexture);
        SGT_ShaderHelper.SetVector(surfaceMaterials, "centrePosition", centrePosition);
        SGT_ShaderHelper.SetFloat(surfaceMaterials, "atmosphereRadius", AtmosphereEquatorialRadius * uniformScale);
        SGT_ShaderHelper.SetFloat(surfaceMaterials, "atmosphereFalloff", atmosphereSurfaceFalloff);
        SGT_ShaderHelper.SetFloat(surfaceMaterials, "maxDepth", maxSurfaceDepth * (1.0f - atmosphereFog));         // FOG
        SGT_ShaderHelper.SetFloat(surfaceMaterials, "surfaceRadius", surfaceRadius * uniformScale);
        SGT_ShaderHelper.SetFloat(surfaceMaterials, "atmosphereHeight", atmosphereHeight * uniformScale);
        SGT_ShaderHelper.SetMatrix(surfaceMaterials, "ellipsoid2sphere", ellipsoid2sphere);

        atmosphereMaterial.SetTexture("atmosphereTexture", atmosphereTexture);
        atmosphereMaterial.SetVector("centrePosition", centrePosition);
        atmosphereMaterial.SetFloat("atmosphereFalloff", Mathf.Lerp(atmosphereSkyFalloff, atmosphereFalloff, altitude01));
        atmosphereMaterial.SetFloat("maxDepth", maxAtmosphereDepth);
        atmosphereMaterial.SetMatrix("ellipsoid2sphere", ellipsoid2sphere);
    }
Esempio n. 8
0
    public void LateUpdate()
    {
        if (thrusterObserver == null)
        {
            thrusterObserver = SGT_Helper.FindCamera();
        }
        if (thrusterFlameGameObject == null)
        {
            thrusterFlameGameObject = SGT_Helper.CreateGameObject("Flame", gameObject);
        }
        if (thrusterFlareGameObject == null)
        {
            thrusterFlareGameObject = SGT_Helper.CreateGameObject("Flare", gameObject);
        }

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

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

        if (thrusterPhysics == true && thrusterPhysicsRigidbody == null)
        {
            thrusterPhysicsRigidbody = SGT_Helper.GetComponentUpwards <Rigidbody>(gameObject);
        }

        var observerPosition = SGT_Helper.GetPosition(thrusterObserver);

        if (Application.isPlaying == true)
        {
            currentThrusterThrottle = Mathf.MoveTowards(currentThrusterThrottle, targetThrusterThrottle, thrusterTweenSpeed * Time.deltaTime);
        }
        else
        {
            currentThrusterThrottle = targetThrusterThrottle;
        }

        if (thrusterFlame == true)
        {
            if (thrusterFlameMesh == null)
            {
                thrusterFlameMesh = new SGT_Mesh();
            }

            // Offset flame
            SGT_Helper.SetLocalPosition(thrusterFlameGameObject.transform, thrusterFlameOffset);

            var finalFlameScale = thrusterFlameScale + thrusterFlameScaleChange * currentThrusterThrottle;

            // Hide/show flame
            if (finalFlameScale == Vector3.zero)
            {
                thrusterFlameMesh.MeshRendererEnabled = false;
            }
            else
            {
                if (Application.isPlaying == true)
                {
                    finalFlameScale *= Random.Range(1.0f - thrusterFlameScaleFlicker, 1.0f);
                }

                thrusterFlameMesh.MeshRendererEnabled = true;

                SGT_Helper.SetLocalScale(thrusterFlameGameObject.transform, finalFlameScale);

                // Roll flame to observer
                var pointDir = transform.InverseTransformPoint(observerPosition);
                var roll     = Mathf.Atan2(pointDir.y, pointDir.x) * Mathf.Rad2Deg;

                SGT_Helper.SetRotation(thrusterFlameGameObject.transform, transform.rotation * Quaternion.Euler(0.0f, 0.0f, roll));
            }

            thrusterFlameMesh.GameObject      = thrusterFlameGameObject;
            thrusterFlameMesh.HasMeshRenderer = true;
            thrusterFlameMesh.Update();
        }
        else
        {
            if (thrusterFlameMesh != null)
            {
                thrusterFlameMesh = thrusterFlameMesh.Clear();
            }
        }

        if (thrusterFlare == true)
        {
            if (thrusterFlareMesh == null)
            {
                thrusterFlareMesh = new SGT_Mesh();
            }

            // Offset flare
            SGT_Helper.SetLocalPosition(thrusterFlareGameObject.transform, thrusterFlareOffset);

            // Flare visible?
            var a               = thrusterFlareGameObject.transform.position;
            var b               = observerPosition;
            var direction       = (b - a).normalized;
            var distance        = (b - a).magnitude;
            var targetFlareSize = 0.0f;

            // If the ray hits something, then hide the flare
            if (Physics.Raycast(a, direction, distance, thrusterFlareRaycastMask) == true)
            {
                targetFlareSize = 0.0f;
            }
            else
            {
                targetFlareSize = 1.0f;
            }

            // Point flare at observer
            if (thrusterObserver != null)
            {
                SGT_Helper.SetRotation(thrusterFlareGameObject.transform, thrusterObserver.transform.rotation);
            }

            // Fade flare in/out based on raycast
            if (Application.isPlaying == true)
            {
                currentThrusterFlareScale = Mathf.MoveTowards(currentThrusterFlareScale, targetFlareSize, thrusterFlareScaleTweenSpeed * Time.deltaTime);
            }
            else
            {
                currentThrusterFlareScale = targetFlareSize;
            }

            var finalFlareScale = currentThrusterFlareScale * (thrusterFlareScale + thrusterFlareScaleChange * currentThrusterThrottle);

            // Hide/show flare
            if (finalFlareScale == Vector3.zero)
            {
                thrusterFlareMesh.MeshRendererEnabled = false;
            }
            else
            {
                if (Application.isPlaying == true)
                {
                    finalFlareScale *= Random.Range(1.0f - thrusterFlareScaleFlicker, 1.0f);
                }

                thrusterFlareMesh.MeshRendererEnabled = true;

                SGT_Helper.SetLocalScale(thrusterFlareGameObject.transform, finalFlareScale);
            }

            thrusterFlareMesh.GameObject      = thrusterFlareGameObject;
            thrusterFlareMesh.HasMeshRenderer = true;
            thrusterFlareMesh.Update();
        }
        else
        {
            if (thrusterFlareMesh != null)
            {
                thrusterFlareMesh = thrusterFlareMesh.Clear();
            }
        }

#if UNITY_EDITOR == true
        if (thrusterFlameMesh != null)
        {
            thrusterFlameMesh.HideInEditor();
        }
        if (thrusterFlareMesh != null)
        {
            thrusterFlareMesh.HideInEditor();
        }

        SGT_Helper.HideGameObject(thrusterFlameGameObject);
        SGT_Helper.HideGameObject(thrusterFlareGameObject);
#endif
    }
Esempio n. 9
0
    private void UpdateShader()
    {
        var uniformScale        = UniformScale;
        var position            = transform.position;
        var lightSourcePosition = SGT_Helper.GetPosition(lightSource);

        ringMaterial.SetTexture("ringTexture", ringTexture);
        ringMaterial.SetVector("position", position);
        ringMaterial.SetFloat("ringRadius", RingRadiusInner * uniformScale);
        ringMaterial.SetFloat("ringThickness", ringWidth * uniformScale);

        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 (lit == true)
        {
            ringMaterial.SetVector("starDirection", StarDirection);
            ringMaterial.SetFloat("ringBrightness", (litBrightnessMin + litBrightnessMax) * 0.5f);
            ringMaterial.SetFloat("ringBrightnessRange", litBrightnessMax - litBrightnessMin);
        }

        if (scattering == true)
        {
            ringMaterial.SetVector("starPosition", lightSourcePosition);

            var mie  = -(1.0f - 1.0f / Mathf.Pow(10.0f, scatteringMie * 5.0f));
            var mie4 = new Vector4(mie * 2.0f, 1.0f - mie * mie, 1.0f + mie * mie, 1.5f);

            ringMaterial.SetVector("mieValues", mie4);
            ringMaterial.SetFloat("occlusion", scatteringOcclusion);
        }
    }