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(); }
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) { 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, editContext.brushSize, 0.0f); 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 bool OnPaint(Terrain terrain, IOnPaint editContext) { if (Event.current.shift) { m_HeightWorldSpace = terrain.terrainData.GetInterpolatedHeight(editContext.uv.x, editContext.uv.y) + terrain.transform.position.y; editContext.Repaint(RepaintFlags.UI); 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); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Set Height"); return(true); }
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 = GetPaintMaterial(); Vector4 brushParams = new Vector4(editContext.brushStrength, 0.0f, m_FeatureSize, 0.0f); mat.SetTexture("_BrushTex", editContext.brushTexture); mat.SetVector("_BrushParams", brushParams); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Terrace Erosion"); return(false); }
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); }
/// <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"); }
public override bool OnPaint(Terrain terrain, IOnPaint editContext) { if (Event.current.type == EventType.MouseDrag) { return(true); } BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, m_BrushSize, m_BrushRotation); PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds()); ApplyBrushInternal(paintContext, m_BrushStrength, editContext.brushTexture, brushXform, terrain); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Tools - Stamp Tool Extended"); return(true); }
public override bool OnPaint(Terrain terrain, IOnPaint editContext) { if (Event.current.shift) { m_Height = terrain.terrainData.GetInterpolatedHeight(editContext.uv.x, editContext.uv.y) / terrain.terrainData.size.y; editContext.RepaintAllInspectors(); 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); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Set Height"); return(true); }
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); } }
private void DrawBrushPreviews(Terrain terrain, IOnSceneGUI editContext) { Vector2 sampleUV; BrushTransform sampleXform; PaintContext sampleContext = null; Material previewMat = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(); // 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, editContext.brushSize, 0); sampleContext = TerrainPaintUtility.BeginPaintHeightmap(m_SampleLocation.terrain, sampleXform.GetBrushXYBounds()); TerrainPaintUtilityEditor.DrawBrushPreview(sampleContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, sampleXform, previewMat, 0); } // draw brush preview and mesh preview for current mouse position if (editContext.hitValidTerrain) { BrushTransform targetXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, editContext.brushSize, 0f); PaintContext targetContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, targetXform.GetBrushXYBounds(), 1); // draw basic preview of brush TerrainPaintUtilityEditor.DrawBrushPreview(targetContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, targetXform, previewMat, 0); if (sampleContext != null && m_PaintHeightmap) { ApplyHeightmap(sampleContext, targetContext, targetXform, terrain, editContext.brushTexture, editContext.brushStrength); // draw preview of brush mesh RenderTexture.active = targetContext.oldRenderTexture; previewMat.SetTexture("_HeightmapOrig", targetContext.sourceRenderTexture); TerrainPaintUtilityEditor.DrawBrushPreview(targetContext, TerrainPaintUtilityEditor.BrushPreview.DestinationRenderTexture, editContext.brushTexture, targetXform, previewMat, 1); } TerrainPaintUtility.ReleaseContextResources(targetContext); } if (sampleContext != null) { TerrainPaintUtility.ReleaseContextResources(sampleContext); } }
override public void OnSceneGUI(Terrain currentTerrain, IOnSceneGUI editContext) { if (editContext.hitValidTerrain) { Terrain terrain = currentTerrain; // the smooth brush size is relative to the main brush size float brushSize = editContext.brushSize * ridgeErodeBrushSize / 100f; BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, brushSize, 0.0f); PaintContext ctx = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1); Material brushPreviewMat = BrushUtilities.GetDefaultBrushPreviewMaterial(); brushPreviewMat.color = ridgeErodeBrushColor; BrushUtilities.DrawBrushPreview(ctx, BrushUtilities.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, brushPreviewMat, 0); TerrainPaintUtility.ReleaseContextResources(ctx); } }
private bool DoPaint(Terrain terrain, float brushStrength, Texture brushTexture, BrushTransform brushXform) { Rect rect = brushXform.GetBrushXYBounds(); PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1); if (paintContext == null) { return(false); } paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear; Material mat = GetPaintMaterial(); PaintContext maskContext = null; if (m_TextureMask || m_TextureStencil) { TerrainLayer maskTerrainLayer = terrain.terrainData.terrainLayers[m_TextureMask ? m_maskIndex : m_stencilIndex]; maskContext = TerrainPaintUtility.BeginPaintTexture(terrain, rect, maskTerrainLayer); if (maskContext == null) { return(false); } mat.SetTexture("_MaskTex", maskContext.sourceRenderTexture); } mat.SetInt("_MaskStencil", m_TextureMask ? 1 : (m_TextureStencil ? 2 : 0)); Vector4 brushParams = new Vector4(brushStrength, 0.0f, m_FeatureSize, 0); mat.SetTexture("_BrushTex", brushTexture); mat.SetVector("_BrushParams", brushParams); TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Masked Smooth Sharpen"); if (maskContext != null) { TerrainPaintUtility.ReleaseContextResources(maskContext); } return(true); }
public override bool OnPaint(Terrain terrain, IOnPaint editContext) { BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f); Rect rect = brushXform.GetBrushXYBounds(); PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, rect, 1); Material mat = GetPaintMaterial(); PaintContext maskContext = null; if (m_TextureMask || m_TextureStencil) { TerrainLayer maskTerrainLayer = terrain.terrainData.terrainLayers[m_TextureMask ? m_maskIndex : m_stencilIndex]; maskContext = TerrainPaintUtility.BeginPaintTexture(terrain, rect, maskTerrainLayer); if (maskContext == null) { return(false); } mat.SetTexture("_MaskTex", maskContext.sourceRenderTexture); } mat.SetInt("_MaskStencil", m_TextureMask ? 1 : (m_TextureStencil ? 2 : 0)); // apply brush Vector4 brushParams = new Vector4( editContext.brushStrength, m_ErosionStrength, m_MixStrength, 0.0f); mat.SetTexture("_BrushTex", editContext.brushTexture); mat.SetVector("_BrushParams", brushParams); TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Masked Ridge Erode"); if (maskContext != null) { TerrainPaintUtility.ReleaseContextResources(maskContext); } return(false); }
public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext) { Event evt = Event.current; if (evt.control && (evt.type == EventType.ScrollWheel)) { const float k_mouseWheelToHeightRatio = -0.0004f; // we use distance to modify the scroll speed, so that when a user is up close to the brush, they get fine adjustment, and when the user is far from the brush, it adjusts quickly m_StampHeightTerrainSpace += Event.current.delta.y * k_mouseWheelToHeightRatio * editContext.raycastHit.distance; evt.Use(); editContext.Repaint(); } // We're only doing painting operations, early out if it's not a repaint if (evt.type != EventType.Repaint) { return; } if (editContext.hitValidTerrain) { BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, editContext.brushSize, 0.0f); PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1); Material material = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(); TerrainPaintUtilityEditor.DrawBrushPreview( paintContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, material, 0); // draw result preview { ApplyBrushInternal(paintContext, editContext.brushStrength, editContext.brushTexture, brushXform, terrain, evt.shift); // restore old render target RenderTexture.active = paintContext.oldRenderTexture; material.SetTexture("_HeightmapOrig", paintContext.sourceRenderTexture); TerrainPaintUtilityEditor.DrawBrushPreview( paintContext, TerrainPaintUtilityEditor.BrushPreview.DestinationRenderTexture, editContext.brushTexture, brushXform, material, 1); } TerrainPaintUtility.ReleaseContextResources(paintContext); } }
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(); Vector4 brushParams = new Vector4(editContext.brushStrength, 0.0f, m_FeatureSize, 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 - Flatten Slope Height"); return(false); }
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 0); mat.SetInt("_KernelSize", (int)Mathf.Max(1, kernelSize)); // kernel size mat.SetVector("_SmoothWeights", smoothWeights); var texelCtx = Utility.CollectTexelValidity(paintContext.originTerrain, brushXform.GetBrushXYBounds(), 1); Utility.SetupMaterialForPaintingWithTexelValidityContext(paintContext, texelCtx, brushXform, mat); paintContext.sourceRenderTexture.wrapMode = TextureWrapMode.Clamp; var temp = RTUtils.GetTempHandle(paintContext.destinationRenderTexture.descriptor); temp.RT.wrapMode = TextureWrapMode.Clamp; mat.SetVector("_BlurDirection", Vector2.right); Graphics.Blit(paintContext.sourceRenderTexture, temp, mat); mat.SetVector("_BlurDirection", Vector2.up); Graphics.Blit(temp, paintContext.destinationRenderTexture, mat); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Smooth Height"); texelCtx.Cleanup(); RTUtils.Release(temp); return(true); } return(false); }
public override bool OnPaint(Terrain terrain, IOnPaint editContext) { float brushStrength = Event.current.shift ? -editContext.brushStrength : editContext.brushStrength; Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); Rect brushRect = TerrainPaintUtility.CalculateBrushRectInTerrainUnits(terrain, editContext.uv, editContext.brushSize); TerrainPaintUtility.PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushRect); // apply brush Vector4 brushParams = new Vector4(brushStrength * 0.01f, 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.RaiseLowerHeight); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Raise or Lower Height"); return(true); }
public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext) { Event evt = Event.current; if (evt.shift && (evt.type == EventType.ScrollWheel)) { m_StampHeight += Event.current.delta.y * -0.0000007f * editContext.raycastHit.distance; evt.Use(); } // We're only doing painting operations, early out if it's not a repaint if (evt.type != EventType.Repaint) { return; } if (editContext.hitValidTerrain) { BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, editContext.brushSize, 0.0f); PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1); Material material = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(); TerrainPaintUtilityEditor.DrawBrushPreview( paintContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, material, 0); // draw result preview { ApplyBrushInternal(paintContext, editContext.brushStrength, editContext.brushTexture, brushXform); // 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); } }
private PaintContext ApplyBrushInternal(Terrain terrain, Vector2 brushCenterTerrainSpaceXZ, int extraBorderPixels) { float maxScale = Mathf.Max(m_StampScale.x, m_StampScale.z); Vector2 brushSizeScaled = new Vector2(maxScale * 2.0f, maxScale * 2.0f); Rect brushRect = new Rect(brushCenterTerrainSpaceXZ - brushSizeScaled * 0.5f, brushSizeScaled); PaintContext context = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushRect, extraBorderPixels); 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); RenderTexture.ReleaseTemporary(m_MeshRenderTexture); m_MeshRenderTexture = null; return(context); }
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 (bridgeToolProperties != null && bridgeToolProperties.widthProfile != null) { float endWidth = Mathf.Abs(bridgeToolProperties.widthProfile.Evaluate(1.0f)); BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, commonUI.raycastHitUnderCursor.textureCoord, commonUI.brushSize * endWidth, commonUI.brushRotation); PaintContext ctx = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1); TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0); TerrainPaintUtility.ReleaseContextResources(ctx); } } if (Event.current.type != EventType.Repaint) { return; } //display a brush preview at the bridge starting location, using starting size from width profile if (m_StartTerrain != null) { float startWidth = Mathf.Abs(bridgeToolProperties.widthProfile.Evaluate(0.0f)); BrushTransform brushTransform = TerrainPaintUtility.CalculateBrushTransform(m_StartTerrain, m_StartPoint, commonUI.brushSize * startWidth, commonUI.brushRotation); PaintContext sampleContext = TerrainPaintUtility.BeginPaintHeightmap(m_StartTerrain, brushTransform.GetBrushXYBounds()); TerrainPaintUtilityEditor.DrawBrushPreview(sampleContext, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushTransform, TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(), 0); TerrainPaintUtility.ReleaseContextResources(sampleContext); } }
private void PaintHeightmap(Rect writeBrushRect, Terrain terrain, Material mat) { TerrainPaintUtility.PaintContext write = TerrainPaintUtility.BeginPaintHeightmap(terrain, writeBrushRect); // read created manually to use write's rect with offset TerrainPaintUtility.PaintContext read = new TerrainPaintUtility.PaintContext(); read.brushRect = new RectInt(write.brushRect.position + m_CloneRectPixelOffsetFromStampRect, write.brushRect.size); // TODO(wyatt): Uncomment this once Andrew's API changes make it to trunk read.CreateTerrainTiles(m_Sample.m_Terrain, m_Sample.m_Terrain.terrainData.heightmapWidth, m_Sample.m_Terrain.terrainData.heightmapHeight); read.CreateRenderTargets(m_Sample.m_Terrain.terrainData.heightmapTexture.format); read.GatherHeightmap(m_Sample.m_Terrain); // render mix of clone and stamp areas StampRender(read, write, mat, (int)ShaderPasses.CloneHeightmap); TerrainPaintUtility.ReleaseContextResources(read); TerrainPaintUtility.EndPaintHeightmap(write, "Terrain Paint - Clone Stamp Tool (Heightmap)"); }
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); Material mat = GetPaintMaterial(); // apply brush Vector4 brushParams = new Vector4( editContext.brushStrength, m_ErosionStrength, m_MixStrength, 0.0f); mat.SetTexture("_BrushTex", editContext.brushTexture); mat.SetVector("_BrushParams", brushParams); TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Ridge Erode"); return(false); }
public override bool OnPaint(Terrain terrain, IOnPaint editContext) { Rect brushRect = TerrainPaintUtility.CalculateBrushRectInTerrainUnits(terrain, editContext.uv, editContext.brushSize); TerrainPaintUtility.PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushRect); 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; } Material mat = GetPaintMaterial(); Vector4 brushParams = new Vector4(editContext.brushStrength, 0.0f, finalTwistAmount, 0.0f); mat.SetTexture("_BrushTex", editContext.brushTexture); mat.SetVector("_BrushParams", brushParams); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Twist Height"); return(false); }
public override bool OnPaint(Terrain terrain, IOnPaint editContext) { if (Event.current.shift) { m_Height = terrain.terrainData.GetInterpolatedHeight(editContext.uv.x, editContext.uv.y) / terrain.terrainData.size.y; editContext.RepaintAllInspectors(); 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.5f * m_Height, 0.0f, 0.0f); mat.SetTexture("_BrushTex", editContext.brushTexture); mat.SetVector("_BrushParams", brushParams); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.SetHeights); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Set Height"); return(true); }
public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext) { // there's probably more logic we need to handle here if (paintInProgress && (Event.current.type == EventType.MouseUp)) { MaterialManager mgr = terrain.GetComponent <MaterialManager>(); if (mgr != null) { mgr.GetIndexMap().SetIndexMapDirty(); } paintInProgress = false; } // We're only doing painting operations, early out if it's not a repaint if (Event.current.type != EventType.Repaint) { return; } if (editContext.hitValidTerrain) { if (lastUV != editContext.raycastHit.textureCoord) { editContext.Repaint(RepaintFlags.UI); } lastUV = editContext.raycastHit.textureCoord; lastBrushSize = editContext.brushSize; BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, editContext.brushSize, 0.0f); PaintContext ctx = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1); Material brushPreviewMaterial = GetBrushPreviewMaterial(); Vector4 brushParams = new Vector4(editContext.brushStrength, 0.0f, 0.0f, 0.0f); brushPreviewMaterial.SetVector("_BrushParams", brushParams); TerrainPaintUtilityEditor.DrawBrushPreview(ctx, TerrainPaintUtilityEditor.BrushPreview.SourceRenderTexture, editContext.brushTexture, brushXform, brushPreviewMaterial, 0); TerrainPaintUtility.ReleaseContextResources(ctx); } }
public override bool OnPaint(Terrain terrain, IOnPaint editContext) { commonUI.OnPaint(terrain, editContext); if (!commonUI.allowPaint) { return(true); } if (Event.current.type == EventType.MouseDown) { m_PrevBrushPos = editContext.uv; return(false); } if (Event.current.type == EventType.MouseDrag && m_PreviousEvent == EventType.MouseDrag) { Vector2 uv = editContext.uv; if (commonUI.ScatterBrushStamp(ref terrain, ref uv)) { Material mat = GetPaintMaterial(); Vector2 smudgeDir = uv - m_PrevBrushPos; BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, uv, commonUI.brushSize, commonUI.brushRotation); Vector4 brushParams = new Vector4(commonUI.brushStrength, smudgeDir.x, smudgeDir.y, 0); mat.SetTexture("_BrushTex", editContext.brushTexture); mat.SetVector("_BrushParams", brushParams); //smudge splat map if (m_AffectMaterials) { for (int i = 0; i < terrain.terrainData.terrainLayers.Length; i++) { TerrainLayer layer = terrain.terrainData.terrainLayers[i]; if (layer == null) { continue; // nothing to paint if the layer is NULL } PaintContext sampleContext = TerrainPaintUtility.BeginPaintTexture(terrain, brushXform.GetBrushXYBounds(), layer); Graphics.Blit(sampleContext.sourceRenderTexture, sampleContext.destinationRenderTexture, mat, 0); TerrainPaintUtility.EndPaintTexture(sampleContext, "Terrain Paint - Twist Brush (Texture)"); } } //smudge the height map if (m_AffectHeight) { PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1); TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Twist Brush (Height)"); } m_PrevBrushPos = uv; } } m_PreviousEvent = Event.current.type; return(false); }
public override bool OnPaint(Terrain terrain, IOnPaint editContext) { Vector2 uv = editContext.uv; //grab the starting position & height if (Event.current.shift) { float height = terrain.terrainData.GetInterpolatedHeight(uv.x, uv.y) / terrain.terrainData.size.y; m_StartPoint = new Vector3(uv.x, uv.y, height); m_StartTerrain = terrain; return(true); } if (!m_StartTerrain || (Event.current.type == EventType.MouseDrag)) { return(true); } //get the target position & height float targetHeight = terrain.terrainData.GetInterpolatedHeight(uv.x, uv.y) / terrain.terrainData.size.y; Vector3 targetPos = new Vector3(uv.x, uv.y, targetHeight); if (terrain != m_StartTerrain) { //figure out the stroke vector in uv,height space Vector2 targetWorld = transformToWorld(terrain, uv); Vector2 targetUVs = transformToUVSpace(m_StartTerrain, targetWorld); targetPos.x = targetUVs.x; targetPos.y = targetUVs.y; } Vector3 stroke = targetPos - m_StartPoint; float strokeLength = stroke.magnitude; int numSplats = (int)(strokeLength / (0.001f * m_Spacing)); Terrain currTerrain = m_StartTerrain; Material mat = GetPaintMaterial(); Vector2 posOffset = new Vector2(0.0f, 0.0f); Vector2 currUV = new Vector2(); Vector4 brushParams = new Vector4(); Vector2 jitterVec = new Vector2(-stroke.z, stroke.x); //perpendicular to stroke direction jitterVec.Normalize(); for (int i = 0; i < numSplats; i++) { float pct = (float)i / (float)numSplats; float widthScale = widthProfile.Evaluate(pct); float heightOffset = heightProfile.Evaluate(pct) / currTerrain.terrainData.size.y; float strengthScale = strengthProfile.Evaluate(pct); float jitterOffset = jitterProfile.Evaluate(pct) / Mathf.Max(currTerrain.terrainData.size.x, currTerrain.terrainData.size.z); Vector3 currPos = m_StartPoint + pct * stroke; //add in jitter offset (needs to happen before tile correction) currPos.x += posOffset.x + jitterOffset * jitterVec.x; currPos.y += posOffset.y + jitterOffset * jitterVec.y; if (currPos.x >= 1.0f && (currTerrain.rightNeighbor != null)) { currTerrain = currTerrain.rightNeighbor; currPos.x -= 1.0f; posOffset.x -= 1.0f; } if (currPos.x <= 0.0f && (currTerrain.leftNeighbor != null)) { currTerrain = currTerrain.leftNeighbor; currPos.x += 1.0f; posOffset.x += 1.0f; } if (currPos.y >= 1.0f && (currTerrain.topNeighbor != null)) { currTerrain = currTerrain.topNeighbor; currPos.y -= 1.0f; posOffset.y -= 1.0f; } if (currPos.y <= 0.0f && (currTerrain.bottomNeighbor != null)) { currTerrain = currTerrain.bottomNeighbor; currPos.y += 1.0f; posOffset.y += 1.0f; } currUV.x = currPos.x; currUV.y = currPos.y; int finalBrushSize = (int)(widthScale * (float)editContext.brushSize); float finalHeight = (m_StartPoint + pct * stroke).z + heightOffset; BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(currTerrain, currUV, finalBrushSize, 0.0f); PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(currTerrain, brushXform.GetBrushXYBounds()); mat.SetTexture("_BrushTex", editContext.brushTexture); brushParams.x = editContext.brushStrength * strengthScale; brushParams.y = 0.5f * finalHeight; mat.SetVector("_BrushParams", brushParams); TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Bridge"); } return(false); }
public override bool OnPaint(Terrain terrain, IOnPaint editContext) { Vector2 uv = editContext.uv; //grab the starting position & height if (Event.current.shift) { //m_Height = terrain.terrainData.GetInterpolatedHeight(uv.x, uv.y) / terrain.terrainData.size.y; float height = terrain.terrainData.GetInterpolatedHeight(uv.x, uv.y) / terrain.terrainData.size.y; m_StartPoint = new Vector3(uv.x, uv.y, height); m_StartTerrain = terrain; return(true); } if (!m_StartTerrain || (Event.current.type == EventType.MouseDrag)) { return(true); } //get the target position & height float targetHeight = terrain.terrainData.GetInterpolatedHeight(uv.x, uv.y) / terrain.terrainData.size.y; Vector3 targetPos = new Vector3(uv.x, uv.y, targetHeight); if (terrain != m_StartTerrain) { //figure out the stroke vector in uv,height space Vector2 targetWorld = transformToWorld(terrain, uv); Vector2 targetUVs = transformToUVSpace(m_StartTerrain, targetWorld); targetPos.x = targetUVs.x; targetPos.y = targetUVs.y; } Vector3 stroke = targetPos - m_StartPoint; float strokeLength = stroke.magnitude; int numSplats = (int)(strokeLength / (0.001f * m_Spacing)); Terrain currTerrain = m_StartTerrain; Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); Vector2 posOffset = new Vector2(0.0f, 0.0f); for (int i = 0; i < numSplats; i++) { float pct = (float)i / (float)numSplats; float widthScale = widthProfile.Evaluate(pct); float heightScale = heightProfile.Evaluate(pct); float strengthScale = strengthProfile.Evaluate(pct); Vector3 currPos = m_StartPoint + pct * stroke; currPos.x += posOffset.x; currPos.y += posOffset.y; if (currPos.x >= 1.0f && (currTerrain.rightNeighbor != null)) { currTerrain = currTerrain.rightNeighbor; currPos.x -= 1.0f; posOffset.x -= 1.0f; } if (currPos.x <= 0.0f && (currTerrain.leftNeighbor != null)) { currTerrain = currTerrain.leftNeighbor; currPos.x += 1.0f; posOffset.x += 1.0f; } if (currPos.y >= 1.0f && (currTerrain.topNeighbor != null)) { currTerrain = currTerrain.topNeighbor; currPos.y -= 1.0f; posOffset.y -= 1.0f; } if (currPos.y <= 0.0f && (currTerrain.bottomNeighbor != null)) { currTerrain = currTerrain.bottomNeighbor; currPos.y += 1.0f; posOffset.y += 1.0f; } Vector2 currUV = new Vector2(currPos.x, currPos.y); int finalBrushSize = (int)(widthScale * (float)editContext.brushSize); float finalHeight = (m_StartPoint + heightScale * stroke).z; Rect brushRect = TerrainPaintUtility.CalculateBrushRectInTerrainUnits(currTerrain, currUV, finalBrushSize); TerrainPaintUtility.PaintContext paintContext = TerrainPaintUtility.BeginPaintHeightmap(currTerrain, brushRect); Vector4 brushParams = new Vector4(strengthScale * editContext.brushStrength * 0.01f, 0.5f * finalHeight, 0.0f, editContext.brushRotation); mat.SetTexture("_BrushTex", editContext.brushTexture); mat.SetVector("_BrushParams", brushParams); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 2); TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Bridge"); } return(false); }