Example #1
0
        private void InstantiateWaterObjects()
        {
            var   waterObject   = _waterObject.gameObject;
            var   spawnPosition = waterObject.transform.position;
            var   spawnRotation = waterObject.transform.rotation;
            var   parent        = waterObject.transform.parent;
            float waterWidth    = _waterObject.MainModule.Width;

            _waterObject.MainModule.LargeWaterAreaManager = this;
            _leftMostWaterSimulationModule = _waterObject.SimulationModule;
            _leftMostWaterSimulationModule.IsControlledByLargeWaterAreaManager = true;

            WaterSimulationModule previousSimulationModule = _leftMostWaterSimulationModule;

            for (int i = 1; i < _waterObjectCount; i++)
            {
                spawnPosition.x += waterWidth;

                Game2DWater waterObjectClone = Instantiate(waterObject, spawnPosition, spawnRotation, parent).GetComponent <Game2DWater>();
                waterObjectClone.MainModule.LargeWaterAreaManager = this;

                WaterSimulationModule currentSimulationModule = waterObjectClone.SimulationModule;

                currentSimulationModule.IsControlledByLargeWaterAreaManager = true;
                currentSimulationModule.PreviousWaterSimulationModule       = previousSimulationModule;
                previousSimulationModule.NextWaterSimulationModule          = currentSimulationModule;

                previousSimulationModule = currentSimulationModule;
            }

            _rightMostWaterSimulationModule = previousSimulationModule;
        }
Example #2
0
        void DrawEdgeColliderPropertyField()
        {
            Game2DWater water2D = target as Game2DWater;

            EditorGUI.BeginChangeCheck();
            bool hasEdgeCollider = EditorGUILayout.Toggle(useEdgeCollider2DLabel, water2D.GetComponent <EdgeCollider2D> () != null);

            if (EditorGUI.EndChangeCheck())
            {
                if (hasEdgeCollider)
                {
                    EdgeCollider2D edgeCollider = water2D.gameObject.AddComponent <EdgeCollider2D> ();
                    float          xOffset, yOffset;
                    xOffset             = -water2D.WaterSize.x / 2f;
                    yOffset             = water2D.WaterSize.y / 2f;
                    edgeCollider.points = new [] {
                        new Vector2(xOffset, yOffset),
                        new Vector2(xOffset, -yOffset),
                        new Vector2(-xOffset, -yOffset),
                        new Vector2(-xOffset, yOffset)
                    };
                    water2D.OnValidate();
                }
                else
                {
                    DestroyImmediate(water2D.GetComponent <EdgeCollider2D> ());
                }
            }
        }
Example #3
0
        private void DrawWaterWireframe()
        {
            Game2DWater    water2D  = target as Game2DWater;
            List <Vector3> vertices = new List <Vector3> ();

            water2D.GetComponent <MeshFilter> ().sharedMesh.GetVertices(vertices);
            int start, end;

            if (water2D.UseCustomBoundaries)
            {
                start = 2;
                end   = vertices.Count - 4;
            }
            else
            {
                start = 0;
                end   = vertices.Count - 2;
            }
            Matrix4x4 localToWorldMatrix = water2D.transform.localToWorldMatrix;

            using (new Handles.DrawingScope(wireframeColor, localToWorldMatrix)) {
                for (int i = start; i <= end; i += 2)
                {
                    Handles.DrawLine(vertices [i], vertices [i + 1]);
                }
            }
        }
Example #4
0
        private void DrawBuoyancyEffectorSurfaceLevelGuideline()
        {
            Game2DWater water2D   = target as Game2DWater;
            Vector2     size      = water2D.WaterSize / 2f;
            float       y         = size.y * (1f - 2f * water2D.BuoyancyEffectorSurfaceLevel);
            Vector3     lineStart = water2D.transform.TransformPoint(-size.x, y, 0f);
            Vector3     lineEnd   = water2D.transform.TransformPoint(size.x, y, 0f);

            Handles.color = buoyancyEffectorSurfaceLevelGuidelineColor;
            Handles.DrawLine(lineStart, lineEnd);
            Handles.color = Color.white;
        }
