Пример #1
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            if (Event.current.type == EventType.MouseDrag)
            {
                return(true);
            }

            Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial();

            Rect brushRect = TerrainPaintUtility.CalculateBrushRectInTerrainUnits(terrain, editContext.uv, editContext.brushSize);

            TerrainPaintUtility.PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushRect);

            Vector4 brushParams = new Vector4(editContext.brushStrength * 0.01f, 0.0f, m_StampHeight, 0.0f);

            if (Event.current.shift)
            {
                brushParams.x = -brushParams.x;
            }

            mat.SetTexture("_BrushTex", editContext.brushTexture);
            mat.SetVector("_BrushParams", brushParams);
            Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.StampHeight);

            TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Stamp");
            return(true);
        }
Пример #2
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);
        }
Пример #3
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            Vector2 uv = editContext.uv;

            if (Event.current.shift)
            {
                height = terrain.terrainData.GetInterpolatedHeight(uv.x, uv.y) / terrain.terrainData.size.y;
                return(true);
            }

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

            paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear;

            Material mat         = GetPaintMaterial();
            int      currentMode = (int)mode;

            if (Event.current.control)
            {
                currentMode *= -1;
            }
            float   modeValue   = Mathf.Clamp01(currentMode);
            Vector4 brushParams = new Vector4(editContext.brushStrength, 0.5f * height, modeValue, 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 - Directional Flatten Height");
            return(false);
        }
Пример #4
0
        private bool Smooth(Terrain terrain, IOnPaint editContext, Vector2 currUV, BrushSettings brushSettings)
        {
            // the brush size is relative to the main brush size
            float brushSize = brushSettings.brushSize * this.smoothBrushSize / 100f;

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

            Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial();

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

            // brushStrength = Event.current.shift ? -brushStrength : 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.SmoothHeights);

            TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Smooth Height");

            return(true);
        }
Пример #5
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            Init();

            if (activeMesh == null || Event.current.type != EventType.MouseDown || Event.current.shift == true || m_editTransform)
            {
                return(false);
            }

            brushUI.OnPaint(terrain, editContext);

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

                BrushTransform brushTransform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, brushUI.brushSize, brushUI.brushRotation);
                PaintContext   ctx            = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushTransform.GetBrushXYBounds());

                ApplyBrushInternal(terrain, ctx, brushTransform);

                TerrainPaintUtility.EndPaintHeightmap(ctx, "Mesh Stamp - Stamp Mesh");
            }

            return(true);
        }
Пример #6
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            if (m_Mesh == null || Event.current.type != EventType.MouseDown || Event.current.shift == true)
            {
                return(false);
            }

            Vector3 terrainSize     = terrain.terrainData.size;
            float   maxScale        = Mathf.Max(m_StampScale.x, m_StampScale.z);
            Vector2 brushSizeScaled = new Vector2(maxScale * 2.0f, maxScale * 2.0f);
            Rect    brushRect       = new Rect(editContext.uv * new Vector2(terrainSize.x, terrainSize.z) - brushSizeScaled * 0.5f, brushSizeScaled);

            TerrainPaintUtility.PaintContext context = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushRect);
            Material mat = GetPaintMaterial();

            DrawMesh(terrain, context, maxScale, mat);

            // draw to heightmap
            mat.SetTexture("_MeshStampTex", m_MeshRenderTexture);
            Graphics.Blit(context.sourceRenderTexture, context.destinationRenderTexture, mat, (int)ShaderPasses.StampToHeightmap);

            TerrainPaintUtility.EndPaintHeightmap(context, "Terrain Paint - Mesh Stamp");
            RenderTexture.ReleaseTemporary(m_MeshRenderTexture);
            m_MeshRenderTexture = null;
            return(true);
        }
