Example #1
0
 public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
 {
     TerrainPaintUtilityEditor.ShowDefaultPreviewBrush(
         terrain,
         editContext.brushTexture,
         editContext.brushSize);
 }
Example #2
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            // We're only doing painting operations, early out if it's not a repaint
            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            if (editContext.hitValidTerrain)
            {
                BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, editContext.brushSize, 0.0f);
                PaintContext   paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);

                Material material = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial();
                TerrainPaintUtilityEditor.DrawBrushPreview(
                    paintContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, material, 0);

                // draw result preview
                {
                    ApplyBrushInternal(paintContext, editContext.brushStrength, editContext.brushTexture, brushXform);

                    // restore old render target
                    RenderTexture.active = paintContext.oldRenderTexture;

                    material.SetTexture("_HeightmapOrig", paintContext.sourceRenderTexture);
                    TerrainPaintUtilityEditor.DrawBrushPreview(
                        paintContext, TerrainPaintUtilityEditor.BrushPreview.DestinationRenderTexture, editContext.brushTexture, brushXform, material, 1);
                }

                TerrainPaintUtility.ReleaseContextResources(paintContext);
            }
        }
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            commonUI.OnSceneGUI2D(terrain, editContext);

            if (!editContext.hitValidTerrain && !commonUI.isInUse)
            {
                return;
            }

            // update brush UI group
            commonUI.OnSceneGUI(terrain, editContext);

            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            if (commonUI.isRaycastHitUnderCursorValid)
            {
                using (IBrushRenderPreviewUnderCursor brushRender = new BrushRenderPreviewUIGroupUnderCursor(commonUI, "PaintHoles", editContext.brushTexture))
                {
                    if (brushRender.CalculateBrushTransform(out BrushTransform brushXform))
                    {
                        PaintContext paintContext = brushRender.AcquireHeightmap(false, brushXform.GetBrushXYBounds(), 1);
                        Material     material     = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial();

                        brushRender.RenderBrushPreview(paintContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, brushXform, material, 0);
                    }
                }
            }
        }
Example #4
0
        private void RemoveTrees(Terrain terrain, IOnPaint editContext, bool clearSelectedOnly)
        {
            PaintTreesDetailsContext ctx = PaintTreesDetailsContext.Create(terrain, editContext.uv);

            for (int i = 0; i < ctx.terrains.Length; ++i)
            {
                Terrain ctxTerrain = ctx.terrains[i];
                if (ctxTerrain != null)
                {
                    Vector2 ctxUV  = ctx.uvs[i];
                    float   radius = 0.5f * brushSize / ctxTerrain.terrainData.size.x;

                    int treePrototype = kInvalidTree;
                    if (clearSelectedOnly && selectedTree != kInvalidTree)
                    {
                        treePrototype = PaintTreesUtils.FindTreePrototype(ctxTerrain, m_TargetTerrain, selectedTree);
                    }

                    if (!clearSelectedOnly || treePrototype != kInvalidTree)
                    {
                        TerrainPaintUtilityEditor.UpdateTerrainDataUndo(ctxTerrain.terrainData, "Terrain - Remove Trees");

                        ctxTerrain.RemoveTrees(ctxUV, radius, treePrototype);
                    }
                }
            }
        }
Example #5
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            commonUI.OnSceneGUI2D(terrain, editContext);

            if (!editContext.hitValidTerrain && !commonUI.isInUse)
            {
                return;
            }

            commonUI.OnSceneGUI(terrain, editContext);

            Event evt = Event.current;

            if (evt.control && (evt.type == EventType.ScrollWheel))
            {
                const float k_mouseWheelToHeightRatio = -0.0004f;
                stampToolProperties.m_StampHeight += Event.current.delta.y * k_mouseWheelToHeightRatio * editContext.raycastHit.distance;
                evt.Use();
                editContext.Repaint();
                SaveSetting();
            }

            // We're only doing painting operations, early out if it's not a repaint
            if (evt.type != EventType.Repaint)
            {
                return;
            }

            if (commonUI.isRaycastHitUnderCursorValid)
            {
                Texture brushTexture = editContext.brushTexture;

                using (IBrushRenderPreviewUnderCursor brushRender = new BrushRenderPreviewUIGroupUnderCursor(commonUI, "Stamp", brushTexture))
                {
                    if (brushRender.CalculateBrushTransform(out BrushTransform brushXform))
                    {
                        PaintContext paintContext = brushRender.AcquireHeightmap(false, brushXform.GetBrushXYBounds(), 1);
                        Material     material     = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial();

                        brushRender.RenderBrushPreview(paintContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, brushXform, material, 0);

                        // draw result preview
                        {
                            ApplyBrushInternal(brushRender, paintContext, commonUI.brushStrength, brushTexture, brushXform, terrain);

                            // restore old render target
                            RenderTexture.active = paintContext.oldRenderTexture;

                            material.SetTexture("_HeightmapOrig", paintContext.sourceRenderTexture);

                            brushRender.RenderBrushPreview(paintContext, TerrainPaintUtilityEditor.BrushPreview.DestinationRenderTexture, brushXform, material, 1);
                        }
                        TerrainPaintUtility.ReleaseContextResources(paintContext);
                    }
                }
            }
        }
