private bool Height(Terrain terrain, IOnPaint editContext, Vector2 currUV, Vector2 prevUV, BrushSettings brushSettings) { // the brush size is relative to the main brush size float brushSize = brushSettings.brushSize * heightBrushSize / 100f; BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, currUV, brushSize, brushSettings.brushRotationDegrees); 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); }
private bool Smooth(Terrain terrain, IOnPaint editContext, Vector2 currUV) { // the brush size is relative to the main brush size float brushSize = editContext.brushSize * this.smoothBrushSize / 100f; BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, brushSize, 0.0f); 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); }
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 finalTwistAmount = m_TwistAmount * -0.001f; //scale to a reasonable value and negate so default mode is clockwise if (Event.current.shift) { finalTwistAmount *= -1.0f; } paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear; Material mat = GetPaintMaterial(); Vector4 brushParams = new Vector4(editContext.brushStrength, 0.0f, finalTwistAmount, 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 - Twist Height"); return(false); }
private bool Smudge(Terrain terrain, IOnPaint editContext, Vector2 currUV, Vector2 prevUV, BrushSettings brushSettings) { // the brush size is relative to the main brush size float brushSize = brushSettings.brushSize * this.smudgeBrushSize / 100f; BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, currUV, brushSize, brushSettings.brushRotationDegrees); PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1); Vector2 smudgeDir = editContext.uv - prevUV; paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear; Material mat = GetMaterial(); float brushStrength = this.smudgeBrushStrength / 100f; Vector4 brushParams = new Vector4(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"); return(true); }
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); }
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); }
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); }
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); }
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, terrain); // 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) { 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); }
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(terrain, paintContext, brushXform, brushPosWS, commonUI.brushRotation, brushStrength, commonUI.brushSize, editContext.brushTexture); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Noise"); } } editContext.Repaint(RepaintFlags.UI); return(true); }
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); }
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); } commonUI.OnPaint(terrain, editContext); if (commonUI.allowPaint) { Texture brushTexture = editContext.brushTexture; BrushTransform brushTransform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, commonUI.brushSize, commonUI.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) { 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) { 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, 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 Smooth"); return(false); }
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)); }
/// <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); }
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); } }
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); } }
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); } }
/// <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"); }
override public void OnSceneGUI(Terrain currentTerrain, IOnSceneGUI editContext) { Terrain terrain = currentTerrain; float brushSize = editContext.brushSize; 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 = bridgeBrushColor; BrushUtilities.DrawBrushPreview(ctx, BrushUtilities.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, brushPreviewMat, 0); TerrainPaintUtility.ReleaseContextResources(ctx); }
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); }
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); }
public void OnSceneGUI(Terrain currentTerrain, IOnSceneGUI editContext, BrushSettings brushSettings) { if (m_StartTerrain != null && m_StartPoint != null) { // anchor is placed on the start terrain, independent of the active one; needed for multi-tiles Terrain terrain = m_StartTerrain; BrushTransform anchorBrushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, m_StartPoint, brushSettings.brushSize, brushSettings.brushRotationDegrees); PaintContext anchorCtx = TerrainPaintUtility.BeginPaintHeightmap(terrain, anchorBrushXform.GetBrushXYBounds(), 1); Material brushPreviewMat = BrushUtilities.GetDefaultBrushPreviewMaterial(); brushPreviewMat.color = anchorBrushColor; BrushUtilities.DrawBrushPreview(anchorCtx, BrushUtilities.BrushPreview.SourceRenderTexture, editContext.brushTexture, anchorBrushXform, brushPreviewMat, 0); TerrainPaintUtility.ReleaseContextResources(anchorCtx); } }
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(); }
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); } }