Example #1
0
        private void InitFromSelection()
        {
            GameObject selected = null;

            foreach (GameObject o in Selection.SelectedObjects)
            {
                selected = o;
                break;
            }
            if (null != selected)
            {
                GrassController controller = selected.GetComponent <GrassController>();
                if (null != controller)
                {
                    grass = controller;

                    // DEBUG - rebuild debug arrays and mesh from controller
                    grass.RebuildDebugMesh();

                    // switch ON the record button
                    uiActiveButton.Checked = true;

                    return;
                }
            }

            grass = null;
        }
Example #2
0
        private void Add(float pressure)
        {
            if (grass == null)
            {
                return;
            }

            // DEBUG
            //line.positionCount = 2 * density;

            // place based on density
            for (int k = 0; k < density; k++)
            {
                float worldBrushSize = brushSize * GlobalState.WorldScale;

                // Random on the brush disc
                Vector2 delta = worldBrushSize * Random.insideUnitCircle;

                // Create rays in a circle around the firstRay, in controller space.
                Ray     ray            = firstRay;
                Vector3 rayOriginDelta = Vector3.zero;
                if (k != 0) // except for the first point, at the center of the circle).
                {
                    rayOriginDelta.x = delta.x;
                    rayOriginDelta.y = delta.y;
                    ray.origin       =
                        toolControllerXf.TransformPoint(
                            toolControllerXf.InverseTransformPoint(firstRay.origin) + rayOriginDelta);
                }

                //line.SetPosition(2 * k + 0, ray.origin);
                //line.SetPosition(2 * k + 1, ray.origin + ray.direction);

                // if the ray hits something thats on the layer mask,
                // within the grass limit and within the y normal limit
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, 200f, hitMask.value) &&
                    currentGrassAmount < grassLimit &&
                    hit.normal.y >= (2.0f * normalLimit - 1.0f))
                {
                    if ((paintMask.value & (1 << hit.transform.gameObject.layer)) > 0)
                    {
                        Vector3 hitPos    = hit.point;
                        Vector3 hitNormal = hit.normal;
                        if (k != 0 || (Vector3.Distance(hit.point, lastPosition) > brushSize))
                        {
                            Vector3 newGrassPosition = grass.transform.InverseTransformPoint(hitPos); // to Local mesh position
                            Vector3 newGrassNormal   = grass.transform.InverseTransformDirection(hit.normal);
                            Vector2 newGrassUV       = new Vector2(widthMultiplier, heightMultiplier);
                            Color   newGrassColor    = adjustedColor;

                            // ADD POINT to the controller.
                            grass.AddPoint(newGrassPosition, newGrassNormal, newGrassUV, newGrassColor); // TODO: do not rebuild for each vertex. Add, then rebuild once.

                            currentGrassAmount++;

                            if (rayOriginDelta == Vector3.zero)
                            {
                                lastPosition = hitPos;
                            }
                        }
                    }
                }
            }

            grass.RebuildDebugMesh();
        }