Example #6
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            commonUI.OnSceneGUI2D(terrain, editContext);

            // only do the rest if user mouse hits valid terrain or they are using the
            // brush parameter hotkeys to resize, etc
            if (!editContext.hitValidTerrain && !commonUI.isInUse)
            {
                return;
            }

            // update brush UI group
            commonUI.OnSceneGUI(terrain, editContext);

            // dont render preview if this isnt a repaint. losing performance if we do
            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            if (commonUI.isRaycastHitUnderCursorValid)
            {
                Texture brushTexture = editContext.brushTexture;

                using (IBrushRenderPreviewUnderCursor brushRender = new BrushRenderPreviewUIGroupUnderCursor(commonUI, "Twist", editContext.brushTexture))
                {
                    //draw brush circle
                    if (brushRender.CalculateBrushTransform(out BrushTransform brushXform))
                    {
                        Material     material = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial();
                        PaintContext ctx      = brushRender.AcquireHeightmap(false, brushXform.GetBrushXYBounds(), 1);

                        brushRender.RenderBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, brushXform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0);

                        // draw result preview
                        {
                            float finalTwistAmount = m_TwistAmount * -0.002f; //scale to a reasonable value and negate so default mode is clockwise
                            if (Event.current.shift)
                            {
                                finalTwistAmount *= -1.0f;
                            }

                            ApplyBrushInternal(brushRender, ctx, commonUI.brushStrength, finalTwistAmount, brushTexture, brushXform);

                            // restore old render target
                            RenderTexture.active = ctx.oldRenderTexture;

                            material.SetTexture("_HeightmapOrig", ctx.sourceRenderTexture);

                            brushRender.RenderBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.DestinationRenderTexture, brushXform, material, 1);
                        }
                    }
                }
            }
        }
Example #7
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            commonUI.OnSceneGUI2D(terrain, editContext);

            if (editContext.hitValidTerrain || commonUI.isInUse)
            {
                commonUI.OnSceneGUI(terrain, editContext);

                if (Event.current.type != EventType.Repaint)
                {
                    return;
                }

                if (pathToolProperties != null && pathToolProperties.widthProfile != null)
                {
                    float endWidth = Mathf.Abs(pathToolProperties.widthProfile.Evaluate(1.0f));

                    BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, commonUI.raycastHitUnderCursor.textureCoord, commonUI.brushSize * endWidth, commonUI.brushRotation);
                    PaintContext   ctx        = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);
                    TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0);
                    TerrainPaintUtility.ReleaseContextResources(ctx);
                }
            }

            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            switch (pathToolProperties.paintMode)
            {
            case PaintMode.Paint:
                // nothing to do, no special indicator
                break;

            case PaintMode.Stroke:
                //display a brush preview at first or last clicked path location, using starting size from width profile
                if (m_StartTerrain != null)
                {
                    float startWidth = Mathf.Abs(pathToolProperties.widthProfile.Evaluate(0.0f));

                    BrushTransform brushTransform = TerrainPaintUtility.CalculateBrushTransform(m_StartTerrain, m_StartPoint, commonUI.brushSize * startWidth, commonUI.brushRotation);
                    PaintContext   sampleContext  = TerrainPaintUtility.BeginPaintHeightmap(m_StartTerrain, brushTransform.GetBrushXYBounds());
                    TerrainPaintUtilityEditor.DrawBrushPreview(sampleContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture,
                                                               editContext.brushTexture, brushTransform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0);
                    TerrainPaintUtility.ReleaseContextResources(sampleContext);
                }
                break;

            default:
                throw new System.Exception(string.Format("Unsupported paint mode {0}", pathToolProperties.paintMode));
            }
        }
Example #8
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            commonUI.OnSceneGUI2D(terrain, editContext);

            // only do the rest if user mouse hits valid terrain or they are using the
            // brush parameter hotkeys to resize, etc
            if (!editContext.hitValidTerrain && !commonUI.isInUse)
            {
                return;
            }

            // update brush UI group
            commonUI.OnSceneGUI(terrain, editContext);

            // dont render preview if this isnt a repaint. losing performance if we do
            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            using (IBrushRenderPreviewUnderCursor brushPreview =
                       new BrushRenderPreviewUIGroupUnderCursor(commonUI, "NoiseHeightTool", editContext.brushTexture))
            {
                float   brushSize     = commonUI.brushSize;
                float   brushRotation = commonUI.brushRotation;
                float   brushStrength = Event.current.control ? -commonUI.brushStrength : commonUI.brushStrength;
                Vector3 brushPosWS    = commonUI.raycastHitUnderCursor.point;

                BrushTransform brushXform;
                brushPreview.CalculateBrushTransform(out brushXform);

                PaintContext ctx = brushPreview.AcquireHeightmap(false, brushXform.GetBrushXYBounds(), 1);

                Material material = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial();
                brushPreview.RenderBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, brushXform, material, 0);

                ApplyBrushInternal(ctx, brushXform, brushPosWS, commonUI.brushRotation,
                                   brushStrength, brushSize, editContext.brushTexture);

                TerrainPaintUtility.SetupTerrainToolMaterialProperties(ctx, brushXform, material);

                // restore old render target
                RenderTexture.active = ctx.oldRenderTexture;

                material.SetTexture("_HeightmapOrig", ctx.sourceRenderTexture);

                TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.DestinationRenderTexture,
                                                           editContext.brushTexture, brushXform, material, 1);

                TerrainPaintUtility.ReleaseContextResources(ctx);
            }
        }
Example #9
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            if (Event.current.shift)
            {
                SampleTerrain(terrain, editContext);
            }
            if (Event.current.type == EventType.MouseUp && Event.current.button == 0)
            {
                disablePaint = false;
                drawLine     = false;
            }

            TerrainPaintUtilityEditor.ShowDefaultPreviewBrush(terrain,
                                                              editContext.brushTexture,
                                                              editContext.brushSize);
        }
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            // We're only doing painting operations, early out if it's not a repaint
            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            if (editContext.hitValidTerrain)
            {
                BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, editContext.brushSize, 0.0f);
                PaintContext   ctx        = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);
                TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0);
                TerrainPaintUtility.ReleaseContextResources(ctx);
            }
        }
Example #11
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            commonUI.OnSceneGUI2D(terrain, editContext);

            // only do the rest if user mouse hits valid terrain or they are using the
            // brush parameter hotkeys to resize, etc
            if (!editContext.hitValidTerrain && !commonUI.isInUse)
            {
                return;
            }

            // update brush UI group
            commonUI.OnSceneGUI(terrain, editContext);

            // dont render preview if this isnt a repaint. losing performance if we do
            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            Texture brushTexture = editContext.brushTexture;

            using (IBrushRenderPreviewUnderCursor brushRender = new BrushRenderPreviewUIGroupUnderCursor(commonUI, "SetHeightTool", brushTexture))
            {
                if (brushRender.CalculateBrushTransform(out BrushTransform brushTransform))
                {
                    Rect         brushBounds  = brushTransform.GetBrushXYBounds();
                    PaintContext paintContext = brushRender.AcquireHeightmap(false, brushBounds, 1);
                    Material     material     = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial();

                    brushRender.RenderBrushPreview(paintContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, brushTransform, material, 0);

                    // draw result preview
                    {
                        ApplyBrushInternal(paintContext, brushRender, commonUI.brushStrength, brushTexture, brushTransform, terrain);

                        // restore old render target
                        RenderTexture.active = paintContext.oldRenderTexture;

                        material.SetTexture("_HeightmapOrig", paintContext.sourceRenderTexture);

                        brushRender.RenderBrushPreview(paintContext, TerrainPaintUtilityEditor.BrushPreview.DestinationRenderTexture, brushTransform, material, 1);
                    }
                }
            }
        }
Example #12
0
        private void DrawBrushPreviews(Terrain terrain, IOnSceneGUI editContext)
        {
            Vector2        sampleUV;
            BrushTransform sampleXform;
            PaintContext   sampleContext = null;
            Material       previewMat    = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial();

            // draw sample location brush and create context data to be used when drawing target brush previews
            if (m_SampleLocation.terrain != null)
            {
                sampleUV      = TerrainUVFromBrushLocation(m_SampleLocation.terrain, m_SampleLocation.pos);
                sampleXform   = TerrainPaintUtility.CalculateBrushTransform(m_SampleLocation.terrain, sampleUV, editContext.brushSize, 0);
                sampleContext = TerrainPaintUtility.BeginPaintHeightmap(m_SampleLocation.terrain, sampleXform.GetBrushXYBounds());
                TerrainPaintUtilityEditor.DrawBrushPreview(sampleContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture,
                                                           editContext.brushTexture, sampleXform, previewMat, 0);
            }

            // draw brush preview and mesh preview for current mouse position
            if (editContext.hitValidTerrain)
            {
                BrushTransform targetXform   = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, editContext.brushSize, 0f);
                PaintContext   targetContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, targetXform.GetBrushXYBounds(), 1);

                // draw basic preview of brush
                TerrainPaintUtilityEditor.DrawBrushPreview(targetContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture,
                                                           editContext.brushTexture, targetXform, previewMat, 0);

                if (sampleContext != null && m_PaintHeightmap)
                {
                    ApplyHeightmap(sampleContext, targetContext, targetXform, terrain, editContext.brushTexture, editContext.brushStrength);

                    // draw preview of brush mesh
                    RenderTexture.active = targetContext.oldRenderTexture;
                    previewMat.SetTexture("_HeightmapOrig", targetContext.sourceRenderTexture);
                    TerrainPaintUtilityEditor.DrawBrushPreview(targetContext, TerrainPaintUtilityEditor.BrushPreview.DestinationRenderTexture,
                                                               editContext.brushTexture, targetXform, previewMat, 1);
                }

                TerrainPaintUtility.ReleaseContextResources(targetContext);
            }

            if (sampleContext != null)
            {
                TerrainPaintUtility.ReleaseContextResources(sampleContext);
            }
        }
Example #13
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            Event evt = Event.current;

            if (evt.control && (evt.type == EventType.ScrollWheel))
            {
                const float k_mouseWheelToHeightRatio = -0.0004f;
                // we use distance to modify the scroll speed, so that when a user is up close to the brush, they get fine adjustment, and when the user is far from the brush, it adjusts quickly
                m_StampHeightTerrainSpace += Event.current.delta.y * k_mouseWheelToHeightRatio * editContext.raycastHit.distance;
                evt.Use();
                editContext.Repaint();
            }

            // We're only doing painting operations, early out if it's not a repaint
            if (evt.type != EventType.Repaint)
            {
                return;
            }

            if (editContext.hitValidTerrain)
            {
                BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, editContext.brushSize, 0.0f);
                PaintContext   paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);

                Material material = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial();

                TerrainPaintUtilityEditor.DrawBrushPreview(
                    paintContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, material, 0);

                // draw result preview
                {
                    ApplyBrushInternal(paintContext, editContext.brushStrength, editContext.brushTexture, brushXform, terrain, evt.shift);

                    // restore old render target
                    RenderTexture.active = paintContext.oldRenderTexture;

                    material.SetTexture("_HeightmapOrig", paintContext.sourceRenderTexture);

                    TerrainPaintUtilityEditor.DrawBrushPreview(
                        paintContext, TerrainPaintUtilityEditor.BrushPreview.DestinationRenderTexture, editContext.brushTexture, brushXform, material, 1);
                }

                TerrainPaintUtility.ReleaseContextResources(paintContext);
            }
        }
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            commonUI.OnSceneGUI2D(terrain, editContext);

            if (editContext.hitValidTerrain || commonUI.isInUse)
            {
                commonUI.OnSceneGUI(terrain, editContext);

                if (Event.current.type != EventType.Repaint)
                {
                    return;
                }

                if (bridgeToolProperties != null && bridgeToolProperties.widthProfile != null)
                {
                    float endWidth = Mathf.Abs(bridgeToolProperties.widthProfile.Evaluate(1.0f));

                    BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, commonUI.raycastHitUnderCursor.textureCoord, commonUI.brushSize * endWidth, commonUI.brushRotation);
                    PaintContext   ctx        = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);
                    TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0);
                    TerrainPaintUtility.ReleaseContextResources(ctx);
                }
            }

            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            //display a brush preview at the bridge starting location, using starting size from width profile
            if (m_StartTerrain != null)
            {
                float startWidth = Mathf.Abs(bridgeToolProperties.widthProfile.Evaluate(0.0f));

                BrushTransform brushTransform = TerrainPaintUtility.CalculateBrushTransform(m_StartTerrain, m_StartPoint, commonUI.brushSize * startWidth, commonUI.brushRotation);
                PaintContext   sampleContext  = TerrainPaintUtility.BeginPaintHeightmap(m_StartTerrain, brushTransform.GetBrushXYBounds());
                TerrainPaintUtilityEditor.DrawBrushPreview(sampleContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture,
                                                           editContext.brushTexture, brushTransform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0);
                TerrainPaintUtility.ReleaseContextResources(sampleContext);
            }
        }
