public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            commonUI.OnPaint(terrain, editContext);

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

                using (IBrushRenderUnderCursor brushRender = new BrushRenderUIGroupUnderCursor(commonUI, "PaintHoles", brushTexture))
                {
                    Vector2        halfTexelOffset    = new Vector2(0.5f / terrain.terrainData.holesResolution, 0.5f / terrain.terrainData.holesResolution);
                    BrushTransform brushXform         = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv - halfTexelOffset, commonUI.brushSize, commonUI.brushRotation);
                    PaintContext   paintContext       = brushRender.AquireHolesTexture(true, brushXform.GetBrushXYBounds());
                    PaintContext   paintContextHeight = brushRender.AcquireHeightmap(false, brushXform.GetBrushXYBounds());

                    // filter stack
                    Material mat       = Utility.GetPaintHeightMaterial();
                    var      brushMask = RTUtils.GetTempHandle(paintContextHeight.sourceRenderTexture.width, paintContextHeight.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);
                    Utility.SetFilterRT(commonUI, paintContextHeight.sourceRenderTexture, brushMask, mat);

                    // hold control key to erase
                    float   brushStrength = Event.current.control ? commonUI.brushStrength : -commonUI.brushStrength;
                    Vector4 brushParams   = new Vector4(brushStrength, 0.0f, 0.0f, 0.0f);
                    mat.SetTexture("_BrushTex", editContext.brushTexture);
                    mat.SetVector("_BrushParams", brushParams);

                    brushRender.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
                    brushRender.RenderBrush(paintContext, mat, (int)TerrainBuiltinPaintMaterialPasses.PaintHoles);

                    TerrainPaintUtility.EndPaintHoles(paintContext, "Terrain Paint - Paint Holes");
                    RTUtils.Release(brushMask);
                }
            }
            return(true);
        }
Exemple #2
0
        public void Release(PaintContext paintContext)
        {
            if (ReferenceEquals(paintContext, m_HeightmapContext))
            {
                if (m_WriteToHeightmap)
                {
                    TerrainPaintUtility.EndPaintHeightmap(m_HeightmapContext, $"{m_Name} - Heightmap");
                }
                else
                {
                    TerrainPaintUtility.ReleaseContextResources(m_HeightmapContext);
                }

                m_HeightmapContext = null;
            }
            else if (ReferenceEquals(paintContext, m_NormalmapContext))
            {
                TerrainPaintUtility.ReleaseContextResources(m_NormalmapContext);
                m_NormalmapContext = null;
            }
            else if (ReferenceEquals(paintContext, m_TextureContext))
            {
                if (m_WriteToTexture)
                {
                    TerrainPaintUtility.EndPaintTexture(m_TextureContext, $"{m_Name} - Texture");
                }
                else
                {
                    TerrainPaintUtility.ReleaseContextResources(m_TextureContext);
                }

                m_TextureContext = null;
            }
#if UNITY_2019_3_OR_NEWER
            else if (ReferenceEquals(paintContext, m_HolesContext))
            {
                if (m_WriteToHoles)
                {
                    TerrainPaintUtility.EndPaintHoles(m_HolesContext, "Terrain Paint - Paint Holes");
                }
                else
                {
                    TerrainPaintUtility.ReleaseContextResources(m_HolesContext);
                }

                m_HolesContext = null;
            }
#endif
        }
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f);
            PaintContext   paintContext = TerrainPaintUtility.BeginPaintHoles(terrain, brushXform.GetBrushXYBounds());

            Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial();

            float   brushStrength = Event.current.shift ? editContext.brushStrength : -editContext.brushStrength;
            Vector4 brushParams   = new Vector4(brushStrength, 0.0f, 0.0f, 0.0f);

            mat.SetTexture("_BrushTex", editContext.brushTexture);
            mat.SetVector("_BrushParams", brushParams);

            TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);

            Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.PaintHoles);

            TerrainPaintUtility.EndPaintHoles(paintContext, "Terrain Paint - Paint Holes");
            return(true);
        }
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            commonUI.OnPaint(terrain, editContext);

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

                using (IBrushRenderUnderCursor brushRender = new BrushRenderUIGroupUnderCursor(commonUI, "PaintHoles", brushTexture))
                {
                    Vector2        halfTexelOffset    = new Vector2(0.5f / terrain.terrainData.holesResolution, 0.5f / terrain.terrainData.holesResolution);
                    BrushTransform brushXform         = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv - halfTexelOffset, commonUI.brushSize, commonUI.brushRotation);
                    PaintContext   paintContext       = brushRender.AquireHolesTexture(true, brushXform.GetBrushXYBounds());
                    PaintContext   paintContextHeight = brushRender.AcquireHeightmap(false, brushXform.GetBrushXYBounds());

                    // 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(paintContextHeight.sourceRenderTexture.width, paintContextHeight.sourceRenderTexture.height);
                    RenderTexture filterMaskRT = commonUI.GetBrushMask(fc, paintContextHeight.sourceRenderTexture);

                    Material mat = GetPaintHolesMaterial();

                    // hold control key to erase
                    float   brushStrength = Event.current.control ? commonUI.brushStrength : -commonUI.brushStrength;
                    Vector4 brushParams   = new Vector4(brushStrength, 0.0f, 0.0f, 0.0f);
                    mat.SetTexture("_BrushTex", editContext.brushTexture);
                    mat.SetTexture("_FilterTex", filterMaskRT);
                    mat.SetVector("_BrushParams", brushParams);

                    brushRender.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
                    brushRender.RenderBrush(paintContext, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.PaintHoles);

                    TerrainPaintUtility.EndPaintHoles(paintContext, "Terrain Paint - Paint Holes");
                }
            }
            return(true);
        }