예제 #1
0
        private void CreateRTHandles(Vector2Int dim)
        {
            m_RTSize = dim;

            var r    = RTUtils.GetDescriptorRW(dim.x, dim.y, 0, RenderTextureFormat.RFloat);
            var rg   = RTUtils.GetDescriptorRW(dim.x, dim.y, 0, RenderTextureFormat.RGFloat);
            var argb = RTUtils.GetDescriptorRW(dim.x, dim.y, 0, RenderTextureFormat.ARGBFloat);

            for (int i = 0; i < 2; i++)
            {
                m_HeightmapRT[i] = RTUtils.GetNewHandle(r).WithName("HydroErosion_Height" + i);
                m_WaterRT[i]     = RTUtils.GetNewHandle(r).WithName("HydroErosion_Water" + i);
                m_WaterVelRT[i]  = RTUtils.GetNewHandle(rg).WithName("HydroErosion_WaterVel" + i);
                m_FluxRT[i]      = RTUtils.GetNewHandle(argb).WithName("HydroErosion_Flux" + i);
                m_SedimentRT[i]  = RTUtils.GetNewHandle(r).WithName("HydroErosion_Sediment" + i);

                m_HeightmapRT[i].RT.Create();
                m_WaterRT[i].RT.Create();
                m_WaterVelRT[i].RT.Create();
                m_FluxRT[i].RT.Create();
                m_SedimentRT[i].RT.Create();
            }

            m_ErodedRT = RTUtils.GetNewHandle(r).WithName("HydroErosion_Eroded");
            m_ErodedRT.RT.Create();

            m_HardnessRT = RTUtils.GetNewHandle(r).WithName("HydroErosion_Hardness");
            m_HardnessRT.RT.Create();
        }
예제 #2
0
        protected override void OnEval(FilterContext fc, RenderTexture source, RenderTexture dest)
        {
            var desc = dest.descriptor;

            desc.enableRandomWrite = true;
            var sourceHandle = RTUtils.GetTempHandle(desc);
            var destHandle   = RTUtils.GetTempHandle(desc);

            using (sourceHandle.Scoped())
                using (destHandle.Scoped())
                {
                    Graphics.Blit(source, sourceHandle);
                    Graphics.Blit(dest, destHandle);

                    ComputeShader cs   = GetComputeShader();
                    int           kidx = cs.FindKernel("GradientMultiply");

                    Texture2D remapTex = GetRemapTexture();

                    cs.SetTexture(kidx, "In_BaseMaskTex", sourceHandle);
                    cs.SetTexture(kidx, "In_HeightTex", fc.rtHandleCollection[FilterContext.Keywords.Heightmap]);
                    cs.SetTexture(kidx, "OutputTex", destHandle);
                    cs.SetTexture(kidx, "RemapTex", remapTex);
                    cs.SetInt("RemapTexRes", remapTex.width);
                    cs.SetFloat("EffectStrength", m_FilterStrength);
                    cs.SetVector("TerrainDimensions", fc.vectorProperties.ContainsKey("_TerrainSize") ? fc.vectorProperties["_TerrainSize"] : Vector4.one);
                    cs.SetVector("TextureResolution", new Vector4(sourceHandle.RT.width, sourceHandle.RT.height, m_Epsilon, fc.floatProperties[FilterContext.Keywords.TerrainScale]));
                    cs.Dispatch(kidx, sourceHandle.RT.width, sourceHandle.RT.height, 1);

                    Graphics.Blit(destHandle, dest);
                }
        }
예제 #3
0
        public void Add_Filter()
        {
            // setup
            float addValue  = 9000f;
            var   addFilter = FilterUtility.CreateInstance <AddFilter>();

            addFilter.value = addValue;

            var prevRT = RenderTexture.active;
            var src    = RTUtils.GetTempHandle(RTUtils.GetDescriptor(1, 1, 0, GraphicsFormat.R16G16B16A16_SFloat, 0, false));
            var dest   = RTUtils.GetTempHandle(RTUtils.GetDescriptor(1, 1, 0, GraphicsFormat.R16G16B16A16_SFloat, 0, false));

            Graphics.Blit(Texture2D.blackTexture, src);
            Graphics.Blit(Texture2D.blackTexture, dest);

            // eval
            addFilter.Eval(m_Context, src, dest);

            var tex = new Texture2D(1, 1, TextureFormat.RGBAFloat, false, true);

            RenderTexture.active = dest;
            tex.ReadPixels(new Rect(0, 0, 1, 1), 0, 0, false);

            var check = tex.GetPixel(0, 0).r;

            // clean up
            RenderTexture.active = prevRT;
            UObject.DestroyImmediate(tex);
            RTUtils.Release(src);
            RTUtils.Release(dest);
            UObject.DestroyImmediate(addFilter);

            Assert.That(check, Is.EqualTo(addValue));
        }
예제 #4
0
        public void Values_Can_Be_Negative()
        {
            // setup
            float addValue  = -10;
            var   addFilter = FilterUtility.CreateInstance <AddFilter>();

            addFilter.value = addValue;
            m_Stack.Add(addFilter);

            var prevRT = RenderTexture.active;
            var dest   = RTUtils.GetTempHandle(RTUtils.GetDescriptor(1, 1, 0, GraphicsFormat.R16G16B16A16_SFloat, 0, false));

            Graphics.Blit(Texture2D.blackTexture, dest); // init to black

            // eval
            m_Stack.Eval(m_Context, null, dest); // source isn't actually used yet

            var tex = new Texture2D(1, 1, TextureFormat.RGBAFloat, false, true);

            RenderTexture.active = dest;
            tex.ReadPixels(new Rect(0, 0, 1, 1), 0, 0, false);

            var check = tex.GetPixel(0, 0).r - 1; // minus 1 because we start off with a white texture within FilterStack.Eval

            // clean up
            RenderTexture.active = prevRT;
            UObject.DestroyImmediate(tex);
            RTUtils.Release(dest);

            Assert.That(check, Is.EqualTo(addValue));
        }
예제 #5
0
        public void Cleanup()
        {
            SceneView.duringSceneGui -= OnSceneGUI;
            Selection.activeObject    = null;
            if (onPaintHistory != null)
            {
                onPaintHistory.Clear();
            }

            // delete test resources
            commonUIInstance?.brushMaskFilterStack?.Clear(true);
            PaintContext.ApplyDelayedActions(); // apply before destroying terrain and terrainData
            if (terrainObj != null)
            {
                UnityEngine.Object.DestroyImmediate(terrainObj.terrainData);
                UnityEngine.Object.DestroyImmediate(terrainObj.gameObject);
            }

            // check Texture memory and RTHandle count
            // var currentTextureMemory = Texture.totalTextureMemory;
            // Assert.True(m_PrevTextureMemory == currentTextureMemory, $"Texture memory leak. Was {m_PrevTextureMemory} but is now {currentTextureMemory}. Diff = {currentTextureMemory - m_PrevTextureMemory}");
            var currentRTHandlesCount = RTUtils.GetHandleCount();

            Assert.True(m_PrevRTHandlesCount == RTUtils.GetHandleCount(), $"RTHandle leak. Was {m_PrevRTHandlesCount} but is now {currentRTHandlesCount}. Diff = {currentRTHandlesCount - m_PrevRTHandlesCount}");
        }
예제 #6
0
        protected override void OnEval(FilterContext fc, RenderTexture source, RenderTexture dest)
        {
            var desc = dest.descriptor;

            desc.enableRandomWrite = true;
            var sourceHandle = RTUtils.GetTempHandle(desc);
            var destHandle   = RTUtils.GetTempHandle(desc);

            using (sourceHandle.Scoped())
                using (destHandle.Scoped())
                {
                    Graphics.Blit(source, sourceHandle);
                    Graphics.Blit(dest, destHandle);

                    ComputeShader cs   = GetComputeShader();
                    int           kidx = cs.FindKernel("AspectRemap");

                    Texture2D remapTex = GetRemapTexture();

                    float rotRad = (fc.brushRotation - 90.0f) * Mathf.Deg2Rad;

                    cs.SetTexture(kidx, "In_BaseMaskTex", sourceHandle);
                    cs.SetTexture(kidx, "In_HeightTex", fc.rtHandleCollection[FilterContext.Keywords.Heightmap]);
                    cs.SetTexture(kidx, "OutputTex", destHandle);
                    cs.SetTexture(kidx, "RemapTex", remapTex);
                    cs.SetInt("RemapTexRes", remapTex.width);
                    cs.SetFloat("EffectStrength", m_EffectStrength);
                    cs.SetVector("TextureResolution", new Vector4(source.width, source.height, 0.0f, 0.0f));
                    cs.SetVector("AspectValues", new Vector4(Mathf.Cos(rotRad), Mathf.Sin(rotRad), m_Epsilon, 0.0f));
                    cs.Dispatch(kidx, source.width, source.height, 1);

                    Graphics.Blit(destHandle, dest);
                }
        }
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            commonUI.OnPaint(terrain, editContext);

            if (commonUI.allowPaint)
            {
                using (IBrushRenderUnderCursor brushRender = new BrushRenderUIGroupUnderCursor(commonUI, "SlopeFlatten", editContext.brushTexture))
                {
                    if (brushRender.CalculateBrushTransform(out BrushTransform brushXform))
                    {
                        PaintContext paintContext = brushRender.AcquireHeightmap(true, brushXform.GetBrushXYBounds(), 1);
                        Material     mat          = GetPaintMaterial();
                        var          brushMask    = RTUtils.GetTempHandle(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);
                        Utility.SetFilterRT(commonUI, paintContext.sourceRenderTexture, brushMask, mat);
                        paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear;
                        Vector4 brushParams = new Vector4(commonUI.brushStrength, 0.0f, commonUI.brushSize, 0);
                        mat.SetTexture("_BrushTex", editContext.brushTexture);
                        mat.SetVector("_BrushParams", brushParams);
                        brushRender.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
                        brushRender.RenderBrush(paintContext, mat, 0);
                        RTUtils.Release(brushMask);
                    }
                }
            }
            return(false);
        }