Example #5
0
        private void DrawWaterResizer()
        {
            Game2DWater water2D = target as Game2DWater;
            Bounds      bounds  = water2D.GetComponent <MeshRenderer> ().bounds;
            Vector3     min     = bounds.min;
            Vector3     max     = bounds.max;
            Vector3     center  = bounds.center;

            Vector3 upHandle    = new Vector3(center.x, max.y, center.z);
            Vector3 downHandle  = new Vector3(center.x, min.y, center.z);
            Vector3 rightHandle = new Vector3(max.x, center.y, center.z);
            Vector3 leftHandle  = new Vector3(min.x, center.y, center.z);

            float handlesSize = HandleUtility.GetHandleSize(center) * 0.5f;

            EditorGUI.BeginChangeCheck();
            Vector3 upPos    = Handles.Slider(upHandle, Vector3.up, handlesSize, Handles.ArrowHandleCap, 1f);
            Vector3 downPos  = Handles.Slider(downHandle, Vector3.down, handlesSize, Handles.ArrowHandleCap, 1f);
            Vector3 rightPos = Handles.Slider(rightHandle, Vector3.right, handlesSize, Handles.ArrowHandleCap, 1f);
            Vector3 leftPos  = Handles.Slider(leftHandle, Vector3.left, handlesSize, Handles.ArrowHandleCap, 1f);

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(water2D, "changing water size");
                Vector3 newCenter = new Vector3((rightPos.x + leftPos.x) / 2f, (upPos.y + downPos.y) / 2f, center.z);
                upPos    = water2D.transform.worldToLocalMatrix.MultiplyPoint(upPos);
                downPos  = water2D.transform.worldToLocalMatrix.MultiplyPoint(downPos);
                rightPos = water2D.transform.worldToLocalMatrix.MultiplyPoint(rightPos);
                leftPos  = water2D.transform.worldToLocalMatrix.MultiplyPoint(leftPos);
                Vector2 newSize = new Vector2(Mathf.Clamp(rightPos.x - leftPos.x, 0f, float.MaxValue), Mathf.Clamp(upPos.y - downPos.y, 0f, float.MaxValue));
                if (newSize.x > 0f && newSize.y > 0f)
                {
                    if (water2D.UseCustomBoundaries)
                    {
                        float halfWidth = newSize.x / 2f;
                        water2D.SecondCustomBoundary = Mathf.Clamp(water2D.SecondCustomBoundary, -halfWidth, halfWidth);
                        water2D.FirstCustomBoundary  = Mathf.Clamp(water2D.FirstCustomBoundary, -halfWidth, halfWidth);
                    }
                    water2D.WaterSize          = newSize;
                    water2D.transform.position = newCenter;
                    EditorUtility.SetDirty(water2D);
                }
            }

            if (GUI.changed)
            {
                SceneView.RepaintAll();
            }
        }
Example #6
0
        private bool DrawFixScalingField()
        {
            Game2DWater water2D = target as Game2DWater;
            Vector2     scale   = water2D.transform.localScale;

            if (!Mathf.Approximately(scale.x, 1f) || !Mathf.Approximately(scale.y, 1f))
            {
                EditorGUILayout.HelpBox(nonUniformScaleWarning, MessageType.Warning, true);
                if (GUILayout.Button(fixScalingButtonLabel))
                {
                    waterSize.vector2Value       = Vector2.Scale(waterSize.vector2Value, scale);
                    water2D.transform.localScale = Vector3.one;
                    return(true);
                }
            }
            return(false);
        }
