Example #1
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);
        }
Example #2
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            // We're only doing painting operations, early out if it's not a repaint
            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

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

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

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

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

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

                TerrainPaintUtility.ReleaseContextResources(paintContext);
            }
        }
        public override 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);
        }
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            if (splatPaintRules == null)
            {
                return(false);
            }


            Material mat = GetPaintMaterial();

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



            // gathering heightmap
            BrushTransform brushXformForGatheringHeightmap   = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f);
            PaintContext   paintContextForGatheringHeightmap = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXformForGatheringHeightmap.GetBrushXYBounds(), 1);

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

            RenderTexture gatheredHeightmap = RenderTexture.GetTemporary(terrain.terrainData.heightmapTexture.descriptor);

            Graphics.Blit(paintContextForGatheringHeightmap.sourceRenderTexture, gatheredHeightmap); //, TerrainPaintUtility.GetBlitMaterial(), 0);
            TerrainPaintUtility.ReleaseContextResources(paintContextForGatheringHeightmap);


            // painting alphamap
            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);
            }


            float   targetAlpha = 1.0f;     // always 1.0 now -- no subtractive painting (we assume this in the ScatterAlphaMap)
            Vector4 brushParams = new Vector4(editContext.brushStrength, targetAlpha, splatPaintRules.useHeightTransition ? 1f : 0f, splatPaintRules.useAngleTransition ? 1f : 0f);
            Vector4 paintRulesParametersHeight  = new Vector4(splatPaintRules.minHeightStart, splatPaintRules.minHeightEnd, splatPaintRules.maxHeightStart, splatPaintRules.maxHeightEnd);
            Vector4 paintRulesParametersAngle   = new Vector4(splatPaintRules.minAngleStart, splatPaintRules.minAngleEnd, splatPaintRules.maxAngleStart, splatPaintRules.maxAngleEnd);
            Vector4 paintRulesInversionAndUsage = new Vector4(splatPaintRules.inverseHeightRule ? 1f : 0f, splatPaintRules.inverseAngleRule ? 1f : 0f, splatPaintRules.applyHeightRule ? 1f : 0f, splatPaintRules.applyAngleRule ? 1f : 0f);

            mat.SetVector("_BrushParams", brushParams);
            mat.SetVector("_TerrainSize", (Vector4)terrain.terrainData.size);
            mat.SetVector("_PaintRulesParametersHeight", paintRulesParametersHeight);
            mat.SetVector("_PaintRulesParametersAngle", paintRulesParametersAngle);
            mat.SetVector("_PaintRulesInversionAndUsage", paintRulesInversionAndUsage);
            mat.SetTexture("_Heightmap", gatheredHeightmap);
            TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
            Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0);
            TerrainPaintUtility.EndPaintTexture(paintContext, "Terrain Paint - Texture");



            RenderTexture.ReleaseTemporary(gatheredHeightmap);
            return(true);
        }
Example #5
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);
        }
        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);
        }
Example #7
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);
        }
Example #8
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);
        }
Example #9
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();
            }
        }
Example #10
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            if (!m_isPainting || m_SampleLocation.terrain == null)
            {
                return(true);
            }

            // grab brush transforms for the sample location (where we are cloning from)
            // and target location (where we are cloning to)
            Vector2        sampleUV         = TerrainUVFromBrushLocation(m_SampleLocation.terrain, m_SampleLocation.pos);
            BrushTransform sampleBrushXform = TerrainPaintUtility.CalculateBrushTransform(m_SampleLocation.terrain, sampleUV, editContext.brushSize, 1);
            BrushTransform targetBrushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 1);

            // set material props that will be used for both heightmap and alphamap painting
            Material mat         = GetPaintMaterial();
            Vector4  brushParams = new Vector4(editContext.brushStrength, m_StampingOffsetFromClone * 0.5f, terrain.terrainData.size.y, 0f);

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

            // apply texture modifications to terrain
            if (m_PaintAlphamap)
            {
                PaintAlphamap(m_SampleLocation.terrain, terrain, sampleBrushXform, targetBrushXform, mat);
            }
            if (m_PaintHeightmap)
            {
                PaintHeightmap(m_SampleLocation.terrain, terrain, sampleBrushXform, targetBrushXform, editContext, mat);
            }

            return(false);
        }
Example #11
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);
        }
Example #12
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);
        }
Example #13
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);
        }
        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);
        }
Example #15
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);
        }
Example #16
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);
        }
Example #17
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);
        }
Example #18
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);
        }
Example #19
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);
        }
Example #20
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f);

            float sign = Event.current.shift?-1.0f:1.0f;

            return(DoPaint(terrain, editContext.brushStrength * sign, editContext.brushTexture, brushXform));
        }
Example #21
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);
        }
Example #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, editContext.brushStrength);
            TerrainPaintUtility.EndPaintHeightmap(targetContext, "Terrain Paint - Clone Brush (Heightmap)");
            TerrainPaintUtility.ReleaseContextResources(sampleContext);
        }
        protected override bool CalculateTransform(ref Terrain terrain, out BrushTransform brushTransform)
        {
            Vector2 uv            = textureCoordUnderCursor;
            float   brushSize     = uiGroup.brushSize;
            float   brushRotation = uiGroup.brushRotation;

            // TODO: Remove this method and replace the preview with a radius effect and scatter at the correct position...
            brushTransform = TerrainPaintUtility.CalculateBrushTransform(terrain, uv, brushSize, brushRotation);
            return(true);
        }
 public override void OnRenderBrushPreview(Terrain terrain, IOnSceneGUI editContext)
 {
     if (m_MouseOnPatchIndex == -1 && editContext.hitValidTerrain && Event.current.type == EventType.Repaint)
     {
         BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, editContext.brushSize, 0.0f);
         PaintContext   ctx        = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1);
         TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainBrushPreviewMode.SourceRenderTexture, editContext.brushTexture, brushXform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0);
         TerrainPaintUtility.ReleaseContextResources(ctx);
     }
 }
        internal static BrushTransform GetBrushTransform(float rotation, float brushSize)
        {
            float   f              = rotation * Mathf.Deg2Rad;
            float   num            = Mathf.Cos(f);
            float   x              = Mathf.Sin(f);
            Vector2 brushU         = new Vector2(num, -x) * brushSize;
            Vector2 brushV         = new Vector2(x, num) * brushSize;
            var     brushTransform = new BrushTransform(Vector2.zero - 0.5f * brushU - 0.5f * brushV, brushU, brushV);

            return(brushTransform);
        }
Example #27
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));
            }
        }
        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);
            }
        }
Example #29
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();
        }
Example #30
0
 public virtual bool CalculateBrushTransform(Terrain terrain, Vector2 uv, float size, float rotation, out BrushTransform brushTransform)
 {
     if (m_UiGroup.ScatterBrushStamp(ref terrain, ref uv))
     {
         brushTransform = TerrainPaintUtility.CalculateBrushTransform(terrain, uv, size, rotation);
         return(true);
     }
     else
     {
         brushTransform = new BrushTransform();
         return(false);
     }
 }
Example #31
0
		public static d.Brush ToGdiPlus(this LinearGradientBrush brush, Rect bounds) {
			var db = CheckDegenerate(brush);
			if (db != null) return db;
			var bt = new BrushTransform(brush, bounds);
			if (bt.DegenerateBrush != null) return bt.DegenerateBrush;

			var start = brush.StartPoint;
			var end = brush.EndPoint;
			if (brush.MappingMode == BrushMappingMode.RelativeToBoundingBox) {
				start = bt.ToAbsolute.Transform(start);
				end = bt.ToAbsolute.Transform(end);
			}

			var wm = brush.SpreadMethod.ToGdiPlus();
			if (wm == d2.WrapMode.Clamp) {
				wm = d2.WrapMode.TileFlipX;
				var delta = (bounds.BottomRight - bounds.TopLeft).Length 
					/ (bt.ToBrush.Transform(end) - bt.ToBrush.Transform(start)).Length;
				var diff = delta * (end - start);
				start -= diff; end += diff;
				brush = brush.Clone();
				var g = brush.GradientStops;
				g.Insert(0, new GradientStop(g[0].Color, -delta));
				g.Add(new GradientStop(g[g.Count - 1].Color, delta + 1));
			}

			var b = new d2.LinearGradientBrush(start.ToGdiPlus(), end.ToGdiPlus(), d.Color.Black, d.Color.White);
			b.InterpolationColors = ConvertGradient(brush);
			b.WrapMode = wm;
			b.MultiplyTransform(bt.ToBrush.ToGdiPlus(), d2.MatrixOrder.Append);
			return b;
		}