예제 #8
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            Texture brushTexture = editContext.brushTexture;

            commonUI.OnPaint(terrain, editContext);

            if (!commonUI.allowPaint)
            {
                return(true);
            }

            using (IBrushRenderUnderCursor brushRender = new BrushRenderUIGroupUnderCursor(commonUI, "ContrastTool", brushTexture))
            {
                if (brushRender.CalculateBrushTransform(out BrushTransform brushXform))
                {
                    PaintContext paintContext = brushRender.AcquireHeightmap(true, brushXform.GetBrushXYBounds(), 1);

                    Material mat       = GetPaintMaterial();
                    var      brushMask = RTUtils.GetTempHandle(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);
                    Utility.SetFilterRT(commonUI, paintContext.sourceRenderTexture, brushMask, mat);
                    paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear;
                    ApplyBrushInternal(brushRender, paintContext, commonUI.brushStrength, editContext.brushTexture, brushXform);
                    RTUtils.Release(brushMask);
                }
            }
            return(false);
        }
예제 #9
0
        private void AppEventDel(eAppEvent ev, String Message)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(delegate() { AppEventDel(ev, Message); }));
            }
            else
            {
                switch (ev)
                {
                case eAppEvent.eSlicedLoaded:     // update the gui to view
                    if (UVDLPApp.Instance().m_slicefile != null)
                    {
                        int totallayers = UVDLPApp.Instance().m_slicefile.NumSlices;
                        SetNumLayers(totallayers);
                    }
                    break;

                case eAppEvent.eObjectSelected:
                    // set the current sel plane object
                    if (UVDLPApp.Instance().SelectedObject != null)
                    {
                        RTUtils.UpdateObjectSelectionPlane(UVDLPApp.Instance().SelectedObject.m_center, m_camera.m_right, m_camera.m_up);
                    }

                    break;
                }
            }
        }
        protected override void OnEval(FilterContext fc, RenderTexture source, RenderTexture dest)
        {
            var desc = dest.descriptor;

            desc.enableRandomWrite = true;
            var sourceHandle = RTUtils.GetTempHandle(desc);
            var destHandle   = RTUtils.GetTempHandle(desc);

            using (sourceHandle.Scoped())
                using (destHandle.Scoped())
                {
                    Graphics.Blit(source, sourceHandle);
                    Graphics.Blit(dest, destHandle);

                    ComputeShader cs   = GetComputeShader();
                    int           kidx = cs.FindKernel("HeightRemap");

                    Texture2D remapTex = GetRemapTexture();

                    cs.SetTexture(kidx, "In_BaseMaskTex", sourceHandle);
                    cs.SetTexture(kidx, "In_HeightTex", fc.rtHandleCollection[FilterContext.Keywords.Heightmap]);
                    cs.SetTexture(kidx, "OutputTex", destHandle);
                    cs.SetTexture(kidx, "RemapTex", remapTex);
                    cs.SetInt("RemapTexRes", remapTex.width);
                    cs.SetFloat("EffectStrength", m_ConcavityStrength);
                    cs.SetVector("HeightRange", new Vector4(m_Height.x, m_Height.y, m_HeightFeather, 0.0f));
                    cs.Dispatch(kidx, source.width, source.height, 1);

                    Graphics.Blit(destHandle, dest);
                }
        }
예제 #11
0
        void Flatten(Terrain terrain)
        {
            Undo.RegisterCompleteObjectUndo(terrain.terrainData, "Set Height - Flatten Tile");

            var ctx = TerrainPaintUtility.BeginPaintHeightmap(terrain, new Rect(0, 0, terrain.terrainData.size.x, terrain.terrainData.size.z), 1);

            Material mat = GetPaintMaterial();

            float terrainHeight = Mathf.Clamp01((m_TargetHeight - terrain.transform.position.y) / terrain.terrainData.size.y);

            Vector4 brushParams = new Vector4(0, 0.5f * terrainHeight, 0.0f, 0.0f);

            mat.SetVector("_BrushParams", brushParams);

            var size = terrain.terrainData.size;

            size.y = 0;
            var brushMask = RTUtils.GetTempHandle(ctx.sourceRenderTexture.width, ctx.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);

            commonUI.GetBrushMask(terrain, ctx.sourceRenderTexture, brushMask, terrain.transform.position, Mathf.Min(size.x, size.z), 0f); // TODO(wyatt): need to handle seams
            mat.SetTexture("_FilterTex", brushMask);
            mat.SetTexture("_MainTex", ctx.sourceRenderTexture);
            Graphics.Blit(ctx.sourceRenderTexture, ctx.destinationRenderTexture, mat, 1);
            TerrainPaintUtility.EndPaintHeightmap(ctx, "Set Height - Flatten Tile");
            RTUtils.Release(brushMask);
            PaintContext.ApplyDelayedActions();
        }
예제 #12
0
        public void RTHandleRW_Has_RW_Flag_Set()
        {
            var handle = RTUtils.GetTempHandle(descriptorRW);

            Assert.True(handle.RT.enableRandomWrite);
            RTUtils.Release(handle);
        }
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            commonUI.OnPaint(terrain, editContext);

            if (commonUI.allowPaint)
            {
                Texture brushTexture = editContext.brushTexture;

                using (IBrushRenderUnderCursor brushRender = new BrushRenderUIGroupUnderCursor(commonUI, "PaintHoles", brushTexture))
                {
                    Vector2        halfTexelOffset    = new Vector2(0.5f / terrain.terrainData.holesResolution, 0.5f / terrain.terrainData.holesResolution);
                    BrushTransform brushXform         = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv - halfTexelOffset, commonUI.brushSize, commonUI.brushRotation);
                    PaintContext   paintContext       = brushRender.AquireHolesTexture(true, brushXform.GetBrushXYBounds());
                    PaintContext   paintContextHeight = brushRender.AcquireHeightmap(false, brushXform.GetBrushXYBounds());

                    // filter stack
                    Material mat       = Utility.GetPaintHeightMaterial();
                    var      brushMask = RTUtils.GetTempHandle(paintContextHeight.sourceRenderTexture.width, paintContextHeight.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);
                    Utility.SetFilterRT(commonUI, paintContextHeight.sourceRenderTexture, brushMask, mat);

                    // hold control key to erase
                    float   brushStrength = Event.current.control ? commonUI.brushStrength : -commonUI.brushStrength;
                    Vector4 brushParams   = new Vector4(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)TerrainBuiltinPaintMaterialPasses.PaintHoles);

                    TerrainPaintUtility.EndPaintHoles(paintContext, "Terrain Paint - Paint Holes");
                    RTUtils.Release(brushMask);
                }
            }
            return(true);
        }
예제 #14
0
        public void RTHandleRW_Temporary_Is_Not_Null()
        {
            var handle = RTUtils.GetTempHandle(descriptorRW);

            Assert.True(handle != null);
            Assert.True(handle.RT != null);
            RTUtils.Release(handle);
        }
예제 #15
0
        public void RTHandle_Release_New()
        {
            var handle = RTUtils.GetNewHandle(descriptor);

            Assert.True(RTUtils.GetHandleCount() == m_PrevRTHandleCount + 1);
            RTUtils.Release(handle);
            Assert.True(RTUtils.GetHandleCount() == m_PrevRTHandleCount);
        }
예제 #16
0
 /// <summary>
 /// Gather/Create all added RTHandles using the provided width, height, and depth value, if provided
 /// <param name="width">The width of the RTHandles to gather</param>
 /// <param name="height">The height of the RTHandles to gather</param>
 /// <param name="depth">The optional depth of the RTHandles to gather</param>
 /// <returns></returns>
 /// </summary>
 public void GatherRTHandles(int width, int height, int depth = 0)
 {
     foreach (int key in m_Hashes)
     {
         var desc = new RenderTextureDescriptor(width, height, m_Formats[key], depth);
         m_Handles[key] = RTUtils.GetNewHandle(desc);
         m_Handles[key].RT.Create();
     }
 }
예제 #17
0
파일: BaseTests.cs 프로젝트: Reflex21/Moba
        public virtual void Teardown()
        {
            // check Texture memory and RTHandle count
            // var currentTextureMemory = Texture.totalTextureMemory;
            // Assert.True(m_PrevTextureMemory == currentTextureMemory, $"Texture memory leak. Was {m_PrevTextureMemory} but is now {currentTextureMemory}. Diff = {currentTextureMemory - m_PrevTextureMemory}");
            var currentRTHandlesCount = RTUtils.GetHandleCount();

            Assert.True(m_PrevRTHandlesCount == RTUtils.GetHandleCount(), $"RTHandle leak. Was {m_PrevRTHandlesCount} but is now {currentRTHandlesCount}. Diff = {currentRTHandlesCount - m_PrevRTHandlesCount}");
        }
예제 #18
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            commonUI.OnPaint(terrain, editContext);

            if (commonUI.allowPaint)
            {
                Texture brushTexture = editContext.brushTexture;

                using (IBrushRenderUnderCursor brushRender = new BrushRenderUIGroupUnderCursor(commonUI, "Twist", brushTexture))
                {
                    if (brushRender.CalculateBrushTransform(out BrushTransform brushXform))
                    {
                        float finalTwistAmount = m_TwistAmount * -0.001f; //scale to a reasonable value and negate so default mode is clockwise
                        if (Event.current.shift)
                        {
                            finalTwistAmount *= -1.0f;
                        }

                        Material mat         = GetPaintMaterial();
                        Vector4  brushParams = new Vector4(commonUI.brushStrength, 0.0f, finalTwistAmount, 0.0f);
                        mat.SetTexture("_BrushTex", editContext.brushTexture);
                        mat.SetVector("_BrushParams", brushParams);

                        //twist splat map
                        if (m_AffectMaterials)
                        {
                            for (int i = 0; i < terrain.terrainData.terrainLayers.Length; i++)
                            {
                                TerrainLayer layer        = terrain.terrainData.terrainLayers[i];
                                PaintContext paintContext = brushRender.AcquireTexture(true, brushXform.GetBrushXYBounds(), layer);
                                var          brushMask    = RTUtils.GetTempHandle(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);
                                Utility.SetFilterRT(commonUI, paintContext.sourceRenderTexture, brushMask, mat);
                                paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear;
                                brushRender.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
                                brushRender.RenderBrush(paintContext, mat, 0);
                                brushRender.Release(paintContext);
                                RTUtils.Release(brushMask);
                            }
                        }

                        //twist height map
                        if (m_AffectHeight)
                        {
                            PaintContext paintContext = brushRender.AcquireHeightmap(true, brushXform.GetBrushXYBounds(), 1);
                            var          brushMask    = RTUtils.GetTempHandle(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);
                            Utility.SetFilterRT(commonUI, paintContext.sourceRenderTexture, brushMask, mat);
                            paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear;
                            ApplyBrushInternal(brushRender, paintContext, commonUI.brushStrength, finalTwistAmount, brushTexture, brushXform);
                            brushRender.Release(paintContext);
                            RTUtils.Release(brushMask);
                        }
                    }
                }
            }

            return(false);
        }