Example #15
0
        private void DrawBrushPreviews(Terrain terrain, IOnSceneGUI editContext)
        {
            Vector2        sampleUV;
            BrushTransform sampleXform;
            PaintContext   sampleContext = null;
            Material       previewMat    = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial();

            // draw sample location brush and create context data to be used when drawing target brush previews
            if (m_SampleLocation.terrain != null)
            {
                sampleUV      = TerrainUVFromBrushLocation(m_SampleLocation.terrain, m_SampleLocation.pos);
                sampleXform   = TerrainPaintUtility.CalculateBrushTransform(m_SampleLocation.terrain, sampleUV, commonUI.brushSize, commonUI.brushRotation);
                sampleContext = TerrainPaintUtility.BeginPaintHeightmap(m_SampleLocation.terrain, sampleXform.GetBrushXYBounds());
                TerrainPaintUtilityEditor.DrawBrushPreview(sampleContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture,
                                                           editContext.brushTexture, sampleXform, previewMat, 0);
            }

            // draw brush preview and mesh preview for current mouse position
            if (commonUI.isRaycastHitUnderCursorValid)
            {
                BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, commonUI.raycastHitUnderCursor.textureCoord, commonUI.brushSize, commonUI.brushRotation);
                PaintContext   ctx        = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);

                TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0);
                if (sampleContext != null && cloneToolProperties.m_PaintHeightmap)
                {
                    ApplyHeightmap(sampleContext, ctx, brushXform, terrain, editContext.brushTexture, commonUI.brushStrength);
                    RenderTexture.active = ctx.oldRenderTexture;
                    previewMat.SetTexture("_HeightmapOrig", ctx.sourceRenderTexture);
                    TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.DestinationRenderTexture,
                                                               editContext.brushTexture, brushXform, previewMat, 1);
                }

                // Restores RenderTexture.active
                ctx.Cleanup();
            }

            // Restores RenderTexture.active
            sampleContext?.Cleanup();
        }
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            // there's probably more logic we need to handle here
            if (paintInProgress && (Event.current.type == EventType.MouseUp))
            {
                MaterialManager mgr = terrain.GetComponent <MaterialManager>();
                if (mgr != null)
                {
                    mgr.GetIndexMap().SetIndexMapDirty();
                }
                paintInProgress = false;
            }

            // We're only doing painting operations, early out if it's not a repaint
            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            if (editContext.hitValidTerrain)
            {
                if (lastUV != editContext.raycastHit.textureCoord)
                {
                    editContext.Repaint(RepaintFlags.UI);
                }
                lastUV        = editContext.raycastHit.textureCoord;
                lastBrushSize = editContext.brushSize;
                BrushTransform brushXform           = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, editContext.brushSize, 0.0f);
                PaintContext   ctx                  = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);
                Material       brushPreviewMaterial = GetBrushPreviewMaterial();
                Vector4        brushParams          = new Vector4(editContext.brushStrength, 0.0f, 0.0f, 0.0f);
                brushPreviewMaterial.SetVector("_BrushParams", brushParams);
                TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, brushPreviewMaterial, 0);
                TerrainPaintUtility.ReleaseContextResources(ctx);
            }
        }
Example #17
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            // don't render mesh previews, etc. if the mesh field has not been populated yet
            // or the mouse is not over a Terrain tile
            if (m_Mesh == null || !editContext.hitValidTerrain)
            {
                return;
            }

            if (!Event.current.shift)
            {
                m_SceneRaycastHitPoint = editContext.raycastHit.point + new Vector3(0, m_StampHeight, 0);
            }

            if (m_LastBrushSize != (int)editContext.brushSize)
            {
                m_LastBrushSize = (int)editContext.brushSize;
                CalculateBrushSizeToWorldScale();
            }

            Vector3      posTerrainSpace = m_SceneRaycastHitPoint - terrain.GetPosition();
            PaintContext context         = ApplyBrushInternal(terrain, new Vector2(posTerrainSpace.x, posTerrainSpace.z), 1);

            Material material = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial();

            // restore old render target
            RenderTexture.active = context.oldRenderTexture;
            material.SetTexture("_HeightmapOrig", context.sourceRenderTexture);
            TerrainPaintUtilityEditor.DrawBrushPreview(context, TerrainPaintUtilityEditor.BrushPreview.DestinationRenderTexture,
                                                       editContext.brushTexture, brushXformIdentity,
                                                       material, 1);

            TerrainPaintUtility.ReleaseContextResources(context);

            // draw transform Handles for rot, scale, and height translation
            if (Event.current.shift)
            {
                EditorGUI.BeginChangeCheck();
                float   size  = HandleUtility.GetHandleSize(m_SceneRaycastHitPoint);
                Vector3 scale = Handles.ScaleHandle(m_StampScale, m_SceneRaycastHitPoint, m_StampRotation, size * 1.5f);

                scale.x = Mathf.Max(scale.x, 0.02f);
                scale.y = Mathf.Max(scale.y, 0.02f);
                scale.z = Mathf.Max(scale.z, 0.02f);

                Quaternion rot = Handles.RotationHandle(m_StampRotation, m_SceneRaycastHitPoint);

                Handles.DrawingScope drawingScope = new Handles.DrawingScope(Handles.yAxisColor);

                float height = (Handles.Slider(m_SceneRaycastHitPoint, Vector3.up, size, Handles.ArrowHandleCap, 0.01f).y - m_SceneRaycastHitPoint.y) * 0.5f;

                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(this, "Mesh Stamp Tool - Scaling Mesh");
                    m_StampScale              = scale;
                    m_StampRotation           = rot;
                    m_SceneRaycastHitPoint.y += height;
                    m_StampHeight            += height;
                    RepaintInspector();
                }
            }
        }
