Skip to content

Instantly share code, notes, and snippets.

@Frooxius
Created October 15, 2018 21:48
Show Gist options
  • Save Frooxius/d6edc004dd20b7771547e678642a7ecb to your computer and use it in GitHub Desktop.
Save Frooxius/d6edc004dd20b7771547e678642a7ecb to your computer and use it in GitHub Desktop.
Using MultiLineMesh to generate a line trail
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrooxEngine;
using BaseX;
namespace NeosSampleLibrary
{
[Category("Examples")]
public class GeneratingLineMesh : Component
{
protected override void OnAttach()
{
// attach a new MultiLineMesh
// Using AttachMesh<MESH, MATERIAL> helper, which automatically setups material and renderer as well
var model = Slot.AttachMesh<MultiLineMesh, PBS_Metallic>();
// get the mesh itself
var mesh = model.mesh;
// add a new line
var line = mesh.Lines.Add();
// set the line profile topology to line
line.Topology.Value = SegmentedBuilder.Topology.Line;
// make it generate as dualsided
line.DualSided.Value = true;
// need only two points for each segment
line.Points.Value = 2;
// Let's start generating segments. I'll just randomly simulate a point moving around
var position = float3.Zero;
var orientation = floatQ.Identity;
// generate a random starting position for the noise
var noisePosition = RandomX.Range(float3.One * -1000, float3.One * 1000);
for(int i = 0; i < 10000; i++)
{
// append a new point
line.Positions.Append(position);
// append corresponding orientation
line.Orientations.Append(orientation);
// append a scale, this maps to the width of the line
// you can omit this and just use uniform scale like this:
// line.Scale = 0.01f;
line.Scales.Append(0.05f + MathX.Abs(MathX.Sin(i * 0.01f) * 0.1f));
// move the point a little bit forward along the direction
position += orientation * float3.Forward * 0.01f;
// rotate the direction by a small angle based on a procedural noise
orientation = floatQ.Euler(MathX.SimplexNoise(noisePosition) * 4f) * orientation;
// advance the noise as well
noisePosition += float3.One * 0.01f;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment