public static void UpdateShadowInfoWithBrightestSpot(CubemapInfo cubemapInfo) { LookDevResources.m_LookDevCubeToLatlong.SetTexture("_MainTex", cubemapInfo.cubemap); LookDevResources.m_LookDevCubeToLatlong.SetVector("_WindowParams", new Vector4(10000f, -1000f, 2f, 0f)); LookDevResources.m_LookDevCubeToLatlong.SetVector("_CubeToLatLongParams", new Vector4(0.0174532924f * cubemapInfo.angleOffset, 0.5f, 1f, 3f)); LookDevResources.m_LookDevCubeToLatlong.SetPass(0); int num = 250; int num2 = 125; Graphics.Blit(cubemapInfo.cubemap, LookDevResources.m_BrightestPointRT, LookDevResources.m_LookDevCubeToLatlong); LookDevResources.m_BrightestPointTexture.ReadPixels(new Rect(0f, 0f, (float)num, (float)num2), 0, 0, false); LookDevResources.m_BrightestPointTexture.Apply(); Color[] pixels = LookDevResources.m_BrightestPointTexture.GetPixels(); float num3 = 0f; for (int i = 0; i < num2; i++) { for (int j = 0; j < num; j++) { Vector3 vector = new Vector3(pixels[i * num + j].r, pixels[i * num + j].g, pixels[i * num + j].b); float num4 = vector.x * 0.2126729f + vector.y * 0.7151522f + vector.z * 0.072175f; if (num3 < num4) { Vector2 vector2 = LookDevEnvironmentWindow.PositionToLatLong(new Vector2((float)j / (float)(num - 1) * 2f - 1f, (float)i / (float)(num2 - 1) * 2f - 1f)); cubemapInfo.shadowInfo.latitude = vector2.x; cubemapInfo.shadowInfo.longitude = vector2.y - cubemapInfo.angleOffset; num3 = num4; } } } }
// Find brightest spot of the cubemap static public void UpdateShadowInfoWithBrightestSpot(CubemapInfo cubemapInfo) { m_LookDevCubeToLatlong.SetTexture("_MainTex", cubemapInfo.cubemap); m_LookDevCubeToLatlong.SetVector("_WindowParams", new Vector4(10000, -1000.0f, 2, 0.0f)); // Neutral value to not clip m_LookDevCubeToLatlong.SetVector("_CubeToLatLongParams", new Vector4(Mathf.Deg2Rad * cubemapInfo.angleOffset, 0.5f, 1.0f, 3.0f)); // We use LOD 3 to take a region rather than a single pixel in the map m_LookDevCubeToLatlong.SetPass(0); int width = (int)LookDevEnvironmentWindow.m_HDRIWidth; int height = (int)LookDevEnvironmentWindow.m_latLongHeight; // Convert cubemap to a 2D LatLong to read on CPU Graphics.Blit(cubemapInfo.cubemap, m_BrightestPointRT, m_LookDevCubeToLatlong); m_BrightestPointTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0, false); m_BrightestPointTexture.Apply(); // CPU read back // From Doc: The returned array is a flattened 2D array, where pixels are laid out left to right, bottom to top (i.e. row after row) Color[] color = m_BrightestPointTexture.GetPixels(); float maxLum = 0.0f; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { Vector3 rgb = new Vector3(color[y * width + x].r, color[y * width + x].g, color[y * width + x].b); float lum = rgb.x * 0.2126729f + rgb.y * 0.7151522f + rgb.z * 0.0721750f; if (maxLum < lum) { Vector2 vec = LookDevEnvironmentWindow.PositionToLatLong(new Vector2(((float)x / (float)(width - 1)) * 2.0f - 1.0f, ((float)y / (float)(height - 1)) * 2.0f - 1.0f)); cubemapInfo.shadowInfo.latitude = vec.x; cubemapInfo.shadowInfo.longitude = vec.y - cubemapInfo.angleOffset; maxLum = lum; } } } }
public void OnGUI(int windowID) { if (!(this.m_LookDevView == null)) { List <CubemapInfo> hdriList = this.m_LookDevView.envLibrary.hdriList; bool flag = 146f * (float)hdriList.Count > this.m_PositionInLookDev.height; if (flag) { this.m_ScrollPosition = EditorGUILayout.BeginScrollView(this.m_ScrollPosition, new GUILayoutOption[0]); } else { this.m_ScrollPosition = new Vector2(0f, 0f); } if (hdriList.Count == 1) { Color color = GUI.color; GUI.color = Color.gray; Vector2 vector = GUI.skin.label.CalcSize(LookDevEnvironmentWindow.styles.sDragAndDropHDRIText); Rect position = new Rect(this.m_PositionInLookDev.width * 0.5f - vector.x * 0.5f, this.m_PositionInLookDev.height * 0.5f - vector.y * 0.5f, vector.x, vector.y); GUI.Label(position, LookDevEnvironmentWindow.styles.sDragAndDropHDRIText); GUI.color = color; } for (int i = 0; i < hdriList.Count; i++) { CubemapInfo cubemapInfo = hdriList[i]; ShadowInfo shadowInfo = cubemapInfo.shadowInfo; int intProperty = this.m_LookDevView.config.GetIntProperty(LookDevProperty.HDRI, LookDevEditionContext.Left); int num = this.m_LookDevView.config.GetIntProperty(LookDevProperty.HDRI, LookDevEditionContext.Right); if (this.m_LookDevView.config.lookDevMode == LookDevMode.Single1 || this.m_LookDevView.config.lookDevMode == LookDevMode.Single2) { num = -1; } bool flag2 = i == intProperty || i == num; Color selectionColor = Color.black; Color selectionColor2 = Color.black; GUIStyle style = EditorStyles.miniLabel; if (flag2) { if (i == intProperty) { selectionColor = LookDevView.m_FirstViewGizmoColor; selectionColor2 = LookDevView.m_FirstViewGizmoColor; style = LookDevEnvironmentWindow.styles.sLabelStyleFirstContext; } else if (i == num) { selectionColor = LookDevView.m_SecondViewGizmoColor; selectionColor2 = LookDevView.m_SecondViewGizmoColor; style = LookDevEnvironmentWindow.styles.sLabelStyleSecondContext; } if (intProperty == num) { selectionColor = LookDevView.m_FirstViewGizmoColor; selectionColor2 = LookDevView.m_SecondViewGizmoColor; style = LookDevEnvironmentWindow.styles.sLabelStyleBothContext; } } GUILayout.BeginVertical(new GUILayoutOption[] { GUILayout.Width(250f) }); int num2 = hdriList.FindIndex((CubemapInfo x) => x == this.m_SelectedCubemapInfo); if ((this.m_SelectedCubemap != null || this.m_DragBeingPerformed) && this.GetInsertionRect(i).Contains(Event.current.mousePosition) && ((num2 - i != 0 && num2 - i != -1) || num2 == -1)) { GUILayout.Label(GUIContent.none, LookDevEnvironmentWindow.styles.sSeparatorStyle, new GUILayoutOption[0]); GUILayoutUtility.GetRect(250f, 16f); } GUILayout.Label(GUIContent.none, LookDevEnvironmentWindow.styles.sSeparatorStyle, new GUILayoutOption[0]); GUILayout.BeginHorizontal(new GUILayoutOption[] { GUILayout.Width(250f), GUILayout.Height(18f) }); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(i.ToString()); stringBuilder.Append(" - "); stringBuilder.Append(cubemapInfo.cubemap.name); GUILayout.Label(stringBuilder.ToString(), style, new GUILayoutOption[] { GUILayout.Height(18f), GUILayout.MaxWidth(175f) }); GUILayout.FlexibleSpace(); if (GUILayout.Button(LookDevEnvironmentWindow.styles.sEnvControlIcon, LookDevView.styles.sToolBarButton, new GUILayoutOption[0])) { Rect last = GUILayoutUtility.topLevel.GetLast(); PopupWindow.Show(last, new LookDevEnvironmentWindow.EnvSettingsWindow(this.m_LookDevView, cubemapInfo)); GUIUtility.ExitGUI(); } using (new EditorGUI.DisabledScope(cubemapInfo.cubemap == LookDevResources.m_DefaultHDRI)) { if (GUILayout.Button(LookDevEnvironmentWindow.styles.sCloseIcon, LookDevView.styles.sToolBarButton, new GUILayoutOption[0])) { this.m_LookDevView.envLibrary.RemoveHDRI(cubemapInfo.cubemap); } } GUILayout.EndHorizontal(); Rect lastRect = GUILayoutUtility.GetLastRect(); if (Event.current.type == EventType.MouseDown && lastRect.Contains(Event.current.mousePosition)) { Event.current.Use(); } Rect rect = GUILayoutUtility.GetRect(250f, 125f); rect.width = 253f; float num3 = 24f; float num4 = num3 * 0.5f; float latitude = shadowInfo.latitude; float longitude = shadowInfo.longitude; Vector2 vector2 = this.LatLongToPosition(latitude, longitude + cubemapInfo.angleOffset) * 0.5f + new Vector2(0.5f, 0.5f); Rect position2 = rect; position2.x = position2.x + vector2.x * rect.width - num4; position2.y = position2.y + (1f - vector2.y) * rect.height - num4; position2.width = num3; position2.height = num3; Rect position3 = rect; position3.x = position3.x + vector2.x * rect.width - num4 * 0.5f; position3.y = position3.y + (1f - vector2.y) * rect.height - num4 * 0.5f; position3.width = num3 * 0.5f; position3.height = num3 * 0.5f; Rect rect2; Rect textureRect; this.GetFrameAndShadowTextureRect(rect, out rect2, out textureRect); if (this.m_LookDevView.config.enableShadowCubemap) { EditorGUIUtility.AddCursorRect(position3, MouseCursor.Pan); } if (Event.current.type == EventType.MouseDown && rect.Contains(Event.current.mousePosition)) { if (!Event.current.control && Event.current.button == 0 && this.m_SelectedCubeMapOffsetIndex == -1) { if (this.m_LookDevView.config.enableShadowCubemap && position3.Contains(Event.current.mousePosition)) { this.m_SelectedLightIconIndex = i; this.m_SelectedShadowInfo = shadowInfo; Undo.RecordObject(this.m_LookDevView.envLibrary, "Light Icon selection"); this.m_SelectedShadowInfo.latitude = this.m_SelectedShadowInfo.latitude + 0.0001f; this.m_SelectedShadowInfo.longitude = this.m_SelectedShadowInfo.longitude + 0.0001f; } if (this.m_SelectedShadowInfo == null) { Rect rect3 = rect2; rect3.x += 100f; rect3.y += 4f; rect3.width = 11f; rect3.height = 11f; if (this.m_LookDevView.config.enableShadowCubemap && rect3.Contains(Event.current.mousePosition)) { Undo.RecordObject(this.m_LookDevView.envLibrary, "Update shadow cubemap"); hdriList[i].SetCubemapShadowInfo(hdriList[i]); this.m_LookDevView.envLibrary.dirty = true; } else { if (this.m_LookDevView.config.enableShadowCubemap && textureRect.Contains(Event.current.mousePosition)) { this.m_SelectedShadowCubemapOwnerInfo = hdriList[i]; this.m_SelectedCubemapInfo = this.m_SelectedShadowCubemapOwnerInfo.cubemapShadowInfo; } else { this.m_SelectedCubemapInfo = hdriList[i]; } this.m_SelectedPositionOffset = Event.current.mousePosition - new Vector2(rect.x, rect.y); this.m_RenderOverlayThumbnailOnce = true; } } } else if (Event.current.control && Event.current.button == 0 && this.m_SelectedCubemapInfo == null && this.m_SelectedShadowInfo == null) { this.m_SelectedCubeMapOffsetIndex = i; this.m_SelectedCubeMapOffsetValue = cubemapInfo.angleOffset - this.ComputeAngleOffsetFromMouseCoord(Event.current.mousePosition); } GUIUtility.hotControl = this.m_LookDevView.hotControl; Event.current.Use(); } if (Event.current.GetTypeForControl(this.m_LookDevView.hotControl) == EventType.MouseDrag) { if (this.m_SelectedShadowInfo == shadowInfo && this.m_SelectedLightIconIndex == i) { Vector2 mousePosition = Event.current.mousePosition; mousePosition.x = (mousePosition.x - rect.x) / rect.width * 2f - 1f; mousePosition.y = (1f - (mousePosition.y - rect.y) / rect.height) * 2f - 1f; Vector2 vector3 = LookDevEnvironmentWindow.PositionToLatLong(mousePosition); this.m_SelectedShadowInfo.latitude = vector3.x; this.m_SelectedShadowInfo.longitude = vector3.y - cubemapInfo.angleOffset; this.m_LookDevView.envLibrary.dirty = true; } } if (Event.current.type == EventType.Repaint) { this.DrawLatLongThumbnail(cubemapInfo, cubemapInfo.angleOffset, 1f, 1f, rect); if (this.m_LookDevView.config.enableShadowCubemap) { if (cubemapInfo.cubemapShadowInfo != cubemapInfo || (this.m_HoveringCubeMapIndex == i && this.m_SelectedCubemapInfo != cubemapInfo)) { CubemapInfo infos = cubemapInfo.cubemapShadowInfo; if (this.m_HoveringCubeMapIndex == i && this.m_SelectedCubemapInfo != cubemapInfo) { infos = this.m_SelectedCubemapInfo; } float alpha = 1f; if (this.m_SelectedShadowInfo == shadowInfo) { alpha = 0.1f; } else if (this.m_HoveringCubeMapIndex == i && this.m_SelectedCubemapInfo != cubemapInfo && cubemapInfo.cubemapShadowInfo != this.m_SelectedCubemapInfo) { alpha = 0.5f; } this.DrawLatLongThumbnail(infos, cubemapInfo.angleOffset, 0.3f, alpha, textureRect); GL.sRGBWrite = (QualitySettings.activeColorSpace == ColorSpace.Linear); GUI.DrawTexture(rect2, LookDevEnvironmentWindow.styles.sLatlongFrameTexture); GL.sRGBWrite = false; } GL.sRGBWrite = (QualitySettings.activeColorSpace == ColorSpace.Linear); GUI.DrawTexture(position2, LookDevEnvironmentWindow.styles.sLightTexture); GL.sRGBWrite = false; } if (flag2) { this.DrawSelectionFeedback(rect, selectionColor, selectionColor2); } } GUILayout.EndVertical(); } GUILayout.BeginVertical(new GUILayoutOption[] { GUILayout.Width(250f) }); if ((this.m_SelectedCubemap != null || this.m_DragBeingPerformed) && this.GetInsertionRect(hdriList.Count).Contains(Event.current.mousePosition)) { GUILayout.Label(GUIContent.none, LookDevEnvironmentWindow.styles.sSeparatorStyle, new GUILayoutOption[0]); GUILayoutUtility.GetRect(250f, 16f); GUILayout.Label(GUIContent.none, LookDevEnvironmentWindow.styles.sSeparatorStyle, new GUILayoutOption[0]); } GUILayout.EndVertical(); if (flag) { EditorGUILayout.EndScrollView(); } this.HandleMouseInput(); this.RenderOverlayThumbnailIfNeeded(); if (Event.current.type == EventType.Repaint) { if (this.m_SelectedCubemap != null) { this.m_LookDevView.Repaint(); } } } }