Unity Tube Extrusion by FedericoGarciaGarcia - 1

Extensions

Tube extrusion generation for Unity from OBJ polyline files and Vector3 arrays

Unknown VersionGNU General Public License v3.0Updated 17 days agoCreated on June 4th, 2020
Go to source

UnityTubeExtrusion

Tube extrusion for polylines in Unity.

Cover

Features

  • Extrusion from OBJ polyline file.
  • Extrusion from Vector3 array.
  • Coloring and texturing.
  • Resolution.
  • Decimation.
  • Multithreading.
  • Realtime.
  • End capping.
  • Load data from URL.

Installing

Download this repository and manually import files in Unity.

How to use

Data

Data must be Vector3 [][]; an array of polylines, where each point is a Vector3.

OBJ File

OBJ files do not need to be included in Unity’s Asset or Resources folder. URLs can also be used.

  1. Create a GameObject and Attach a TubeGeneratorFromObj script to it.
  2. Set the .obj’s file path or URL in TubeGeneratorFromObj’s filePath variable.
  3. Tube extrusion will commence automatically.

The .obj file used is located in the Resources folder.

Vector3 Array

As an example, a simple cylinder will be generated through code from a 2-point polyline.

  1. Create a new script in Unity, and make it inherit TubeGenerator.
public class MyTubeGenerator : TubeGenerator
  1. Create an array of Vector3 arrays of size 1; make the first element (polyline) of this array a new Vector3 of size 2. Give each point a position.
Vector3 [][] polylines = new Vector3[1][];
polylines[0] = new Vector3[2];
polylines[0][0] = new Vector3(0, -1, 0);
polylines[0][1] = new Vector3(0, 1, 0);
  1. Pass the array to Generate() to begin the tube extrusion.
Generate(polylines);
  1. Any properties may be changed from Unity’s Editor or through code. For example, to change the tube radius to 0.5:
radius = 0.5f;

The full code is:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public class ExampleCylinder : TubeGenerator
{
	void Start()
	{
		Vector3 [][] polylines = new Vector3[1][];
		polylines[0] = new Vector3[2];
		polylines[0][0] = new Vector3(0, -1, 0);
		polylines[0][1] = new Vector3(0, 1, 0);
		
		radius = 0.5f;
		
		Generate(polylines);
	}
}

TubeGenerator properties

Before tube extrusion, the following properties may be specified:

  • Deque size: how many tubes are to be sent to the GPU each frame after extrusion. If value is too high, there will be a performance hit. 50 by default.
  • Decimation: beween 0 and 1. Percentage of points to be used in each polyline. 0 means all points are used, 0.5 means only half the points are used, etc. Points are evenly skipped. Useful for dense polylines. 0 by default.
  • Scale: rescaling of the polylines. 1 by default (no rescaling).
  • Radius: the tube ‘thickness’. 1 by default.
  • Resolution: number of sides in each tube. 3 by default.
  • Material: texture and shader material. None by default.
  • Color Start: the color of the first tube. White by default.
  • Color End: the color of the last tube; other tubes will be interpolated between Color Start and Color End. White by default.
  • Threads: how many threads to use for tube extrusion. It is not recommended to use all available CPU threads, as Unity uses one for the Main thread. 1 by default.

Showcase

Extrusion from OBJ polyline file

Obj https://raw.githubusercontent.com/FedericoGarciaGarcia/UnityTubeExtrusion/master/Images/Corpus callosum AO.jpg

Extrusion from Vector3 array

Array

Coloring and texturing

Texturing

Resolution

Resolution

Decimation

Decimation

Realtime

Realtime

Authors

  • Federico Garcia Garcia

Acknowledgments

Textures and materials taken from:

Show all projects by FedericoGarciaGarcia