Instantly share code, notes, and snippets.

Embed
What would you like to do?
using UnityEngine;
using Unity.Entities;
using Unity.Collections;
using Unity.Mathematics;
using Unity.Transforms;
public class NoiseBootstrap : MonoBehaviour
{
// Position, MeshInstanceRenderer, Cubeを持つGameObjectEntity
[SerializeField] GameObject prefab;
void Start()
{
var em = World.Active.GetExistingManager<EntityManager>();
// 地面一面にCubeを敷き詰める
const int lap = 100;
using (var entities = new NativeArray<Entity>(lap * lap, Allocator.Temp))
{
em.Instantiate(prefab, entities);
for (int i = 1; i < entities.Length; i++)
{
var x = i / lap;
var z = i % lap;
em.SetComponentData(entities[i], new Position() { Value = new float3(x, 0, z) });
}
}
}
}
using UnityEngine;
using Unity.Entities;
using Unity.Collections;
using Unity.Jobs;
using Unity.Burst;
using Unity.Transforms;
using Unity.Mathematics;
public class NoiseSystem : JobComponentSystem
{
protected override JobHandle OnUpdate(JobHandle inputDeps) => new Process()
{
offset = Time.timeSinceLevelLoad
}.Schedule(this, inputDeps);
[RequireComponentTag(typeof(Cube))]
[BurstCompile]
struct Process : IJobProcessComponentData<Position>
{
public float offset;
const float height = 2;
const float per = 128;
const float size = 0.05f;
public void Execute(ref Position data)
{
var x = data.Value.x * size + offset;
var z = data.Value.z * size + offset;
var pos = new float2(x, z);
data.Value.y = noise.psrnoise(pos, new float2(per, per)) * height;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment