コード例 #1
0
        public void OnSceneGUI()
        {
            if (addMode || deleteMode || paintMode)
            {
                //Debug.Log ("ColoredCubesVolumeEditor.OnSceneGUI()");
                Event e = Event.current;

                Ray ray = Camera.current.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight));

                if (((e.type == EventType.MouseDown) || (e.type == EventType.MouseDrag)) && (e.button == 0))
                {
                    // Perform the raycasting. If there's a hit the position will be stored in these ints.
                    PickVoxelResult pickResult;
                    if (addMode)
                    {
                        bool hit = Picking.PickLastEmptyVoxel(coloredCubesVolume, ray, 1000.0f, out pickResult);
                        if (hit)
                        {
                            coloredCubesVolume.data.SetVoxel(pickResult.volumeSpacePos.x, pickResult.volumeSpacePos.y, pickResult.volumeSpacePos.z, (QuantizedColor)paintColor);
                        }
                    }
                    else if (deleteMode)
                    {
                        bool hit = Picking.PickFirstSolidVoxel(coloredCubesVolume, ray, 1000.0f, out pickResult);
                        if (hit)
                        {
                            coloredCubesVolume.data.SetVoxel(pickResult.volumeSpacePos.x, pickResult.volumeSpacePos.y, pickResult.volumeSpacePos.z, new QuantizedColor(0, 0, 0, 0));
                        }
                    }
                    else if (paintMode)
                    {
                        bool hit = Picking.PickFirstSolidVoxel(coloredCubesVolume, ray, 1000.0f, out pickResult);
                        if (hit)
                        {
                            coloredCubesVolume.data.SetVoxel(pickResult.volumeSpacePos.x, pickResult.volumeSpacePos.y, pickResult.volumeSpacePos.z, (QuantizedColor)paintColor);
                        }
                    }

                    Selection.activeGameObject = coloredCubesVolume.gameObject;

                    // Need to force an update, otherwise there is noticable lag when painting voxels.
                    coloredCubesVolume.ForceUpdate();
                }
                else if (e.type == EventType.Layout)
                {
                    // See: http://answers.unity3d.com/questions/303248/how-to-paint-objects-in-the-editor.html
                    HandleUtility.AddDefaultControl(GUIUtility.GetControlID(GetHashCode(), FocusType.Passive));
                }
            }
        }
コード例 #2
0
        public void OnSceneGUI()
        {
            // We use these 'Handles' functions to allow us to embed OnGUI() code into OnSceneGUI().
            // See http://answers.unity3d.com/questions/26669/using-editorguilayout-controls-in-onscenegui.html
            Handles.BeginGUI();
            terrainVolume.OnGUI();
            Handles.EndGUI();

            // If we don't have a renderer then there's no terrain being
            // displayed, and so not much we can do in this function.
            TerrainVolumeRenderer terrainVolumeRenderer = terrainVolume.GetComponent <TerrainVolumeRenderer>();

            if (terrainVolumeRenderer == null)
            {
                return;
            }

            // By default we disable the brush marker, and only turn it on if we later find a hit.
            Material      material = terrainVolumeRenderer.material;
            List <string> keywords = new List <string> {
                "BRUSH_MARKER_OFF"
            };

            if (sculptPressed || smoothPressed || paintPressed)
            {
                Event e = Event.current;

                Ray ray = Camera.current.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight));

                // Perform the raycasting.
                PickSurfaceResult pickResult;
                bool hit = Picking.PickSurface(terrainVolume, ray.origin, ray.direction, 1000.0f, out pickResult);

                if (hit)
                {
                    //Debug.Log("Hit");
                    // Selected brush is in the range 0 to NoOfBrushes - 1. Convert this to a 0 to 1 range.
                    float brushInnerScaleFactor = (float)selectedBrush / ((float)(NoOfBrushes - 1));
                    // Use this value to compute the inner radius as a proportion of the outer radius.
                    float brushInnerRadius = brushOuterRadius * brushInnerScaleFactor;

                    if (material != null)
                    {
                        keywords = new List <string> {
                            "BRUSH_MARKER_ON"
                        };
                        material.SetVector("BrushCenter", pickResult.volumeSpacePos);
                        material.SetVector("BrushSettings", new Vector4(brushInnerRadius, brushOuterRadius, brushOpacity, 0.0f));
                        material.SetVector("BrushColor", new Vector4(0.0f, 0.5f, 1.0f, 1.0f));
                    }

                    if (((e.type == EventType.MouseDown) || (e.type == EventType.MouseDrag)) && (e.button == 0))
                    {
                        if (sculptPressed)
                        {
                            float multiplier = 1.0f;
                            if (e.modifiers == EventModifiers.Shift)
                            {
                                multiplier = -1.0f;
                            }
                            TerrainVolumeEditor.SculptTerrainVolume(terrainVolume, pickResult.volumeSpacePos.x, pickResult.volumeSpacePos.y, pickResult.volumeSpacePos.z, brushInnerRadius, brushOuterRadius, brushOpacity * multiplier);
                        }
                        else if (smoothPressed)
                        {
                            TerrainVolumeEditor.BlurTerrainVolume(terrainVolume, pickResult.volumeSpacePos.x, pickResult.volumeSpacePos.y, pickResult.volumeSpacePos.z, brushInnerRadius, brushOuterRadius, brushOpacity);
                        }
                        else if (paintPressed)
                        {
                            TerrainVolumeEditor.PaintTerrainVolume(terrainVolume, pickResult.volumeSpacePos.x, pickResult.volumeSpacePos.y, pickResult.volumeSpacePos.z, brushInnerRadius, brushOuterRadius, brushOpacity, (uint)selectedTexture);
                        }
                    }


                    // Forcing the update seems to be requireded in Unity 5, whereas in Unity 4 it seems to happen automatically
                    // later as a result of assigning the shader keywords. Seems best to manually call it just in case.
                    terrainVolume.ForceUpdate();
                }

                if (e.type == EventType.Layout)
                {
                    // See: http://answers.unity3d.com/questions/303248/how-to-paint-objects-in-the-editor.html
                    HandleUtility.AddDefaultControl(GUIUtility.GetControlID(GetHashCode(), FocusType.Passive));
                }

                // We need to repaint so that the brush marker follows the mouse even when a mouse button is not pressed.
                HandleUtility.Repaint();
            }

            if (material != null)
            {
                material.shaderKeywords = keywords.ToArray();
            }
        }