Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add runtime demo #261

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Tests/Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions Tests/Runtime/RuntimeDemo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;

namespace andywiecko.BurstTriangulator.Tests.Runtime
{
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class RuntimeDemo : MonoBehaviour
{
[SerializeField, Min(3)]
private int pointsCount = 1024;

[SerializeField, Min(1e-9f)]
private float syncTime = 1f / 30;

private Mesh mesh;
private Triangulator triangulator;
private NativeArray<double2> positions;
private NativeArray<Vector3> vertices;
private NativeReference<Unity.Mathematics.Random> random;

private void Start()
{
positions = new(pointsCount, Allocator.Persistent);
vertices = new(pointsCount, Allocator.Persistent);
random = new(new(seed: 42), Allocator.Persistent);
triangulator = new(Allocator.Persistent)
{
Input = { Positions = positions },
Settings = { ValidateInput = false }
};

GetComponent<MeshFilter>().mesh = mesh = new Mesh();
GetComponent<MeshRenderer>().material = new(Shader.Find("Hidden/BurstTriangulator/RuntimeDemo"));
}

private void OnDestroy()
{
dependencies.Complete();

positions.Dispose();
vertices.Dispose();
triangulator.Dispose();
random.Dispose();
}

private JobHandle dependencies;
private float t;

private void Update()
{
if (t <= syncTime)
{
t += Time.deltaTime;
return;
}

dependencies.Complete();
mesh.SetVertices(vertices);
mesh.SetIndices(triangulator.Output.Triangles.AsArray(), MeshTopology.Triangles, 0);
mesh.RecalculateBounds();

dependencies = default;
dependencies = new GeneratePositionsJob(this).Schedule(dependencies);
dependencies = triangulator.Schedule(dependencies);

t = 0;
}

[BurstCompile]
private struct GeneratePositionsJob : IJob
{
private NativeArray<double2> positions;
private NativeArray<Vector3> vertices;
private NativeReference<Unity.Mathematics.Random> random;

public GeneratePositionsJob(RuntimeDemo demo)
{
positions = demo.positions;
vertices = demo.vertices;
random = demo.random;
}

public void Execute()
{
var rnd = random.Value;
for (int i = 0; i < positions.Length; i++)
{
var p = positions[i] = rnd.NextDouble2();
vertices[i] = math.float3((float2)p, 0);
}
random.Value = rnd;
}
}
}
}
11 changes: 11 additions & 0 deletions Tests/Runtime/RuntimeDemo.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions Tests/Runtime/RuntimeDemo.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Shader "Hidden/BurstTriangulator/RuntimeDemo"
{
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

struct v2f
{
float4 pos : SV_POSITION;
};

v2f vert(appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}

float4 frag(v2f IN, uint primID : SV_PrimitiveID) : SV_Target
{
// A crude random value based on the primitive ID
float red = frac((float)primID * 0.34563456);
float green = frac((float)primID * 0.85446);
float blue = frac((float)primID * 0.212345);
return float4(red, green, blue, 1);
}
ENDCG
}
}
FallBack "Diffuse"
}
9 changes: 9 additions & 0 deletions Tests/Runtime/RuntimeDemo.shader.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Tests/Runtime/RuntimeDemoTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;

namespace andywiecko.BurstTriangulator.Tests.Runtime
{
public class RuntimeDemoTest
{
[UnityTest]
public IEnumerator DemoTest()
{
yield return EditorSceneManager.LoadSceneAsyncInPlayMode(path: "Packages/com.andywiecko.burst.triangulator/Tests/Runtime/RuntimeDemoTest.unity", new(LoadSceneMode.Single));
yield return new WaitForSeconds(3f);
}
}
}
11 changes: 11 additions & 0 deletions Tests/Runtime/RuntimeDemoTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading