private void CalculateOcclusion(int layers, Vector4 worldEye, Vector4 worldTgt, ref float occlusion) { if (SgtOcclusion.IsValid(occlusion, layers, gameObject) == true && OuterRadius > 0.0f && outerDepthTex != null) { var eye = cachedTransform.InverseTransformPoint(worldEye) / OuterRadius; var tgt = cachedTransform.InverseTransformPoint(worldTgt) / OuterRadius; var dir = Vector3.Normalize(tgt - eye); var len = Vector3.Magnitude(tgt - eye); var length = default(float); if (GetLength(eye, dir, len, ref length) == true) { var localPosition = cachedTransform.InverseTransformPoint(worldEye); var localDistance = localPosition.magnitude; var innerThickness = default(float); var outerThickness = default(float); var innerRatio = SgtHelper.Divide(innerMeshRadius, OuterRadius); var middleRatio = Mathf.Lerp(innerRatio, 1.0f, middle); var distance = SgtHelper.Divide(localDistance, OuterRadius); var outerDensity = 1.0f - outerFog; SgtHelper.CalculateHorizonThickness(innerRatio, middleRatio, distance, out innerThickness, out outerThickness); length *= SgtHelper.Reciprocal(outerThickness * outerDensity); var depth = outerDepthTex.GetPixelBilinear(length, length).a; depth = Mathf.Clamp01(depth + (1.0f - depth) * GetSky(worldEye, localDistance)); occlusion += (1.0f - occlusion) * depth; } } }
private void CameraPreRender(Camera camera) { if (model != null) { model.Restore(camera); } // Write camera-dependant shader values if (innerMaterial != null && outerMaterial != null) { var localPosition = cachedTransform.InverseTransformPoint(camera.transform.position); var localDistance = localPosition.magnitude; var height01 = Mathf.InverseLerp(OuterRadius, InnerMeshRadius, localDistance); var innerThickness = default(float); var outerThickness = default(float); var innerRatio = SgtHelper.Divide(InnerMeshRadius, OuterRadius); var middleRatio = Mathf.Lerp(innerRatio, 1.0f, Middle); var distance = SgtHelper.Divide(localDistance, OuterRadius); var innerDensity = 1.0f - InnerFog; var outerDensity = 1.0f - OuterFog; SgtHelper.CalculateHorizonThickness(innerRatio, middleRatio, distance, out innerThickness, out outerThickness); innerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(innerThickness * innerDensity)); outerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(outerThickness * outerDensity)); if (OuterDepthTex != null) { #if UNITY_EDITOR SgtHelper.MakeTextureReadable(OuterDepthTex); #endif outerMaterial.SetFloat(SgtShader._Sky, GetSky(camera) * OuterDepthTex.GetPixelBilinear(height01 / outerDensity, 0.0f).a); } var scale = SgtHelper.Divide(OuterMeshRadius, OuterRadius); var worldToLocal = Matrix4x4.Scale(new Vector3(scale, scale, scale)) * cachedTransform.worldToLocalMatrix; // cachedTransform might not be set here, so use the property innerMaterial.SetMatrix(SgtShader._WorldToLocal, worldToLocal); outerMaterial.SetMatrix(SgtShader._WorldToLocal, worldToLocal); // Write lights and shadows SgtHelper.SetTempMaterial(innerMaterial, outerMaterial); var mask = 1 << gameObject.layer; var lights = SgtLight.Find(Lit, mask); SgtShadow.Find(Lit, mask, lights); SgtShadow.FilterOutSphere(transform.position); SgtShadow.Write(Lit, 2); SgtLight.FilterOut(transform.position); SgtLight.Write(Lit, transform.position, cachedTransform, null, ScatteringStrength, 2); } }
private void HandleCameraDraw(Camera camera) { if (SgtHelper.CanDraw(gameObject, camera) == false) { return; } // Write camera-dependent shader values if (innerMaterial != null && outerMaterial != null) { var localPosition = CachedTransform.InverseTransformPoint(camera.transform.position); var localDistance = localPosition.magnitude; var innerThickness = default(float); var outerThickness = default(float); var innerRatio = SgtHelper.Divide(innerMeshRadius, OuterRadius); var middleRatio = Mathf.Lerp(innerRatio, 1.0f, middle); var distance = SgtHelper.Divide(localDistance, OuterRadius); var innerDensity = 1.0f - innerFog; var outerDensity = 1.0f - outerFog; SgtHelper.CalculateHorizonThickness(innerRatio, middleRatio, distance, out innerThickness, out outerThickness); innerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(innerThickness * innerDensity)); outerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(outerThickness * outerDensity)); if (outerDepthTex != null) { #if UNITY_EDITOR SgtHelper.MakeTextureReadable(outerDepthTex); #endif outerMaterial.SetFloat(SgtShader._Sky, GetSky(localDistance)); } UpdateMaterialNonSerialized(); } var scale = SgtHelper.Divide(OuterRadius, outerMeshRadius); var matrix = CachedTransform.localToWorldMatrix * Matrix4x4.Scale(Vector3.one * scale); if (cameraOffset != 0.0f) { var direction = Vector3.Normalize(camera.transform.position - cachedTransform.position); matrix = Matrix4x4.Translate(direction * cameraOffset) * matrix; } Graphics.DrawMesh(outerMesh, matrix, outerMaterial, gameObject.layer, camera); }
private void CameraPreRender(Camera camera) { if (model != null) { model.Restore(camera); } // Write camera-dependant shader values if (innerMaterial != null && outerMaterial != null) { var localPosition = cachedTransform.InverseTransformPoint(camera.transform.position); var localDistance = localPosition.magnitude; var height01 = Mathf.InverseLerp(OuterRadius, InnerMeshRadius, localDistance); var innerThickness = default(float); var outerThickness = default(float); var innerRatio = SgtHelper.Divide(InnerMeshRadius, OuterRadius); var middleRatio = Mathf.Lerp(innerRatio, 1.0f, Middle); var distance = SgtHelper.Divide(localDistance, OuterRadius); var innerDensity = 1.0f - InnerFog; var outerDensity = 1.0f - OuterFog; SgtHelper.CalculateHorizonThickness(innerRatio, middleRatio, distance, out innerThickness, out outerThickness); innerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(innerThickness * innerDensity)); outerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(outerThickness * outerDensity)); if (OuterDepthTex != null) { #if UNITY_EDITOR SgtHelper.MakeTextureReadable(OuterDepthTex); #endif outerMaterial.SetFloat(SgtShader._Sky, Sky * OuterDepthTex.GetPixelBilinear(height01 / outerDensity, 0.0f).a); } UpdateMaterialNonSerialized(); } }