• Stars
    star
    141
  • Rank 259,971 (Top 6 %)
  • Language
    C#
  • License
    MIT License
  • Created about 6 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ECS approach to render a line.

ECS Line Renderer

screenshot

ECS approach to render a line I made for personal use on commissioned non-game visualization work. The feature is not quite enough but I decided to open source it.

Coded with mostly archetype chunk iteration API since it will be the most resistant to API changes in this preview phase. So if you want to contribute, please try to stick to chunk iteration if it is a main thread work, and IJobChunk for worker thread. Thank you! (With IJobChunk instead of IJobForEach, we could control chunk version bumps and add some DidChange optimizations.)

How to include with GitHub functionality of Unity Package Manager

Add this line "com.e7.ecs.line-renderer": "git://github.com/5argon/ECSLineRenderer.git", to your manifest.json

It does not update automatically when I push fixes to this repo. You must remove the lock in your manifest.

Core idea

I want to find a new way to render an imperfect yet efficient line by trying to leverage geometry instancing and ECS together.

A typical line renderer works by triangulation of a set of connecting points. For example Unity's LineRenderer always create 2 triangles per one pair of point regardless of their angle.

line

Per 1 line with any number of points Unity generated 1 unique mesh. But, this mesh is unlikely to be the same as other lines, it could not be rendered by instancing due to different mesh. (Even though they could be batched by the same material.) Also if you want to animate line's points, every little movement would need an entire mesh regenerated.

In this package, I want to try using the same mesh as much as possible to assemble a line that could be instanced. I am aware that this may produce an ugly looking line especially the corner if I decided to just overlap rectangles (and it wouldn't work well for non-solid material), but I think it is an interesting experiment and could be useful for some cases, like a line small enough that the defects aren't that visible in exchange of performance. And lastly, I would like to be able to move each point on the line without mesh regeneration at all. Application includes an efficient wireframe, a good base for application like animated graphs, or just drawing grids/tables in general.

The top image has overlapping corner which produces jagged look, but it looks acceptable at glance because the line is white and its width is small enough. Only when extremely zoomed in that you start to notice defects.

See this issue for details.

How it works here

It generate 1 piece of a thin rectangle mesh that goes 1 unit in the Z axis. We could use z scale as line length, x scale as line width, position as line from, and rotation as line to. Assuming that this is only one segment of a line.

LineSegment contains from-to absolute position in world coordinate. To construct complex lines, we create many LineSegment entity with connecting from-to. They should be render instanced as they are using the same mesh. Line width is also on this component.

The line then need a material. So you specify that on LineStyle ISharedComponentData. Without both LineSegment and LineStyle my systems will not work on them.

Finally all lines should be rotated to face the main camera in billboard rendering style. The system will find a "main camera" and take the camera's transform into calculation.

// ECSLineRenderer

LineSegment -> Transform + NonUniformScale
LineStyle -> RenderMesh
Main Camera -> Rotation

// Unity's built-in TransformSystemGroup

Transform + Rotation + NonUniformScale -> LocalToWorld

// Unity's Hybrid Renderer Package

LocalToWorld + RenderMesh -> You see things on screen.

billboard

Rendering

The rendering is handled by HybridRenderer package, by generating RenderMesh shared component data for all your lines. So this package's work are all in simulation system group.

For someone that is always looking for "pure ECS" approach to everything, no, it is not possible to see something on the screen in pure currently. Even the package is named hybrid renderer. I suggest you just drop the pure/hybrid wording altogether and just use what's the best.

Minimum code to get a line from nothing

var e = EntityManager.CreateEntity();
EntityManager.AddComponentData(e, new LineSegment(math.float3(1, 1, 1), math.float3(1, 2, 1)));
var mat = UnityEditor.AssetDatabase.LoadAssetAtPath<Material>("Assets/ECSLineRenderer/YellowLineMaterial.mat");
EntityManager.AddSharedComponentData(e, new LineStyle { material = mat });

Info

  • Line width is in Unity's standard grid unit.
  • If position from and to are the same the system will not update the line. (Stays at previous position)

GameObject Conversion / SubScene support

This is a new and preferred way to author ECS content. GameObjectEntity has a performance problem to maintain a link to GameObject. ConvertToEntity has a problem that things disappear when you enter play mode if you want pure ECS at runtime (without inject mode, which would arrive the same performance problem) and also it is a problem in edit time that you don't know how it would end up.

SubScene is the best of both worlds. Things with IConvertGameObjectToEntity inside SubScene get converted in play mode like ConvertToEntity, but they remains touchable in Hierarchy and anytime you do something they will get reconverted seamlessly. It feels like old GameObjectEntity approach except instead of synchronization it keeps reconverting in one way, then in the real build it works like pure ECS since you cannot touch Hierarchy in the real build.

I have added LineAuthoring : IConvertGameObjectToEntity for this. See the green line in sample scene how this get converted to LineSegment and LineStyle. Since all systems here has [ExecuteAlways], you can turn on DOTS > Live Link Mode > Live Link Conversion in Edit Mode then choose SceneView mode you like to see SubScene in action even in edit mode.

Systems

  • LineSegmentRegistrationSystem : Having both LineSegment and LineStyle on the same Entity will qualify for a registration, which you will get a RenderMesh with migrated material from your LineStyle plus other TRS components.
  • LineSegmentTransformSystem : Calculate TRS which would then turns into LocalToWorld then RenderMesh use it. Based on information in LineSegment. Move your line by changing LineSegment's data before this system.

Limitations + problems

Please see Issues section. For instance, currently the billboard rotation is wrong. If anyone understand http://www.opengl-tutorial.org/intermediate-tutorials/billboards-particles/billboards/ you can send PR.. thanks.

This asset is sponsored by

My other assets.. other than pull requests you can contribute by getting these. Thank you.

  • Introloop - Easily play looping music with intro section (without physically splitting them) (Unity 2017.0+)
  • Native Audio - Lower audio latency via OS's native audio library. (Unity 2017.1+, iOS uses OpenAL / Android uses OpenSL ES)

More Repositories

1

NotchSolution

A set of components and tools to solve notched/cutout phones layout problems for Unity.
C#
664
star
2

protobuf-unity

Automatic .proto files compilation in Unity project to C# as you edit them, plus utilities.
C#
445
star
3

E7Unity

Common Unity resources Exceed7 Experiments uses in projects.
C#
163
star
4

SpineTimeline

Animate SkeletonAnimation or SkeletonGraphic with Unity's Timeline
C#
81
star
5

E7ECS

Misc stuff I made for ECS.
C#
50
star
6

Minefield

Program a concise navigation play mode test in Unity.
C#
50
star
7

ModifyEditorStyle

Change fonts throughout Unity editor
C#
44
star
8

AHLCG3mmDivider

Card divider graphics you can print out and cut up to use with Arkham Horror : The Card Game (LCG)
C#
39
star
9

UnityiOSNativeAudio

This project confirms that the Unity's audio problem is not just audio latency, but also input latency.
Objective-C++
38
star
10

OdinHierarchy

Pimp your Hierarchy based on pattern matching.
C#
34
star
11

GradientGenerator

A Unity script to generate multiple variants of evenly distributed `Gradient` based on input `AnimationCurve`.
C#
34
star
12

EcsTesting

Utilities to help writing tests for Unity Entities.
C#
29
star
13

ECS.HybridTextMesh

Use Transform systems and Hybrid Renderer to render pre-generated meshes of glyphs.
C#
29
star
14

EnumDispatcher

ECS backed C# enums as Flux/Redux action in Unity
C#
27
star
15

JobAnimationCurve

An AnimationCurve that works inside a job. Build one by providing an original AnimationCurve.
C#
26
star
16

UnitySystemVisualizer

Unity ECS's system visualizer
C#
22
star
17

UnitySendMessageEfficiencyTest

UnitySendMessage is crucial for calling back from native to C#. How can we make it the fastest?
C#
18
star
18

GoogleSheetsUnity

Get data from your private Google Sheets to Unity!! (Read-only)
C#
17
star
19

UnityTextureMemoryResearch

A project for trying out Unity's behaviour of texture memory loading/unloading. Read : https://gametorrahod.com/unity-texture-memory-loading-unloading-7054819e4ae8
C#
17
star
20

UnityNativeDrumPad

This project show the lowest latency way I could think of in making an iOS/Android musical app in Unity.
C#
15
star
21

UnoSDVXCon

Sound Voltex/K-Shoot Mania Controller Script for an Arduino Uno
CSS
14
star
22

MacbookUbuntuTrackpad

Makes Macbook's Force Touch Trackpad less annoying in dual booted Ubuntu.
13
star
23

Firestorm

Makeshift Cloud Firestore C# API that works with Unity via REST API. Contains only basic functions.
C#
13
star
24

UILesson

Unity UI Animation Lesson - From KU Game Dev Class 14/03/2017
C#
13
star
25

IntBoolFloat

Hybrid Unity ECS benchmark against classic GameObject Update
C#
12
star
26

GrooveCoasterController

Experimental app to use your Android device as a wireless controller for playing Groove Coaster for Steam (https://store.steampowered.com/app/744060/Groove_Coaster/)
Kotlin
11
star
27

ink-VN

Unity Visual Novel extension for ink language (https://github.com/inkle/ink) - in development
C#
10
star
28

RichUp

Preprocess Markdown in TMP_Text into rich text.
C#
10
star
29

EcsGadgets

Random ECS stuff for sharing across projects.
C#
8
star
30

arkham-starter

TypeScript
7
star
31

spine-unity-upm

C#
5
star
32

UniRx.Async-UPM

neuecc's UniRx.Async UPM pullable version
C#
5
star
33

FirebaseUPM

Firebase Unity SDK in UPM format.
Python
4
star
34

monokai-blender

A Blender theme for programmers
4
star
35

NewInputLatencyTest

Unity's new input system latency test
C#
4
star
36

AndroidNotchCutoutDatabase

A database of all Android phones with a cutout/notch.
3
star
37

AndroidPlayAudio

Plays an audio with SoundPool & AudioTrack when you touch the button.
Java
2
star
38

UnityTapSound

Tap the screen then it will sound. A simple project to test the audio latency.
C++
2
star
39

superagent-graphql

Shortcut plugin for making a GraphQL query with SuperAgent
JavaScript
2
star
40

yarnv2-react-test

An example project structure that test React hooks separately with yarn v2 workspace.
TypeScript
2
star
41

resource-script

Resource Script is a programmer-centric data storage format that looks like TypeScript code.
TypeScript
2
star
42

bms-preview-maker

Make a preview.ogg file from .bms and measure range
CSS
1
star
43

ProtobufIL2CPP2018

A project to test Protobuf with Unity
C#
1
star
44

AssetStoreExtensions

1
star
45

bms-portfolio

A playable BMS portfolio (soon to be forkable to make your own but please wait until I can do it myself first..)
Shell
1
star
46

NativeTouchAndroidStudio

CMake
1
star
47

SarCrypt

Stupid text encryption
C#
1
star
48

sveltekit_stories_watcher

TypeScript
1
star
49

iOSSoundTest

SoundTest - An XCode project that uses AVAudioPlayer to play sound on tap.
Objective-C
1
star