Esempio n. 1
0
        private void StampRender(TerrainPaintUtility.PaintContext read, TerrainPaintUtility.PaintContext write, Material mat, int materialPass)
        {
            // since we only blit the areas that a writeRect is located, we need to get the rest of the texture in destination too
            Graphics.Blit(write.sourceRenderTexture, write.destinationRenderTexture, TerrainPaintUtility.GetBlitMaterial(), 0);

            read.sourceRenderTexture.filterMode = FilterMode.Point;
            mat.SetTexture("_CloneLocation", read.sourceRenderTexture);

            // only blit areas that we can read from in the read context (protects against writing uninitialized memory in the texture when read rect is off of terrain)
            for (int i = 0; i < read.clippedTiles.Length; ++i)
            {
                RectInt writeRect = read.clippedTiles[i];
                if (writeRect.width == 0 || writeRect.height == 0)
                {
                    continue;
                }

                writeRect.x      /= read.brushRect.width;
                writeRect.y      /= read.brushRect.height;
                writeRect.width  /= read.brushRect.width;
                writeRect.height /= read.brushRect.height;
                mat.SetVector("_WriteRect", new Vector4(writeRect.x, writeRect.y, writeRect.xMax, writeRect.yMax));
                Graphics.Blit(write.sourceRenderTexture, write.destinationRenderTexture, mat, materialPass);
            }
        }
        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);
        }