Пример #1
0
        private void UpdateToolPaint(Vector3 position, Quaternion _)
        {
            // ON TRIGGER
            VRInput.ButtonEvent(VRInput.primaryController, CommonUsages.trigger, () =>
            {
                BeginPaint();

                paintPrevPosition = Vector3.zero;
                undoGroup         = new CommandGroup("Paint");
            },
                                () =>
            {
                try
                {
                    EndCurrentPaint();
                }
                finally
                {
                    if (null != undoGroup)
                    {
                        undoGroup.Submit();
                        undoGroup = null;
                    }
                }
            });

            float triggerValue = VRInput.GetValue(VRInput.primaryController, CommonUsages.trigger);

            // Change brush size
            if (navigation.CanUseControls(NavigationMode.UsedControls.RIGHT_JOYSTICK))
            {
                Vector2 val = VRInput.GetValue(VRInput.primaryController, CommonUsages.primary2DAxis);
                if (val != Vector2.zero)
                {
                    if (val.y > 0.3f)
                    {
                        brushSize += 0.001f;
                    }
                    if (val.y < -0.3f)
                    {
                        brushSize -= 0.001f;
                    }
                    brushSize             = Mathf.Clamp(brushSize, 0.001f, 0.5f);
                    mouthpiece.localScale = new Vector3(brushSize, brushSize, brushSize);
                }
            }

            paintLineRenderer.enabled = false;
            Vector3 penPosition = mouthpiece.position;

            if (paintOnSurface)
            {
                Vector3 direction = transform.forward; // (paintItem.position - centerEye.position).normalized;
                Vector3 startRay  = penPosition + mouthpiece.lossyScale.x * direction;
                Vector3 endRay    = startRay + 1000f * direction;
                paintLineRenderer.enabled       = true;
                paintLineRenderer.positionCount = 2;
                paintLineRenderer.SetPosition(0, startRay);
                paintLineRenderer.SetPosition(1, endRay);
                paintLineRenderer.startWidth = 0.005f / GlobalState.WorldScale;
                paintLineRenderer.endWidth   = paintLineRenderer.startWidth;
                bool hit = Physics.Raycast(startRay, direction, out RaycastHit hitInfo, Mathf.Infinity);
                if (!hit)
                {
                    return;
                }
                penPosition = hitInfo.point - 0.001f * direction;
                paintLineRenderer.SetPosition(1, penPosition);
            }
            else if (paintTool == PaintTools.Volume)
            {
                if (currentVolume)
                {
                    VolumeController controller = currentVolume.GetComponent <VolumeController>();
                    if (null != controller)
                    {
                        paintLineRenderer.enabled = true;

                        Vector3 C = controller.bounds.center;
                        Vector3 E = controller.bounds.extents;

                        Vector3 tlf = controller.transform.TransformPoint(C + new Vector3(-E.x, E.y, -E.z));
                        Vector3 trf = controller.transform.TransformPoint(C + new Vector3(E.x, E.y, -E.z));
                        Vector3 blf = controller.transform.TransformPoint(C + new Vector3(-E.x, -E.y, -E.z));
                        Vector3 brf = controller.transform.TransformPoint(C + new Vector3(E.x, -E.y, -E.z));
                        Vector3 tlb = controller.transform.TransformPoint(C + new Vector3(-E.x, E.y, E.z));
                        Vector3 trb = controller.transform.TransformPoint(C + new Vector3(E.x, E.y, E.z));
                        Vector3 blb = controller.transform.TransformPoint(C + new Vector3(-E.x, -E.y, E.z));
                        Vector3 brb = controller.transform.TransformPoint(C + new Vector3(E.x, -E.y, E.z));

                        paintLineRenderer.positionCount = 16;
                        paintLineRenderer.SetPositions(new Vector3[] {
                            blf, tlf, brf, trf, brb, trb, blb,
                            blf, brf, brb, blb,
                            tlb, tlf, trf, trb, tlb
                        });
                        paintLineRenderer.startWidth = 0.001f / GlobalState.WorldScale;
                        paintLineRenderer.endWidth   = 0.001f / GlobalState.WorldScale;
                    }
                }
            }

            // Draw
            float deadZone = VRInput.deadZoneIn;

            if (triggerValue >= deadZone &&
                (
                    (position != paintPrevPosition && currentPaint != null) || currentVolume != null)
                )
            {
                // Add a point (the current world position) to the line renderer

                float pressure = (triggerValue - deadZone) / (1f - deadZone);
                float value    = brushSize / GlobalState.WorldScale * pressure;

                switch (paintTool)
                {
                case PaintTools.Pencil: freeDraw.AddControlPoint(penPosition, 0.5f * value); break;

                case PaintTools.FlatPencil: freeDraw.AddFlatLineControlPoint(penPosition, -transform.forward, 0.5f * value); break;

                case PaintTools.ConvexHull: freeDraw.AddConvexHullPoint(penPosition); break;

                case PaintTools.Volume: volumeGenerator.AddPoint(penPosition, 2.0f * value * strength); break;
                }

                switch (paintTool)
                {
                case PaintTools.Pencil:
                case PaintTools.FlatPencil:
                case PaintTools.ConvexHull:
                {
                    // set mesh components
                    MeshFilter meshFilter = currentPaint.GetComponent <MeshFilter>();
                    Mesh       mesh       = meshFilter.mesh;
                    mesh.Clear();
                    mesh.vertices  = freeDraw.vertices;
                    mesh.normals   = freeDraw.normals;
                    mesh.triangles = freeDraw.triangles;
                    break;
                }

                case PaintTools.Volume:
                    if (null != currentVolume)
                    {
                        MeshFilter meshFilter = currentVolume.GetComponent <MeshFilter>();
                        Mesh       mesh       = meshFilter.mesh;
                        mesh.Clear();
                        mesh.vertices  = volumeGenerator.vertices;
                        mesh.triangles = volumeGenerator.triangles;
                        mesh.RecalculateNormals();

                        // Recompute collider
                        MeshCollider meshCollider = currentVolume.GetComponent <MeshCollider>();
                        meshCollider.sharedMesh = mesh;
                        // force update
                        meshCollider.enabled = false;
                        meshCollider.enabled = true;

                        VolumeController controller = currentVolume.GetComponent <VolumeController>();
                        controller.origin     = volumeGenerator.origin;
                        controller.bounds     = volumeGenerator.bounds; // TODO: dont duplicate data? use volumeparameters in volumegenerator?
                        controller.field      = volumeGenerator.field;
                        controller.resolution = volumeGenerator.resolution;
                        controller.stepSize   = volumeGenerator.stepSize;
                        //controller.UpdateBoundsRenderer();
                    }
                    break;
                }
            }

            paintPrevPosition = position;
        }