Пример #7
0
        void Flatten(Terrain terrain)
        {
            Undo.RegisterCompleteObjectUndo(terrain.terrainData, "Set Height - Flatten Tile");

            var ctx = TerrainPaintUtility.BeginPaintHeightmap(terrain, new Rect(0, 0, terrain.terrainData.size.x, terrain.terrainData.size.z), 1);

            Material mat = GetPaintMaterial();

            float terrainHeight = Mathf.Clamp01((m_TargetHeight - terrain.transform.position.y) / terrain.terrainData.size.y);

            Vector4 brushParams = new Vector4(0, 0.5f * terrainHeight, 0.0f, 0.0f);

            mat.SetVector("_BrushParams", brushParams);

            var size = terrain.terrainData.size;

            size.y = 0;
            var brushMask = RTUtils.GetTempHandle(ctx.sourceRenderTexture.width, ctx.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);

            commonUI.GetBrushMask(terrain, ctx.sourceRenderTexture, brushMask, terrain.transform.position, Mathf.Min(size.x, size.z), 0f); // TODO(wyatt): need to handle seams
            mat.SetTexture("_FilterTex", brushMask);
            mat.SetTexture("_MainTex", ctx.sourceRenderTexture);
            Graphics.Blit(ctx.sourceRenderTexture, ctx.destinationRenderTexture, mat, 1);
            TerrainPaintUtility.EndPaintHeightmap(ctx, "Set Height - Flatten Tile");
            RTUtils.Release(brushMask);
            PaintContext.ApplyDelayedActions();
        }
Пример #8
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;
            }
        }
Пример #9
0
        private bool Height(Terrain terrain, IOnPaint editContext, Vector2 currUV, Vector2 prevUV)
        {
            // the brush size is relative to the main brush size
            float brushSize = editContext.brushSize * heightBrushSize / 100f;

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

            paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear;

            Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial();

            float brushStrength = heightBrushStrength / 100f;

            brushStrength = Event.current.shift ? -brushStrength : brushStrength;

            brushStrength *= 0.001f; // magic number ...

            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, 0);

            TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Raise or Lower Height");

            return(true);
        }
Пример #10
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(), 1);

            float finalPinchAmount = m_PinchAmount * 0.005f; //scale to a reasonable value and negate so default mode is clockwise

            if (Event.current.shift)
            {
                finalPinchAmount *= -1.0f;
            }

            paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear;

            Material mat         = GetPaintMaterial();
            Vector4  brushParams = new Vector4(editContext.brushStrength, 0.0f, finalPinchAmount, 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, "Terrain Paint - Pinch Height");
            return(false);
        }
Пример #11
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            Init();

            commonUI.OnPaint(terrain, editContext);

            if (commonUI.allowPaint)
            {
                Vector2 uv = editContext.uv;

                if (commonUI.ScatterBrushStamp(ref terrain, ref uv))
                {
                    BrushTransform brushXform    = TerrainPaintUtility.CalculateBrushTransform(terrain, uv, commonUI.brushSize, commonUI.brushRotation);
                    PaintContext   paintContext  = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds());
                    float          brushStrength = Event.current.control ? -commonUI.brushStrength : commonUI.brushStrength;

                    Vector3 brushPosWS = WSPosFromTerrainUV(terrain, uv);

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

                    TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Noise");
                }
            }
            editContext.Repaint(RepaintFlags.UI);

            return(true);
        }
Пример #12
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)
            {
                Rect brushRect = TerrainPaintUtility.CalculateBrushRectInTerrainUnits(terrain, editContext.uv, editContext.brushSize);
                TerrainPaintUtility.PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushRect);

                Vector2 smudgeDir = editContext.uv - m_PrevBrushPos;

                Material mat         = GetPaintMaterial();
                Vector4  brushParams = new Vector4(editContext.brushStrength, smudgeDir.x, smudgeDir.y, 0);
                mat.SetTexture("_BrushTex", editContext.brushTexture);
                mat.SetVector("_BrushParams", brushParams);
                Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0);

                TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Smudge Height");

                m_PrevBrushPos = editContext.uv;
            }
            m_PreviousEvent = Event.current.type;
            return(false);
        }
Пример #13
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);
        }
        void Update()
        {
            if (stepsRemaining <= 0)
            {
                ToggleRenderSurface(false);
                return;
            }

            ToggleRenderSurface(true);

            int stepsThisUpdate = (int)(m_SimSpeed * 20.0f) + 1;

            while (stepsThisUpdate-- > 0 && stepsRemaining > 0)
            {
                Erode();
            }

            if (stepsRemaining <= 0)
            {
                continuing = false;
                ToggleRenderSurface(false);
                TerrainPaintUtility.EndPaintHeightmap(heightContext, "Terrain Paint - Hydraulic Erosion");
                if (maskContext != null)
                {
                    TerrainPaintUtility.ReleaseContextResources(maskContext);
                }
            }
            else
            {
                heightContext.ScatterHeightmap("Terrain Paint - Hydraulic Erosion");
            }
        }
        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);
        }
