首页 > 其他分享 >DOTS计算Voronoi图形生成,根据点自动划分区域生成多边形

DOTS计算Voronoi图形生成,根据点自动划分区域生成多边形

时间:2024-09-24 20:50:05浏览次数:1  
标签:DOTS width int Points texture 生成 Voronoi points new

 

 如图,生成Voronoi图形,代码如下。

 

using UnityEngine;
using Unity.Mathematics;
using Unity.Jobs;
using Unity.Collections;
using Unity.Profiling;

[ExecuteInEditMode]
public class VoronoiTextureBurstJobComponent : MonoBehaviour
{
    [SerializeField][Min(1)] uint _seed = 769;
    [SerializeField][Range(2,512)] int _width = 300, _height = 300;
    [SerializeField][Range(1,64)] int _numRandomPoints = 3;

    Texture2D _texture;
    NativeList<ushort2> _points;
    uint lastSeedUsed = 0;
    
    void OnValidate ()
    {
        bool inputChanged = false;

        if( !_points.IsCreated )
        {
            _points = new NativeList<ushort2>( Allocator.Persistent );
            inputChanged = true;
        }
        if( _points.Length!=_numRandomPoints )
        {
            _points.Length = _numRandomPoints;
            inputChanged = true;
        }

        if( _texture==null || _texture.width!=_width || _texture.height!=_height )
        {
            if( _texture==null ) DestroyItWillYou( _texture );
            _texture = new Texture2D( _width , _height , TextureFormat.RGBA32 , mipCount:0 , linear:true );
            inputChanged = true;
        }

        if( inputChanged || _seed!=lastSeedUsed )
        {
            lastSeedUsed = _seed;
            var watch = System.Diagnostics.Stopwatch.StartNew();
            var dep = new FillRandomPointsJob{
                TextureWidth    = _width ,
                TextureHeight    = _height ,
                Seed            = _seed ,
                Points            = _points ,
            }.Schedule();
            NativeArray<RGBA32> textureData = _texture.GetRawTextureData<RGBA32>();
            new GenerateVoronoiDiagramJob{
                Points            = _points ,
                TextureWidth    = _width ,
                TextureHeight    = _height ,
                TextureOutput    = textureData ,
            }.Schedule( textureData.Length , _width , dep ).Complete();
            _texture.Apply();
            Debug.Log( $"jobs took {watch.ElapsedMilliseconds} [ms] to complete");
        }
    }

    void OnEnable ()
        => OnValidate();

    void OnDisable ()
    {
        if( _texture!=null ) DestroyItWillYou(_texture);
        if( _points.IsCreated ) _points.Dispose();
    }

    void OnGUI ()
        => GUI.DrawTexture( new Rect(0,0,_width,_height) , _texture );

    void DestroyItWillYou ( Object obj )
    {
        if( Application.isPlaying ) Destroy( obj );
        else DestroyImmediate( obj );
    }

    [Unity.Burst.BurstCompile]
    struct FillRandomPointsJob : IJob
    {
        public int TextureWidth, TextureHeight;
        public uint Seed;
        [WriteOnly] public NativeArray<ushort2> Points;
        void IJob.Execute ()
        {
            var rnd = new Unity.Mathematics.Random( Seed );
            int len = Points.Length;
            for( int i=0 ; i<len ; i++ )
                Points[i] = new ushort2{ x=(ushort)rnd.NextInt(TextureWidth) , y=(ushort)rnd.NextInt(TextureHeight) };
        }
    }

    [Unity.Burst.BurstCompile]
    struct GenerateVoronoiDiagramJob : IJobParallelFor
    {
        [ReadOnly] public NativeArray<ushort2> Points;
        public int TextureWidth, TextureHeight;
        [WriteOnly] public NativeArray<RGBA32> TextureOutput;
        void IJobParallelFor.Execute ( int jobIndex )
        {
            float2 pixelPos = new int2{ x=jobIndex%TextureWidth , y=jobIndex/TextureWidth };

            float pointDist = float.MaxValue;
            int pointIndex = -1;
            for( int i=Points.Length-1 ; i!=-1 ; i-- )
            {
                float2 pointPos = new float2{ x=Points[i].x , y=Points[i].y };
                
                float dist = math.lengthsq( pointPos - pixelPos );// dist sq
                
                // float2 vec =  math.abs( pointPos - pixelPos );
                // float dist = vec.x + vec.y;// manhattan distance
                
                if( dist<pointDist )
                {
                    pointDist = dist;
                    pointIndex = i;
                }

                if( dist<math.max(math.min(TextureWidth*0.01f,TextureHeight*0.01f),1) )
                {
                    TextureOutput[jobIndex] = new RGBA32{ r=0 , g=0 , b=0 , a=255 };
                    return;
                }
            }

            RGBA32 colorFromCoord;
            {
                ushort2 pointCoord = Points[pointIndex];
                float2 f = new float2{ x=pointCoord.x , y=pointCoord.y } / ( new float2{ x=TextureWidth , y=TextureHeight } - new float2{ x=1 , y=1 } );
                byte r = (byte)( 255*f.x );
                byte g = (byte)( 255*f.y );
                byte b = (byte)( (r+g)%255 );
                colorFromCoord = new RGBA32{ r=r , g=g , b=b , a=255 };
            }
            TextureOutput[jobIndex] = colorFromCoord;
        }
    }

    struct ushort2 { public ushort x, y; }

    struct RGBA32 { public byte r, g, b, a; }

}

 

标签:DOTS,width,int,Points,texture,生成,Voronoi,points,new
From: https://www.cnblogs.com/flamesky/p/18429992

相关文章

  • exec 在函数和生成器中
    我需要在python中编写一个自定义exec函数(出于多种目的,但这不是这里的问题,所以这个名为myExec的自定义exec的功能将与现在的exec完全一样)。我进入了这个问题:defmyExec(code):exec(code)code="""a=1print(a)u=[aforxinrange(3)......
  • 小渡AI论文写作原创论文生成研究生论文写作技巧
    步骤:1.明确研究目标和问题:首先,确定你的研究目标和问题,明确你想要解决的研究问题和要达到的目标。这个步骤对于帮助你集中精力和确保研究的有效性非常重要。2.进行文献综述:进行文献综述是论文写作中的重要环节。你需要搜索和查阅相关文献,了解已有的研究成果和学术观点。文献......
  • 结对项目:生成四则运算
    个人项目——论文查重这个作业属于哪个课程计科12班这个作业的要求在哪里作业要求这个作业的目标实现一个自动生成小学四则运算题目的命令行程序github链接:github题目需求使用-n参数控制生成题目的个数,例如Myapp.exe-n10将生成10个题目。使用-......
  • C#在Winform中截图指定控件中的内容生成图像
    开发上位机过程中,收到需求:在软件跑完数据之后保存报告和图表截图。因为界面控件都做了大小拉伸缩放的适配,所以简单的设置截图起始点和长宽时无法满足需求的。所以要做一个根据控件本身大小来做截取动作的功能,所以我写了一个截取指定控件内图像的函数。 函数如下,只需传入控件,和存......
  • 国内大厂这款「一站式」AI视频创作平台,一键将剧本生成视频!实测体验(附内测申请)
    大家好,我是程序员X小鹿,前互联网大厂程序员,自由职业2年+,也一名AIGC爱好者,持续分享更多前沿的「AI工具」和「AI副业玩法」,欢迎一起交流~AI现在已经可以一键创作剧本、生成分镜图了?今天介绍的这个一站式AI视频创作平台厉害了!它可以自动将剧本拆分成分镜图,并转化成视频。还将多种......
  • MemLong: 基于记忆增强检索的长文本LLM生成方法
    本文将介绍MemLong,这是一种创新的长文本语言模型生成方法。MemLong通过整合外部检索器来增强模型处理长上下文的能力,从而显著提升了大型语言模型(LLM)在长文本处理任务中的表现。核心概念MemLong的设计理念主要包括以下几点:高效扩展LLM上下文窗口的轻量级方法。利用不可训练的外部记......
  • SAP 销售凭证流增加自主生成的凭证
    在标准销售凭证流增加自定义凭证信息,本质上是ALVTREE增加显示内容1、显示结果,新增一个会计凭证,此凭证是使用F-02生成,但是凭证挂接着交货单号信息,可通过交货单号信息与正常凭证流程挂接 2、实现此结果,使用到的增强有两处(1)  (2)第二个增强,在凭证流中选中这个预折会计凭证......
  • 2024年中国生成式AI行业最佳应用实践|附100页PDF文件下载
    前言8月28日,由弗若斯特沙利文(Frost&Sullivan,简称“沙利文”)主办的第十八届中国增长、科创与领导力峰会暨第三届新投资大会上,沙利文携手头豹研究院共同发布了《2024年中国生成式AI行业最佳应用实践》报告,并揭晓了多项实践方案大奖。其中,商汤科技与海通证券凭借双方联合打造的金融......
  • 美图视频生成大模型「奇想」完成升级;李飞飞:空间智能不仅适用虚拟世界生成,还可融合现实
       开发者朋友们大家好: 这里是「RTE开发者日报」,每天和大家一起看新闻、聊八卦。我们的社区编辑团队会整理分享RTE(Real-TimeEngagement)领域内「有话题的新闻」、「有态度的观点」、「有意思的数据」、「有思考的文章」、「有看点的会议」,但内容仅代表编......
  • 勤哲Excel服务器自动生成网络工程管理信息化系统
    在当今信息化技术高速发展的时代,有线网络工程作为信息交流和数据传输的重要基础设施,已经渗透到各个领域。有线网络工程的规模和复杂性日益增长,对其全面管理的需求也与日俱增。传统的人工管理已经无法满足大规模网络环境下的管理需求,因此,信息化管理成为必然的趋势。据悉,有线网络......