Beispiel #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 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);
        }
        if (dustCamera == null)
        {
            dustCamera = SGT_Helper.FindCamera();
        }

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

        var finalColour           = SGT_Helper.Premultiply(particleColour);
        var oldDustCameraRotation = dustCameraRotation;

        if (dustCamera != null)
        {
            dustCameraRotation = dustCamera.transform.rotation;
        }

        var cameraRotationDelta = Quaternion.Inverse(oldDustCameraRotation) * dustCameraRotation;

        particleRoll -= cameraRotationDelta.eulerAngles.z;

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

        dustMaterial.SetTexture("dustTexture", particleTexture);
        dustMaterial.SetFloat("dustRadius", dustRadius);
        dustMaterial.SetColor("particleColour", finalColour);
        dustMaterial.SetFloat("particleFadeInDistance", dustRadius / particleFadeInDistance);
        dustMaterial.SetFloat("particleFadeOutDistance", dustRadius / particleFadeOutDistance);
        dustMaterial.SetMatrix("particleRoll", SGT_MatrixHelper.Rotation(roll));

        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 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);
        }
    }
Beispiel #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();
    }
Beispiel #5
0
    private void UpdateShader()
    {
        var oldCameraRotation = starfieldCameraRotation; if (starfieldCamera != null)
        {
            starfieldCameraRotation = starfieldCamera.transform.rotation;
        }
        var cameraRotationDelta = Quaternion.Inverse(oldCameraRotation) * starfieldCameraRotation;

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

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

        starfieldMaterial.SetTexture("starTexture", starfieldTexture);
        starfieldMaterial.SetFloat("starPulseRateMax", starPulseRateMax);
        starfieldMaterial.SetMatrix("cameraRoll", SGT_MatrixHelper.Rotation(roll));
    }
    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);
    }
    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);
        }
    }