Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
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));
            }
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
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   ctx        = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);
                TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0);
                TerrainPaintUtility.ReleaseContextResources(ctx);
            }
        }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
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);
            }
        }
Exemplo n.º 8
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();
        }
Exemplo n.º 9
0
        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);
            }
        }
Exemplo n.º 10
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);
                }
            }
        }
Exemplo n.º 11
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();
                }
            }
        }
Exemplo n.º 12
0
 public void RenderBrushPreview(PaintContext paintContext, TerrainPaintUtilityEditor.BrushPreview previewTexture, BrushTransform brushTransform, Material material, int pass)
 {
     TerrainPaintUtilityEditor.DrawBrushPreview(paintContext, previewTexture, brushTexture, brushTransform, material, pass);
 }
Exemplo n.º 13
0
        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
        }
Exemplo n.º 14
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();
                }
            }
        }