public override void OnInspectorGUI() { PlanarReflections planarReflections = (PlanarReflections)target; EditorGUILayout.Space(); EditorGUILayout.LabelField("General", EditorStyles.helpBox); var labelWidth = EditorGUIUtility.labelWidth; EditorGUIUtility.labelWidth = 160; EditorGUILayout.PropertyField(reflectionTarget, EditorGUIUtility.TrTextContent("Water Object")); EditorGUILayout.PropertyField(hideReflectionCamera, EditorGUIUtility.TrTextContent("Hide Reflection Camera")); EditorGUIUtility.labelWidth = labelWidth; EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Visuals", EditorStyles.helpBox); EditorGUILayout.PropertyField(renderScale, EditorGUIUtility.TrTextContent("Render Scale")); EditorGUILayout.PropertyField(reflectionPlaneOffset, EditorGUIUtility.TrTextContent("Height Offset")); EditorGUILayout.PropertyField(reflectionLayer, EditorGUIUtility.TrTextContent("Layer Mask")); EditorGUILayout.PropertyField(reflectSkybox, EditorGUIUtility.TrTextContent("Reflect Skybox")); EditorGUILayout.Space(); EditorGUILayout.HelpBox("Render scale, height offset, layer mask and skybox reflection settings are shared between all water objects.", MessageType.Info); EditorGUILayout.Space(); EditorGUILayout.Space(); serializedObject.ApplyModifiedProperties(); }
private void UpdateReflectionCamera(Camera realCamera) { if (_reflectionCamera == null) { _reflectionCamera = InitializeReflectionCamera(); } Vector3 pos = Vector3.zero; Vector3 normal = Vector3.up; if (reflectionTarget != null) { pos = reflectionTarget.transform.position + Vector3.up * reflectionPlaneOffset; normal = reflectionTarget.transform.up; } UpdateCamera(realCamera, _reflectionCamera); _reflectionCamera.gameObject.hideFlags = (hideReflectionCamera) ? HideFlags.HideAndDontSave : HideFlags.DontSave; #if UNITY_EDITOR EditorApplication.DirtyHierarchyWindowSorting(); #endif var d = -Vector3.Dot(normal, pos); var reflectionPlane = new Vector4(normal.x, normal.y, normal.z, d); var reflection = Matrix4x4.identity; reflection *= Matrix4x4.Scale(new Vector3(1, -1, 1)); PlanarReflections.CalculateReflectionMatrix(ref reflection, reflectionPlane); var oldPosition = realCamera.transform.position - new Vector3(0, pos.y * 2, 0); var newPosition = PlanarReflections.ReflectPosition(oldPosition); _reflectionCamera.transform.forward = Vector3.Scale(realCamera.transform.forward, new Vector3(1, -1, 1)); _reflectionCamera.worldToCameraMatrix = realCamera.worldToCameraMatrix * reflection; var clipPlane = CameraSpacePlane(_reflectionCamera, pos - Vector3.up * 0.1f, normal, 1.0f); var projection = realCamera.CalculateObliqueMatrix(clipPlane); _reflectionCamera.projectionMatrix = projection; _reflectionCamera.cullingMask = reflectionLayer; _reflectionCamera.transform.position = newPosition; }