private void LateUpdate() { if (OceanRenderer.Instance == null) { _rend.enabled = false; return; } float heightOffset = OceanRenderer.Instance.ViewerHeightAboveWater; // Disable skirt when camera not close to water. In the first few frames collision may not be avail, in that case no choice // but to assume enabled. In the future this could detect if camera is far enough under water, render a simple quad to avoid // finding the intersection line. _rend.enabled = heightOffset < _maxHeightAboveWater; if (_rend.enabled) { if (_copyParamsEachFrame) { _rend.material.CopyPropertiesFromMaterial(OceanRenderer.Instance.OceanMaterial); } // Assign lod0 shape - trivial but bound every frame because lod transform comes from here if (_mpb == null) { _mpb = new PropertyWrapperMPB(); } _rend.GetPropertyBlock(_mpb.materialPropertyBlock); // Underwater rendering uses displacements for intersecting the waves with the near plane, and ocean depth/shadows for ScatterColour() _mpb.SetFloat(OceanRenderer.sp_LD_SliceIndex, 0); OceanRenderer.Instance._lodDataAnimWaves.BindResultData(_mpb); if (OceanRenderer.Instance._lodDataSeaDepths) { OceanRenderer.Instance._lodDataSeaDepths.BindResultData(_mpb); } else { LodDataMgrSeaFloorDepth.BindNull(_mpb); } if (OceanRenderer.Instance._lodDataShadow) { OceanRenderer.Instance._lodDataShadow.BindResultData(_mpb); } else { LodDataMgrShadow.BindNull(_mpb); } _mpb.SetFloat(sp_HeightOffset, heightOffset); _rend.SetPropertyBlock(_mpb.materialPropertyBlock); } }
private void LateUpdate() { #if UNITY_EDITOR // We don't run in "prefab scenes", i.e. when editing a prefab. Bail out if prefab scene is detected. if (PrefabStageUtility.GetCurrentPrefabStage() != null) { return; } #endif if (OceanRenderer.Instance == null || !ShowEffect()) { _rend.enabled = false; return; } // Pass true in last arg for a crap reason - in edit mode LateUpdate can be called very frequently, and the height sampler mistakenly thinks // this is erroneous and complains. _sampleWaterHeight.Init(transform.position, 0f, true); _sampleWaterHeight.Sample(out var waterHeight); float heightOffset = transform.position.y - waterHeight; // Disable skirt when camera not close to water. In the first few frames collision may not be avail, in that case no choice // but to assume enabled. In the future this could detect if camera is far enough under water, render a simple quad to avoid // finding the intersection line. _rend.enabled = heightOffset < _maxHeightAboveWater; if (_rend.enabled) { // Only execute when playing to stop CopyPropertiesFromMaterial from corrupting and breaking the material. if (!isMeniscus && _copyParamsEachFrame) { _rend.material.CopyPropertiesFromMaterial(OceanRenderer.Instance.OceanMaterial); } // Assign lod0 shape - trivial but bound every frame because lod transform comes from here if (_mpb == null) { _mpb = new PropertyWrapperMPB(); } _rend.GetPropertyBlock(_mpb.materialPropertyBlock); // Underwater rendering uses displacements for intersecting the waves with the near plane, and ocean depth/shadows for ScatterColour() _mpb.SetInt(LodDataMgr.sp_LD_SliceIndex, 0); LodDataMgrAnimWaves.Bind(_mpb); LodDataMgrSeaFloorDepth.Bind(_mpb); LodDataMgrShadow.Bind(_mpb); _mpb.SetFloat(sp_HeightOffset, heightOffset); _rend.SetPropertyBlock(_mpb.materialPropertyBlock); } }
private void LateUpdate() { if (OceanRenderer.Instance == null) { _rend.enabled = false; return; } float heightOffset = OceanRenderer.Instance.ViewerHeightAboveWater; if (_rend.enabled) { //if (_copyParamsEachFrame) //{ // _rend.material.CopyPropertiesFromMaterial(OceanRenderer.Instance.OceanMaterial); //} // Assign lod0 shape - trivial but bound every frame because lod transform comes from here if (_mpb == null) { _mpb = new MaterialPropertyBlock(); } _rend.GetPropertyBlock(_mpb); // Underwater rendering uses displacements for intersecting the waves with the near plane, and ocean depth/shadows for ScatterColour() OceanRenderer.Instance._lodDataAnimWaves.BindResultData(0, 0, _mpb); if (OceanRenderer.Instance._lodDataSeaDepths) { OceanRenderer.Instance._lodDataSeaDepths.BindResultData(0, 0, _mpb); } else { LodDataMgrSeaFloorDepth.BindNull(0, _mpb); } if (OceanRenderer.Instance._lodDataShadow) { OceanRenderer.Instance._lodDataShadow.BindResultData(0, 0, _mpb); } else { LodDataMgrShadow.BindNull(0, _mpb); } _mpb.SetFloat("_HeightOffset", heightOffset); _rend.SetPropertyBlock(_mpb); } }
private void CleanUp() { foreach (var lodData in _lodDatas) { lodData.OnDisable(); } _lodDatas.Clear(); #if UNITY_EDITOR if (!EditorApplication.isPlaying && Root != null) { DestroyImmediate(Root.gameObject); } else #endif if (Root != null) { Destroy(Root.gameObject); } Root = null; _lodTransform = null; _lodDataAnimWaves = null; _lodDataClipSurface = null; _lodDataDynWaves = null; _lodDataFlow = null; _lodDataFoam = null; _lodDataSeaDepths = null; _lodDataShadow = null; if (CollisionProvider != null) { CollisionProvider.CleanUp(); CollisionProvider = null; } if (FlowProvider != null) { FlowProvider.CleanUp(); FlowProvider = null; } _oceanChunkRenderers.Clear(); }
// Called when visible to a camera void OnWillRenderObject() { if (OceanRenderer.Instance == null || Rend == null) { return; } // check if built-in pipeline being used if (Camera.current != null) { _currentCamera = Camera.current; } // Depth texture is used by ocean shader for transparency/depth fog, and for fading out foam at shoreline. _currentCamera.depthTextureMode |= DepthTextureMode.Depth; if (Rend.sharedMaterial != OceanRenderer.Instance.OceanMaterial) { Rend.sharedMaterial = OceanRenderer.Instance.OceanMaterial; } // per instance data if (_mpb == null) { _mpb = new PropertyWrapperMPB(); } Rend.GetPropertyBlock(_mpb.materialPropertyBlock); // blend LOD 0 shape in/out to avoid pop, if the ocean might scale up later (it is smaller than its maximum scale) var needToBlendOutShape = _lodIndex == 0 && OceanRenderer.Instance.ScaleCouldIncrease; var meshScaleLerp = needToBlendOutShape ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 0f; // blend furthest normals scale in/out to avoid pop, if scale could reduce var needToBlendOutNormals = _lodIndex == _totalLodCount - 1 && OceanRenderer.Instance.ScaleCouldDecrease; var farNormalsWeight = needToBlendOutNormals ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 1f; _mpb.SetVector(sp_InstanceData, new Vector3(meshScaleLerp, farNormalsWeight, _lodIndex)); // geometry data // compute grid size of geometry. take the long way to get there - make sure we land exactly on a power of two // and not inherit any of the lossy-ness from lossyScale. var scale_pow_2 = OceanRenderer.Instance.CalcLodScale(_lodIndex); var gridSizeGeo = scale_pow_2 / (0.25f * _lodDataResolution / _geoDownSampleFactor); var gridSizeLodData = gridSizeGeo / _geoDownSampleFactor; var mul = 1.875f; // fudge 1 var pow = 1.4f; // fudge 2 var normalScrollSpeed0 = Mathf.Pow(Mathf.Log(1f + 2f * gridSizeLodData) * mul, pow); var normalScrollSpeed1 = Mathf.Pow(Mathf.Log(1f + 4f * gridSizeLodData) * mul, pow); _mpb.SetVector(sp_GeomData, new Vector4(gridSizeLodData, gridSizeGeo, normalScrollSpeed0, normalScrollSpeed1)); // Assign LOD data to ocean shader var ldaws = OceanRenderer.Instance._lodDataAnimWaves; var ldsds = OceanRenderer.Instance._lodDataSeaDepths; var ldclip = OceanRenderer.Instance._lodDataClipSurface; var ldfoam = OceanRenderer.Instance._lodDataFoam; var ldflow = OceanRenderer.Instance._lodDataFlow; var ldshadows = OceanRenderer.Instance._lodDataShadow; _mpb.SetInt(LodDataMgr.sp_LD_SliceIndex, _lodIndex); if (ldaws != null) { ldaws.BindResultData(_mpb); } if (ldflow != null) { ldflow.BindResultData(_mpb); } else { LodDataMgrFlow.BindNull(_mpb); } if (ldfoam != null) { ldfoam.BindResultData(_mpb); } else { LodDataMgrFoam.BindNull(_mpb); } if (ldsds != null) { ldsds.BindResultData(_mpb); } else { LodDataMgrSeaFloorDepth.BindNull(_mpb); } if (ldclip != null) { ldclip.BindResultData(_mpb); } else { LodDataMgrClipSurface.BindNull(_mpb); } if (ldshadows != null) { ldshadows.BindResultData(_mpb); } else { LodDataMgrShadow.BindNull(_mpb); } var reflTex = PreparedReflections.GetRenderTexture(_currentCamera.GetHashCode()); if (reflTex) { _mpb.SetTexture(sp_ReflectionTex, reflTex); } else { _mpb.SetTexture(sp_ReflectionTex, Texture2D.blackTexture); } // Hack - due to SV_IsFrontFace occasionally coming through as true for back faces, // add a param here that forces ocean to be in underwater state. I think the root // cause here might be imprecision or numerical issues at ocean tile boundaries, although // i'm not sure why cracks are not visible in this case. var heightOffset = OceanRenderer.Instance.ViewerHeightAboveWater; _mpb.SetFloat(sp_ForceUnderwater, heightOffset < -2f ? 1f : 0f); Rend.SetPropertyBlock(_mpb.materialPropertyBlock); }
void CreateDestroySubSystems() { { if (_lodDataAnimWaves == null) { _lodDataAnimWaves = new LodDataMgrAnimWaves(this); _lodDatas.Add(_lodDataAnimWaves); } } if (CreateClipSurfaceData) { if (_lodDataClipSurface == null) { _lodDataClipSurface = new LodDataMgrClipSurface(this); _lodDatas.Add(_lodDataClipSurface); } } else { if (_lodDataClipSurface != null) { _lodDataClipSurface.OnDisable(); _lodDatas.Remove(_lodDataClipSurface); _lodDataClipSurface = null; } } if (CreateDynamicWaveSim) { if (_lodDataDynWaves == null) { _lodDataDynWaves = new LodDataMgrDynWaves(this); _lodDatas.Add(_lodDataDynWaves); } } else { if (_lodDataDynWaves != null) { _lodDataDynWaves.OnDisable(); _lodDatas.Remove(_lodDataDynWaves); _lodDataDynWaves = null; } } if (CreateFlowSim) { if (_lodDataFlow == null) { _lodDataFlow = new LodDataMgrFlow(this); _lodDatas.Add(_lodDataFlow); } if (FlowProvider != null && !(FlowProvider is QueryFlow)) { FlowProvider.CleanUp(); FlowProvider = null; } } else { if (_lodDataFlow != null) { _lodDataFlow.OnDisable(); _lodDatas.Remove(_lodDataFlow); _lodDataFlow = null; } if (FlowProvider != null && FlowProvider is QueryFlow) { FlowProvider.CleanUp(); FlowProvider = null; } } if (FlowProvider == null) { FlowProvider = _lodDataAnimWaves.Settings.CreateFlowProvider(this); } if (CreateFoamSim) { if (_lodDataFoam == null) { _lodDataFoam = new LodDataMgrFoam(this); _lodDatas.Add(_lodDataFoam); } } else { if (_lodDataFoam != null) { _lodDataFoam.OnDisable(); _lodDatas.Remove(_lodDataFoam); _lodDataFoam = null; } } if (CreateSeaFloorDepthData) { if (_lodDataSeaDepths == null) { _lodDataSeaDepths = new LodDataMgrSeaFloorDepth(this); _lodDatas.Add(_lodDataSeaDepths); } } else { if (_lodDataSeaDepths != null) { _lodDataSeaDepths.OnDisable(); _lodDatas.Remove(_lodDataSeaDepths); _lodDataSeaDepths = null; } } if (CreateShadowData) { if (_lodDataShadow == null) { _lodDataShadow = new LodDataMgrShadow(this); _lodDatas.Add(_lodDataShadow); } } else { if (_lodDataShadow != null) { _lodDataShadow.OnDisable(); _lodDatas.Remove(_lodDataShadow); _lodDataShadow = null; } } // Potential extension - add 'type' field to collprovider and change provider if settings have changed - this would support runtime changes. if (CollisionProvider == null) { CollisionProvider = _lodDataAnimWaves.Settings.CreateCollisionProvider(); } }
internal static void UpdatePostProcessMaterial( RenderTexture source, Camera camera, PropertyWrapperMaterial underwaterPostProcessMaterialWrapper, UnderwaterSphericalHarmonicsData sphericalHarmonicsData, bool isMeniscusEnabled, bool copyParamsFromOceanMaterial, bool debugViewPostProcessMask, float horizonSafetyMarginMultiplier, float farPlaneMultiplier, int dataSliceOffset ) { Material underwaterPostProcessMaterial = underwaterPostProcessMaterialWrapper.material; if (copyParamsFromOceanMaterial) { // Measured this at approx 0.05ms on dell laptop underwaterPostProcessMaterial.CopyPropertiesFromMaterial(OceanRenderer.Instance.OceanMaterial); } // Enable/Disable meniscus. if (isMeniscusEnabled) { underwaterPostProcessMaterial.EnableKeyword("CREST_MENISCUS"); } else { underwaterPostProcessMaterial.DisableKeyword("CREST_MENISCUS"); } // Enabling/disabling keywords each frame don't seem to have large measurable overhead if (debugViewPostProcessMask) { underwaterPostProcessMaterial.EnableKeyword(DEBUG_VIEW_OCEAN_MASK); } else { underwaterPostProcessMaterial.DisableKeyword(DEBUG_VIEW_OCEAN_MASK); } underwaterPostProcessMaterial.SetFloat(LodDataMgr.sp_LD_SliceIndex, 0); underwaterPostProcessMaterial.SetVector(sp_InstanceData, new Vector4(OceanRenderer.Instance.ViewerAltitudeLevelAlpha, 0f, 0f, OceanRenderer.Instance.CurrentLodCount)); LodDataMgrAnimWaves.Bind(underwaterPostProcessMaterialWrapper); LodDataMgrSeaFloorDepth.Bind(underwaterPostProcessMaterialWrapper); LodDataMgrShadow.Bind(underwaterPostProcessMaterialWrapper); float seaLevel = OceanRenderer.Instance.SeaLevel; { // We only apply the horizon safety margin multiplier to horizon if and only if // concrete height of the camera relative to the water and the height of the camera // relative to the sea-level are the same. This ensures that in incredibly turbulent // water - if in doubt - use the neutral horizon. float seaLevelHeightDifference = camera.transform.position.y - seaLevel; if (seaLevelHeightDifference >= 0.0f ^ OceanRenderer.Instance.ViewerHeightAboveWater >= 0.0f) { horizonSafetyMarginMultiplier = 0.0f; } } { underwaterPostProcessMaterial.SetFloat(sp_OceanHeight, seaLevel); underwaterPostProcessMaterial.SetInt(sp_DataSliceOffset, dataSliceOffset); float maxOceanVerticalDisplacement = OceanRenderer.Instance.MaxVertDisplacement * 0.5f; float cameraYPosition = camera.transform.position.y; float nearPlaneFrustumWorldHeight; { float current = camera.ViewportToWorldPoint(new Vector3(0f, 0f, camera.nearClipPlane)).y; float maxY = current, minY = current; current = camera.ViewportToWorldPoint(new Vector3(0f, 1f, camera.nearClipPlane)).y; maxY = Mathf.Max(maxY, current); minY = Mathf.Min(minY, current); current = camera.ViewportToWorldPoint(new Vector3(1f, 0f, camera.nearClipPlane)).y; maxY = Mathf.Max(maxY, current); minY = Mathf.Min(minY, current); current = camera.ViewportToWorldPoint(new Vector3(1f, 1f, camera.nearClipPlane)).y; maxY = Mathf.Max(maxY, current); minY = Mathf.Min(minY, current); nearPlaneFrustumWorldHeight = maxY - minY; } // We don't both setting the horizon value if we know we are going to be having to apply the post-processing // effect full-screen anyway. bool forceFullShader = (cameraYPosition + nearPlaneFrustumWorldHeight + maxOceanVerticalDisplacement) <= seaLevel; underwaterPostProcessMaterial.SetFloat(sp_OceanHeight, seaLevel); if (forceFullShader) { underwaterPostProcessMaterial.EnableKeyword(FULL_SCREEN_EFFECT); } else { underwaterPostProcessMaterial.DisableKeyword(FULL_SCREEN_EFFECT); } } // Have to set these explicitly as the built-in transforms aren't in world-space for the blit function if (XRHelpers.IsSinglePass) { XRHelpers.SetViewProjectionMatrices(camera); Matrix4x4 cameraProjectionMatrix = camera.projectionMatrix; camera.projectionMatrix = XRHelpers.LeftEyeProjectionMatrix; var inverseViewProjectionMatrix = (XRHelpers.LeftEyeProjectionMatrix * camera.worldToCameraMatrix).inverse; underwaterPostProcessMaterial.SetMatrix(sp_InvViewProjection, inverseViewProjectionMatrix); { GetHorizonPosNormal(camera, Camera.MonoOrStereoscopicEye.Left, seaLevel, horizonSafetyMarginMultiplier, farPlaneMultiplier, out Vector2 pos, out Vector2 normal); underwaterPostProcessMaterial.SetVector(sp_HorizonPosNormal, new Vector4(pos.x, pos.y, normal.x, normal.y)); } camera.projectionMatrix = XRHelpers.RightEyeProjectionMatrix; var inverseViewProjectionMatrixRightEye = (XRHelpers.RightEyeProjectionMatrix * camera.worldToCameraMatrix).inverse; underwaterPostProcessMaterial.SetMatrix(sp_InvViewProjectionRight, inverseViewProjectionMatrixRightEye); { GetHorizonPosNormal(camera, Camera.MonoOrStereoscopicEye.Right, seaLevel, horizonSafetyMarginMultiplier, farPlaneMultiplier, out Vector2 pos, out Vector2 normal); underwaterPostProcessMaterial.SetVector(sp_HorizonPosNormalRight, new Vector4(pos.x, pos.y, normal.x, normal.y)); } // Revert to original matrix. Not sure if we need to do this. camera.projectionMatrix = cameraProjectionMatrix; } else { if (XRHelpers.IsNewSDKRunning) { XRHelpers.SetViewProjectionMatrices(camera); } var inverseViewProjectionMatrix = (camera.projectionMatrix * camera.worldToCameraMatrix).inverse; underwaterPostProcessMaterial.SetMatrix(sp_InvViewProjection, inverseViewProjectionMatrix); { GetHorizonPosNormal(camera, Camera.MonoOrStereoscopicEye.Mono, seaLevel, horizonSafetyMarginMultiplier, farPlaneMultiplier, out Vector2 pos, out Vector2 normal); underwaterPostProcessMaterial.SetVector(sp_HorizonPosNormal, new Vector4(pos.x, pos.y, normal.x, normal.y)); } } // Not sure why we need to do this - blit should set it...? underwaterPostProcessMaterial.SetTexture(sp_MainTex, source); // Compute ambient lighting SH { // We could pass in a renderer which would prime this lookup. However it doesnt make sense to use an existing render // at different position, as this would then thrash it and negate the priming functionality. We could create a dummy invis GO // with a dummy Renderer which might be enoguh, but this is hacky enough that we'll wait for it to become a problem // rather than add a pre-emptive hack. UnityEngine.Profiling.Profiler.BeginSample("Underwater sample spherical harmonics"); LightProbes.GetInterpolatedProbe(OceanRenderer.Instance.ViewCamera.transform.position, null, out SphericalHarmonicsL2 sphericalHarmonicsL2); sphericalHarmonicsL2.Evaluate(sphericalHarmonicsData._shDirections, sphericalHarmonicsData._ambientLighting); underwaterPostProcessMaterial.SetVector(sp_AmbientLighting, sphericalHarmonicsData._ambientLighting[0]); UnityEngine.Profiling.Profiler.EndSample(); } }
void CreateDestroyLodDatas() { { if (_lodDataAnimWaves == null) { _lodDataAnimWaves = new LodDataMgrAnimWaves(this); _lodDatas.Add(_lodDataAnimWaves); } } if (CreateClipSurfaceData) { if (_lodDataClipSurface == null) { _lodDataClipSurface = new LodDataMgrClipSurface(this); _lodDatas.Add(_lodDataClipSurface); } } else { if (_lodDataClipSurface != null) { _lodDataClipSurface.OnDisable(); _lodDatas.Remove(_lodDataClipSurface); _lodDataClipSurface = null; } } if (CreateDynamicWaveSim) { if (_lodDataDynWaves == null) { _lodDataDynWaves = new LodDataMgrDynWaves(this); _lodDatas.Add(_lodDataDynWaves); } } else { if (_lodDataDynWaves != null) { _lodDataDynWaves.OnDisable(); _lodDatas.Remove(_lodDataDynWaves); _lodDataDynWaves = null; } } if (CreateFlowSim) { if (_lodDataFlow == null) { _lodDataFlow = new LodDataMgrFlow(this); _lodDatas.Add(_lodDataFlow); } } else { if (_lodDataFlow != null) { _lodDataFlow.OnDisable(); _lodDatas.Remove(_lodDataFlow); _lodDataFlow = null; } } if (CreateFoamSim) { if (_lodDataFoam == null) { _lodDataFoam = new LodDataMgrFoam(this); _lodDatas.Add(_lodDataFoam); } } else { if (_lodDataFoam != null) { _lodDataFoam.OnDisable(); _lodDatas.Remove(_lodDataFoam); _lodDataFoam = null; } } if (CreateSeaFloorDepthData) { if (_lodDataSeaDepths == null) { _lodDataSeaDepths = new LodDataMgrSeaFloorDepth(this); _lodDatas.Add(_lodDataSeaDepths); } } else { if (_lodDataSeaDepths != null) { _lodDataSeaDepths.OnDisable(); _lodDatas.Remove(_lodDataSeaDepths); _lodDataSeaDepths = null; } } if (CreateShadowData) { if (_lodDataShadow == null) { _lodDataShadow = new LodDataMgrShadow(this); _lodDatas.Add(_lodDataShadow); } } else { if (_lodDataShadow != null) { _lodDataShadow.OnDisable(); _lodDatas.Remove(_lodDataShadow); _lodDataShadow = null; } } }
// Called when visible to a camera void OnWillRenderObject() { // check if built-in pipeline being used if (Camera.current != null) { _currentCamera = Camera.current; } // Depth texture is used by ocean shader for transparency/depth fog, and for fading out foam at shoreline. _currentCamera.depthTextureMode |= DepthTextureMode.Depth; if (_rend.sharedMaterial != OceanRenderer.Instance.OceanMaterial) { _rend.sharedMaterial = OceanRenderer.Instance.OceanMaterial; } // per instance data if (_mpb == null) { _mpb = new MaterialPropertyBlock(); } _rend.GetPropertyBlock(_mpb); // blend LOD 0 shape in/out to avoid pop, if the ocean might scale up later (it is smaller than its maximum scale) var needToBlendOutShape = _lodIndex == 0 && OceanRenderer.Instance.ScaleCouldIncrease; var meshScaleLerp = needToBlendOutShape ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 0f; // blend furthest normals scale in/out to avoid pop, if scale could reduce var needToBlendOutNormals = _lodIndex == _totalLodCount - 1 && OceanRenderer.Instance.ScaleCouldDecrease; var farNormalsWeight = needToBlendOutNormals ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 1f; _mpb.SetVector("_InstanceData", new Vector4(meshScaleLerp, farNormalsWeight, _lodIndex)); // geometry data // compute grid size of geometry. take the long way to get there - make sure we land exactly on a power of two // and not inherit any of the lossy-ness from lossyScale. var scale_pow_2 = Mathf.Pow(2f, Mathf.Round(Mathf.Log(transform.lossyScale.x) / Mathf.Log(2f))); var gridSizeGeo = scale_pow_2 / (0.25f * _lodDataResolution / _geoDownSampleFactor); var gridSizeLodData = gridSizeGeo / _geoDownSampleFactor; var mul = 1.875f; // fudge 1 var pow = 1.4f; // fudge 2 var normalScrollSpeed0 = Mathf.Pow(Mathf.Log(1f + 2f * gridSizeLodData) * mul, pow); var normalScrollSpeed1 = Mathf.Pow(Mathf.Log(1f + 4f * gridSizeLodData) * mul, pow); _mpb.SetVector("_GeomData", new Vector4(gridSizeLodData, gridSizeGeo, normalScrollSpeed0, normalScrollSpeed1)); // assign lod data to ocean shader var ldaws = OceanRenderer.Instance._lodDataAnimWaves; var ldsds = OceanRenderer.Instance._lodDataSeaDepths; var ldfoam = OceanRenderer.Instance._lodDataFoam; var ldflow = OceanRenderer.Instance._lodDataFlow; var ldshadows = OceanRenderer.Instance._lodDataShadow; ldaws.BindResultData(_lodIndex, 0, _mpb); if (ldflow) ldflow.BindResultData(_lodIndex, 0, _mpb); if (ldfoam) ldfoam.BindResultData(_lodIndex, 0, _mpb); if (ldsds) ldsds.BindResultData(_lodIndex, 0, _mpb); if (ldshadows) ldshadows.BindResultData(_lodIndex, 0, _mpb); else LodDataMgrShadow.BindNull(0, _mpb); if (_lodIndex + 1 < OceanRenderer.Instance.CurrentLodCount) { ldaws.BindResultData(_lodIndex + 1, 1, _mpb); if (ldflow) ldflow.BindResultData(_lodIndex + 1, 1, _mpb); if (ldfoam) ldfoam.BindResultData(_lodIndex + 1, 1, _mpb); if (ldsds) ldsds.BindResultData(_lodIndex + 1, 1, _mpb); if (ldshadows) ldshadows.BindResultData(_lodIndex + 1, 1, _mpb); else LodDataMgrShadow.BindNull(1, _mpb); } var reflTex = OceanPlanarReflection.GetRenderTexture(_currentCamera.targetDisplay); if (reflTex) { _mpb.SetTexture(_reflectionTexId, reflTex); } else { _mpb.SetTexture(_reflectionTexId, Texture2D.blackTexture); } // Hack - due to SV_IsFrontFace occasionally coming through as true for backfaces, // add a param here that forces ocean to be in undrwater state. I think the root // cause here might be imprecision or numerical issues at ocean tile boundaries, although // i'm not sure why cracks are not visible in this case. var heightOffset = OceanRenderer.Instance.ViewerHeightAboveWater; _mpb.SetFloat("_ForceUnderwater", heightOffset < -2f ? 1f : 0f); _rend.SetPropertyBlock(_mpb); if (_drawRenderBounds) { _rend.bounds.DebugDraw(); } }
private void LateUpdate() { if (OceanRenderer.Instance == null) { _rend.enabled = false; return; } float waterHeight = OceanRenderer.Instance.SeaLevel; // Pass true in last arg for a crap reason - in edit mode LateUpdate can be called very frequently, and the height sampler mistakenly thinks // this is erroneous and complains. _sampleWaterHeight.Init(transform.position, 0f, true); _sampleWaterHeight.Sample(ref waterHeight); float heightOffset = transform.position.y - waterHeight; // Disable skirt when camera not close to water. In the first few frames collision may not be avail, in that case no choice // but to assume enabled. In the future this could detect if camera is far enough under water, render a simple quad to avoid // finding the intersection line. _rend.enabled = heightOffset < _maxHeightAboveWater; if (_rend.enabled) { if (_copyParamsEachFrame) { _rend.sharedMaterial.CopyPropertiesFromMaterial(OceanRenderer.Instance.OceanMaterial); } // Assign lod0 shape - trivial but bound every frame because lod transform comes from here if (_mpb == null) { _mpb = new PropertyWrapperMPB(); } _rend.GetPropertyBlock(_mpb.materialPropertyBlock); // Underwater rendering uses displacements for intersecting the waves with the near plane, and ocean depth/shadows for ScatterColour() _mpb.SetInt(LodDataMgr.sp_LD_SliceIndex, 0); OceanRenderer.Instance._lodDataAnimWaves.BindResultData(_mpb); if (OceanRenderer.Instance._lodDataSeaDepths != null) { OceanRenderer.Instance._lodDataSeaDepths.BindResultData(_mpb); } else { LodDataMgrSeaFloorDepth.BindNull(_mpb); } if (OceanRenderer.Instance._lodDataShadow != null) { OceanRenderer.Instance._lodDataShadow.BindResultData(_mpb); } else { LodDataMgrShadow.BindNull(_mpb); } _mpb.SetFloat(sp_HeightOffset, heightOffset); _mpb.SetVector(OceanChunkRenderer.sp_InstanceData, new Vector3(OceanRenderer.Instance.ViewerAltitudeLevelAlpha, 0f, 0f)); _rend.SetPropertyBlock(_mpb.materialPropertyBlock); } }