コード例 #1
0
        private void PaintAlphamap(Terrain sampleTerrain, Terrain targetTerrain, BrushTransform sampleXform, BrushTransform targetXform, Material mat)
        {
            Rect sampleRect             = sampleXform.GetBrushXYBounds();
            Rect targetRect             = targetXform.GetBrushXYBounds();
            int  numSampleTerrainLayers = sampleTerrain.terrainData.terrainLayers.Length;

            for (int i = 0; i < numSampleTerrainLayers; ++i)
            {
                TerrainLayer layer = sampleTerrain.terrainData.terrainLayers[i];

                if (layer == null)
                {
                    continue;                // nothing to paint if the layer is NULL
                }
                PaintContext sampleContext = TerrainPaintUtility.BeginPaintTexture(sampleTerrain, sampleRect, layer);

                // manually create target context since we are possibly applying another terrain's layers and not its own
                int          layerIndex    = TerrainPaintUtility.FindTerrainLayerIndex(sampleTerrain, layer);
                Texture2D    layerTexture  = TerrainPaintUtility.GetTerrainAlphaMapChecked(sampleTerrain, layerIndex >> 2);
                PaintContext targetContext = PaintContext.CreateFromBounds(targetTerrain, targetRect, layerTexture.width, layerTexture.height);
                targetContext.CreateRenderTargets(RenderTextureFormat.R8);
                targetContext.GatherAlphamap(layer, true);
                sampleContext.sourceRenderTexture.filterMode = FilterMode.Point;
                mat.SetTexture("_CloneTex", sampleContext.sourceRenderTexture);
                Graphics.Blit(targetContext.sourceRenderTexture, targetContext.destinationRenderTexture, mat, (int)ShaderPasses.CloneAlphamap);
                // apply texture modifications and perform cleanup. same thing as calling TerrainPaintUtility.EndPaintTexture
                targetContext.ScatterAlphamap("Terrain Paint - Clone Brush (Texture)");
                targetContext.Cleanup();
            }
        }
コード例 #2
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            BrushTransform targetBrushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 1);

            Material mat         = GetPaintMaterial();
            Vector4  brushParams = new Vector4(editContext.brushStrength, 0, 0, 0f);

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

            Rect targetRect             = targetBrushXform.GetBrushXYBounds();
            int  numSampleTerrainLayers = terrain.terrainData.terrainLayers.Length;

            for (int i = 0; i < numSampleTerrainLayers; ++i)
            {
                TerrainLayer layer = terrain.terrainData.terrainLayers[i];
                if (layer == null)
                {
                    continue;
                }

                int          layerIndex    = TerrainPaintUtility.FindTerrainLayerIndex(terrain, layer);
                Texture2D    layerTexture  = TerrainPaintUtility.GetTerrainAlphaMapChecked(terrain, layerIndex >> 2);
                PaintContext targetContext = PaintContext.CreateFromBounds(terrain, targetRect, layerTexture.width, layerTexture.height);
                targetContext.CreateRenderTargets(RenderTextureFormat.R8);
                targetContext.GatherAlphamap(layer, true);
                Graphics.Blit(targetContext.sourceRenderTexture, targetContext.destinationRenderTexture, mat, 0);

                TerrainPaintUtility.EndPaintTexture(targetContext, "Terrain Paint - Smooth Splatmaps");
            }
            return(false);
        }
コード例 #3
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f);
            Rect           rect       = brushXform.GetBrushXYBounds();

            PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, rect);
            Material     mat          = GetPaintMaterial();

            PaintContext maskContext = null;

            if (m_TextureMask || m_TextureStencil)
            {
                TerrainLayer maskTerrainLayer = terrain.terrainData.terrainLayers[m_TextureMask ? m_maskIndex : m_stencilIndex];
                maskContext = TerrainPaintUtility.BeginPaintTexture(terrain, rect, maskTerrainLayer);
                if (maskContext == null)
                {
                    return(false);
                }
                mat.SetTexture("_MaskTex", maskContext.sourceRenderTexture);
            }
            mat.SetInt("_MaskStencil", m_TextureMask ? 1 : (m_TextureStencil ? 2 : 0));

            ApplyBrushInternal(paintContext, editContext.brushStrength, editContext.brushTexture, brushXform, mat);

            if (maskContext != null)
            {
                TerrainPaintUtility.ReleaseContextResources(maskContext);
            }

            TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Masked Terrace Erosion");
            return(false);
        }