Example #18
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            commonUI.OnSceneGUI2D(terrain, editContext);

            // only do the rest if user mouse hits valid terrain or they are using the
            // brush parameter hotkeys to resize, etc
            if (!editContext.hitValidTerrain && !commonUI.isInUse)
            {
                return;
            }

            // update brush UI group
            commonUI.OnSceneGUI(terrain, editContext);

            // dont render preview if this isnt a repaint. losing performance if we do
            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            using (IBrushRenderPreviewUnderCursor brushRender = new BrushRenderPreviewUIGroupUnderCursor(commonUI, "HydroErosion", editContext.brushTexture))
            {
                if (brushRender.CalculateBrushTransform(out BrushTransform brushXform))
                {
                    PaintContext ctx = brushRender.AcquireHeightmap(false, brushXform.GetBrushXYBounds(), 1);

                    brushRender.RenderBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, brushXform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0);
                }
            }
        }
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            if (m_TargetTerrain == null ||
                selectedDetail == kInvalidDetail ||
                selectedDetail >= m_TargetTerrain.terrainData.detailPrototypes.Length)
            {
                return(false);
            }

            Texture2D brush = editContext.brushTexture as Texture2D;

            if (brush == null)
            {
                Debug.LogError("Brush texture is not a Texture2D.");
                return(false);
            }

            if (m_BrushRep == null)
            {
                m_BrushRep = new BrushRep();
            }

            PaintTreesDetailsContext ctx = PaintTreesDetailsContext.Create(terrain, editContext.uv);

            for (int t = 0; t < ctx.terrains.Length; ++t)
            {
                Terrain ctxTerrain = ctx.terrains[t];
                if (ctxTerrain != null)
                {
                    int detailPrototype = PaintDetailsUtils.FindDetailPrototype(ctxTerrain, m_TargetTerrain, selectedDetail);
                    if (detailPrototype == kInvalidDetail)
                    {
                        detailPrototype = PaintDetailsUtils.CopyDetailPrototype(ctxTerrain, m_TargetTerrain, selectedDetail);
                    }

                    TerrainData terrainData = ctxTerrain.terrainData;

                    TerrainPaintUtilityEditor.UpdateTerrainDataUndo(terrainData, "Terrain - Detail Edit");

                    int size = (int)Mathf.Max(1.0f, editContext.brushSize * ((float)terrainData.detailResolution / terrainData.size.x));

                    m_BrushRep.CreateFromBrush(brush, size);

                    Vector2 ctxUV = ctx.uvs[t];

                    int xCenter = Mathf.FloorToInt(ctxUV.x * terrainData.detailWidth);
                    int yCenter = Mathf.FloorToInt(ctxUV.y * terrainData.detailHeight);

                    int intRadius   = Mathf.RoundToInt(size) / 2;
                    int intFraction = Mathf.RoundToInt(size) % 2;

                    int xmin = xCenter - intRadius;
                    int ymin = yCenter - intRadius;

                    int xmax = xCenter + intRadius + intFraction;
                    int ymax = yCenter + intRadius + intFraction;

                    if (xmin >= terrainData.detailWidth || ymin >= terrainData.detailHeight || xmax <= 0 || ymax <= 0)
                    {
                        continue;
                    }

                    xmin = Mathf.Clamp(xmin, 0, terrainData.detailWidth - 1);
                    ymin = Mathf.Clamp(ymin, 0, terrainData.detailHeight - 1);

                    xmax = Mathf.Clamp(xmax, 0, terrainData.detailWidth);
                    ymax = Mathf.Clamp(ymax, 0, terrainData.detailHeight);

                    int width  = xmax - xmin;
                    int height = ymax - ymin;

                    float targetStrength = detailStrength;
                    if (Event.current.shift || Event.current.control)
                    {
                        targetStrength = -targetStrength;
                    }

                    int[] layers = { detailPrototype };
                    if (targetStrength < 0.0F && !Event.current.control)
                    {
                        layers = terrainData.GetSupportedLayers(xmin, ymin, width, height);
                    }

                    for (int i = 0; i < layers.Length; i++)
                    {
                        int[,] alphamap = terrainData.GetDetailLayer(xmin, ymin, width, height, layers[i]);

                        for (int y = 0; y < height; y++)
                        {
                            for (int x = 0; x < width; x++)
                            {
                                int   xBrushOffset = (xmin + x) - (xCenter - intRadius + intFraction);
                                int   yBrushOffset = (ymin + y) - (yCenter - intRadius + intFraction);
                                float opa          = detailOpacity * m_BrushRep.GetStrengthInt(xBrushOffset, yBrushOffset);

                                float targetValue = Mathf.Lerp(alphamap[y, x], targetStrength, opa);
                                alphamap[y, x] = Mathf.RoundToInt(targetValue - .5f + Random.value);
                            }
                        }

                        terrainData.SetDetailLayer(xmin, ymin, layers[i], alphamap);
                    }
                }
            }

            return(false);
        }
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            Event evt = Event.current;

            // brush rotation
            if (evt.control && !evt.shift && evt.type == EventType.ScrollWheel)
            {
                m_BrushRotation += Event.current.delta.y;

                if (m_BrushRotation >= 360)
                {
                    m_BrushRotation -= 360;
                }

                if (m_BrushRotation < 0)
                {
                    m_BrushRotation += 360;
                }

                m_BrushRotation %= 360;

                evt.Use();
                editContext.Repaint();
            }

            // brush resize
            if (evt.control && evt.type == EventType.MouseDrag)
            {
                m_BrushSize += Event.current.delta.x;

                evt.Use();
                editContext.Repaint();
            }

            // stamp height
            if (evt.control && evt.shift && evt.type == EventType.ScrollWheel)
            {
                m_StampHeight += Event.current.delta.y * k_mouseWheelToHeightRatio * editContext.raycastHit.distance;

                evt.Use();
                editContext.Repaint();
            }

            if (evt.type != EventType.Repaint)
            {
                return;
            }

            if (editContext.hitValidTerrain)
            {
                BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, m_BrushSize, m_BrushRotation);
                PaintContext   paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);

                Material material = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial();

                TerrainPaintUtilityEditor.DrawBrushPreview(paintContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, material, 0);

                ApplyBrushInternal(paintContext, m_BrushStrength, editContext.brushTexture, brushXform, terrain);

                RenderTexture.active = paintContext.oldRenderTexture;

                material.SetTexture("_HeightmapOrig", paintContext.sourceRenderTexture);

                TerrainPaintUtilityEditor.DrawBrushPreview(paintContext, TerrainPaintUtilityEditor.BrushPreview.DestinationRenderTexture, editContext.brushTexture, brushXform, material, 1);

                TerrainPaintUtility.ReleaseContextResources(paintContext);
            }

            #region BrushPreview3d

            if (preview3dEnabled)
            {
                UpdateBrushPreview3d(editContext);
            }

            #endregion BrushPreview3d
        }