예제 #19
0
        public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            commonUI.OnPaint(terrain, editContext);

            if (!commonUI.allowPaint)
            {
                return(true);
            }

            Vector2 uv = editContext.uv;

            if (commonUI.ScatterBrushStamp(ref terrain, ref uv))
            {
                using (IBrushRenderUnderCursor brushRender = new BrushRenderUIGroupUnderCursor(commonUI, "WindErosion", editContext.brushTexture))
                {
                    if (brushRender.CalculateBrushTransform(out BrushTransform brushXform))
                    {
                        var          brushBounds  = brushXform.GetBrushXYBounds();
                        PaintContext paintContext = brushRender.AcquireHeightmap(true, brushBounds, 4);
                        paintContext.sourceRenderTexture.filterMode = FilterMode.Bilinear;

                        //paintContext.sourceRenderTexture = input heightmap
                        //Add Velocity (user wind direction and strength, or texture input, noise, forces, drag etc...)
                        float angle = commonUI.brushRotation; //m_WindAngleDegrees + r;
                        float r     = 0.5f * (2.0f * UnityEngine.Random.value - 1.0f) * 0.01f * m_Eroder.m_WindSpeedJitter;
                        float speed = m_Eroder.m_WindSpeed.value + r;

                        float rad = angle * Mathf.Deg2Rad;
                        m_Eroder.m_WindVel = speed * (new Vector4(-Mathf.Sin(rad), Mathf.Cos(rad), 0.0f, 0.0f));
                        m_Eroder.inputTextures["Height"] = paintContext.sourceRenderTexture;

                        var     heightRT  = RTUtils.GetTempHandle(RTUtils.GetDescriptorRW((int)brushBounds.width, (int)brushBounds.height, 0, RenderTextureFormat.RFloat));
                        Vector2 texelSize = new Vector2(terrain.terrainData.size.x / terrain.terrainData.heightmapResolution,
                                                        terrain.terrainData.size.z / terrain.terrainData.heightmapResolution);
                        m_Eroder.ErodeHeightmap(heightRT, terrain.terrainData.size, brushBounds, texelSize);

                        //Blit the result onto the new height map
                        Material mat       = GetPaintMaterial();
                        var      brushMask = RTUtils.GetTempHandle(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);
                        Utility.SetFilterRT(commonUI, paintContext.sourceRenderTexture, brushMask, mat);
                        Vector4 brushParams = new Vector4(commonUI.brushStrength, 0.0f, 0.0f, 0.0f);
                        mat.SetTexture("_BrushTex", editContext.brushTexture);
                        mat.SetTexture("_NewHeightTex", heightRT);
                        mat.SetVector("_BrushParams", brushParams);

                        brushRender.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
                        brushRender.RenderBrush(paintContext, mat, 0);
                        brushRender.Release(paintContext);
                        RTUtils.Release(brushMask);
                        RTUtils.Release(heightRT);
                    }
                }
            }

            return(true);
        }
예제 #20
0
        private void ApplyBrushInternal(Terrain terrain, PaintContext paintContext, float brushStrength, Texture brushTexture, BrushTransform brushXform)
        {
            /*
             * ComputeShader cs = GetDiffusionShader();
             *
             * int kernel = cs.FindKernel("Diffuse");
             * cs.SetFloat("dt", 0.1f);
             * cs.SetFloat("diff", 0.01f);
             * cs.SetVector("texDim", new Vector4(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0.0f, 0.0f));
             * cs.SetTexture(kernel, "InputTex", paintContext.sourceRenderTexture);
             * cs.SetTexture(kernel, "OutputTex", paintContext.destinationRenderTexture);
             * cs.Dispatch(kernel, paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 1);
             */

            RenderTexture prev = RenderTexture.active;

            Material mat       = GetMaterial();
            var      brushMask = RTUtils.GetTempHandle(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);

            Utility.SetFilterRT(commonUI, paintContext.sourceRenderTexture, brushMask, mat);
            Vector4 brushParams = new Vector4(Mathf.Clamp(brushStrength, 0.0f, 1.0f), 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);

            mat.SetInt("_KernelSize", (int)Mathf.Max(1, m_KernelSize)); // kernel size
            mat.SetVector("_SmoothWeights", smoothWeights);

            var texelCtx = Utility.CollectTexelValidity(terrain, brushXform.GetBrushXYBounds());

            Utility.SetupMaterialForPaintingWithTexelValidityContext(paintContext, texelCtx, brushXform, mat);

            paintContext.sourceRenderTexture.wrapMode      = TextureWrapMode.Clamp;
            paintContext.destinationRenderTexture.wrapMode = TextureWrapMode.Clamp;

            // Two pass blur (first horizontal, then vertical)
            var tmpRT = RTUtils.GetTempHandle(paintContext.destinationRenderTexture.descriptor);

            tmpRT.RT.wrapMode = TextureWrapMode.Clamp;
            mat.SetVector("_BlurDirection", Vector2.right);
            Graphics.Blit(paintContext.sourceRenderTexture, tmpRT, mat);
            mat.SetVector("_BlurDirection", Vector2.up);
            Graphics.Blit(tmpRT, paintContext.destinationRenderTexture, mat);

            RTUtils.Release(tmpRT);
            RTUtils.Release(brushMask);
            texelCtx.Cleanup();

            RenderTexture.active = prev;
            RenderTexture.ReleaseTemporary(brushMask);
        }
