Ejemplo n.º 1
0
        private void OnEnable()
        {
            PoissonPlacer placer = (PoissonPlacer)target;

            _helper = new PoissonHelper(placer);
            _helper.Init();
        }
Ejemplo n.º 2
0
        public bool RemoveAndAdd(IPoissonDataHolder obj, PoissonHelper helper)
        {
            bool existed = Remove(obj);

            SceneView.onSceneGUIDelegate += helper.OnSceneGUI;
            if (!helper.DataHolder.IsWindow)
            {
                Undo.undoRedoPerformed += helper.OnUndoRedoPerformedPlacer;
            }
            _helpers.Add(helper);
            _placers.Add(obj);

            return(existed);
        }
        private bool CreateRandomObjectAtLoc(Vector2 loc, float finalMinDistSqrd, out bool objectPlaced, WeightedArray weightedObjects, ObjectOptions[] objectOptions, bool preview)
        {
            objectPlaced = false;

            if (InBounds(loc))
            {
                bool toCloseToProcess = true;
                // Don't do InNeighbourhood for clumping algorithm
                if (finalMinDistSqrd <= 0 || (!InNeighbourhood(loc, finalMinDistSqrd, out toCloseToProcess) && !InInnerBounds(loc)))
                {
                    RaycastHit hit;
                    if (CastRay(loc, out hit))
                    {
                        int           index   = weightedObjects.GetRandomObjectIndex();
                        ObjectOptions options = objectOptions[index];

                        float dotResult = Vector3.Dot(hit.normal, Vector3.up);
                        if (dotResult >= options.MinDot && dotResult <= options.MaxDot)
                        {
                            Vector3    pos = hit.point;
                            Quaternion rot;
                            Vector3    scale;
                            ApplySettingsToObject(options, hit.normal, ref pos, out rot, out scale);

                            GameObject newObject = weightedObjects.GetGameObject(index);
                            if (newObject != null)
                            {
                                if (_activeData.SphereCollisionCheck || _activeData.BoxCollisionCheck)
                                {
                                    Bounds bounds;
                                    if (GetBounds(newObject, _activeData.BoundsMode, pos, rot, scale, out bounds))
                                    {
                                        if (ModeData.Mode == DistributionMode.Surface)
                                        {
                                            ModeData.Surface.gameObject.SetActive(false);
                                        }

                                        bool      hasOverlap    = true;
                                        LayerMask correctedMask = InternalEditorUtility.ConcatenatedLayersMaskToLayerMask(_activeData.OverlapLayerMask);
                                        if (_activeData.SphereCollisionCheck)
                                        {
                                            hasOverlap = CheckSphereOverlap(bounds, correctedMask);
                                        }
                                        if (_activeData.BoxCollisionCheck && hasOverlap)
                                        {
                                            hasOverlap = CheckBoxOverlap(bounds, rot, correctedMask);
                                        }
                                        if (ModeData.Mode == DistributionMode.Surface)
                                        {
                                            ModeData.Surface.gameObject.SetActive(true);
                                        }
                                        if (hasOverlap)
                                        {
                                            return(!toCloseToProcess);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                return(!toCloseToProcess);
                            }
                            objectPlaced = true;
                            GameObject obj = (GameObject)PrefabUtility.InstantiatePrefab(newObject);
                            obj.transform.position   = pos;
                            obj.transform.rotation   = rot;
                            obj.transform.localScale = scale;

                            // When not doing this the colliders aren't properly initialized and OverlapXXX won't work
                            obj.SetActive(false);
                            obj.SetActive(true);

                            obj.transform.parent = options.Parent;

                            EditorData.PlacedObjects[_activeLevel].Add(obj);

                            if (_currNestingLevel < _maxNestingLevel)
                            {
                                PoissonPlacer placer = obj.GetComponent <PoissonPlacer>();
                                if (placer != null && placer.enabled)
                                {
                                    PoissonHelper helper = new PoissonHelper(placer);
                                    helper.Init();
                                    bool isValidSurface, preValid, currValid, postValid;
                                    int  highestValid;
                                    helper.LoadDataHolder();
                                    helper.ValidateSettings(false, out isValidSurface, out preValid, out currValid, out postValid, out highestValid);

                                    placer.EditorData.LastFrameValid = isValidSurface && preValid && currValid && postValid;
                                    if (highestValid >= 0)
                                    {
                                        Random.State randState = Random.state;
                                        helper.DistributePoisson(0, highestValid, preview, _currNestingLevel + 1, _maxNestingLevel);
                                        Random.state = randState;
                                    }
                                    helper.StoreDataHolder();
                                }
                            }
                        }
                    }
                }
                return(!toCloseToProcess || objectPlaced);
            }
            return(false);
        }
 private void OnDestroy()
 {
     PoissonHelper.CleanupPlacedObjects(_helper.EditorData, 0);
 }
 public PoissonDiskSamplingWindow()
 {
     _helper = new PoissonHelper(this);
 }