Example #21
0
 public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
 {
     TerrainPaintUtilityEditor.ShowDefaultPreviewBrush(terrain, editContext.brushTexture, editContext.brushStrength * 0.01f, editContext.brushSize, m_StampHeight);
 }
Example #22
0
        private void PlaceTrees(Terrain terrain, IOnPaint editContext)
        {
            if (m_TargetTerrain == null ||
                selectedTree == kInvalidTree ||
                selectedTree >= m_TargetTerrain.terrainData.treePrototypes.Length)
            {
                return;
            }

            PaintTreesDetailsContext ctx = PaintTreesDetailsContext.Create(terrain, editContext.uv);

            int placedTreeCount = 0;

            int treePrototype = PaintTreesUtils.FindTreePrototype(terrain, m_TargetTerrain, selectedTree);

            if (treePrototype == kInvalidTree)
            {
                treePrototype = PaintTreesUtils.CopyTreePrototype(terrain, m_TargetTerrain, selectedTree);
            }

            if (PaintTreesUtils.ValidateTreePrototype(terrain, treePrototype))
            {
                // When painting single tree
                // And just clicking we always place it, so you can do overlapping trees
                Vector3 position          = new Vector3(editContext.uv.x, 0, editContext.uv.y);
                bool    checkTreeDistance = Event.current.type == EventType.MouseDrag || brushSize > 1;
                if (!checkTreeDistance || TerrainInspectorUtil.CheckTreeDistance(terrain.terrainData, position, treePrototype, spacing))
                {
                    TerrainPaintUtilityEditor.UpdateTerrainDataUndo(terrain.terrainData, "Terrain - Place Trees");

                    PaintTreesUtils.PlaceTree(terrain, treePrototype, position, GetTreeColor(), GetTreeHeight(), lockWidthToHeight ? GetTreeHeight() : GetTreeWidth(), GetTreeRotation());
                    ++placedTreeCount;
                }
            }

            for (int i = 0; i < ctx.terrains.Length; ++i)
            {
                Terrain ctxTerrain = ctx.terrains[i];
                if (ctxTerrain != null)
                {
                    Vector2 ctxUV = ctx.uvs[i];

                    treePrototype = PaintTreesUtils.FindTreePrototype(ctxTerrain, m_TargetTerrain, selectedTree);
                    if (treePrototype == kInvalidTree)
                    {
                        treePrototype = PaintTreesUtils.CopyTreePrototype(ctxTerrain, m_TargetTerrain, selectedTree);
                    }

                    if (PaintTreesUtils.ValidateTreePrototype(ctxTerrain, treePrototype))
                    {
                        Vector3 size = TerrainInspectorUtil.GetPrototypeExtent(ctxTerrain.terrainData, treePrototype);
                        size.y = 0;
                        float treeCountOneAxis = brushSize / (size.magnitude * spacing * .5f);
                        int   treeCount        = (int)((treeCountOneAxis * treeCountOneAxis) * .5f);
                        treeCount = Mathf.Clamp(treeCount, 0, 100);
                        // Plant a bunch of trees
                        for (int j = ctxTerrain == terrain ? 1 : 0; j < treeCount && placedTreeCount < treeCount; ++j)
                        {
                            Vector2 randomOffset = 0.5f * Random.insideUnitCircle;
                            randomOffset.x *= brushSize / ctxTerrain.terrainData.size.x;
                            randomOffset.y *= brushSize / ctxTerrain.terrainData.size.z;
                            Vector3 position = new Vector3(ctxUV.x + randomOffset.x, 0, ctxUV.y + randomOffset.y);
                            if (position.x >= 0 && position.x <= 1 && position.z >= 0 && position.z <= 1 &&
                                TerrainInspectorUtil.CheckTreeDistance(ctxTerrain.terrainData, position, treePrototype, spacing * .5f))
                            {
                                TerrainPaintUtilityEditor.UpdateTerrainDataUndo(ctxTerrain.terrainData, "Terrain - Place Trees");

                                PaintTreesUtils.PlaceTree(ctxTerrain, treePrototype, position, GetTreeColor(), GetTreeHeight(), lockWidthToHeight ? GetTreeHeight() : GetTreeWidth(), GetTreeRotation());
                                ++placedTreeCount;
                            }
                        }
                    }
                }
            }
        }
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            TerrainPaintUtilityEditor.ShowDefaultPreviewBrush(terrain,
                                                              editContext.brushTexture,
                                                              editContext.brushStrength,
                                                              editContext.brushSize,
                                                              0.0f);

            bool drawCloneBrush = true;

            // on mouse up
            if (Event.current.type == EventType.MouseUp)
            {
                m_ActivePaint = false;
                if (!m_Aligned)
                {
                    m_Sample.m_UV       = m_SnapbackCache.m_UV;
                    m_Sample.m_Terrain  = m_SnapbackCache.m_Terrain;
                    m_Sample.m_Position = m_SnapbackCache.m_Position;
                }
            }

            // on mouse move
            if (Event.current.type == EventType.MouseMove || Event.current.type == EventType.MouseDrag)
            {
                if (m_Aligned && m_PaintedOnce)
                {
                    RaycastHit hit;
                    Ray        mouseRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                    terrain.GetComponent <Collider>().Raycast(mouseRay, out hit, Mathf.Infinity);

                    // check for moving across tiles
                    if (terrain != m_LastPaintLocation.m_Terrain && m_LastPaintLocation.m_Terrain != null)
                    {
                        UpdateLastPosition(hit, terrain);
                    }

                    if (m_Sample.m_Terrain != null)
                    {
                        PositionCloneBrush(hit);
                    }

                    // capture last (current) location for next frame
                    m_LastPaintLocation.m_UV       = hit.textureCoord;
                    m_LastPaintLocation.m_Position = hit.point;
                    m_LastPaintLocation.m_Terrain  = terrain;
                }

                if (m_Aligned && !m_PaintedOnce)
                {
                    drawCloneBrush = false; // dont draw if we havent selected where to paint yet when aligned
                }
            }

            // draw the clone brush preview
            if (m_Sample.m_Terrain != null && drawCloneBrush)
            {
                Rect sampleRect = TerrainPaintUtility.CalculateBrushRectInTerrainUnits(m_Sample.m_Terrain, m_Sample.m_UV, editContext.brushSize);
                TerrainPaintUtility.PaintContext ctx = TerrainPaintUtility.BeginPaintHeightmap(m_Sample.m_Terrain, sampleRect);

                FilterMode prevCtxFilterMode          = ctx.sourceRenderTexture.filterMode;
                FilterMode prevBrushTextureFilterMode = editContext.brushTexture.filterMode;

                ctx.sourceRenderTexture.filterMode  = FilterMode.Bilinear;
                editContext.brushTexture.filterMode = FilterMode.Bilinear;

                Vector2 topLeft = ctx.brushRect.min;
                float   xfrac   = ((topLeft.x - (int)topLeft.x) / (float)ctx.sourceRenderTexture.width);
                float   yfrac   = ((topLeft.y - (int)topLeft.y) / (float)ctx.sourceRenderTexture.height);

                Vector4 texScaleOffset = new Vector4(0.5f, 0.5f, 0.5f + xfrac + 0.5f / (float)ctx.sourceRenderTexture.width, 0.5f + yfrac + 0.5f / (float)ctx.sourceRenderTexture.height);

                RaycastHit hit = new RaycastHit();
                hit.point = m_Sample.m_Position;

                TerrainPaintUtilityEditor.DrawDefaultBrushPreviewMesh(m_Sample.m_Terrain, hit, ctx.sourceRenderTexture, editContext.brushTexture, 0.0001f, editContext.brushSize, TerrainPaintUtilityEditor.defaultPreviewPatchMesh, true, texScaleOffset);

                ctx.sourceRenderTexture.filterMode  = prevCtxFilterMode;
                editContext.brushTexture.filterMode = prevBrushTextureFilterMode;

                TerrainPaintUtility.ReleaseContextResources(ctx);
            }
        }