Example #7
0
        public override void OnInspectorGUI()
        {
            Game2DWater water2D        = target as Game2DWater;
            bool        hasRefraction  = false;
            bool        hasReflection  = false;
            bool        hasAudioSource = water2D.GetComponent <AudioSource> () != null;

            Material water2DMaterial = water2D.GetComponent <MeshRenderer> ().sharedMaterial;

            if (water2DMaterial)
            {
                hasRefraction = water2DMaterial.IsKeywordEnabled("Water2D_Refraction");
                hasReflection = water2DMaterial.IsKeywordEnabled("Water2D_Reflection");
            }
            if (PrefabUtility.GetPrefabType(target) == PrefabType.Prefab)
            {
                hasRefraction = true;
                hasReflection = true;
            }

            serializedObject.Update();

            if (!isMultiEditing)
            {
                DrawFixScalingField();
            }

            waterPropertiesExpanded.target = EditorGUILayout.Foldout(waterPropertiesExpanded.target, waterPropertiesFoldoutLabel, true);
            using (var group = new EditorGUILayout.FadeGroupScope(waterPropertiesExpanded.faded)) {
                if (group.visible)
                {
                    EditorGUI.indentLevel++;
                    EditorGUI.BeginChangeCheck();
                    EditorGUILayout.LabelField(meshPropertiesLabel, EditorStyles.boldLabel);
                    EditorGUI.BeginChangeCheck();
                    EditorGUILayout.PropertyField(waterSize, waterSizeLabel);
                    EditorGUILayout.PropertyField(subdivisionsCountPerUnit, subdivisionsCountPerUnitLabel);

                    EditorGUILayout.Space();
                    EditorGUILayout.LabelField(wavePropertiesLabel, EditorStyles.boldLabel);
                    EditorGUILayout.PropertyField(stiffness, stiffnessLabel);
                    EditorGUILayout.PropertyField(spread, spreadLabel);
                    EditorGUILayout.Slider(damping, 0f, 1f, dampingLabel);
                    EditorGUILayout.PropertyField(useCustomBoundaries, useCustomBoundariesLabel);
                    if (useCustomBoundaries.boolValue)
                    {
                        EditorGUILayout.PropertyField(firstCustomBoundary, firstCustomBoundaryLabel);
                        EditorGUILayout.PropertyField(secondCustomBoundary, secondCustomBoundaryLabel);
                    }

                    EditorGUILayout.Space();
                    EditorGUILayout.LabelField(disturbancePropertiesLabel, EditorStyles.boldLabel);
                    EditorGUILayout.PropertyField(minimumDisturbance, minimumDisturbanceLabel);
                    EditorGUILayout.PropertyField(maximumDisturbance, maximumDisturbanceLabel);
                    EditorGUILayout.PropertyField(velocityMultiplier, velocityMultiplierLabel);

                    EditorGUILayout.Space();
                    EditorGUILayout.LabelField(collisionPropertiesLabel, EditorStyles.boldLabel);
                    EditorGUILayout.PropertyField(collisionMinimumDepth, collisionMinimumDepthLabel);
                    EditorGUILayout.PropertyField(collisionMaximumDepth, collisionMaximumDepthLabel);
                    EditorGUILayout.PropertyField(collisionMask, collisionMaskLabel);

                    EditorGUILayout.Space();
                    EditorGUILayout.LabelField(miscLabel, EditorStyles.boldLabel);
                    EditorGUILayout.Slider(buoyancyEffectorSurfaceLevel, 0f, 1f, buoyancyEffectorSurfaceLevelLabel);
                    if (!isMultiEditing)
                    {
                        DrawEdgeColliderPropertyField();
                    }

                    EditorGUI.indentLevel--;
                }
            }

            if (hasRefraction)
            {
                refractionPropertiesExpanded.target = EditorGUILayout.Foldout(refractionPropertiesExpanded.target, refractionPropertiesFoldoutLabel, true);
                using (var group = new EditorGUILayout.FadeGroupScope(refractionPropertiesExpanded.faded)) {
                    if (group.visible)
                    {
                        EditorGUI.indentLevel++;
                        EditorGUILayout.PropertyField(refractionCullingMask, cameraCullingMaskLabel);
                        EditorGUILayout.Slider(refractionRenderTextureResizeFactor, 0f, 1f, refractionRenderTextureResizeFactorLabel);
                        EditorGUI.indentLevel--;
                    }
                }
            }

            if (hasReflection)
            {
                reflectionPropertiesExpanded.target = EditorGUILayout.Foldout(reflectionPropertiesExpanded.target, reflectionPropertiesFoldoutLabel, true);
                using (var group = new EditorGUILayout.FadeGroupScope(reflectionPropertiesExpanded.faded)) {
                    if (group.visible)
                    {
                        EditorGUI.indentLevel++;
                        EditorGUILayout.PropertyField(reflectionCullingMask, cameraCullingMaskLabel);
                        EditorGUILayout.Slider(reflectionRenderTextureResizeFactor, 0f, 1f, reflectionRenderTextureResizeFactorLabel);
                        EditorGUILayout.PropertyField(reflectionZOffset, reflectionZOffsetLabel);
                        EditorGUI.indentLevel--;
                    }
                }
            }

            renderingSettingsExpanded.target = EditorGUILayout.Foldout(renderingSettingsExpanded.target, renderingSettingsFoldoutLabel, true);
            using (var group = new EditorGUILayout.FadeGroupScope(renderingSettingsExpanded.faded)) {
                if (group.visible)
                {
                    EditorGUI.indentLevel++;
                    if (hasReflection || hasRefraction)
                    {
                        EditorGUILayout.PropertyField(farClipPlane, farClipPlaneLabel);
                        EditorGUILayout.PropertyField(renderPixelLights, renderPixelLightsLabel);
                        EditorGUILayout.PropertyField(allowMSAA, allowMSAALabel);
                        EditorGUILayout.PropertyField(allowHDR, allowHDRLabel);
                    }
                    DrawSortingLayerField(sortingLayerID, sortingOrder);
                    EditorGUI.indentLevel--;
                }
            }

            if (hasAudioSource)
            {
                audioSettingsExpanded.target = EditorGUILayout.Foldout(audioSettingsExpanded.target, audioSettingsFoldoutLabel, true);
                using (var group = new EditorGUILayout.FadeGroupScope(audioSettingsExpanded.faded)) {
                    if (group.visible)
                    {
                        EditorGUI.indentLevel++;
                        EditorGUILayout.PropertyField(splashAudioClip, splashAudioClipLabel);
                        EditorGUILayout.Slider(minimumAudioPitch, -3f, 3f, minimumAudioPitchLabel);
                        EditorGUILayout.Slider(maximumAudioPitch, -3f, 3f, maximumAudioPicthLabel);
                        EditorGUILayout.HelpBox(audioPitchMessage, MessageType.None, true);
                        EditorGUI.indentLevel--;
                    }
                }
            }
            else
            {
                EditorGUILayout.HelpBox(audioSourceMesage, MessageType.None, true);
            }

            if (!hasRefraction)
            {
                EditorGUILayout.HelpBox(refractionMessage, MessageType.None, true);
            }
            if (!hasReflection)
            {
                EditorGUILayout.HelpBox(reflectionMessage, MessageType.None, true);
            }

            serializedObject.ApplyModifiedProperties();
        }