Пример #16
0
        private bool Bridge(Terrain terrain, IOnPaint editContext, Vector2 currUV, float pct, Vector3 stroke, Vector3 startPoint)
        {
            float heightOffset  = heightProfile.Evaluate(pct) / terrain.terrainData.size.y;
            float strengthScale = strengthProfile.Evaluate(pct);
            float widthScale    = widthProfile.Evaluate(pct);

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

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

            Material mat         = GetMaterial();
            Vector4  brushParams = new Vector4();

            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(true);
        }
Пример #17
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();
                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 - Smudge Height");

                m_PrevBrushPos = editContext.uv;
            }
            m_PreviousEvent = Event.current.type;
            return(false);
        }
Пример #18
0
        /// <summary>
        /// This is the handler that is called when one of the gizmo is moved.
        /// </summary>
        /// <param name="gizmo">
        /// The gizmo that was dragged.
        /// </param>
        /// <param name="handleId">
        /// The id of the handle that is being dragged.
        /// </param>
        private void OnGizmoDragUpdate(Gizmo gizmo, int handleId)
        {
            // This is where we want to adjust the position of the TerrainData heights for mesh control points
            // and change the terrain height. First, we need to check the drag
            // channel. The drag channel informs us about the kind of drag that is
            // being applied (offset, rotation or scale). Tt will most
            // likely always be offset, but it's safer to always check.
            if (gizmo.ActiveDragChannel == GizmoDragChannel.Offset)
            {
#if FALSE
                Vector3        pos          = transform.position;
                Vector2        pos2         = new Vector2(pos.x, pos.z);
                BrushTransform brushXform   = TerrainPaintUtility.CalculateBrushTransform(theTerrain, pos2, gcount, 0.0f);
                PaintContext   paintContext = TerrainPaintUtility.BeginPaintHeightmap(theTerrain, brushXform.GetBrushXYBounds(), 1);

                //float finalPinchAmount = m_PinchAmount * 0.005f; //scale to a reasonable value and negate so default mode is clockwise
                //if (Event.current.shift)
                //{
                //	finalPinchAmount *= -1.0f;
                //}

                paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear;

                Material mat         = GetPaintMaterial();
                Vector4  brushParams = new Vector4(1.0f, 0.0f, 1.0f /*finalPinchAmount*/, 0.0f);
                mat.SetFloatArray("_Brush", grid);
                mat.SetFloat("_BrushSize", gcount);
                mat.SetVector("_BrushParams", brushParams);
                TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
                Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0);

                TerrainPaintUtility.EndPaintHeightmap(paintContext, null);
#endif
                // We are offsetting the gizmo. Good. Now we need to apply the drag offset to the TerrainData (or mesh control point)
                // associated with the gizmo.

                // Note: pseudo code to adjust TerrainData heights.
                //       The gizmo positions and terrain heights don't match up after a gizmo move - this pseudo code is just meant to show the concept.
                //       Depending on what we come up with, I feel a terrain mesh with vertice control points might be the best method?

                // Get the amount of height change from the gizmo move.

                switch (interpolation)
                {
                case Interpolation.Bilinear: UpdateTerrainBilinear(gizmo); break;

                case Interpolation.Bicubic: UpdateTerrainBicubic(gizmo); break;
                }


                // ideally use a mesh instead of TerrainData and change control points? e.g. controlPoint.Position += dragOffset;
                // The control point could be moved using the amount of drag that was applied to the gizmo.
                // As I mentioned, the the Ogre application I did, the terrain was a mesh generated by creating a spline from control points
                // and tessellated into a mesh so the result from the spline was a fairly smooth mesh.
                // I'm open to the best method we can come up with that is easy for the user to adjust terrain with and also allow importing
                // of control points from 2D CAD data to build the mesh.
            }
        }
        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);
        }
Пример #20
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, editContext.brushStrength);
            TerrainPaintUtility.EndPaintHeightmap(targetContext, "Terrain Paint - Clone Brush (Heightmap)");
            TerrainPaintUtility.ReleaseContextResources(sampleContext);
        }
Пример #21
0
    void ModifyTerrain(Rect selection)
    {
        //terrain = Terrain.activeTerrain;
        if (trees.Count > 0)
        {
            trees.AddRange(terrain.terrainData.treeInstances);
            terrain.terrainData.treeInstances = trees.ToArray();
            trees.Clear();
        }
        //Trees inside circle
        foreach (var tree in terrain.terrainData.treeInstances)
        {
            Vector2 p = new Vector2((tree.position.x * terrain.terrainData.size.x) + terrain.transform.position.x, (tree.position.z * terrain.terrainData.size.z) + terrain.transform.position.z);
            float   d = Mathf.Sqrt(Mathf.Pow(p.x - transform.position.x, 2) + Mathf.Pow(p.y - transform.position.z, 2));
            if (d < rectSize.x * .5f)
            {
                trees.Add(tree);
            }
        }
        if (trees.Count > 0)
        {
            terrain.terrainData.treeInstances = terrain.terrainData.treeInstances.Except(trees).ToArray();
        }

        PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, selection);

        Debug.Log("Rect: " + selection + " contextRect: " + paintContext.pixelRect + "/" + paintContext.pixelSize);
        RenderTexture terrainRenderTexture = new RenderTexture(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0, RenderTextureFormat.R16);

        terrainRenderTexture.enableRandomWrite = true;
        Graphics.CopyTexture(paintContext.sourceRenderTexture, terrainRenderTexture);

        prevRenderTexture = new RenderTexture(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0, RenderTextureFormat.R16);
        Graphics.CopyTexture(paintContext.sourceRenderTexture, prevRenderTexture);

        float h0 = terrain.SampleHeight(new Vector3(selection.position.x + terrain.transform.position.x, 0, selection.position.y + terrain.transform.position.z));
        float h1 = terrain.SampleHeight(new Vector3(selection.position.x + terrain.transform.position.x, 0, selection.position.y + terrain.transform.position.z + rectSize.y));
        float h2 = terrain.SampleHeight(new Vector3(selection.position.x + terrain.transform.position.x + rectSize.x, 0, selection.position.y + terrain.transform.position.z + rectSize.y));
        float h3 = terrain.SampleHeight(new Vector3(selection.position.x + terrain.transform.position.x + rectSize.x, 0, selection.position.y + terrain.transform.position.z));

        height = (((h0 + h1 + h2 + h3) / 4f) /* + terrain.transform.position.y*/) / terrain.terrainData.size.y;

        terrainPaintShader.SetFloat("height", height * smoothOffset);
        terrainPaintShader.SetTexture(terrainPaintShader.FindKernel("CSMain"), "heightmap", terrainRenderTexture);
        terrainPaintShader.SetTexture(terrainPaintShader.FindKernel("CSMain"), "brush", brushTexture);

        terrainPaintShader.Dispatch(terrainPaintShader.FindKernel("CSMain"), (int)Mathf.Ceil(selection.width / 32), (int)Mathf.Ceil(selection.height / 32), 1);

        Graphics.CopyTexture(terrainRenderTexture, paintContext.destinationRenderTexture);

        TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain");

        terrain.terrainData.SyncHeightmap();
    }
Пример #22
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            if (m_Mesh == null || Event.current.type != EventType.MouseDown || Event.current.shift == true)
            {
                return(false);
            }

            Vector3      terrainSize = terrain.terrainData.size;
            PaintContext context     = ApplyBrushInternal(terrain, editContext.uv * new Vector2(terrainSize.x, terrainSize.z), 0);

            TerrainPaintUtility.EndPaintHeightmap(context, "Terrain Paint - Mesh Stamp");
            return(true);
        }
Пример #23
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();
        }
Пример #24
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");
    }
Пример #25
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);
        }
Пример #26
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
        }
Пример #27
0
    void RestoreTerrain(Rect selection)
    {
        if (baked)
        {
            baked = false;
            return;
        }
        PaintContext prevPaintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, selection);

        Graphics.CopyTexture(prevRenderTexture, prevPaintContext.destinationRenderTexture);

        TerrainPaintUtility.EndPaintHeightmap(prevPaintContext, "Terrain");

        terrain.terrainData.SyncHeightmap();
    }
Пример #28
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);
        }
Пример #29
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");
    }
Пример #30
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            Rect brushRect = TerrainPaintUtility.CalculateBrushRectInTerrainUnits(terrain, editContext.uv, editContext.brushSize);

            TerrainPaintUtility.PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushRect);

            Material mat         = TerrainPaintUtility.GetBuiltinPaintMaterial();
            Vector4  brushParams = new Vector4(editContext.brushStrength, 0.0f, 0.0f, 0.0f);

            mat.SetTexture("_BrushTex", editContext.brushTexture);
            mat.SetVector("_BrushParams", brushParams);
            Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.SmoothHeights);

            TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Smooth Height");
            return(true);
        }