コード例 #1
0
        /// <summary>
        /// Add prefabs, mode Poisson
        /// </summary>
        public void AddPrefabs_Poisson_Any(Vector3 position, Vector3 normal)
        {
            GameObject container = editorTarget.container as GameObject;

            float brushSize   = editorTarget.brushSettings.brushSize;
            float brushRadius = brushSize / 2.0f;
            float discRadius  = editorTarget.brushSettings.poissonDiscSize / 2;

            PoissonDiscSampler sampler = new PoissonDiscSampler(brushSize, brushSize, discRadius);

            foreach (Vector2 sample in sampler.Samples())
            {
                // brush is currenlty a disc => ensure the samples are within the disc
                if (Vector2.Distance(sample, new Vector2(brushRadius, brushRadius)) > brushRadius)
                {
                    continue;
                }

                // x/z come from the poisson sample
                float x = position.x + sample.x - brushRadius;
                float z = position.z + sample.y - brushRadius;

                float   y = position.y + editorTarget.brushSettings.poissonDiscRaycastOffset;
                Vector3 currentPosition = new Vector3(x, y, z);

                // TODO: raycast hit against layer
                //       see https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
                if (Physics.Raycast(currentPosition, Vector3.down, out RaycastHit raycastHitDown, Mathf.Infinity))
                {
                    y = raycastHitDown.point.y;
                }
コード例 #2
0
        /// <summary>
        /// Get the list of samples.
        /// The samples are re-used unless forceNew is true. This is usually after the current sample set has been used for painting.
        /// Otherwise the sample set will be used for drawing the gizmo
        /// </summary>
        /// <param name="brushSize"></param>
        /// <param name="brushRadius"></param>
        /// <param name="discRadius"></param>
        /// <param name="forceNew"></param>
        /// <returns></returns>
        public IEnumerable <Vector2> Samples(float brushSize, float brushRadius, float discRadius, bool forceNewDistribution)
        {
            if (discRadius < MIN_DISC_SIZE)
            {
                Debug.LogError("Poisson Disc Size is too small. Setting it to " + MIN_DISC_SIZE);
                discRadius = MIN_DISC_SIZE;
            }

            bool createNewList = forceNewDistribution || sampler == null || sampleParameters.HasChanged(brushSize, brushRadius, discRadius);

            if (createNewList)
            {
                sampler     = new PoissonDiscSampler(brushSize, brushSize, discRadius);
                samplesList = CreateSamplesList(brushSize, brushSize, discRadius);
            }

            // store parameters for reuse of the list
            sampleParameters = new SampleParameters(brushSize, brushRadius, discRadius);

            return(samplesList);
        }