Example #32
0
		public static d.Brush ToGdiPlus(this RadialGradientBrush brush, Rect bounds) {
			var db = CheckDegenerate(brush);
			if (db != null) return db;
			var bt = new BrushTransform(brush, bounds);
			if (bt.DegenerateBrush != null) return bt.DegenerateBrush;

			var center = brush.Center;
			var focus = brush.GradientOrigin;
			var size = new Vector(brush.RadiusX, brush.RadiusY);
			if (brush.MappingMode == BrushMappingMode.RelativeToBoundingBox) {
				center = bt.ToAbsolute.Transform(center);
				focus = bt.ToAbsolute.Transform(focus);
				size = bt.ToAbsolute.Transform(size);
			}

			var ts = bt.ToBrush.Transform(size);
			var delta = (int)Math.Ceiling(4 * (bounds.BottomRight - bounds.TopLeft).Length 
				/ Math.Min(Math.Abs(ts.X), Math.Abs(ts.Y)));
			size *= delta;
			center += (delta - 1) * (center - focus);
			brush = brush.Clone();
			var g = brush.GradientStops;
			var last = g.Count - 1;
			var offset = 1.00000001;
			switch(brush.SpreadMethod) {
			case GradientSpreadMethod.Pad:
				g.Add(new GradientStop(g[last].Color, delta));
				break;
			case GradientSpreadMethod.Repeat:
				for(var i = 0; i < delta; ++i)
					for(var j = 0; j <= last; ++j)
						g.Add(new GradientStop(g[j].Color, i + g[j].Offset + (j == last ? 1 : offset)));
				break;
			case GradientSpreadMethod.Reflect:
				for(var i = 0; i < delta; ++i)
					if (i % 2 == 0) 
						for(var j = 0; j <= last; ++j)
							g.Add(new GradientStop(g[j].Color, i + (1 - g[j].Offset) + (j == 0 ? 1 : offset)));
					else
						for(var j = 0; j <= last; ++j)
							g.Add(new GradientStop(g[j].Color, i + g[j].Offset + (j == last ? 1 : offset)));
				break;
			}

			var b = new d2.PathGradientBrush(new EllipseGeometry(center, size.X, size.Y).ToGdiPlus());
			b.CenterPoint = focus.ToGdiPlus();
			b.InterpolationColors = ConvertGradient(brush);
			b.WrapMode = brush.SpreadMethod.ToGdiPlus();
			b.MultiplyTransform(bt.ToBrush.ToGdiPlus(), d2.MatrixOrder.Append);
			return b;
		}
Example #33
0
		public static d.Brush ToGdiPlus(this ImageBrush brush, Rect bounds) {
			var img = brush.ImageSource;
			var bt = new BrushTransform(brush, bounds);
			if (bt.DegenerateBrush != null) return bt.DegenerateBrush;

			var viewbox = brush.Viewbox;
			if (brush.ViewboxUnits == BrushMappingMode.RelativeToBoundingBox)
				viewbox.Scale(img.Width, img.Height);
			var viewport = brush.Viewport;
			if (brush.ViewportUnits == BrushMappingMode.RelativeToBoundingBox)
				viewport.Transform(bt.ToAbsolute);

			var ia = new di.ImageAttributes();
			ia.SetColorMatrix(new di.ColorMatrix { Matrix33 = (float)brush.Opacity });
			var b = new d.TextureBrush(img.ToGdiPlus(), viewbox.ToGdiPlus(), ia);
			b.WrapMode = brush.TileMode.ToGdiPlus();

			b.TranslateTransform((float)viewport.X, (float)viewport.Y);
			b.ScaleTransform((float)(viewport.Width/viewbox.Width), (float)(viewport.Height/viewbox.Height));
			b.MultiplyTransform(bt.ToBrush.ToGdiPlus(), d2.MatrixOrder.Append);

			return b;
		}