예제 #1
0
        private static StarData CreateStarData(Vector3 eulerAngles, WeightedArray <Material> materials)
        {
            var materialIndex = Rand.WeightedIndex(materials);
            var distance      = Rand.Value > .01f ? Rand.Range(6900, 9000) : Rand.Range(5000, 6000);
            var size          = Rand.Range(400, 420 + 200 * 1 - materialIndex / (float)materials.Values.Length);

            if (materialIndex < 2)
            {
                size += Rand.Range(0, 300);
            }

            return(new StarData(eulerAngles, distance, size, materialIndex));
        }
예제 #2
0
파일: FlowModel.cs 프로젝트: umm/cafu_flow
 public FlowModel(
     float start,
     float end,
     float intervalTime,
     float intervalDivationTime,
     int units,
     WeightedArray <int> lotteryWeightArray
     )
 {
     this.Start                = start;
     this.End                  = end;
     this.IntervalTime         = intervalTime;
     this.IntervalDivationTime = intervalDivationTime;
     this.Units                = units;
     this.LotteryWeightArray   = lotteryWeightArray;
     this.GeneratedCount       = 0;
     this.emitList             = new List <KeyValuePair <int, float> >();
 }
예제 #3
0
        public static T WeightedElement <T>(WeightedArray <T> array)
        {
            var total = 0f;

            foreach (var element in array.Values)
            {
                total += element.Item2;
            }

            var randomValue = Range(0f, total);

            foreach (var(item1, item2) in array.Values)
            {
                randomValue -= item2;
                if (randomValue <= 0)
                {
                    return(item1);
                }
            }

            return(array.Values[0].Item1);
        }