Example #8
0
        private void InstantiateWaterObjects()
        {
            var waterSize         = _waterObject.MainModule.WaterSize;
            var shouldResizeWater = ValidateWaterSize(ref waterSize);

            if (shouldResizeWater)
            {
                _waterObject.MainModule.SetSize(waterSize, true);
            }

            var waterObject        = _waterObject.gameObject;
            var spawnPosition      = waterObject.transform.position;
            var spawnRotation      = waterObject.transform.rotation;
            var parent             = waterObject.transform.parent;
            var waterWidth         = waterSize.x;
            var areSineWavesActive = _waterObject.SimulationModule.AreSineWavesActive;

            int surfaceVertexCount;

            if (_waterObject.SimulationModule.IsUsingCustomBoundaries)
            {
                surfaceVertexCount = 4 + Mathf.RoundToInt(_waterObject.MeshModule.SubdivisionsPerUnit * (_waterObject.SimulationModule.RightCustomBoundary - _waterObject.SimulationModule.LeftCustomBoundary));
            }
            else
            {
                surfaceVertexCount = 2 + Mathf.RoundToInt(_waterObject.MeshModule.SubdivisionsPerUnit * waterWidth);
            }

            if (_isConstrained)
            {
                if (spawnPosition.x + waterWidth * (_waterObjectCount - 1) + waterWidth * 0.5f > (_constrainedRegionXMax - 0.001f))
                {
                    spawnPosition.x = _constrainedRegionXMax - waterWidth * (_waterObjectCount - 1) - waterWidth * 0.5f;
                }
                else if (spawnPosition.x - waterWidth * 0.5f < (_constrainedRegionXMin + 0.001f))
                {
                    spawnPosition.x = _constrainedRegionXMin + waterWidth * 0.5f;
                }
                else
                {
                    spawnPosition.x = _constrainedRegionXMin + waterWidth * 0.5f + waterWidth * Mathf.Round(((spawnPosition.x - _constrainedRegionXMin) / waterWidth));
                }
            }
            else
            {
                spawnPosition.x -= waterWidth;
            }

            _waterObject.MainModule.Position = spawnPosition;
            _waterObject.MainModule.LargeWaterAreaManager = this;
            _leftMostWaterSimulationModule = _waterObject.SimulationModule;
            _leftMostWaterSimulationModule.IsControlledByLargeWaterAreaManager = true;

            WaterSimulationModule previousSimulationModule = _leftMostWaterSimulationModule;

            for (int i = 1; i < _waterObjectCount; i++)
            {
                spawnPosition.x += waterWidth;

                Game2DWater waterObjectClone = Instantiate(waterObject, spawnPosition, spawnRotation, parent).GetComponent <Game2DWater>();

                if (shouldResizeWater)
                {
                    waterObjectClone.MainModule.SetSize(waterSize, true);
                }

                waterObjectClone.MainModule.LargeWaterAreaManager = this;

                WaterSimulationModule currentSimulationModule = waterObjectClone.SimulationModule;

                currentSimulationModule.IsControlledByLargeWaterAreaManager = true;
                currentSimulationModule.PreviousWaterSimulationModule       = previousSimulationModule;
                previousSimulationModule.NextWaterSimulationModule          = currentSimulationModule;

                if (areSineWavesActive)
                {
                    currentSimulationModule.SineWavesOffset = previousSimulationModule.SineWavesOffset + surfaceVertexCount - 1;
                }

                previousSimulationModule = currentSimulationModule;
            }

            _rightMostWaterSimulationModule = previousSimulationModule;
        }