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

        // 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 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;
    }