예제 #4
0
        public static int WeightedIndex <T>(WeightedArray <T> array)
        {
            var total = 0f;

            foreach (var element in array.Values)
            {
                total += element.Item2;
            }

            var randomValue = Range(0f, total);

            for (var i = 0; i < array.Values.Length; i++)
            {
                var(item1, item2) = array.Values[i];
                randomValue      -= item2;
                if (randomValue <= 0)
                {
                    return(i);
                }
            }

            return(0);
        }
        private void CreateOptions(WeightedArray objects, ObjectOptions[] options, ref int selectedIndex, float halfWidth)
        {
            bool disabled = (!objects?.HasWeightedElementsNonNull()) ?? true;

            Color oldColor = GUI.backgroundColor;

            GUI.backgroundColor = BACKGROUND_COLOR_SUB_MENU;
            EditorGUILayout.BeginVertical(SubBoxStyle);
            halfWidth          -= (DataHolder.IsWindow) ? BoxMargin : BoxMargin * 0.5f;
            GUI.backgroundColor = oldColor;

            using (new EditorGUI.DisabledScope(disabled))
            {
                GUIStyle centerStyle = new GUIStyle();
                centerStyle.alignment = TextAnchor.MiddleCenter;

                string[] objectStrings = null;
                IEnumerable <Tuple <WeightedObject, int> > objectsNonNull = null;

                if (!disabled)
                {
                    objectsNonNull = objects.Objects
                                     .Select((o, i) => new Tuple <WeightedObject, int>(o, i))
                                     .Where((o) => o.Item1.Object != null && o.Item1.Weight > 0);

                    objectStrings = objectsNonNull
                                    .Select((a, i) => i + ": " + objects.GetChance(a.Item1).ToString("F2") + "%) " + a.Item1.Object.name)
                                    .ToArray();

                    if (selectedIndex >= objectStrings.Count())
                    {
                        selectedIndex = Mathf.Max(objectStrings.Count() - 1, 0);
                    }
                }
                else
                {
                    objectStrings = new string[] { "" };
                    selectedIndex = 0;
                }

                ObjectOptions selectedOptions;
                EditorGUILayout.BeginHorizontal(RowStyle);
                EditorGUILayout.BeginVertical(SubLeftColumnStyle, GUILayout.MaxWidth(halfWidth));

                // Pass in an empty label or set EditorGUIUtility.fieldWidth to the EditorGUIUtility.labelWidth (== LABEL_WIDTH)
                // otherwise the width will be incorrect when the vertical scrollbar is visible
                selectedIndex = EditorGUILayout.Popup("", selectedIndex, objectStrings, PopupStyle);
                if (disabled)
                {
                    selectedOptions = new ObjectOptions();
                }
                else
                {
                    selectedOptions = options[objectsNonNull.ElementAt(selectedIndex).Item2];
                }
                EditorGUIUtility.fieldWidth = 0;
                EditorGUILayout.EndVertical();
                EditorGUILayout.BeginVertical(RightColumnStyle, GUILayout.MaxWidth(halfWidth));
                EditorGUILayout.LabelField("");
                EditorGUILayout.EndVertical();
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal(RowStyle);
                using (new EditorGUILayout.VerticalScope(SubLeftColumnStyle, GUILayout.MaxWidth(halfWidth)))
                {
                    selectedOptions.Parent = (Transform)EditorGUILayout.ObjectField("Parent:", selectedOptions.Parent, typeof(Transform), true);

                    if (selectedOptions.Parent != null)
                    {
                        PrefabType prefabType = PrefabUtility.GetPrefabType(selectedOptions.Parent);
                        if (prefabType == PrefabType.Prefab || prefabType == PrefabType.ModelPrefab)
                        {
                            selectedOptions.Parent = null;
                        }
                    }

                    EditorGUILayout.BeginHorizontal(RowStyle);
                    EditorGUIUtility.labelWidth = LABEL_WIDTH;
                    EditorGUILayout.PrefixLabel("Rotate:", RowStyle);
                    EditorGUIUtility.labelWidth = 12.0f;
                    using (new EditorGUI.DisabledScope(selectedOptions.AlignToSurface == true))
                    {
                        selectedOptions.RotateX = EditorGUILayout.Toggle("X", selectedOptions.RotateX, ToggleStyle, GUILayout.ExpandWidth(false));
                    }
                    selectedOptions.RotateY = EditorGUILayout.Toggle("Y", selectedOptions.RotateY, ToggleStyle, GUILayout.ExpandWidth(false));
                    using (new EditorGUI.DisabledScope(selectedOptions.AlignToSurface == true))
                    {
                        selectedOptions.RotateZ = EditorGUILayout.Toggle("Z", selectedOptions.RotateZ, ToggleStyle, GUILayout.ExpandWidth(false));
                    }
                    EditorGUILayout.EndHorizontal();

                    EditorGUILayout.BeginHorizontal(RowStyle);
                    EditorGUIUtility.labelWidth = LABEL_WIDTH;
                    EditorGUILayout.PrefixLabel("Scale:", RowStyle);
                    EditorGUIUtility.labelWidth = 12.0f;
                    selectedOptions.ScaleX      = EditorGUILayout.Toggle("X", selectedOptions.ScaleX, ToggleStyle, GUILayout.ExpandWidth(false));
                    selectedOptions.ScaleY      = EditorGUILayout.Toggle("Y", selectedOptions.ScaleY, ToggleStyle, GUILayout.ExpandWidth(false));
                    selectedOptions.ScaleZ      = EditorGUILayout.Toggle("Z", selectedOptions.ScaleZ, ToggleStyle, GUILayout.ExpandWidth(false));
                    EditorGUILayout.EndHorizontal();

                    EditorGUIUtility.labelWidth    = LABEL_WIDTH;
                    selectedOptions.AlignToSurface = EditorGUILayout.Toggle("Align Z to surface:", selectedOptions.AlignToSurface, ToggleStyle);
                    selectedOptions.UniformScaling = EditorGUILayout.Toggle("Uniform scaling:", selectedOptions.UniformScaling, ToggleStyle);
                }
                using (new EditorGUILayout.VerticalScope(RightColumnStyle, GUILayout.MaxWidth(halfWidth)))
                {
                    // Magic number 9
                    EditorGUIUtility.fieldWidth = (halfWidth - LABEL_WIDTH) * 0.5f - 9.0f;

                    EditorGUILayout.BeginHorizontal(RowStyle);
                    // Add non-empty label to skip the first column
                    EditorGUILayout.PrefixLabel(" ", EditorStyles.objectField, RowStyle);
                    // Because unity layouting is pain
                    EditorGUIUtility.labelWidth = 0.0000001f;
                    EditorGUILayout.LabelField("Min", centerStyle, GUILayout.ExpandWidth(true));
                    EditorGUILayout.LabelField("Max", centerStyle, GUILayout.ExpandWidth(true));
                    EditorGUILayout.EndHorizontal();

                    EditorGUIUtility.labelWidth = LABEL_WIDTH;

                    EditorGUILayout.BeginHorizontal(RowStyle);
                    EditorGUILayout.PrefixLabel("Scale:", RowStyle);
                    selectedOptions.MinScale = EditorGUILayout.FloatField(selectedOptions.MinScale, NumberFieldStyle);
                    selectedOptions.MaxScale = EditorGUILayout.FloatField(selectedOptions.MaxScale, NumberFieldStyle);
                    EditorGUILayout.EndHorizontal();

                    EditorGUILayout.BeginHorizontal(RowStyle);
                    EditorGUILayout.PrefixLabel("Height offset:", RowStyle);
                    selectedOptions.MinHeightOffset = EditorGUILayout.FloatField(selectedOptions.MinHeightOffset, NumberFieldStyle);
                    selectedOptions.MaxHeightOffset = EditorGUILayout.FloatField(selectedOptions.MaxHeightOffset, NumberFieldStyle);
                    EditorGUILayout.EndHorizontal();

                    EditorGUILayout.BeginHorizontal(RowStyle);
                    EditorGUILayout.PrefixLabel("Face . Up:", RowStyle);
                    selectedOptions.MinDot = EditorGUILayout.FloatField(selectedOptions.MinDot, NumberFieldStyle);
                    selectedOptions.MaxDot = EditorGUILayout.FloatField(selectedOptions.MaxDot, NumberFieldStyle);
                    EditorGUILayout.EndHorizontal();

                    EditorGUIUtility.fieldWidth = 0;
                    using (new EditorGUI.DisabledScope(selectedOptions.ScaleY == false))
                    {
                        selectedOptions.ScaleHeightOffset = EditorGUILayout.Toggle(new GUIContent("Scale height offset:", "If enabled the height offset is scaled by the calculated Y scale"), selectedOptions.ScaleHeightOffset, ToggleStyle);
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
            EditorGUILayout.EndVertical();
        }
        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);
        }
예제 #7
0
 public FlowModelSpy(float start, float end, float intervalTime, float intervalDivationTime, int units,
                     WeightedArray <int> lotteryWeightArray) : base(start, end, intervalTime, intervalDivationTime, units,
                                                                    lotteryWeightArray)
 {
 }