Example #24
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            commonUI.OnSceneGUI2D(terrain, editContext);
#if UNITY_2019_1_OR_NEWER
            // Don't paint if eyedropper is selected
            if (m_EyedropperSelected)
            {
                EditorGUIUtility.AddCursorRect(new Rect(0, 0, Screen.width, Screen.height), MouseCursor.CustomCursor);
                editContext.Repaint();
                return;
            }
#endif

            // We're only doing painting operations, early out if it's not a repaint
            if (!editContext.hitValidTerrain && !commonUI.isInUse)
            {
                return;
            }

            // update brush UI group
            commonUI.OnSceneGUI(terrain, editContext);

            // Don't render preview if this isn't a repaint. losing performance if we do
            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            Texture brushTexture = editContext.brushTexture;

            using (IBrushRenderPreviewUnderCursor brushRender = new BrushRenderPreviewUIGroupUnderCursor(commonUI, "PaintTextureTool", brushTexture))
            {
                if (brushRender.CalculateBrushTransform(out BrushTransform brushTransform))                 //cute.
                {
                    RenderTexture tmpRT            = RenderTexture.active;
                    Rect          brushBounds      = brushTransform.GetBrushXYBounds();
                    PaintContext  heightmapContext = brushRender.AcquireHeightmap(false, brushBounds, 1);
                    Material      brushMaterial    = GetBrushPreviewMaterial();

                    if (commonUI.brushMaskFilterStack.filters.Count > 0)
                    {
                        // Evaluate the brush mask filter stack
                        Vector3       brushPos = new Vector3(commonUI.raycastHitUnderCursor.point.x, 0, commonUI.raycastHitUnderCursor.point.z);
                        FilterContext fc       = new FilterContext(terrain, brushPos, commonUI.brushSize, commonUI.brushRotation);
                        fc.renderTextureCollection.GatherRenderTextures(heightmapContext.sourceRenderTexture.width, heightmapContext.sourceRenderTexture.height);
                        RenderTexture filterMaskRT = commonUI.GetBrushMask(fc, heightmapContext.sourceRenderTexture);

                        //Composite the brush texture onto the filter stack result
                        RenderTexture compRT   = RenderTexture.GetTemporary(filterMaskRT.descriptor);
                        Material      blendMat = GetBlendMaterial();
                        blendMat.SetTexture("_BlendTex", editContext.brushTexture);
                        blendMat.SetVector("_BlendParams", new Vector4(0.0f, 0.0f, -(commonUI.brushRotation * Mathf.Deg2Rad), 0.0f));
                        TerrainPaintUtility.SetupTerrainToolMaterialProperties(heightmapContext, brushTransform, blendMat);
                        Graphics.Blit(filterMaskRT, compRT, blendMat, 0);

                        RenderTexture.active = tmpRT;

                        BrushTransform identityBrushTransform = TerrainPaintUtility.CalculateBrushTransform(commonUI.terrainUnderCursor, commonUI.raycastHitUnderCursor.textureCoord, commonUI.brushSize, 0.0f);
                        TerrainPaintUtility.SetupTerrainToolMaterialProperties(heightmapContext, identityBrushTransform, brushMaterial);
                        TerrainPaintUtilityEditor.DrawBrushPreview(heightmapContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, compRT, identityBrushTransform, brushMaterial, 0);
                        RenderTexture.ReleaseTemporary(compRT);
                    }

                    brushRender.RenderBrushPreview(heightmapContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, brushTransform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0);
                }
            }
        }
