public void RenderMesh() { #region Heightmap Backup { // create heightmap backup if (!heightMapBackupRt) { CreateHeightMapBackup(); } // heightmap backup debug view Graphics.Blit(heightMapBackupRt, m_rtCollection[RenderTextureIDs.heightMapOriginal], new Vector2(1f, 1f), new Vector2(0f, 0f)); } #endregion Heightmap Backup #region Mesh Projection { Projector projector = CreateProjector(); { // ensure the render matrix will be the dimensions of the render texture projector.PushTargetTexture(); { projector.camera.targetTexture = m_rtCollection[RenderTextureIDs.cameraDepthRT].RT; RenderTexture prevRT = RenderTexture.active; { // change camera, otherwise scene view camera would be used Camera prevCamera = Camera.current; Camera.SetupCurrent(projector.camera); { RenderTexture rt = m_rtCollection[RenderTextureIDs.cameraDepthRT].RT; Graphics.SetRenderTarget(rt); GL.Viewport(new Rect(0, 0, rt.width, rt.height)); GL.Clear(true, true, projector.camera.backgroundColor, 1f); GL.PushMatrix(); { GL.LoadProjectionMatrix(projector.camera.projectionMatrix); GL.PushMatrix(); { RenderGameObjectNow(gameObject, (int)0); } GL.PopMatrix(); } GL.PopMatrix(); // restore matrix Graphics.ClearRandomWriteTargets(); } Camera.SetupCurrent(prevCamera); } // Calling SetRenderTarget with just a RenderTexture argument is the same as setting RenderTexture.active property. // https://docs.unity3d.com/ScriptReference/Graphics.SetRenderTarget.html RenderTexture.active = prevRT; } projector.PopTargetTexture(); Graphics.Blit(m_rtCollection[RenderTextureIDs.cameraDepthRT], m_rtCollection[RenderTextureIDs.meshHeight]); } #region Perspective correction if (settings.direction == Direction.BottomUp) { ConvertBottomUpToTopDown(m_rtCollection[RenderTextureIDs.meshHeight], m_rtCollection[RenderTextureIDs.meshHeight], projector, gameObject); } #endregion Perspective correction ReleaseProjector(projector); } #endregion Mesh Projection #region Effects { if (settings.blur) { ApplyBlur(m_rtCollection[RenderTextureIDs.meshHeight], m_rtCollection[RenderTextureIDs.meshHeight]); } } #endregion Effects #region Create Heightmap { RenderTexture prev = RenderTexture.active; { RenderTexture.active = m_rtCollection[RenderTextureIDs.combinedHeightMap]; Material mat = GetBlendMaterial(); mat.SetTexture("_BlendTex", m_rtCollection[RenderTextureIDs.meshHeight]); mat.SetInt("_BlendMode", (int)settings.blendMode); mat.SetFloat("_Blend", settings.valueBlend); Graphics.Blit(m_rtCollection[RenderTextureIDs.heightMapOriginal], m_rtCollection[RenderTextureIDs.combinedHeightMap], mat); if (settings.featureEnabled) { ToolboxHelper.CopyTextureToTerrainHeight( settings.terrain.terrainData, m_rtCollection[RenderTextureIDs.combinedHeightMap], new Vector2Int(0, 0), m_rtCollection[RenderTextureIDs.combinedHeightMap].RT.width, 1, 0f, 1f); } } RenderTexture.active = prev; } #endregion Create Heightmap #region Debug View { if (settings.debug) { Graphics.Blit(settings.terrain.terrainData.heightmapTexture, m_rtCollection[RenderTextureIDs.heightMapCurrent]); } } #endregion Debug View }