Esempio n. 1
0
        public PaintContext AcquireNormalmap(bool writable, Terrain terrain, Rect boundsInTerrainSpace, int extraBorderPixels = 0)
        {
            m_NormalmapContext = TerrainPaintUtility.CollectNormals(terrain, boundsInTerrainSpace, extraBorderPixels);

            if (m_NormalmapContext == null)
            {
                return(null);
            }

            m_NormalmapContext.sourceRenderTexture.name      = $"{m_Name}::NormalContext::SourceRT";
            m_NormalmapContext.destinationRenderTexture.name = $"{m_Name}::NormalContext::DestRT";
            return(m_NormalmapContext);
        }
        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);
        }
Esempio n. 3
0
 public PaintContext AcquireNormalmap(bool writable, Terrain terrain, Rect boundsInTerrainSpace, int extraBorderPixels = 0)
 {
     m_NormalmapContext = TerrainPaintUtility.CollectNormals(terrain, boundsInTerrainSpace, extraBorderPixels);
     return(m_NormalmapContext);
 }
 public static PaintContext CollectNormals(Terrain terrain)
 {
     return(TerrainPaintUtility.CollectNormals(terrain, GetTerrainBounds(terrain)));
 }
Esempio n. 5
0
        private bool DoPaint(Terrain terrain, float brushStrength, Texture brushTexture, Rect rect, BrushTransform brushXform)
        {
            if (disablePaint)
            {
                return(false);
            }

            m_SelectedTerrainLayer = terrain.terrainData.terrainLayers[m_textureIndex];
            PaintContext paintContext = TerrainPaintUtility.BeginPaintTexture(terrain, rect, m_SelectedTerrainLayer);

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

            PaintContext heightContext = TerrainPaintUtility.BeginPaintHeightmap(terrain, rect, 1);

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

            if (terrain.normalmapTexture == null)
            {
                Debug.Log("Please check the Draw Instanced option under terrain settings to use this tool");
                return(false);
            }
            PaintContext normalContext = TerrainPaintUtility.CollectNormals(terrain, rect, 0);

            Material mat = GetPaintMaterial();

            float extent = textureParams[m_textureIndex].heightExtent;
            float blend  = textureParams[m_textureIndex].heightBlend;
            float height = textureParams[m_textureIndex].heightMaxPoint;

            if (textureParams[m_textureIndex].heightRelative)
            {
                extent /= relativeHeightMult;
                height /= relativeHeightMult;
            }
            else
            {
                extent /= terrain.terrainData.size.y;
                height /= terrain.terrainData.size.y;
            }
            Vector4 brushParams = new Vector4(brushStrength, height,
                                              textureParams[m_textureIndex].heightMask ? extent * (1.0f - blend) : 10000.0f, extent * blend);

            extent = textureParams[m_textureIndex].slopeExtent;
            blend  = textureParams[m_textureIndex].slopeBlend;
            Vector4 slopeParams = new Vector4(textureParams[m_textureIndex].slopeMaxPoint,
                                              textureParams[m_textureIndex].slopeMask ? extent * (1.0f - blend) : 1.0f, extent * blend, textureParams[m_textureIndex].maxValue);

            PaintContext maskContext = null;

            if (textureParams[m_textureIndex].textureMask || textureParams[m_textureIndex].textureStencil)
            {
                TerrainLayer maskTerrainLayer = terrain.terrainData.terrainLayers[textureParams[m_textureIndex].textureMask ? textureParams[m_textureIndex].maskIndex : textureParams[m_textureIndex].stencilIndex];
                maskContext = TerrainPaintUtility.BeginPaintTexture(terrain, rect, maskTerrainLayer);
                if (maskContext == null)
                {
                    return(false);
                }
                mat.SetTexture("_MaskTex", maskContext.sourceRenderTexture);
            }
            mat.SetInt("_MaskStencil", textureParams[m_textureIndex].textureMask ? 1 : (textureParams[m_textureIndex].textureStencil ? 2 : 0));

            mat.SetTexture("_BrushTex", brushTexture);

            mat.SetTexture("_HeightTex", heightContext.sourceRenderTexture);
            mat.SetTexture("_NormalTex", normalContext.sourceRenderTexture);

            mat.SetVector("_BrushParams", brushParams);
            mat.SetVector("_SlopeParams", slopeParams);
            mat.SetFloat("_FeatureSize", textureParams[m_textureIndex].heightRelative?textureParams[m_textureIndex].heightFeatureSize:0.0f);

            if (brushTexture == null)
            {
                mat.SetFloat("_AspectRatio", terrain.terrainData.size.x / terrain.terrainData.size.z);
            }
            else
            {
                mat.SetFloat("_AspectRatio", (float)paintContext.sourceRenderTexture.height / (float)paintContext.sourceRenderTexture.width);
            }
            TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
            Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0);
            TerrainPaintUtility.EndPaintTexture(paintContext, "Terrain Paint - Masked Texture");
            TerrainPaintUtility.ReleaseContextResources(heightContext);
            TerrainPaintUtility.ReleaseContextResources(normalContext);
            if (maskContext != null)
            {
                TerrainPaintUtility.ReleaseContextResources(maskContext);
            }

            return(true);
        }