Example #25
0
 public void RenderBrushPreview(PaintContext paintContext, TerrainPaintUtilityEditor.BrushPreview previewTexture, BrushTransform brushTransform, Material material, int pass)
 {
     TerrainPaintUtilityEditor.DrawBrushPreview(paintContext, previewTexture, brushTexture, brushTransform, material, pass);
 }
Example #26
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            Init();

            // m_rtCollection.DebugGUI( editContext.sceneView );

            brushUI.OnSceneGUI2D(terrain, editContext);

            // only do the rest if user mouse hits valid terrain or they are using the
            // brush parameter hotkeys to resize, etc
            if (!editContext.hitValidTerrain && !brushUI.isInUse && !m_editTransform && !debugOrtho)
            {
                return;
            }

            // update brush UI group
            brushUI.OnSceneGUI(terrain, editContext);

            bool justPressedEditKey = m_editTransform && !m_prevEditTransform;
            bool justReleaseEditKey = m_prevEditTransform && !m_editTransform;

            m_prevEditTransform = m_editTransform;

            if (justPressedEditKey)
            {
                (brushUI as MeshBrushUIGroup).LockTerrainUnderCursor(true);
                m_baseHandlePos        = brushUI.raycastHitUnderCursor.point;
                m_handleHeightOffsetWS = 0;
            }
            else if (justReleaseEditKey)
            {
                (brushUI as MeshBrushUIGroup).UnlockTerrainUnderCursor();
                m_handleHeightOffsetWS = 0;
            }

            // don't render mesh previews, etc. if the mesh field has not been populated yet
            if (activeMesh == null)
            {
                return;
            }

            // dont render preview if this isnt a repaint. losing performance if we do
            if (Event.current.type == EventType.Repaint)
            {
                Terrain currTerrain   = brushUI.terrainUnderCursor;
                Vector2 uv            = brushUI.raycastHitUnderCursor.textureCoord;
                float   brushSize     = brushUI.brushSize;
                float   brushRotation = brushUI.brushRotation;

                if (/* debugOrtho || */ brushUI.isRaycastHitUnderCursorValid)
                {
                    // if(debugOrtho)
                    // {
                    //     uv = Vector2.one * .5f;
                    // }

                    BrushTransform brushTransform = TerrainPaintUtility.CalculateBrushTransform(currTerrain, uv, brushSize, brushRotation);
                    PaintContext   ctx            = TerrainPaintUtility.BeginPaintHeightmap(brushUI.terrainUnderCursor, brushTransform.GetBrushXYBounds(), 1);
                    Material       material       = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial();

                    // don't draw the brush mask preview
                    // but draw the resulting mesh stamp preview
                    {
                        ApplyBrushInternal(terrain, ctx, brushTransform);

                        TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture,
                                                                   m_rtCollection[RenderTextureIDs.meshStamp], brushTransform, material, 0);

                        RenderTexture.active = ctx.oldRenderTexture;

                        material.SetTexture("_HeightmapOrig", ctx.sourceRenderTexture);
                        TerrainPaintUtility.SetupTerrainToolMaterialProperties(ctx, brushTransform, material);
                        TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.DestinationRenderTexture,
                                                                   m_rtCollection[RenderTextureIDs.meshStamp], brushTransform, material, 1);

                        TerrainPaintUtility.ReleaseContextResources(ctx);
                    }
                }
            }

            if (m_editTransform)
            {
                EditorGUI.BeginChangeCheck();
                {
                    Vector3 prevHandlePosWS = m_baseHandlePos + Vector3.up * m_handleHeightOffsetWS;

                    // draw transform handles
                    float handleSize = HandleUtility.GetHandleSize(prevHandlePosWS);
                    toolSettings.scale = Handles.ScaleHandle(toolSettings.scale, prevHandlePosWS, toolSettings.rotation, handleSize * 1.5f);
                    Quaternion brushRotation = Quaternion.AngleAxis(brushUI.brushRotation, Vector3.up);
                    toolSettings.rotation = Handles.RotationHandle(toolSettings.rotation, prevHandlePosWS);

                    Vector3 currHandlePosWS = Handles.Slider(prevHandlePosWS, Vector3.up, handleSize, Handles.ArrowHandleCap, 1f);
                    float   deltaHeight     = (currHandlePosWS.y - prevHandlePosWS.y);
                    m_handleHeightOffsetWS   += deltaHeight;
                    toolSettings.stampHeight += deltaHeight;
                }

                if (EditorGUI.EndChangeCheck())
                {
                    editContext.Repaint();
                    SaveSettings();
                }
            }
        }