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); }
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 void AddHeight(Terrain terrain, IOnPaint editContext) { using (IBrushRenderUnderCursor brushRender = new BrushRenderUIGroupUnderCursor(commonUI, "ThermalErosion", editContext.brushTexture)) { if (brushRender.CalculateBrushTransform(out BrushTransform brushXform)) { PaintContext paintContext = brushRender.AcquireHeightmap(true, brushXform.GetBrushXYBounds(), 1); paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear; Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); float brushStrength = Event.current.shift ? -m_Eroder.m_AddHeightAmt : m_Eroder.m_AddHeightAmt; brushStrength *= (commonUI.brushStrength); if (Event.current.control) { brushStrength = 0.0f; } Vector4 brushParams = new Vector4(0.0001f * 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)TerrainPaintUtility.BuiltinPaintMaterialPasses.RaiseLowerHeight); } } }
private void ApplyBrushInternal(PaintContext paintContext, IBrushRenderUnderCursor brushRender, float brushStrength, Texture brushTexture, BrushTransform brushTransform, Terrain terrain) { Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); float terrainHeight = Mathf.Clamp01((m_HeightWorldSpace - terrain.transform.position.y) / terrain.terrainData.size.y); Vector4 brushParams = new Vector4(brushStrength * 0.01f, 0.5f * terrainHeight, 0.0f, 0.0f); mat.SetTexture("_BrushTex", brushTexture); mat.SetVector("_BrushParams", brushParams); brushRender.SetupTerrainToolMaterialProperties(paintContext, brushTransform, mat); brushRender.RenderBrush(paintContext, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.SetHeights); }
private void ApplyBrushInternal(IPaintContextRender renderer, PaintContext paintContext, float brushStrength, Texture brushTexture, BrushTransform brushTransform) { Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); Vector4 brushParams = new Vector4(0.05f * brushStrength, 0.0f, 0.0f, 0.0f); mat.SetTexture("_BrushTex", brushTexture); mat.SetVector("_BrushParams", brushParams); renderer.SetupTerrainToolMaterialProperties(paintContext, brushTransform, mat); renderer.RenderBrush(paintContext, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.RaiseLowerHeight); }
private void ApplyBrushInternal(PaintContext paintContext, float brushStrength, Texture brushTexture, BrushTransform brushXform) { Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); Vector4 brushParams = new Vector4(0.01f * brushStrength, 0.0f, m_StampHeight, 0.0f); mat.SetTexture("_BrushTex", brushTexture); mat.SetVector("_BrushParams", brushParams); TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.StampHeight); }
private void ApplyBrushInternal(PaintContext paintContext, float brushStrength, Texture brushTexture, BrushTransform brushXform, Terrain terrain) { Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); float brushTargetHeight = Mathf.Clamp01((m_TargetHeight - paintContext.heightWorldSpaceMin) / paintContext.heightWorldSpaceSize); Vector4 brushParams = new Vector4(brushStrength * 0.01f, PaintContext.kNormalizedHeightScale * brushTargetHeight, 0.0f, 0.0f); mat.SetTexture("_BrushTex", brushTexture); mat.SetVector("_BrushParams", brushParams); TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.SetHeights); }
/// <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) { 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); }
private void ApplyBrushInternal(PaintContext paintContext, float brushStrength, Texture brushTexture, BrushTransform brushXform, Terrain terrain, bool negate) { Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); float height = m_StampHeightTerrainSpace / terrain.terrainData.size.y; if (negate) { height = -height; } Vector4 brushParams = new Vector4(brushStrength, 0.0f, height, m_MaxBlendAdd); mat.SetTexture("_BrushTex", brushTexture); mat.SetVector("_BrushParams", brushParams); TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.StampHeight); }
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); }
private void ApplyBrushInternal(PaintContext paintContext, float brushStrength, Texture brushTexture, BrushTransform brushXform) { Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); Vector4 brushParams = new Vector4(brushStrength, 0.0f, 0.0f, 0.0f); mat.SetTexture("_BrushTex", brushTexture); mat.SetVector("_BrushParams", brushParams); Vector4 smoothWeights = new Vector4( Mathf.Clamp01(1.0f - Mathf.Abs(m_direction)), // centered Mathf.Clamp01(-m_direction), // min Mathf.Clamp01(m_direction), // max 0.0f); // unused mat.SetVector("_SmoothWeights", smoothWeights); TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.SmoothHeights); }
private void ApplyBrushInternal(IPaintContextRender renderer, PaintContext paintContext, float brushStrength, Texture brushTexture, BrushTransform brushXform, Terrain terrain) { Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); float height = stampToolProperties.m_StampHeight / (terrain.terrainData.size.y * 2.0f); if (stampToolProperties.stampDown) { height = -height; } Vector4 brushParams = new Vector4(brushStrength, 0.0f, height, stampToolProperties.m_MaxBlendAdd); mat.SetTexture("_BrushTex", brushTexture); mat.SetVector("_BrushParams", brushParams); renderer.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat); renderer.RenderBrush(paintContext, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.StampHeight); }
public override bool OnPaint(Terrain terrain, IOnPaint editContext) { BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f); PaintContext paintContext = TerrainPaintUtility.BeginPaintSurfaceMask(terrain, brushXform.GetBrushXYBounds()); Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); float brushStrength = Event.current.shift ? editContext.brushStrength : -editContext.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.PaintSurfaceMask); TerrainPaintUtility.EndPaintSurfaceMask(paintContext, "Terrain Paint - Paint Surface Mask"); return(true); }
public override bool OnPaint(Terrain terrain, IOnPaint editContext) { Rect brushRect = TerrainPaintUtility.CalculateBrushRectInTerrainUnits(terrain, editContext.uv, editContext.brushSize); TerrainPaintUtility.PaintContext paintContext = TerrainPaintUtility.BeginPaintTexture(terrain, brushRect, m_SelectedTerrainLayer); if (paintContext == null) { return(false); } Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); // apply brush Vector4 brushParams = new Vector4(editContext.brushStrength, m_SplatAlpha, 0.0f, 0.0f); mat.SetTexture("_BrushTex", editContext.brushTexture); mat.SetVector("_BrushParams", brushParams); Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.PaintTexture); TerrainPaintUtility.EndPaintTexture(paintContext, "Terrain Paint - Texture"); return(true); }
private void ApplyBrushInternal(PaintContext paintContext, IBrushRenderUnderCursor brushRender, float brushStrength, Texture brushTexture, BrushTransform brushTransform, Terrain terrain) { Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); float terrainHeight = Mathf.Clamp01((m_HeightWorldSpace - terrain.transform.position.y) / terrain.terrainData.size.y); Vector4 brushParams = new Vector4(brushStrength * 0.01f, 0.5f * terrainHeight, 0.0f, 0.0f); Vector3 brushPos = new Vector3(commonUI.raycastHitUnderCursor.point.x, 0, commonUI.raycastHitUnderCursor.point.z); FilterContext fc = new FilterContext(terrain, brushPos, commonUI.brushSize, commonUI.brushRotation); fc.renderTextureCollection.GatherRenderTextures(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height); RenderTexture filterMaskRT = commonUI.GetBrushMask(fc, paintContext.sourceRenderTexture); mat.SetTexture("_FilterTex", filterMaskRT); mat.SetTexture("_BrushTex", brushTexture); mat.SetVector("_BrushParams", brushParams); brushRender.SetupTerrainToolMaterialProperties(paintContext, brushTransform, mat); brushRender.RenderBrush(paintContext, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.SetHeights); }
private void ApplyBrushInternal(IPaintContextRender renderer, PaintContext paintContext, float brushStrength, Texture brushTexture, BrushTransform brushXform, Terrain terrain) { Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial(); float height = stampToolProperties.m_StampHeight / (terrain.terrainData.size.y * 2.0f); if (stampToolProperties.stampDown) { height = -height; } Vector4 brushParams = new Vector4(brushStrength, 0.0f, height, stampToolProperties.m_MaxBlendAdd); mat.SetTexture("_BrushTex", brushTexture); mat.SetVector("_BrushParams", brushParams); var brushMask = RTUtils.GetTempHandle(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0, FilterUtility.defaultFormat); Utility.SetFilterRT(commonUI, paintContext.sourceRenderTexture, brushMask, mat); renderer.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat); renderer.RenderBrush(paintContext, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.StampHeight); RTUtils.Release(brushMask); }
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 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); }