void OnRenderImage(RenderTexture source, RenderTexture destination) { if (RequiredResourcesAreMissing()) { Graphics.Blit(source, destination); return; } // Pass the requisite camera matrices needed by all of the shaders Shader.SetGlobalMatrix("_InverseViewMatrix", CurrentCamera.cameraToWorldMatrix); Shader.SetGlobalMatrix("_InverseProjectionMatrix", CurrentCamera.projectionMatrix.inverse); CalculateFogDensityAndColor.SetFloat("_FarClipPlane", CurrentCamera.farClipPlane); // *** START OF Pass the additional requested settings/values to the fog density shader *** // // *** Required Assets *** // Directed light values CalculateFogDensityAndColor.SetFloat("_DirectionalLightIntensity", directionalLight.GetComponent <Light>().intensity); CalculateFogDensityAndColor.SetVector("_DirectionalLightDirection", directionalLight.GetComponent <Light>().transform.forward); CalculateFogDensityAndColor.SetColor("_DirectionalLightRGBColor", directionalLight.GetComponent <Light>().color); // Stretch fog to cover entire scene? if (scenewideFog) { calculateFogDensityAndColor.EnableKeyword("INFINITE_FOG"); } else { calculateFogDensityAndColor.DisableKeyword("INFINITE_FOG"); } // CG of fog in the scene coordinate system CalculateFogDensityAndColor.SetVector("_FogWorldPosition", fogVolume.transform.localPosition); // width/height/length of fog volume CalculateFogDensityAndColor.SetVector("_FogDimensions", fogVolume.transform.localScale); // Orientation of fog volume if (useGlobalXYZ) { calculateFogDensityAndColor.EnableKeyword("USE_GLOBAL_XYZ"); } else { calculateFogDensityAndColor.DisableKeyword("USE_GLOBAL_XYZ"); } CalculateFogDensityAndColor.SetVector("_FogUp", fogVolume.transform.up); CalculateFogDensityAndColor.SetVector("_FogForward", fogVolume.transform.forward); CalculateFogDensityAndColor.SetVector("_FogRight", fogVolume.transform.right); CalculateFogDensityAndColor.SetTexture("_RandomNoiseTex", randomNoiseTexture); // *** Additional Lights *** // Allow extra lights to interact with this fog volume? if (allowExtraLights) { calculateFogDensityAndColor.EnableKeyword("MULTIPLE_LIGHTS"); } else { calculateFogDensityAndColor.DisableKeyword("MULTIPLE_LIGHTS"); } // Point light values CalculateFogDensityAndColor.SetFloat("_PointLightIntensity", pointLight.GetComponent <Light>().intensity); CalculateFogDensityAndColor.SetColor("_PointLightRGBColor", pointLight.GetComponent <Light>().color); CalculateFogDensityAndColor.SetVector("_PointLightLocation", pointLight.position); CalculateFogDensityAndColor.SetFloat("_PointLightRange", pointLight.GetComponent <Light>().range); CalculateFogDensityAndColor.SetFloat("_ConstantAttenuation", constantAttenuation); CalculateFogDensityAndColor.SetFloat("_LinearAttenuation", linearAttenuation); CalculateFogDensityAndColor.SetFloat("_ExponentialAttenuation", exponentialAttenuation); // *** Fog Density *** // Number of steps/samples to take for each frame CalculateFogDensityAndColor.SetInt("_RaymarchSteps", maxNumberOfSteps); // Distance at which fog fully saturates the current pixel CalculateFogDensityAndColor.SetFloat("_DistanceToFogSaturation", distanceToFogSaturation); // Use randomized evaluation points? if (randomizeEvaluationPoints) { calculateFogDensityAndColor.EnableKeyword("RANDOMIZED_EVALUATION_POSITION"); } else { calculateFogDensityAndColor.DisableKeyword("RANDOMIZED_EVALUATION_POSITION"); } // Use height density falloff? if (useHeightDensityFalloff) { calculateFogDensityAndColor.EnableKeyword("HEIGHT_DENSITY_FALLOFF"); } else { calculateFogDensityAndColor.DisableKeyword("HEIGHT_DENSITY_FALLOFF"); } // Use linear or exponential height falloff? if (exponentialFalloffInY) { calculateFogDensityAndColor.EnableKeyword("EXPONENTIAL_HEIGHT_DENSITY_FALLOFF"); } else { calculateFogDensityAndColor.DisableKeyword("EXPONENTIAL_HEIGHT_DENSITY_FALLOFF"); } // Where does falloff begin? This is either relative to bottom of box or to global cg, // depending on if the fog is scenewide or not CalculateFogDensityAndColor.SetFloat("_HeightToStartFalloffAt", heightToStartFalloffAt); CalculateFogDensityAndColor.SetFloat("_YFalloffDistance", Mathf.Abs(YFalloffDistance)); // Fog volume falloff in x/z directions if (useEdgeDensityFalloff) { calculateFogDensityAndColor.EnableKeyword("EDGE_DENSITY_FALLOFF"); } else { calculateFogDensityAndColor.DisableKeyword("EDGE_DENSITY_FALLOFF"); } // Use linear or exponential height falloff? if (exponentialFalloffInXZ) { calculateFogDensityAndColor.EnableKeyword("EXPONENTIAL_EDGE_DENSITY_FALLOFF"); } else { calculateFogDensityAndColor.DisableKeyword("EXPONENTIAL_EDGE_DENSITY_FALLOFF"); } CalculateFogDensityAndColor.SetFloat("_FogEdgeFalloffInX", fogFalloffInX / 100.0f); CalculateFogDensityAndColor.SetFloat("_FogEdgeFalloffInZ", fogFalloffInZ / 100.0f); // Knockdown on light strength in shadows if (useShadowsInFog) { calculateFogDensityAndColor.EnableKeyword("FOG_SHADOWS"); } else { calculateFogDensityAndColor.DisableKeyword("FOG_SHADOWS"); } CalculateFogDensityAndColor.SetFloat("_ShadowStrength", shadowStrength / 100.0f); // *** Fog Noise *** // Noise Strength: Used to adjust the influence of simplex noise on the fog volume if (useNoiseInFog) { calculateFogDensityAndColor.EnableKeyword("NOISY_FOG"); } else { calculateFogDensityAndColor.DisableKeyword("NOISY_FOG"); } if (useAdditiveNoise) { calculateFogDensityAndColor.EnableKeyword("ADDITIVE_NOISE"); } else { calculateFogDensityAndColor.DisableKeyword("ADDITIVE_NOISE"); } CalculateFogDensityAndColor.SetFloat("_NoiseStrength", noiseStrength); CalculateFogDensityAndColor.SetFloat("_NoiseSize", noiseSize); CalculateFogDensityAndColor.SetVector("_NoiseVelocity", noiseVelocity); // *** Atmospheric Lighting *** // Ambient Fog: Used to bleed some of the lit fog into shadowed areas if (useAmbientLitFog) { calculateFogDensityAndColor.EnableKeyword("AMBIENT_FOG"); } else { calculateFogDensityAndColor.DisableKeyword("AMBIENT_FOG"); } CalculateFogDensityAndColor.SetFloat("_AmbientLitFog", ambientLitFog); CalculateFogDensityAndColor.SetVector("_AmbientFogRGBColor", ambientFogColor); BlendFogWithScene(source, destination, CalculateFogDensityAndColor); // *** Fog Box CGs and Normals *** // CGs boxCG.x = fogVolume.GetComponent <BoxGeometry>().BoxCG.x; boxCG.y = fogVolume.GetComponent <BoxGeometry>().BoxCG.y; boxCG.z = fogVolume.GetComponent <BoxGeometry>().BoxCG.z; boxLeft.x = fogVolume.GetComponent <BoxGeometry>().Left.x; boxLeft.y = fogVolume.GetComponent <BoxGeometry>().Left.y; boxLeft.z = fogVolume.GetComponent <BoxGeometry>().Left.z; boxRight.x = fogVolume.GetComponent <BoxGeometry>().Right.x; boxRight.y = fogVolume.GetComponent <BoxGeometry>().Right.y; boxRight.z = fogVolume.GetComponent <BoxGeometry>().Right.z; boxFront.x = fogVolume.GetComponent <BoxGeometry>().Front.x; boxFront.y = fogVolume.GetComponent <BoxGeometry>().Front.y; boxFront.z = fogVolume.GetComponent <BoxGeometry>().Front.z; boxBack.x = fogVolume.GetComponent <BoxGeometry>().Back.x; boxBack.y = fogVolume.GetComponent <BoxGeometry>().Back.y; boxBack.z = fogVolume.GetComponent <BoxGeometry>().Back.z; boxTop.x = fogVolume.GetComponent <BoxGeometry>().Top.x; boxTop.y = fogVolume.GetComponent <BoxGeometry>().Top.y; boxTop.z = fogVolume.GetComponent <BoxGeometry>().Top.z; boxBottom.x = fogVolume.GetComponent <BoxGeometry>().Bottom.x; boxBottom.y = fogVolume.GetComponent <BoxGeometry>().Bottom.y; boxBottom.z = fogVolume.GetComponent <BoxGeometry>().Bottom.z; // Normals leftNormal.x = fogVolume.GetComponent <BoxGeometry>().LeftFace.x; leftNormal.y = fogVolume.GetComponent <BoxGeometry>().LeftFace.y; leftNormal.z = fogVolume.GetComponent <BoxGeometry>().LeftFace.z; rightNormal.x = fogVolume.GetComponent <BoxGeometry>().RightFace.x; rightNormal.y = fogVolume.GetComponent <BoxGeometry>().RightFace.y; rightNormal.z = fogVolume.GetComponent <BoxGeometry>().RightFace.z; frontNormal.x = fogVolume.GetComponent <BoxGeometry>().FrontFace.x; frontNormal.y = fogVolume.GetComponent <BoxGeometry>().FrontFace.y; frontNormal.z = fogVolume.GetComponent <BoxGeometry>().FrontFace.z; backNormal.x = fogVolume.GetComponent <BoxGeometry>().BackFace.x; backNormal.y = fogVolume.GetComponent <BoxGeometry>().BackFace.y; backNormal.z = fogVolume.GetComponent <BoxGeometry>().BackFace.z; topNormal.x = fogVolume.GetComponent <BoxGeometry>().TopFace.x; topNormal.y = fogVolume.GetComponent <BoxGeometry>().TopFace.y; topNormal.z = fogVolume.GetComponent <BoxGeometry>().TopFace.z; bottomNormal.x = fogVolume.GetComponent <BoxGeometry>().BottomFace.x; bottomNormal.y = fogVolume.GetComponent <BoxGeometry>().BottomFace.y; bottomNormal.z = fogVolume.GetComponent <BoxGeometry>().BottomFace.z; // Send to shader: Fog Box CGs and Normals CalculateFogDensityAndColor.SetVector("_LeftCG", boxLeft); CalculateFogDensityAndColor.SetVector("_LeftNormal", leftNormal); CalculateFogDensityAndColor.SetVector("_RightCG", boxRight); CalculateFogDensityAndColor.SetVector("_RightNormal", rightNormal); CalculateFogDensityAndColor.SetVector("_FrontCG", boxFront); CalculateFogDensityAndColor.SetVector("_FrontNormal", frontNormal); CalculateFogDensityAndColor.SetVector("_BackCG", boxBack); CalculateFogDensityAndColor.SetVector("_BackNormal", backNormal); CalculateFogDensityAndColor.SetVector("_TopCG", boxTop); CalculateFogDensityAndColor.SetVector("_TopNormal", topNormal); CalculateFogDensityAndColor.SetVector("_BottomCG", boxBottom); CalculateFogDensityAndColor.SetVector("_BottomNormal", bottomNormal); }
void OnRenderImage(RenderTexture source, RenderTexture destination) { if (RequiredResourcesAreMissing()) { Graphics.Blit(source, destination); return; } // Pass the requisite camera matrices needed by all of the shaders Shader.SetGlobalMatrix("_InverseViewMatrix", CurrentCamera.cameraToWorldMatrix); Shader.SetGlobalMatrix("_InverseProjectionMatrix", CurrentCamera.projectionMatrix.inverse); // *** START OF Pass the additional requested settings/values to the fog density shader *** // // *** Required Assets *** // Directed light values CalculateFogDensityAndColor.SetFloat("_DirectionalLightIntensity", directionalLight.GetComponent <Light>().intensity); CalculateFogDensityAndColor.SetVector("_DirectionalLightDirection", directionalLight.GetComponent <Light>().transform.forward); CalculateFogDensityAndColor.SetColor("_DirectionalLightRGBColor", directionalLight.GetComponent <Light>().color); // Stretch fog to cover entire scene? if (scenewideFog) { calculateFogDensityAndColor.EnableKeyword("INFINITE_FOG"); } else { calculateFogDensityAndColor.DisableKeyword("INFINITE_FOG"); } // CG of fog in the scene coordinate system CalculateFogDensityAndColor.SetVector("_FogWorldPosition", fogVolume.transform.localPosition); // width/height/length of fog volume CalculateFogDensityAndColor.SetVector("_FogDimensions", fogVolume.transform.localScale); // *** Additional Lights *** // Allow extra lights to interact with this fog volume? if (allowExtraLights) { calculateFogDensityAndColor.EnableKeyword("MULTIPLE_LIGHTS"); } else { calculateFogDensityAndColor.DisableKeyword("MULTIPLE_LIGHTS"); } // Point light values CalculateFogDensityAndColor.SetFloat("_PointLightIntensity", pointLight.GetComponent <Light>().intensity); CalculateFogDensityAndColor.SetColor("_PointLightRGBColor", pointLight.GetComponent <Light>().color); CalculateFogDensityAndColor.SetVector("_PointLightLocation", pointLight.position); CalculateFogDensityAndColor.SetFloat("_PointLightRange", pointLight.GetComponent <Light>().range); // *** Fog Density *** // Number of steps/samples to take for each frame CalculateFogDensityAndColor.SetInt("_RaymarchSteps", maxNumberOfSteps); // Distance at which fog fully saturates the current pixel CalculateFogDensityAndColor.SetFloat("_DistanceToFogSaturation", distanceToFogSaturation); // Use height density falloff? if (useHeightDensityFalloff) { calculateFogDensityAndColor.EnableKeyword("HEIGHT_DENSITY_FALLOFF"); } else { calculateFogDensityAndColor.DisableKeyword("HEIGHT_DENSITY_FALLOFF"); } // Where does falloff begin? This is either relative to bottom of box or to global cg, // depending on if the fog is scenewide or not CalculateFogDensityAndColor.SetFloat("_HeightToStartFalloffAt", heightToStartFalloffAt); // How much the density of the fog falls off in vertical direction (if using height fog) CalculateFogDensityAndColor.SetFloat("_ExponentialHeightDensity", exponentialHeightDensity); // Fog volume falloff in x/z directions if (useEdgeDensityFalloff) { calculateFogDensityAndColor.EnableKeyword("EDGE_DENSITY_FALLOFF"); } else { calculateFogDensityAndColor.DisableKeyword("EDGE_DENSITY_FALLOFF"); } CalculateFogDensityAndColor.SetFloat("_FogEdgeFalloffInX", fogFalloffInX); CalculateFogDensityAndColor.SetFloat("_FogEdgeFalloffInZ", fogFalloffInZ); // Knockdown on light strength in shadows if (useShadowsInFog) { calculateFogDensityAndColor.EnableKeyword("FOG_SHADOWS"); } else { calculateFogDensityAndColor.DisableKeyword("FOG_SHADOWS"); } CalculateFogDensityAndColor.SetFloat("_ShadowStrength", shadowStrength); // Ambient Fog: Used to bleed some of the lit fog into shadowed areas if (useAmbientLitFog) { calculateFogDensityAndColor.EnableKeyword("AMBIENT_FOG"); } else { calculateFogDensityAndColor.DisableKeyword("AMBIENT_FOG"); } CalculateFogDensityAndColor.SetFloat("_AmbientLitFog", ambientLitFog); // *** Fog Noise *** // Noise Strength: Used to adjust the influence of simplex noise on the fog volume if (useNoiseInFog) { calculateFogDensityAndColor.EnableKeyword("NOISY_FOG"); } else { calculateFogDensityAndColor.DisableKeyword("NOISY_FOG"); } CalculateFogDensityAndColor.SetFloat("_NoiseStrength", noiseStrength); CalculateFogDensityAndColor.SetVector("_NoiseVelocity", noiseVelocity); // *** Testing and Debug *** // ********** START DEBUG ********** // if (fogColorByCascade) { calculateFogDensityAndColor.EnableKeyword("COLOR_FOG_BY_CASCADES"); } else { calculateFogDensityAndColor.DisableKeyword("COLOR_FOG_BY_CASCADES"); } // ********** END DEBUG ********** // // *** MISC (NOT CURRENTLY IN USE) *** // Extinction Coefficient (Beer-Lambert Law) for atmospheric scattering // CalculateFogDensityAndColor.SetFloat("_ExtinctionCoef", _ExtinctionCoefficient); // *** END OF Pass the additional requested settings/values to the fog density shader *** // BlendFogWithScene(source, destination, CalculateFogDensityAndColor); }