Skip to content

Commit

Permalink
Add Top/Bottom Radius parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
keijiro committed Dec 12, 2020
1 parent 9e201cb commit 6495868
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions Packages/jp.keijiro.metamesh/Editor/Cylinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
using System.Linq;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Serialization;
using Unity.Mathematics;

namespace Metamesh {

[System.Serializable]
public sealed class Cylinder
{
public float Radius = 1;
[FormerlySerializedAs("Radius")] public float TopRadius = 1;
[FormerlySerializedAs("Radius")] public float BottomRadius = 1;
public float Height = 1;
public uint Columns = 24;
public uint Rows = 12;
Expand All @@ -31,6 +33,10 @@ public void Generate(Mesh mesh)
va[(ai + 0) % 3] = 1;
vx[(ai + 1) % 3] = 1;

// Normal vector for the first vertex
var edge = (TopRadius - BottomRadius) * vx + Height * va;
var n0 = math.normalize(math.cross(math.cross(va, vx), edge));

// Vertex array
var vtx = new List<float3>();
var nrm = new List<float3>();
Expand All @@ -44,9 +50,10 @@ public void Generate(Mesh mesh)
var u = (float)ix / res.x;
var v = (float)iy / res.y;

var r = math.lerp(BottomRadius, TopRadius, v);
var rot = quaternion.AxisAngle(va, u * math.PI * -2);
var n = math.mul(rot, vx);
var p = n * Radius + va * (v - 0.5f) * Height;
var n = math.mul(rot, n0);
var p = math.mul(rot, vx) * r + va * (v - 0.5f) * Height;

vtx.Add(p);
nrm.Add(n);
Expand All @@ -71,10 +78,10 @@ public void Generate(Mesh mesh)
var u = (float)ix / res.x * math.PI * 2;

var rot = quaternion.AxisAngle(va, -u);
var p = math.mul(rot, vx) * Radius;
var p = math.mul(rot, vx);

vtx.Add(p + va * Height / -2);
vtx.Add(p + va * Height / +2);
vtx.Add(p * BottomRadius + va * Height / -2);
vtx.Add(p * TopRadius + va * Height / +2);

nrm.Add(-va);
nrm.Add(+va);
Expand Down

0 comments on commit 6495868

Please sign in to comment.