Unity DOTS ECS 1.0版本,海浪Demo

国内2023年6月10日更新了Unity2022的LTS版本,ECS 也发布到1.0.10版本,之前的海浪demo需要升级一下API;
旧地址

ECS1.0.10测试demo图片

安装

manifest.json添加下面三行

"com.unity.entities.graphics": "1.0.10",

注意

  1. 部分注意事项在旧地址有提及,不再赘述;

Demo脚本

新建场景,挂载StartBehaviour.cs,直接运行

Component

HeightComponent.cs
using Unity.Entities;

namespace Component
{
    public struct HeightComponent : IComponentData
    {
        public float InitiateHeight;
        public float MaxHeight;
    }
}
SpeedComponent.cs
using Unity.Entities;

namespace Component
{
    public struct SpeedComponent : IComponentData
    {
        public float Speed;
    }
}

Baker

public class MyAuthoring : MonoBehaviour
    {
        // [SerializeField] public int count = 100;
        public Mesh unitMesh;
        public Material unitMaterial;
    }
public class MyBaker : Baker<MyAuthoring>
    {
        public int count = 2;
        public float noiseRange = 20f;
        public float noiseValue = 2f;
        public float speed = 4f;
        public float maxHeight = 1f;
        public Mesh unitMesh;
        public Material unitMaterial;

        private EntitiesGraphicsSystem m_RendererSystem;
        private EntityManager _entityManager;

        private MaterialMeshInfo materialMeshInfo;

        public override void Bake(MyAuthoring authoring)
        {
            DefaultWorldInitialization.Initialize("TestWorld");
            _entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
            m_RendererSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystemManaged<EntitiesGraphicsSystem>();
            var archetype = _entityManager.CreateArchetype(
                typeof(LocalToWorld)
            );
            materialMeshInfo = new MaterialMeshInfo()
            {
                MeshID = m_RendererSystem.RegisterMesh(unitMesh),
                MaterialID = m_RendererSystem.RegisterMaterial(unitMaterial),
            };

            for (int i = 0; i < count; i++)
            {
                for (int j = 0; j < count; j++)
                {
                    var pos = new float3(i * 1, 0, j * 1);
                    CreateEntity(archetype, pos);
                }
            }
        }

        private void CreateEntity(EntityArchetype archetype, float3 pos)
        {
            float2 float2 = new float2(pos.x / noiseRange, pos.z / noiseRange);
            float f = noise.snoise(float2) / noiseValue;
            pos.y = f;

            Entity entity = _entityManager.CreateEntity(archetype);

            CreateGameObject(ref entity, pos);

            _entityManager.AddComponentData(entity, new HeightComponent()
            {
                InitiateHeight = f,
                MaxHeight = maxHeight,
            });
            _entityManager.AddComponentData(entity, new SpeedComponent()
            {
                Speed = speed
            });

            _entityManager.AddComponentData(entity, new MyOwnColor()
            {
                Value = new float4(),
                Value2 = 1,
            });
        }

        private void CreateGameObject(ref Entity entity, float3 pos)
        {
            LocalToWorld toWorldTransform = _entityManager.GetComponentData<LocalToWorld>(entity);
            toWorldTransform.Value = float4x4.Translate(pos);

            _entityManager.SetComponentData(entity, toWorldTransform);

            _entityManager.AddComponentData(entity, materialMeshInfo);

            var renderMeshDescription = new RenderMeshDescription()
            {
                FilterSettings = new RenderFilterSettings()
                {
                    RenderingLayerMask = 1,
                    ShadowCastingMode = ShadowCastingMode.On,
                    ReceiveShadows = true,
                }
            };

            var renderMeshArray = new RenderMeshArray(new []{unitMaterial}, new []{unitMesh});

            RenderMeshUtility.AddComponents(entity, _entityManager, renderMeshDescription, renderMeshArray, materialMeshInfo);
        }
    }

System

RotationSystem.cs
using Component;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;

public partial class RotationSystem : SystemBase
{
    protected override void OnUpdate()
    {
        Entities.ForEach((ref LocalToWorld localToWorldTransform, in HeightComponent heightComponent,
            in SpeedComponent speedComponent) =>
        {
            var elapsedTime = SystemAPI.Time.ElapsedTime;
            var heightComponentMaxHeight =
                (float) math.sin((elapsedTime + heightComponent.InitiateHeight) * speedComponent.Speed) *
                heightComponent.MaxHeight;
            var valuePosition = localToWorldTransform.Position;
            valuePosition.y = heightComponentMaxHeight;
            localToWorldTransform.Value = float4x4.Translate(valuePosition);
        }).WithoutBurst().Run();
    }
}
StartBehaviour.cs
using UnityEngine;

public class StartBehaviour : MonoBehaviour
{
    public MyAuthoring Obj;

        [SerializeField] public int count = 2;
        [SerializeField] public float noiseRange = 20f;
        [SerializeField] public float noiseValue = 2f;
        [SerializeField] public float speed = 4f;
        [SerializeField] public float maxHeight = 1f;
        [SerializeField] public Mesh unitMesh;
        [SerializeField] public Material unitMaterial;

        private void Start()
        {
            MyBaker baker = new MyBaker()
            {
                count = count,
                noiseRange = noiseRange,
                noiseValue = noiseValue,
                speed = speed,
                maxHeight = maxHeight,
                unitMesh = unitMesh,
                unitMaterial = unitMaterial,
            };
            baker.Bake(Obj);
        }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值