コード例 #4
0
ファイル: SmoothHeightTool.cs プロジェクト: Reflex21/Moba
        private void ApplyBrushInternal(Terrain terrain, PaintContext paintContext, float brushStrength, Texture brushTexture, BrushTransform brushXform)
        {
            /*
             * ComputeShader cs = GetDiffusionShader();
             *
             * int kernel = cs.FindKernel("Diffuse");
             * cs.SetFloat("dt", 0.1f);
             * cs.SetFloat("diff", 0.01f);
             * cs.SetVector("texDim", new Vector4(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0.0f, 0.0f));
             * cs.SetTexture(kernel, "InputTex", paintContext.sourceRenderTexture);
             * cs.SetTexture(kernel, "OutputTex", paintContext.destinationRenderTexture);
             * cs.Dispatch(kernel, paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 1);
             */

            RenderTexture prev = RenderTexture.active;

            Material mat       = GetMaterial();
            var      brushMask = RTUtils.GetTempHandle(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);

            Utility.SetFilterRT(commonUI, paintContext.sourceRenderTexture, brushMask, mat);
            Vector4 brushParams = new Vector4(Mathf.Clamp(brushStrength, 0.0f, 1.0f), 0.0f, 0.0f, 0.0f);

            mat.SetTexture("_BrushTex", brushTexture);
            mat.SetVector("_BrushParams", brushParams);
            Vector4 smoothWeights = new Vector4(
                Mathf.Clamp01(1.0f - Mathf.Abs(m_direction)),   // centered
                Mathf.Clamp01(-m_direction),                    // min
                Mathf.Clamp01(m_direction),                     // max
                0);

            mat.SetInt("_KernelSize", (int)Mathf.Max(1, m_KernelSize)); // kernel size
            mat.SetVector("_SmoothWeights", smoothWeights);

            var texelCtx = Utility.CollectTexelValidity(terrain, brushXform.GetBrushXYBounds());

            Utility.SetupMaterialForPaintingWithTexelValidityContext(paintContext, texelCtx, brushXform, mat);

            paintContext.sourceRenderTexture.wrapMode      = TextureWrapMode.Clamp;
            paintContext.destinationRenderTexture.wrapMode = TextureWrapMode.Clamp;

            // Two pass blur (first horizontal, then vertical)
            var tmpRT = RTUtils.GetTempHandle(paintContext.destinationRenderTexture.descriptor);

            tmpRT.RT.wrapMode = TextureWrapMode.Clamp;
            mat.SetVector("_BlurDirection", Vector2.right);
            Graphics.Blit(paintContext.sourceRenderTexture, tmpRT, mat);
            mat.SetVector("_BlurDirection", Vector2.up);
            Graphics.Blit(tmpRT, paintContext.destinationRenderTexture, mat);

            RTUtils.Release(tmpRT);
            RTUtils.Release(brushMask);
            texelCtx.Cleanup();

            RenderTexture.active = prev;
            RenderTexture.ReleaseTemporary(brushMask);
        }
コード例 #5
0
    /// <summary>Terraces the HeightMap.</summary>
    /// <param name="featureSize">The height of each terrace.</param>
    /// <param name="interiorCornerWeight">The weight of the terrace effect.</param>
    public static void TerraceErodeHeightmap(float featureSize, float interiorCornerWeight)
    {
        Material       mat          = new Material((Shader)AssetDatabase.LoadAssetAtPath("Packages/com.unity.terrain-tools/Shaders/TerraceErosion.shader", typeof(Shader)));
        BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(Land, heightmapCentre, Land.terrainData.size.x, 0.0f);
        PaintContext   paintContext = TerrainPaintUtility.BeginPaintHeightmap(Land, brushXform.GetBrushXYBounds());
        Vector4        brushParams  = new Vector4(1.0f, featureSize, interiorCornerWeight, 0.0f);

        mat.SetTexture("_BrushTex", terrainFilterTexture);
        mat.SetVector("_BrushParams", brushParams);
        TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
        Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0);
        TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Filter - TerraceErosion");
    }
コード例 #6
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            if (Event.current.type == EventType.MouseDown)
            {
                m_PrevBrushPos = editContext.uv;
                return(false);
            }

            if (Event.current.type == EventType.MouseDrag && m_PreviousEvent == EventType.MouseDrag)
            {
                BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f);
                PaintContext   paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);

                Vector2 smudgeDir = editContext.uv - m_PrevBrushPos;

                paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear;

                Material mat = GetPaintMaterial();

                PaintContext maskContext = null;
                if (m_TextureMask || m_TextureStencil)
                {
                    TerrainLayer maskTerrainLayer = terrain.terrainData.terrainLayers[m_TextureMask ? m_maskIndex : m_stencilIndex];
                    maskContext = TerrainPaintUtility.BeginPaintTexture(terrain, brushXform.GetBrushXYBounds(), maskTerrainLayer);
                    if (maskContext == null)
                    {
                        return(false);
                    }
                    mat.SetTexture("_MaskTex", maskContext.sourceRenderTexture);
                }
                mat.SetInt("_MaskStencil", m_TextureMask ? 1 : (m_TextureStencil ? 2 : 0));

                Vector4 brushParams = new Vector4(editContext.brushStrength, smudgeDir.x, smudgeDir.y, 0);
                mat.SetTexture("_BrushTex", editContext.brushTexture);
                mat.SetVector("_BrushParams", brushParams);

                TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
                Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0);

                TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Masked Smudge");
                if (maskContext != null)
                {
                    TerrainPaintUtility.ReleaseContextResources(maskContext);
                }

                m_PrevBrushPos = editContext.uv;
            }
            m_PreviousEvent = Event.current.type;
            return(false);
        }
コード例 #7
0
    /// <summary>Smooths the terrain.</summary>
    /// <param name="filterStrength">The strength of the smoothing.</param>
    /// <param name="blurDirection">The direction the smoothing should preference. Between -1f - 1f.</param>
    public static void SmoothHeightmap(float filterStrength, float blurDirection)
    {
        Material       mat          = TerrainPaintUtility.GetBuiltinPaintMaterial();
        BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(Land, heightmapCentre, Land.terrainData.size.x, 0.0f);
        PaintContext   paintContext = TerrainPaintUtility.BeginPaintHeightmap(Land, brushXform.GetBrushXYBounds());
        Vector4        brushParams  = new Vector4(filterStrength, 0.0f, 0.0f, 0.0f);

        mat.SetTexture("_BrushTex", terrainFilterTexture);
        mat.SetVector("_BrushParams", brushParams);
        Vector4 smoothWeights = new Vector4(Mathf.Clamp01(1.0f - Mathf.Abs(blurDirection)), Mathf.Clamp01(-blurDirection), Mathf.Clamp01(blurDirection), 0.0f);

        mat.SetVector("_SmoothWeights", smoothWeights);
        TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
        Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.SmoothHeights);
        TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Filter - Smooth Heights");
    }
コード例 #8
0
        private bool DoPaint(Terrain terrain, float brushStrength, Texture brushTexture, BrushTransform brushXform)
        {
            Rect rect = brushXform.GetBrushXYBounds();

            PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);

            if (paintContext == null)
            {
                return(false);
            }

            paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear;

            Material mat = GetPaintMaterial();

            PaintContext maskContext = null;

            if (m_TextureMask || m_TextureStencil)
            {
                TerrainLayer maskTerrainLayer = terrain.terrainData.terrainLayers[m_TextureMask ? m_maskIndex : m_stencilIndex];
                maskContext = TerrainPaintUtility.BeginPaintTexture(terrain, rect, maskTerrainLayer);
                if (maskContext == null)
                {
                    return(false);
                }
                mat.SetTexture("_MaskTex", maskContext.sourceRenderTexture);
            }
            mat.SetInt("_MaskStencil", m_TextureMask ? 1 : (m_TextureStencil ? 2 : 0));

            Vector4 brushParams = new Vector4(brushStrength, 0.0f, m_FeatureSize, 0);

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

            TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);

            Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0);

            TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Masked Smooth Sharpen");
            if (maskContext != null)
            {
                TerrainPaintUtility.ReleaseContextResources(maskContext);
            }

            return(true);
        }
コード例 #9
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f);

            Rect rect = brushXform.GetBrushXYBounds();

            PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, rect, 1);

            Material mat = GetPaintMaterial();

            PaintContext maskContext = null;

            if (m_TextureMask || m_TextureStencil)
            {
                TerrainLayer maskTerrainLayer = terrain.terrainData.terrainLayers[m_TextureMask ? m_maskIndex : m_stencilIndex];
                maskContext = TerrainPaintUtility.BeginPaintTexture(terrain, rect, maskTerrainLayer);
                if (maskContext == null)
                {
                    return(false);
                }
                mat.SetTexture("_MaskTex", maskContext.sourceRenderTexture);
            }
            mat.SetInt("_MaskStencil", m_TextureMask ? 1 : (m_TextureStencil ? 2 : 0));

            // apply brush
            Vector4 brushParams = new Vector4(
                editContext.brushStrength,
                m_ErosionStrength,
                m_MixStrength,
                0.0f);

            mat.SetTexture("_BrushTex", editContext.brushTexture);
            mat.SetVector("_BrushParams", brushParams);
            TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
            Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0);

            TerrainPaintUtility.EndPaintHeightmap(paintContext, "Masked Ridge Erode");

            if (maskContext != null)
            {
                TerrainPaintUtility.ReleaseContextResources(maskContext);
            }

            return(false);
        }
コード例 #10
0
        private void PaintAlphamap(Terrain sampleTerrain, Terrain targetTerrain, BrushTransform sampleXform, BrushTransform targetXform, Material mat)
        {
            Rect sampleRect             = sampleXform.GetBrushXYBounds();
            Rect targetRect             = targetXform.GetBrushXYBounds();
            int  numSampleTerrainLayers = sampleTerrain.terrainData.terrainLayers.Length;

            for (int i = 0; i < numSampleTerrainLayers; ++i)
            {
                TerrainLayer layer = sampleTerrain.terrainData.terrainLayers[i];

                if (layer == null)
                {
                    continue; // nothing to paint if the layer is NULL
                }
                PaintContext sampleContext = TerrainPaintUtility.BeginPaintTexture(sampleTerrain, sampleRect, layer);

                int          layerIndex    = TerrainPaintUtility.FindTerrainLayerIndex(sampleTerrain, layer);
                Texture2D    layerTexture  = TerrainPaintUtility.GetTerrainAlphaMapChecked(sampleTerrain, layerIndex >> 2);
                PaintContext targetContext = PaintContext.CreateFromBounds(targetTerrain, targetRect, layerTexture.width, layerTexture.height);
                targetContext.CreateRenderTargets(RenderTextureFormat.R8);
                targetContext.GatherAlphamap(layer, true);
                sampleContext.sourceRenderTexture.filterMode = FilterMode.Point;
                mat.SetTexture("_CloneTex", sampleContext.sourceRenderTexture);
                mat.SetVector("_SampleUVScaleOffset", ComputeSampleUVScaleOffset(sampleContext, targetContext));

                var brushMask = RTUtils.GetTempHandle(targetContext.sourceRenderTexture.width, targetContext.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);
                Utility.SetFilterRT(commonUI, targetContext.sourceRenderTexture, brushMask, mat);
                TerrainPaintUtility.SetupTerrainToolMaterialProperties(targetContext, targetXform, mat);
                Graphics.Blit(targetContext.sourceRenderTexture, targetContext.destinationRenderTexture, mat, (int)ShaderPasses.CloneAlphamap);
                // apply texture modifications and perform cleanup. same thing as calling TerrainPaintUtility.EndPaintTexture
                targetContext.ScatterAlphamap("Terrain Paint - Clone Brush (Texture)");

                // Restores RenderTexture.active
                targetContext.Cleanup();
                RTUtils.Release(brushMask);
            }
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        private bool PaintTexture(Terrain terrain, IOnPaint editContext, Vector2 currUV)
        {
            // the brush size is relative to the main brush size
            float brushSize = editContext.brushSize * paintBrushSize / 100f;

            BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(terrain, currUV, brushSize, 0.0f);
            PaintContext   paintContext = TerrainPaintUtility.BeginPaintTexture(terrain, brushXform.GetBrushXYBounds(), m_SelectedInnerTerrainLayer);

            if (paintContext == null)
            {
                return(false);
            }


            Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial();

            float targetAlpha   = 1.0f;                      // always 1.0 now -- no subtractive painting (we assume this in the ScatterAlphaMap)
            float brushStrength = paintBrushStrength / 100f; // editContext.brushStrength

            // apply brush
            Vector4 brushParams = new Vector4(brushStrength, targetAlpha, 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.PaintTexture);

            TerrainPaintUtility.EndPaintTexture(paintContext, "Terrain Paint - Texture");

            return(true);
        }
コード例 #13
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            commonUI.OnPaint(terrain, editContext);

            if (!commonUI.allowPaint)
            {
                return(true);
            }

            if (Event.current.type == EventType.MouseDown)
            {
                m_PrevBrushPos = editContext.uv;
                return(false);
            }

            if (Event.current.type == EventType.MouseDrag && m_PreviousEvent == EventType.MouseDrag)
            {
                Vector2 uv = editContext.uv;

                if (commonUI.ScatterBrushStamp(ref terrain, ref uv))
                {
                    Material mat       = GetPaintMaterial();
                    Vector2  smudgeDir = uv - m_PrevBrushPos;

                    BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, uv, commonUI.brushSize, commonUI.brushRotation);

                    Vector4 brushParams = new Vector4(commonUI.brushStrength, smudgeDir.x, smudgeDir.y, 0);
                    mat.SetTexture("_BrushTex", editContext.brushTexture);
                    mat.SetVector("_BrushParams", brushParams);

                    //smudge splat map
                    if (m_AffectMaterials)
                    {
                        for (int i = 0; i < terrain.terrainData.terrainLayers.Length; i++)
                        {
                            TerrainLayer layer = terrain.terrainData.terrainLayers[i];

                            if (layer == null)
                            {
                                continue;                // nothing to paint if the layer is NULL
                            }
                            PaintContext sampleContext = TerrainPaintUtility.BeginPaintTexture(terrain, brushXform.GetBrushXYBounds(), layer);
                            Graphics.Blit(sampleContext.sourceRenderTexture, sampleContext.destinationRenderTexture, mat, 0);
                            TerrainPaintUtility.EndPaintTexture(sampleContext, "Terrain Paint - Twist Brush (Texture)");
                        }
                    }

                    //smudge the height map
                    if (m_AffectHeight)
                    {
                        PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);
                        TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
                        Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0);
                        TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Twist Brush (Height)");
                    }

                    m_PrevBrushPos = uv;
                }
            }
            m_PreviousEvent = Event.current.type;
            return(false);
        }