예제 #21
0
        public void SetUp()
        {
            EditorWindow.GetWindow <SceneView>().Focus();

            m_PrevTextureMemory  = Texture.totalTextureMemory;
            m_PrevRTHandlesCount = RTUtils.GetHandleCount();

            // Enables the core brush playback on OnSceneGUI
            SceneView.duringSceneGui -= OnSceneGUI;
            SceneView.duringSceneGui += OnSceneGUI;
        }
예제 #22
0
 /// <summary>
 /// Release the RTHandle resources that have been gathered
 /// </summary>
 public void ReleaseRTHandles()
 {
     foreach (int key in m_Hashes)
     {
         if (m_Handles[key] != null)
         {
             var handle = m_Handles[key];
             RTUtils.Release(handle);
             m_Handles[key] = null;
         }
     }
 }
        /*
         * public class Config
         * {
         *  int xres, yres;
         * // double
         * }
         * */

        public static bool FindIntersection(Vector3d direction, Point3d origin, ref Point3d intersect)
        {
            UVDLPApp.Instance().CalcScene();
            //bool intersected = false;

            //  Point3d bpoint, tpoint;
            //  Point3d lowest = new Point3d(); // the lowest point of intersection on the z axis
            direction.Normalize();
            direction.Scale(100.0);
            Point3d endp = new Point3d();

            endp.Set(origin);
            endp.x += direction.x;
            endp.y += direction.y;
            endp.z += direction.z;

            /*
             * intersect = new Point3d();
             * intersect.x = 0.0d;
             * intersect.y = 0.0d;
             * intersect.z = 0.0d;
             */
            //intersect the scene with a ray

            // intersected = false;
            foreach (Polygon p in UVDLPApp.Instance().Scene.m_lstpolys)
            {
                intersect = new Point3d();
                // try a less- costly sphere intersect here
                if (RTUtils.IntersectSphere(origin, endp, ref intersect, p.m_center, p.m_radius))
                {
                    // if it intersects,
                    if (RTUtils.IntersectPoly(p, origin, endp, ref intersect))
                    {
                        return(true);

                        /*
                         * // and it's the lowest one
                         * if (intersect.z <= lowest.z)
                         * {
                         *  //save this point
                         *  intersected = true;
                         *  lowest.Set(intersect);
                         * }
                         * */
                    }
                }
            }


            return(false);
        }
예제 #24
0
        Object3d GetSupportParrent(float x, float y, float z)
        {
            //Object3d obj;
            List <Object3d> matchingObjects = new List <Object3d>();

            foreach (Object3d obj in UVDLPApp.Instance().Engine3D.m_objects)
            {
                if (obj.tag != Object3d.OBJ_NORMAL)
                {
                    continue;
                }
                if ((x > obj.m_min.x) && (x < obj.m_max.x) && (y > obj.m_min.y) && (y < obj.m_max.y))
                {
                    matchingObjects.Add(obj);
                }
            }
            if (matchingObjects.Count == 0)
            {
                return(null); // Should not happen!
            }
            if (matchingObjects.Count == 1)
            {
                return(matchingObjects[0]); // the easy case.
            }
            Point3d origin;

            origin = new Point3d(); // bottom point
            origin.Set(x, y, 0.0f);
            //intersected = false; // reset the intersected flag to be false

            Vector3d up = new Vector3d(); // the up vector

            up.x = 0.0f;
            up.y = 0.0f;
            up.z = 1.0f;

            List <ISectData> lstISects = RTUtils.IntersectObjects(up, origin, matchingObjects, false);
            Object3d         objFound  = null;
            float            minzdiff  = 99999999f;

            // find the intersection closest to z.
            foreach (ISectData htd in lstISects)
            {
                float zdiff = Math.Abs(htd.intersect.z - z);
                if (zdiff < minzdiff)
                {
                    minzdiff = zdiff;
                    objFound = htd.obj;
                }
            }
            return(objFound);
        }
예제 #25
0
        public void RTHandle_Scope_Cleanup()
        {
            var handle = RTUtils.GetTempHandle(descriptorRW);

            Assert.True(m_PrevRTHandleCount + 1 == RTUtils.GetHandleCount());

            using (handle.Scoped())
            {
                // keep empty
            }

            Assert.True(m_PrevRTHandleCount == RTUtils.GetHandleCount());
        }
예제 #26
0
        private void ReleaseRTHandles()
        {
            for (int i = 0; i < 2; i++)
            {
                RTUtils.Release(m_HeightmapRT[i]);
                RTUtils.Release(m_WaterRT[i]);
                RTUtils.Release(m_WaterVelRT[i]);
                RTUtils.Release(m_FluxRT[i]);
                RTUtils.Release(m_SedimentRT[i]);
            }

            RTUtils.Release(m_ErodedRT);
            RTUtils.Release(m_HardnessRT);
        }
