Ejemplo n.º 1
0
        static Vector3 GetPositionForNewBrush()
        {
            Vector3 newPosition = Vector3.zero;

            if (SceneView.lastActiveSceneView != null)
            {
                Transform cameraTransform = SceneView.lastActiveSceneView.camera.transform;
                Ray       ray             = new Ray(cameraTransform.position, cameraTransform.forward);

                // First of all try to cast against existing brushes
                List <PolygonRaycastHit> hits = csgModel.RaycastBrushesAll(ray, false);
                if (hits.Count > 0)
                {
                    newPosition = hits[0].Point;
                    // Back a unit, since the brush is around 2 units in each dimensions
                    newPosition += hits[0].Normal;
                    newPosition -= csgModel.GetComponent <Transform>().position;

                    if (CurrentSettings.PositionSnappingEnabled)
                    {
                        float snapDistance = CurrentSettings.PositionSnapDistance;
                        newPosition = MathHelper.RoundVector3(newPosition, snapDistance);
                    }
                }
                else
                {
                    // Couldn't hit an existing brush, so try to cast against the grid
                    float hitDistance = 0;
                    Plane activePlane = GetActivePlane();
                    if (activePlane.Raycast(ray, out hitDistance))
                    {
                        newPosition = ray.GetPoint(hitDistance);
                        // Back a unit, since the brush is around 2 units in each dimensions
                        newPosition += activePlane.normal;
                        newPosition -= csgModel.GetComponent <Transform>().position;

                        if (CurrentSettings.PositionSnappingEnabled)
                        {
                            float snapDistance = CurrentSettings.PositionSnapDistance;
                            newPosition = MathHelper.RoundVector3(newPosition, snapDistance);
                        }
                    }
                    else
                    {
                        // Couldn't hit the grid, probably because they're looking up at the sky, so fallback to the camera pivot point
                        newPosition = SceneView.lastActiveSceneView.pivot;
                        if (CurrentSettings.PositionSnappingEnabled)
                        {
                            float snapDistance = CurrentSettings.PositionSnapDistance;
                            newPosition = MathHelper.RoundVector3(newPosition, snapDistance);
                        }
                    }
                }
            }
            return(newPosition);
        }