コード例 #14
0
        private void DrawBrushPreviews(Terrain terrain, IOnSceneGUI editContext)
        {
            Vector2        sampleUV;
            BrushTransform sampleXform;
            PaintContext   sampleContext = null;
            Material       previewMat    = Utility.GetDefaultPreviewMaterial();

            // 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());
                var texelCtx = Utility.CollectTexelValidity(sampleContext.originTerrain, sampleXform.GetBrushXYBounds());
                Utility.SetupMaterialForPaintingWithTexelValidityContext(sampleContext, texelCtx, sampleXform, previewMat);
                TerrainPaintUtilityEditor.DrawBrushPreview(sampleContext, TerrainBrushPreviewMode.SourceRenderTexture,
                                                           editContext.brushTexture, sampleXform, previewMat, 0);
                texelCtx.Cleanup();
            }

            // 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);
                var            texelCtx   = Utility.CollectTexelValidity(ctx.originTerrain, brushXform.GetBrushXYBounds());
                Utility.SetupMaterialForPaintingWithTexelValidityContext(ctx, texelCtx, brushXform, previewMat);

                TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainBrushPreviewMode.SourceRenderTexture,
                                                           editContext.brushTexture, brushXform, previewMat, 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, TerrainBrushPreviewMode.DestinationRenderTexture,
                                                               editContext.brushTexture, brushXform, previewMat, 1);
                }

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

            // Restores RenderTexture.active
            sampleContext?.Cleanup();
        }
コード例 #15
0
        private void PaintHeightmap(Terrain sampleTerrain, Terrain targetTerrain, BrushTransform sampleXform,
                                    BrushTransform targetXform, IOnPaint editContext, Material mat)
        {
            PaintContext sampleContext = TerrainPaintUtility.BeginPaintHeightmap(sampleTerrain, sampleXform.GetBrushXYBounds());
            PaintContext targetContext = TerrainPaintUtility.BeginPaintHeightmap(targetTerrain, targetXform.GetBrushXYBounds());

            ApplyHeightmap(sampleContext, targetContext, targetXform, targetTerrain, editContext.brushTexture, commonUI.brushStrength);
            TerrainPaintUtility.EndPaintHeightmap(targetContext, "Terrain Paint - Clone Brush (Heightmap)");

            // Restores RenderTexture.active
            sampleContext.Cleanup();
            targetContext.Cleanup();
        }
コード例 #16
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);
            }
        }
コード例 #17
0
        public bool OnPaint(Terrain terrain, IOnPaint editContext, float brushSize, float brushRotation, float brushStrength, Vector2 uv)
        {
            if (Event.current != null && Event.current.shift)
            {
                BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(terrain, uv, brushSize, brushRotation);
                PaintContext   paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds());

                Material mat = GetMaterial(); //TerrainPaintUtility.GetBuiltinPaintMaterial();

                float m_direction = 0.0f;     //TODO: UI for this

                Vector4 brushParams = new Vector4(brushStrength, 0.0f, 0.0f, 0.0f);
                mat.SetTexture("_BrushTex", editContext.brushTexture);
                mat.SetVector("_BrushParams", brushParams);
                Vector4 smoothWeights = new Vector4(
                    Mathf.Clamp01(1.0f - Mathf.Abs(m_direction)),   // centered
                    Mathf.Clamp01(-m_direction),                    // min
                    Mathf.Clamp01(m_direction),                     // max
                    kernelSize);                                    // blur kernel size
                mat.SetVector("_SmoothWeights", smoothWeights);
                TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);

                RenderTexture temp = RenderTexture.GetTemporary(paintContext.destinationRenderTexture.descriptor);

                Graphics.Blit(paintContext.sourceRenderTexture, temp, mat, 0);
                Graphics.Blit(temp, paintContext.destinationRenderTexture, mat, 1);

                RenderTexture.ReleaseTemporary(temp);

                TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Smooth Height");
                return(true);
            }
            return(false);
        }
コード例 #18
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f);
            PaintContext   paintContext = TerrainPaintUtility.BeginPaintTexture(terrain, brushXform.GetBrushXYBounds(), m_SelectedTerrainLayer);

            if (paintContext == null)
            {
                return(false);
            }

            Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial();

            // apply brush
            float   targetAlpha = 1.0f;     // always 1.0 now -- no subtractive painting (we assume this in the ScatterAlphaMap)
            Vector4 brushParams = new Vector4(editContext.brushStrength, targetAlpha, 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.PaintTexture);

            TerrainPaintUtility.EndPaintTexture(paintContext, "Terrain Paint - Texture");
            return(true);
        }
コード例 #19
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);
            }
        }
コード例 #20
0
        public static void ShowDefaultPreviewBrush(Terrain terrain, Texture brushTexture, float brushSize)
        {
            Ray        mouseRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
            RaycastHit hit;

            if (terrain.GetComponent <Collider>().Raycast(mouseRay, out hit, Mathf.Infinity))
            {
                BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, hit.textureCoord, brushSize, 0.0f);
                PaintContext   ctx        = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);
                DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, brushTexture, brushXform, GetDefaultBrushPreviewMaterial(), 0);
                TerrainPaintUtility.ReleaseContextResources(ctx);
            }
        }
コード例 #21
0
        override public void OnSceneGUI(Terrain currentTerrain, IOnSceneGUI editContext)
        {
            if (editContext.hitValidTerrain)
            {
                Terrain terrain = currentTerrain;

                // the smooth brush size is relative to the main brush size
                float brushSize = editContext.brushSize * ridgeErodeBrushSize / 100f;

                BrushTransform brushXform      = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, brushSize, 0.0f);
                PaintContext   ctx             = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);
                Material       brushPreviewMat = BrushUtilities.GetDefaultBrushPreviewMaterial();
                brushPreviewMat.color = ridgeErodeBrushColor;
                BrushUtilities.DrawBrushPreview(ctx, BrushUtilities.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, brushPreviewMat, 0);
                TerrainPaintUtility.ReleaseContextResources(ctx);
            }
        }
コード例 #22
0
ファイル: BridgeTool.cs プロジェクト: lqi11/robotaut
        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;
                }

                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);
            }
        }
コード例 #23
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            Vector2 uv = editContext.uv;

            //grab the starting position & height
            if (Event.current.shift)
            {
                float height = terrain.terrainData.GetInterpolatedHeight(uv.x, uv.y) / terrain.terrainData.size.y;
                m_StartPoint   = new Vector3(uv.x, uv.y, height);
                m_StartTerrain = terrain;
                return(true);
            }

            if (!m_StartTerrain || (Event.current.type == EventType.MouseDrag))
            {
                return(true);
            }

            //get the target position & height
            float   targetHeight = terrain.terrainData.GetInterpolatedHeight(uv.x, uv.y) / terrain.terrainData.size.y;
            Vector3 targetPos    = new Vector3(uv.x, uv.y, targetHeight);

            if (terrain != m_StartTerrain)
            {
                //figure out the stroke vector in uv,height space
                Vector2 targetWorld = transformToWorld(terrain, uv);
                Vector2 targetUVs   = transformToUVSpace(m_StartTerrain, targetWorld);
                targetPos.x = targetUVs.x;
                targetPos.y = targetUVs.y;
            }

            Vector3 stroke       = targetPos - m_StartPoint;
            float   strokeLength = stroke.magnitude;
            int     numSplats    = (int)(strokeLength / (0.001f * m_Spacing));

            Terrain  currTerrain = m_StartTerrain;
            Material mat         = GetPaintMaterial();

            Vector2 posOffset   = new Vector2(0.0f, 0.0f);
            Vector2 currUV      = new Vector2();
            Vector4 brushParams = new Vector4();

            Vector2 jitterVec = new Vector2(-stroke.z, stroke.x); //perpendicular to stroke direction

            jitterVec.Normalize();

            for (int i = 0; i < numSplats; i++)
            {
                float pct = (float)i / (float)numSplats;

                float widthScale    = widthProfile.Evaluate(pct);
                float heightOffset  = heightProfile.Evaluate(pct) / currTerrain.terrainData.size.y;
                float strengthScale = strengthProfile.Evaluate(pct);
                float jitterOffset  = jitterProfile.Evaluate(pct) / Mathf.Max(currTerrain.terrainData.size.x, currTerrain.terrainData.size.z);

                Vector3 currPos = m_StartPoint + pct * stroke;

                //add in jitter offset (needs to happen before tile correction)
                currPos.x += posOffset.x + jitterOffset * jitterVec.x;
                currPos.y += posOffset.y + jitterOffset * jitterVec.y;

                if (currPos.x >= 1.0f && (currTerrain.rightNeighbor != null))
                {
                    currTerrain  = currTerrain.rightNeighbor;
                    currPos.x   -= 1.0f;
                    posOffset.x -= 1.0f;
                }
                if (currPos.x <= 0.0f && (currTerrain.leftNeighbor != null))
                {
                    currTerrain  = currTerrain.leftNeighbor;
                    currPos.x   += 1.0f;
                    posOffset.x += 1.0f;
                }
                if (currPos.y >= 1.0f && (currTerrain.topNeighbor != null))
                {
                    currTerrain  = currTerrain.topNeighbor;
                    currPos.y   -= 1.0f;
                    posOffset.y -= 1.0f;
                }
                if (currPos.y <= 0.0f && (currTerrain.bottomNeighbor != null))
                {
                    currTerrain  = currTerrain.bottomNeighbor;
                    currPos.y   += 1.0f;
                    posOffset.y += 1.0f;
                }

                currUV.x = currPos.x;
                currUV.y = currPos.y;

                int   finalBrushSize = (int)(widthScale * (float)editContext.brushSize);
                float finalHeight    = (m_StartPoint + pct * stroke).z + heightOffset;

                BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(currTerrain, currUV, finalBrushSize, 0.0f);
                PaintContext   paintContext = TerrainPaintUtility.BeginPaintHeightmap(currTerrain, brushXform.GetBrushXYBounds());

                mat.SetTexture("_BrushTex", editContext.brushTexture);

                brushParams.x = editContext.brushStrength * strengthScale;
                brushParams.y = 0.5f * finalHeight;

                mat.SetVector("_BrushParams", brushParams);

                TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);

                Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0);

                TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Bridge");
            }
            return(false);
        }
コード例 #24
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            MaterialManager mgr = terrain.GetComponent <MaterialManager>();

            if (mgr != null)
            {
                //                IndexMap indexMap = mgr.GetIndexMap();
                //                RenderTexture rt= indexMap.GetTempRenderTexture(true);
                paintInProgress = true;
                BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f);

                PaintContext indexCtx = PaintContext.CreateFromBounds(terrain, brushXform.GetBrushXYBounds(), 64, 64, 1);
                indexCtx.CreateRenderTargets(RenderTextureFormat.ARGB32);

                PaintContext normalCtx = TerrainPaintUtility.CollectNormals(terrain, brushXform.GetBrushXYBounds(), 2);

                Material blitMaterial = TerrainPaintUtility.GetBlitMaterial();
                indexCtx.Gather(
                    TerrainToIndexMapTexture,
                    new Color(0.0f, 0.0f, 0.0f, 0.0f),
                    blitMaterial, 0,
                    null,       // before
                    null);      // after

                // render source -> dest here
                //                RenderTexture.active = ctx.oldRenderTexture;

                Material paintMaterial = GetPaintMaterial();

                float   brushStrength = Event.current.shift ? -editContext.brushStrength : editContext.brushStrength;
                Vector4 brushParams   = new Vector4(
                    brushStrength,
                    0.0f,
                    0.0f, 0.0f);
                paintMaterial.SetTexture("_BrushTex", editContext.brushTexture);
                paintMaterial.SetVector("_BrushParams", brushParams);
                paintMaterial.SetFloat("materialIndex", materialIndex);
                paintMaterial.SetTexture("_NormalMap", normalCtx.sourceRenderTexture);

                Vector4 indexToNormalXform;
                TerrainPaintUtility.BuildTransformPaintContextUVToPaintContextUV(
                    indexCtx,
                    normalCtx,
                    out indexToNormalXform);
                paintMaterial.SetVector("_indexToNormalXform", indexToNormalXform);

                Vector4 xformParams = new Vector4();
                xformParams.x = (randomRotation ? minRotation : fixedRotation) / 360.0f;
                xformParams.y = (randomRotation ? maxRotation : fixedRotation) / 360.0f;
                paintMaterial.SetVector("_xformParams", xformParams);

                Vector4 randoms = new Vector4(
                    UnityEngine.Random.Range(0.0f, 1.0f),
                    UnityEngine.Random.Range(0.0f, 1.0f),
                    UnityEngine.Random.Range(0.0f, 1.0f),
                    UnityEngine.Random.Range(0.0f, 1.0f));
                paintMaterial.SetVector("_randoms", randoms);

                TerrainPaintUtility.SetupTerrainToolMaterialProperties(indexCtx, brushXform, paintMaterial);
                Graphics.Blit(indexCtx.sourceRenderTexture, indexCtx.destinationRenderTexture, paintMaterial, 0);

                //  ctx.ScatterToTexture()          // we should do this ... less temp render textures
                // and users don't have to store render textures at all...
                indexCtx.Scatter(
                    TerrainToIndexMapRenderTexture,
                    blitMaterial, 0,
                    null,
                    null);

                normalCtx.Cleanup();
                indexCtx.Cleanup();
            }

            /*
             *          BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f);
             *          PaintContext paintContext = TerrainPaintUtility.BeginPaintTexture(terrain, brushXform.GetBrushXYBounds(), m_SelectedTerrainLayer);
             *          if (paintContext == null)
             *              return false;
             *
             *          Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial();
             *
             *          // apply brush
             *          float targetAlpha = 1.0f;       // always 1.0 now -- no subtractive painting (we assume this in the ScatterAlphaMap)
             *          Vector4 brushParams = new Vector4(editContext.brushStrength, targetAlpha, 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.PaintTexture);
             *
             *          TerrainPaintUtility.EndPaintTexture(paintContext, "Terrain Paint - Texture");
             */
            return(true);
        }
コード例 #25
0
        private bool Smudge(Terrain terrain, IOnPaint editContext, Vector2 currUV, Vector2 prevUV)
        {
            // the brush size is relative to the main brush size
            float brushSize = editContext.brushSize * ridgeErodeBrushSize / 100f;

            BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(terrain, currUV, brushSize, 0.0f);
            PaintContext   paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);

            Vector2 smudgeDir = editContext.uv - prevUV;

            paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear;

            Material mat = GetMaterial();

            float brushStrength = ridgeErodeBrushStrength / 100f; // editContext.brushStrength;

            Vector4 brushParams = new Vector4(brushStrength, erosionStrength, mixStrength, 0);

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

            TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
            Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0);

            TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Ridge Erode");

            return(true);
        }
コード例 #26
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));
            }
        }
コード例 #27
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f);
            PaintContext   paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds());

            ApplyBrushInternal(paintContext, editContext.brushStrength, editContext.brushTexture, brushXform);
            TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Smooth Height");
            return(true);
        }
コード例 #28
0
        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);
        }
コード例 #29
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);
            }
        }
コード例 #30
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            // ignore mouse drags
            if (Event.current.type == EventType.MouseDrag)
            {
                return(true);
            }

            BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f);
            PaintContext   paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds());

            ApplyBrushInternal(paintContext, editContext.brushStrength, editContext.brushTexture, brushXform, terrain, Event.current.shift);
            TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Stamp");
            return(true);
        }