Advertisement
Guest User

Proof))))

a guest
Feb 13th, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.60 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using TMPro;
  4. using Unity.Burst;
  5. using Unity.Collections;
  6. using Unity.Jobs;
  7. using Unity.Profiling;
  8. using UnityEngine;
  9. using static Unity.Mathematics.math;
  10. using static ProofMath.mathUtils;
  11. using float3 = Unity.Mathematics.float3;
  12. using Random = Unity.Mathematics.Random;
  13.  
  14. public struct Entity
  15. {
  16.     public float3 Position;
  17.     public float Orientation;
  18.     public float3 Scale;
  19.     public float3 MovementDirection;
  20.     public float Speed;
  21.     public float RotationSpeed;
  22. }
  23.  
  24. public struct EntityRenderState
  25. {
  26.     public Material Material;
  27.     public Mesh Mesh;
  28. }
  29.  
  30. public struct InstanceData
  31. {
  32.     public Matrix4x4 objectToWorld;
  33. }
  34.  
  35. public class Proof : MonoBehaviour
  36. {
  37.     public TMP_Text FrameTime;
  38.     public TMP_Text FrameRate;
  39.     public int EntitiesCount = 10000;
  40.     public Sprite EntitySprite;
  41.     public Material EntityMaterial;
  42.     public Texture EntityTexture;
  43.  
  44.     private NativeArray<Entity> _entities;
  45.     private EntityRenderState _renderState;
  46.     private static InstanceData[] _perInstanceData;
  47.     private static readonly int MainTex = Shader.PropertyToID("_MainTex");
  48.  
  49.     private static readonly ProfilerMarker UpdateEntities = new ProfilerMarker(nameof(UpdateEntities));
  50.     private static readonly ProfilerMarker Render = new ProfilerMarker(nameof(Render));
  51.  
  52.     private void Start()
  53.     {
  54.         _entities = new NativeArray<Entity>(EntitiesCount, Allocator.Persistent);
  55.         _perInstanceData = new InstanceData[EntitiesCount];
  56.        
  57.         var random = Random.CreateFromIndex(123);
  58.         var size = new float3(100f, 100f, 0f);
  59.         var one = new float3(1f, 1f, 1f);
  60.        
  61.         for (var i = 0; i < EntitiesCount; ++i)
  62.         {
  63.             var position = random.NextFloat3(-size, size);
  64.             var direction = random.NextFloat3(new float3(1, 1, 0));
  65.             var speed = random.NextFloat(15f);
  66.             var rotationSpeed = random.NextFloat(180f);
  67.             var startOrientation = random.NextFloat(360f);
  68.  
  69.             _entities[i] = new Entity
  70.             {
  71.                 Position = position,
  72.                 MovementDirection = direction,
  73.                 Speed = speed,
  74.                 RotationSpeed = rotationSpeed,
  75.                 Orientation = startOrientation,
  76.                 Scale = one
  77.             };
  78.         }
  79.        
  80.         //Convert sprite to mesh
  81.        
  82.         var mesh = new Mesh();
  83.                
  84.         mesh.SetVertices(Array.ConvertAll(EntitySprite.vertices, i => (Vector3)i).ToList());
  85.         mesh.SetUVs(0, EntitySprite.uv.ToList());
  86.         mesh.SetTriangles(Array.ConvertAll(EntitySprite.triangles, i => (int)i),0);
  87.  
  88.         _renderState = new EntityRenderState()
  89.         {
  90.             Material = new Material(EntityMaterial),
  91.             Mesh = mesh
  92.         };
  93.        
  94.         _renderState.Material.SetTexture(MainTex, EntityTexture);
  95.     }
  96.  
  97.     private void OnDestroy()
  98.     {
  99.         _entities.Dispose();
  100.     }
  101.  
  102.     private void Update()
  103.     {
  104.         //move entities
  105.         var deltaTime = Time.deltaTime;
  106.         FrameTime.text = deltaTime.ToString();
  107.         FrameRate.text = (1.0f / deltaTime).ToString();
  108.         UpdateEntities.Begin();
  109.  
  110.         var job = new MovementJob()
  111.         {
  112.             Entities = _entities,
  113.             DeltaTime = deltaTime
  114.         };
  115.  
  116.         var handle = job.Schedule(EntitiesCount, 32);
  117.        
  118.         handle.Complete();
  119.  
  120.  
  121.         UpdateEntities.End();
  122.         //Render entities
  123.  
  124.         Render.Begin();
  125.         var rp = new RenderParams(_renderState.Material);
  126.  
  127.         for (var i = 0; i < EntitiesCount; ++i)
  128.         {
  129.             var entity = _entities[i];
  130.  
  131.             var trs = Matrix4x4.TRS(
  132.                 entity.Position,
  133.                 Quaternion.Euler(0, 0, entity.Orientation),
  134.                 entity.Scale);
  135.  
  136.             _perInstanceData[i].objectToWorld = trs;
  137.         }
  138.  
  139.         Graphics.RenderMeshInstanced(rp, _renderState.Mesh, 1, _perInstanceData, EntitiesCount);
  140.        
  141.         Render.End();
  142.     }
  143. }
  144.    
  145. [BurstCompile]
  146. public struct MovementJob : IJobParallelFor
  147. {
  148.     public NativeArray<Entity> Entities;
  149.     public float DeltaTime;
  150.        
  151.     public void Execute(int index)
  152.     {
  153.         var entity = Entities[index];
  154.         var targetOrientation = degrees(atan2(entity.MovementDirection.y, entity.MovementDirection.x)) - 90f;
  155.  
  156.         entity.Position += entity.MovementDirection * entity.Speed * DeltaTime;
  157.         entity.Orientation = movetoangle(entity.Orientation, targetOrientation, entity.RotationSpeed * DeltaTime);
  158.  
  159.         Entities[index] = entity;
  160.     }
  161. }
Tags: Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement