Exemplo n.º 1
0
        public override void OnSceneGUI(Terrain terrain, IOnSceneGUI editContext)
        {
            TerrainPaintUtilityEditor.ShowDefaultPreviewBrush(terrain,
                                                              editContext.brushTexture,
                                                              editContext.brushStrength,
                                                              editContext.brushSize,
                                                              0.0f);

            bool drawCloneBrush = true;

            // on mouse up
            if (Event.current.type == EventType.MouseUp)
            {
                m_ActivePaint = false;
                if (!m_Aligned)
                {
                    m_Sample.m_UV       = m_SnapbackCache.m_UV;
                    m_Sample.m_Terrain  = m_SnapbackCache.m_Terrain;
                    m_Sample.m_Position = m_SnapbackCache.m_Position;
                }
            }

            // on mouse move
            if (Event.current.type == EventType.MouseMove || Event.current.type == EventType.MouseDrag)
            {
                if (m_Aligned && m_PaintedOnce)
                {
                    RaycastHit hit;
                    Ray        mouseRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                    terrain.GetComponent <Collider>().Raycast(mouseRay, out hit, Mathf.Infinity);

                    // check for moving across tiles
                    if (terrain != m_LastPaintLocation.m_Terrain && m_LastPaintLocation.m_Terrain != null)
                    {
                        UpdateLastPosition(hit, terrain);
                    }

                    if (m_Sample.m_Terrain != null)
                    {
                        PositionCloneBrush(hit);
                    }

                    // capture last (current) location for next frame
                    m_LastPaintLocation.m_UV       = hit.textureCoord;
                    m_LastPaintLocation.m_Position = hit.point;
                    m_LastPaintLocation.m_Terrain  = terrain;
                }

                if (m_Aligned && !m_PaintedOnce)
                {
                    drawCloneBrush = false; // dont draw if we havent selected where to paint yet when aligned
                }
            }

            // draw the clone brush preview
            if (m_Sample.m_Terrain != null && drawCloneBrush)
            {
                Rect sampleRect = TerrainPaintUtility.CalculateBrushRectInTerrainUnits(m_Sample.m_Terrain, m_Sample.m_UV, editContext.brushSize);
                TerrainPaintUtility.PaintContext ctx = TerrainPaintUtility.BeginPaintHeightmap(m_Sample.m_Terrain, sampleRect);

                FilterMode prevCtxFilterMode          = ctx.sourceRenderTexture.filterMode;
                FilterMode prevBrushTextureFilterMode = editContext.brushTexture.filterMode;

                ctx.sourceRenderTexture.filterMode  = FilterMode.Bilinear;
                editContext.brushTexture.filterMode = FilterMode.Bilinear;

                Vector2 topLeft = ctx.brushRect.min;
                float   xfrac   = ((topLeft.x - (int)topLeft.x) / (float)ctx.sourceRenderTexture.width);
                float   yfrac   = ((topLeft.y - (int)topLeft.y) / (float)ctx.sourceRenderTexture.height);

                Vector4 texScaleOffset = new Vector4(0.5f, 0.5f, 0.5f + xfrac + 0.5f / (float)ctx.sourceRenderTexture.width, 0.5f + yfrac + 0.5f / (float)ctx.sourceRenderTexture.height);

                RaycastHit hit = new RaycastHit();
                hit.point = m_Sample.m_Position;

                TerrainPaintUtilityEditor.DrawDefaultBrushPreviewMesh(m_Sample.m_Terrain, hit, ctx.sourceRenderTexture, editContext.brushTexture, 0.0001f, editContext.brushSize, TerrainPaintUtilityEditor.defaultPreviewPatchMesh, true, texScaleOffset);

                ctx.sourceRenderTexture.filterMode  = prevCtxFilterMode;
                editContext.brushTexture.filterMode = prevBrushTextureFilterMode;

                TerrainPaintUtility.ReleaseContextResources(ctx);
            }
        }