Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active October 31, 2018 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/719cc0422a2156861c66a0a7a597c7a1 to your computer and use it in GitHub Desktop.
Save tsubaki/719cc0422a2156861c66a0a7a597c7a1 to your computer and use it in GitHub Desktop.
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