private void PaintAlphamap(Terrain sampleTerrain, Terrain targetTerrain, BrushTransform sampleXform, BrushTransform targetXform, Material mat) { Rect sampleRect = sampleXform.GetBrushXYBounds(); Rect targetRect = targetXform.GetBrushXYBounds(); int numSampleTerrainLayers = sampleTerrain.terrainData.terrainLayers.Length; for (int i = 0; i < numSampleTerrainLayers; ++i) { TerrainLayer layer = sampleTerrain.terrainData.terrainLayers[i]; if (layer == null) { continue; // nothing to paint if the layer is NULL } PaintContext sampleContext = TerrainPaintUtility.BeginPaintTexture(sampleTerrain, sampleRect, layer); // manually create target context since we are possibly applying another terrain's layers and not its own int layerIndex = TerrainPaintUtility.FindTerrainLayerIndex(sampleTerrain, layer); Texture2D layerTexture = TerrainPaintUtility.GetTerrainAlphaMapChecked(sampleTerrain, layerIndex >> 2); PaintContext targetContext = PaintContext.CreateFromBounds(targetTerrain, targetRect, layerTexture.width, layerTexture.height); targetContext.CreateRenderTargets(RenderTextureFormat.R8); targetContext.GatherAlphamap(layer, true); sampleContext.sourceRenderTexture.filterMode = FilterMode.Point; mat.SetTexture("_CloneTex", sampleContext.sourceRenderTexture); Graphics.Blit(targetContext.sourceRenderTexture, targetContext.destinationRenderTexture, mat, (int)ShaderPasses.CloneAlphamap); // apply texture modifications and perform cleanup. same thing as calling TerrainPaintUtility.EndPaintTexture targetContext.ScatterAlphamap("Terrain Paint - Clone Brush (Texture)"); targetContext.Cleanup(); } }
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 static PaintContext BeginFillHeightmap(Terrain terrain) { RenderTexture rt = terrain.terrainData.heightmapTexture; PaintContext ctx = new PaintContext(terrain, new RectInt(0, 0, rt.width, rt.height), rt.width, rt.height); ctx.CreateRenderTargets(rt.format); ctx.GatherHeightmap(); return(ctx); }
private void PaintAlphamap(Terrain sampleTerrain, Terrain targetTerrain, BrushTransform sampleXform, BrushTransform targetXform, Material mat) { Rect sampleRect = sampleXform.GetBrushXYBounds(); Rect targetRect = targetXform.GetBrushXYBounds(); int numSampleTerrainLayers = sampleTerrain.terrainData.terrainLayers.Length; for (int i = 0; i < numSampleTerrainLayers; ++i) { TerrainLayer layer = sampleTerrain.terrainData.terrainLayers[i]; if (layer == null) { continue; // nothing to paint if the layer is NULL } PaintContext sampleContext = TerrainPaintUtility.BeginPaintTexture(sampleTerrain, sampleRect, layer); int layerIndex = TerrainPaintUtility.FindTerrainLayerIndex(sampleTerrain, layer); Texture2D layerTexture = TerrainPaintUtility.GetTerrainAlphaMapChecked(sampleTerrain, layerIndex >> 2); PaintContext targetContext = PaintContext.CreateFromBounds(targetTerrain, targetRect, layerTexture.width, layerTexture.height); targetContext.CreateRenderTargets(RenderTextureFormat.R8); targetContext.GatherAlphamap(layer, true); sampleContext.sourceRenderTexture.filterMode = FilterMode.Point; mat.SetTexture("_CloneTex", sampleContext.sourceRenderTexture); mat.SetVector("_SampleUVScaleOffset", ComputeSampleUVScaleOffset(sampleContext, targetContext)); var brushMask = RTUtils.GetTempHandle(targetContext.sourceRenderTexture.width, targetContext.sourceRenderTexture.height, 0, FilterUtility.defaultFormat); Utility.SetFilterRT(commonUI, targetContext.sourceRenderTexture, brushMask, mat); TerrainPaintUtility.SetupTerrainToolMaterialProperties(targetContext, targetXform, mat); Graphics.Blit(targetContext.sourceRenderTexture, targetContext.destinationRenderTexture, mat, (int)ShaderPasses.CloneAlphamap); // apply texture modifications and perform cleanup. same thing as calling TerrainPaintUtility.EndPaintTexture targetContext.ScatterAlphamap("Terrain Paint - Clone Brush (Texture)"); // Restores RenderTexture.active targetContext.Cleanup(); RTUtils.Release(brushMask); } }
public override bool OnPaint(Terrain terrain, IOnPaint editContext) { MaterialManager mgr = terrain.GetComponent <MaterialManager>(); if (mgr != null) { // IndexMap indexMap = mgr.GetIndexMap(); // RenderTexture rt= indexMap.GetTempRenderTexture(true); paintInProgress = true; BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f); PaintContext indexCtx = PaintContext.CreateFromBounds(terrain, brushXform.GetBrushXYBounds(), 64, 64, 1); indexCtx.CreateRenderTargets(RenderTextureFormat.ARGB32); PaintContext normalCtx = TerrainPaintUtility.CollectNormals(terrain, brushXform.GetBrushXYBounds(), 2); Material blitMaterial = TerrainPaintUtility.GetBlitMaterial(); indexCtx.Gather( TerrainToIndexMapTexture, new Color(0.0f, 0.0f, 0.0f, 0.0f), blitMaterial, 0, null, // before null); // after // render source -> dest here // RenderTexture.active = ctx.oldRenderTexture; Material paintMaterial = GetPaintMaterial(); float brushStrength = Event.current.shift ? -editContext.brushStrength : editContext.brushStrength; Vector4 brushParams = new Vector4( brushStrength, 0.0f, 0.0f, 0.0f); paintMaterial.SetTexture("_BrushTex", editContext.brushTexture); paintMaterial.SetVector("_BrushParams", brushParams); paintMaterial.SetFloat("materialIndex", materialIndex); paintMaterial.SetTexture("_NormalMap", normalCtx.sourceRenderTexture); Vector4 indexToNormalXform; TerrainPaintUtility.BuildTransformPaintContextUVToPaintContextUV( indexCtx, normalCtx, out indexToNormalXform); paintMaterial.SetVector("_indexToNormalXform", indexToNormalXform); Vector4 xformParams = new Vector4(); xformParams.x = (randomRotation ? minRotation : fixedRotation) / 360.0f; xformParams.y = (randomRotation ? maxRotation : fixedRotation) / 360.0f; paintMaterial.SetVector("_xformParams", xformParams); Vector4 randoms = new Vector4( UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f)); paintMaterial.SetVector("_randoms", randoms); TerrainPaintUtility.SetupTerrainToolMaterialProperties(indexCtx, brushXform, paintMaterial); Graphics.Blit(indexCtx.sourceRenderTexture, indexCtx.destinationRenderTexture, paintMaterial, 0); // ctx.ScatterToTexture() // we should do this ... less temp render textures // and users don't have to store render textures at all... indexCtx.Scatter( TerrainToIndexMapRenderTexture, blitMaterial, 0, null, null); normalCtx.Cleanup(); indexCtx.Cleanup(); } /* * 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); }