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; }
protected virtual void BindData(IPropertyWrapper properties, Texture applyData, bool blendOut, ref LodTransform.RenderData[] renderData, bool sourceLod = false) { if (applyData) { properties.SetTexture(GetParamIdSampler(sourceLod), applyData); } var lt = OceanRenderer.Instance._lodTransform; for (int lodIdx = 0; lodIdx < OceanRenderer.Instance.CurrentLodCount; lodIdx++) { // NOTE: gets zeroed by unity, see https://www.alanzucconi.com/2016/10/24/arrays-shaders-unity-5-4/ _BindData_paramIdPosScales[lodIdx] = new Vector4( renderData[lodIdx]._posSnapped.x, renderData[lodIdx]._posSnapped.z, OceanRenderer.Instance.CalcLodScale(lodIdx), 0f); _BindData_paramIdOceans[lodIdx] = new Vector4(renderData[lodIdx]._texelWidth, renderData[lodIdx]._textureRes, 1f, 1f / renderData[lodIdx]._textureRes); } // Duplicate the last element as the shader accesses element {slice index + 1] in a few situations. This way going // off the end of this parameter is the same as going off the end of the texture array with our clamped sampler. _BindData_paramIdPosScales[OceanRenderer.Instance.CurrentLodCount] = _BindData_paramIdPosScales[OceanRenderer.Instance.CurrentLodCount - 1]; _BindData_paramIdOceans[OceanRenderer.Instance.CurrentLodCount] = _BindData_paramIdOceans[OceanRenderer.Instance.CurrentLodCount - 1]; properties.SetVectorArray(LodTransform.ParamIdPosScale(sourceLod), _BindData_paramIdPosScales); properties.SetVectorArray(LodTransform.ParamIdOcean(sourceLod), _BindData_paramIdOceans); }
public static int ParamIdSampler(int slot) { if (_paramsSampler == null) { LodTransform.CreateParamIDs(ref _paramsSampler, "_LD_Sampler_SeaFloorDepth_"); } return(_paramsSampler[slot]); }
public static int ParamIdSampler(int slot) { if (_paramsSampler == null) { LodTransform.CreateParamIDs(ref _paramsSampler, "_LD_Sampler_DynamicWaves_"); } return(_paramsSampler[slot]); }
protected virtual void BindData(int lodIdx, int shapeSlot, IPropertyWrapper properties, Texture applyData, bool blendOut, ref LodTransform.RenderData renderData) { if (applyData) { properties.SetTexture(GetParamIdSampler(shapeSlot), applyData); } var lt = OceanRenderer.Instance._lods[lodIdx]; properties.SetVector(LodTransform.ParamIdPosScale(shapeSlot), new Vector3(renderData._posSnapped.x, renderData._posSnapped.z, lt.transform.lossyScale.x)); properties.SetVector(LodTransform.ParamIdOcean(shapeSlot), new Vector4(renderData._texelWidth, renderData._textureRes, 1f, 1f / renderData._textureRes)); }
protected override void BindData(int lodIdx, int shapeSlot, IPropertyWrapper properties, Texture applyData, bool blendOut, ref LodTransform.RenderData renderData) { base.BindData(lodIdx, shapeSlot, properties, applyData, blendOut, ref renderData); var lt = OceanRenderer.Instance._lods[lodIdx]; // need to blend out shape if this is the largest lod, and the ocean might get scaled down later (so the largest lod will disappear) bool needToBlendOutShape = lodIdx == OceanRenderer.Instance.CurrentLodCount - 1 && OceanRenderer.Instance.ScaleCouldDecrease && blendOut; float shapeWeight = needToBlendOutShape ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 1f; properties.SetVector(LodTransform.ParamIdOcean(shapeSlot), new Vector4( lt._renderData._texelWidth, lt._renderData._textureRes, shapeWeight, 1f / lt._renderData._textureRes)); }
protected override void BindData(IPropertyWrapper properties, Texture applyData, bool blendOut, ref LodTransform.RenderData[] renderData, bool sourceLod = false) { base.BindData(properties, applyData, blendOut, ref renderData, sourceLod); var lt = OceanRenderer.Instance._lodTransform; for (int lodIdx = 0; lodIdx < OceanRenderer.Instance.CurrentLodCount; lodIdx++) { // need to blend out shape if this is the largest lod, and the ocean might get scaled down later (so the largest lod will disappear) bool needToBlendOutShape = lodIdx == OceanRenderer.Instance.CurrentLodCount - 1 && OceanRenderer.Instance.ScaleCouldDecrease && blendOut; float shapeWeight = needToBlendOutShape ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 1f; _BindData_paramIdOceans[lodIdx] = new Vector4( lt._renderData[lodIdx]._texelWidth, lt._renderData[lodIdx]._textureRes, shapeWeight, 1f / lt._renderData[lodIdx]._textureRes); } properties.SetVectorArray(LodTransform.ParamIdOcean(sourceLod), _BindData_paramIdOceans); }
protected virtual void BindData(IPropertyWrapper properties, Texture applyData, bool blendOut, ref LodTransform.RenderData[] renderData, bool sourceLod = false) { if (applyData) { properties.SetTexture(GetParamIdSampler(sourceLod), applyData); } var lt = OceanRenderer.Instance._lodTransform; for (int lodIdx = 0; lodIdx < OceanRenderer.Instance.CurrentLodCount; lodIdx++) { // NOTE: gets zeroed by unity, see https://www.alanzucconi.com/2016/10/24/arrays-shaders-unity-5-4/ _BindData_paramIdPosScales[lodIdx] = new Vector4(renderData[lodIdx]._posSnapped.x, renderData[lodIdx]._posSnapped.z, lt.GetLodTransform(lodIdx).lossyScale.x, 0); _BindData_paramIdOceans[lodIdx] = new Vector4(renderData[lodIdx]._texelWidth, renderData[lodIdx]._textureRes, 1f, 1f / renderData[lodIdx]._textureRes); } properties.SetVectorArray(LodTransform.ParamIdPosScale(sourceLod), _BindData_paramIdPosScales); properties.SetVectorArray(LodTransform.ParamIdOcean(sourceLod), _BindData_paramIdOceans); }
// Drive state from OnEnable and OnDisable? OnEnable on RegisterLodDataInput seems to get called on script reload void OnEnable() { // We don't run in "prefab scenes", i.e. when editing a prefab. Bail out if prefab scene is detected. #if UNITY_EDITOR if (PrefabStageUtility.GetCurrentPrefabStage() != null) { return; } #endif if (!_primaryLight && _searchForPrimaryLightOnStartup) { _primaryLight = RenderSettings.sun; } if (!VerifyRequirements()) { enabled = false; return; } #if UNITY_EDITOR if (EditorApplication.isPlaying && !Validate(this, ValidatedHelper.DebugLog)) { enabled = false; return; } #endif _isFirstUpdate = true; Instance = this; Scale = Mathf.Clamp(Scale, _minScale, _maxScale); _lodTransform = new LodTransform(); _lodTransform.InitLODData(_lodCount); // Resolution is 4 tiles across. var baseMeshDensity = _lodDataResolution * 0.25f / _geometryDownSampleFactor; // 0.4f is the "best" value when base mesh density is 8. Scaling down from there produces results similar to // hand crafted values which looked good when the ocean is flat. _lodAlphaBlackPointFade = 0.4f / (baseMeshDensity / 8f); // We could calculate this in the shader, but we can save two subtractions this way. _lodAlphaBlackPointWhitePointFade = 1f - _lodAlphaBlackPointFade - _lodAlphaBlackPointFade; Root = OceanBuilder.GenerateMesh(this, _oceanChunkRenderers, _lodDataResolution, _geometryDownSampleFactor, _lodCount); CreateDestroySubSystems(); _commandbufferBuilder = new BuildCommandBuffer(); InitViewpoint(); if (_attachDebugGUI && GetComponent <OceanDebugGUI>() == null) { gameObject.AddComponent <OceanDebugGUI>().hideFlags = HideFlags.DontSave; } #if UNITY_EDITOR EditorApplication.update -= EditorUpdate; EditorApplication.update += EditorUpdate; #endif foreach (var lodData in _lodDatas) { lodData.OnEnable(); } _canSkipCulling = false; }