예제 #27
0
        private void ApplyBrushInternal(Terrain terrain, IPaintContextRender renderer, PaintContext paintContext, float brushStrength, Texture brushTexture, BrushTransform brushTransform)
        {
            Material mat       = Utility.GetPaintHeightMaterial();
            var      brushMask = RTUtils.GetTempHandle(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);

            Utility.SetFilterRT(commonUI, paintContext.sourceRenderTexture, brushMask, mat);
            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)TerrainBuiltinPaintMaterialPasses.RaiseLowerHeight);
            RTUtils.Release(brushMask);
        }
예제 #28
0
        /// <summary>
        /// Evaluate the FilterStack. Composited result will be copied into fc.destinationRenderTexture
        /// <param name="fc">The FilterContext that should be used for composition</param>
        /// <exception>Throws an exception if source or destination RenderTexture is null</exception>
        /// </summary>
        public void Eval(FilterContext fc, RenderTexture source, RenderTexture dest)
        {
            if (dest == null)
            {
                throw new InvalidOperationException("FilterContext::Eval: Source and destination RenderTextures are not properly set up");
            }

            using (new ActiveRenderTextureScope(RenderTexture.active))
            {
                int count     = filters.Count;
                int srcIndex  = 0;
                int destIndex = 1;

                var descriptor = RTUtils.GetDescriptor(dest.width, dest.height, 0, FilterUtility.defaultFormat);
                swapBuffer[0] = RTUtils.GetTempHandle(descriptor).WithName(k_BufferName0);
                swapBuffer[1] = RTUtils.GetTempHandle(descriptor).WithName(k_BufferName1);
                // ensure the textures are created for compute usage
                swapBuffer[0].RT.Create();
                swapBuffer[1].RT.Create();

                //don't remove this! needed for compute shaders to work correctly.
                Graphics.Blit(Texture2D.whiteTexture, swapBuffer[0]); // TODO: change this to black or source. should build up the mask instead of always multiply
                Graphics.Blit(Texture2D.blackTexture, swapBuffer[1]);

                for (int i = 0; i < count; ++i)
                {
                    var filter = filters[i];
                    if (!filter.enabled)
                    {
                        continue;
                    }

                    filter.Eval(fc, swapBuffer[srcIndex], swapBuffer[destIndex]);

                    // swap indices
                    destIndex += srcIndex;
                    srcIndex   = destIndex - srcIndex;
                    destIndex  = destIndex - srcIndex;
                }

                Graphics.Blit(swapBuffer[srcIndex], dest);

                RTUtils.Release(swapBuffer[0]);
                RTUtils.Release(swapBuffer[1]);
                swapBuffer[0] = null;
                swapBuffer[1] = null;
            }
        }
예제 #29
0
        public void RTHandle_Count_Is_Correct()
        {
            var handles = new RTHandle[10];

            for (int i = 0; i < handles.Length; ++i)
            {
                handles[i] = RTUtils.GetTempHandle(descriptor);
            }

            Assert.True(RTUtils.GetHandleCount() == m_PrevRTHandleCount + handles.Length);

            for (int i = 0; i < handles.Length; ++i)
            {
                RTUtils.Release(handles[i]);
            }
        }
예제 #30
0
        private void ApplyBrushInternal(Terrain terrain, IPaintContextRender renderer, PaintContext paintContext, float brushStrength, Texture brushTexture, BrushTransform brushXform)
        {
            Material mat       = GetPaintMaterial();
            var      brushMask = RTUtils.GetTempHandle(paintContext.sourceRenderTexture.width, paintContext.sourceRenderTexture.height, 0, FilterUtility.defaultFormat);

            Utility.SetFilterRT(commonUI, paintContext.sourceRenderTexture, brushMask, mat);
            float   delta = terraceToolProperties.m_JitterTerraceCount * 50.0f;
            float   jitteredFeatureSize = terraceToolProperties.m_FeatureSize + Random.Range(terraceToolProperties.m_FeatureSize - delta, terraceToolProperties.m_FeatureSize + delta);
            Vector4 brushParams         = new Vector4(brushStrength, jitteredFeatureSize, terraceToolProperties.m_BevelAmountInterior, 0.0f);

            mat.SetTexture("_BrushTex", brushTexture);
            mat.SetVector("_BrushParams", brushParams);

            renderer.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
            renderer.RenderBrush(paintContext, mat, 0);
            RTUtils.Release(brushMask);
        }