public static void GenerateCylinder(int resolution, Mesh mesh, Color32 color0, Color32 color1) { if (cachedGeometries.ContainsKey(resolution)) { cachedGeometries [resolution].SetMesh(mesh, color0, color1); } else { List <Vector3> vertices = new List <Vector3> (); List <int> triangleList = new List <int> (); int divisions = 4 * (int)Mathf.Pow(2, resolution); for (int division = 0; division < divisions; division++) { float angle = 2 * Mathf.PI * division / divisions; float x = Mathf.Cos(angle); float y = Mathf.Sin(angle); //Add vertices vertices.Add(new Vector3(x, y, -0.5f)); vertices.Add(new Vector3(x, y, 0.5f)); int v0 = division * 2; int v1 = division * 2 + 1; int v2 = (division == divisions - 1 ? 0 : division * 2 + 2); int v3 = (division == divisions - 1 ? 1 : division * 2 + 3); triangleList.Add(v0); triangleList.Add(v2); triangleList.Add(v1); triangleList.Add(v1); triangleList.Add(v2); triangleList.Add(v3); } Vector3[] verts = vertices.ToArray(); CylinderGeometry cylinder = new CylinderGeometry(triangleList.ToArray(), verts, verts); cylinder.SetMesh(mesh, color0, color1); cachedGeometries [resolution] = cylinder; } }
private CylinderGeometry GetGeometry() { var radius = RadiusUpDown.Value ?? 5; var length = LengthUpDown.Value ?? 100; var resolution = ResolutionUpDown.Value ?? 5; var span = TimeUpDown.Value ?? 100; var interval = TimeIntervalUpDown.Value ?? 10; var flux = (FluxUpDown.Value ?? 1) * Math.Pow(10, 15); var count = TrialCountUpDown.Value ?? 100000; var cylinder = new CylinderReflector(0, Vector.Zero, Vector.Forward, length, radius); var bottom = new Shield(1, Vector.Forward * length, Vector.Forward, radius); var geometry = new CylinderGeometry(cylinder, bottom, null, span, interval, flux, resolution, (int)count, 100, Atom.ReflectionPattern.Specularly); return(geometry); }
internal static CylinderGeometry BakeToBodySpace( this CylinderGeometry cylinder, float4x4 localToWorld, float4x4 shapeToWorld, EulerAngles orientation ) { using (var geometry = new NativeArray <CylinderGeometry>(1, Allocator.TempJob) { [0] = cylinder }) { var job = new BakeCylinderJob { Cylinder = geometry, localToWorld = localToWorld, shapeToWorld = shapeToWorld, orientation = orientation }; job.Run(); return(geometry[0]); } }
public void Execute() { var center = Cylinder[0].Center; var height = Cylinder[0].Height; var radius = Cylinder[0].Radius; var size = new float3(radius * 2f, radius * 2f, height); var basisToWorld = GetBasisToWorldMatrix(localToWorld, center, orientation, size); var basisPriority = k_DefaultAxisPriority; var sheared = localToWorld.HasShear(); if (localToWorld.HasNonUniformScale() || sheared) { if (sheared) { basisPriority = GetBasisAxisPriority(basisToWorld); } MakeZAxisPrimaryBasis(ref basisPriority); } var bakeToShape = GetPrimitiveBakeToShapeMatrix(localToWorld, shapeToWorld, ref center, ref orientation, 1f, basisPriority); height *= math.length(bakeToShape.c2); radius *= math.max(math.length(bakeToShape.c0), math.length(bakeToShape.c1)); var s = new float3( math.length(bakeToShape[basisPriority[2]]), math.length(bakeToShape[basisPriority[1]]), math.length(bakeToShape[basisPriority[0]]) ); Cylinder[0] = new CylinderGeometry { Center = center, Orientation = orientation, Height = height, Radius = radius, BevelRadius = math.min(Cylinder[0].BevelRadius * math.cmax(s / size), height * 0.5f), SideCount = Cylinder[0].SideCount }; }
public void Execute() { var center = Cylinder[0].Center; var height = Cylinder[0].Height; var radius = Cylinder[0].Radius; var bevelRadius = Cylinder[0].BevelRadius; var bakeToShape = GetBakeToShape(localToWorld, shapeToWorld, ref center, ref orientation); var scale = bakeToShape.DecomposeScale(); height *= scale.z; radius *= math.cmax(scale.xy); Cylinder[0] = new CylinderGeometry { Center = center, Orientation = orientation, Height = height, Radius = radius, BevelRadius = math.min(bevelRadius, math.min(height * 0.5f, radius)), SideCount = Cylinder[0].SideCount }; }
/// <summary> /// /// </summary> /// <param name="control"></param> public override void Load(Control control) { base.Load(control); camera = new PerspectiveCamera(40, control.Width / (float)control.Height, 1, 15000); this.camera.Position.Z = 3200; scene = new Scene(); sphere = new Mesh(new SphereGeometry(100, 20, 20), new MeshNormalMaterial(new Hashtable { { "shading", Three.SmoothShading } })); scene.Add(sphere); var geometry = new CylinderGeometry(0, 10, 100, 3); geometry.ApplyMatrix(new Matrix4().MakeRotationFromEuler(new Euler((float)Math.PI / 2, (float)Math.PI, 0))); var material = new MeshNormalMaterial(); for (var i = 0; i < 1000; i++) { var mesh = new Mesh(geometry, material); mesh.Position.X = Mat.Random() * 4000 - 2000; mesh.Position.Y = Mat.Random() * 4000 - 2000; mesh.Position.Z = Mat.Random() * 4000 - 2000; mesh.Scale.X = mesh.Scale.Y = mesh.Scale.Z = Mat.Random() * 4 + 2; scene.Add(mesh); } scene.MatrixAutoUpdate = false; renderer.SetClearColor(Color.White); renderer.SortObjects = false; }
public static unsafe BlobAssetReference <Collider> GenerateRandomConvex(ref Random rnd) { ColliderType colliderType = (ColliderType)rnd.NextInt((int)ColliderType.Cylinder + 1); float convexRadius = (rnd.NextInt(4) > 0) ? rnd.NextFloat(0.5f) : 0.0f; switch (colliderType) { case ColliderType.Convex: { int numPoints = rnd.NextInt(1, 16); if (numPoints == 3) // TODO - hull builder doesn't build faces for flat shapes, work around it for now to run the test { numPoints++; } var points = new NativeArray <float3>(numPoints, Allocator.TempJob); for (int i = 0; i < numPoints; i++) { points[i] = rnd.NextFloat3(-1.0f, 1.0f); } var generationParameters = ConvexHullGenerationParameters.Default; generationParameters.BevelRadius = convexRadius; var collider = ConvexCollider.Create(points, generationParameters, CollisionFilter.Default); points.Dispose(); return(collider); } case ColliderType.Sphere: { return(SphereCollider.Create(new SphereGeometry { Center = (rnd.NextInt(4) > 0) ? float3.zero : rnd.NextFloat3(-0.5f, 0.5f), Radius = rnd.NextFloat(0.01f, 0.5f) })); } case ColliderType.Capsule: { float3 point0 = rnd.NextFloat3(0.0f, 1.0f); float3 point1 = (rnd.NextInt(4) > 0) ? -point0 : rnd.NextFloat3(-1.0f, 1.0f); return(CapsuleCollider.Create(new CapsuleGeometry { Vertex0 = point0, Vertex1 = point1, Radius = rnd.NextFloat(0.01f, 0.5f) })); } case ColliderType.Triangle: { return(PolygonCollider.CreateTriangle(rnd.NextFloat3(-1.0f, 1.0f), rnd.NextFloat3(-1.0f, 1.0f), rnd.NextFloat3(-1.0f, 1.0f))); } case ColliderType.Quad: { // Pick 3 totally random points, then choose a fourth that makes a flat and convex quad float3 point0 = rnd.NextFloat3(-1.0f, 1.0f); float3 point1 = rnd.NextFloat3(-1.0f, 1.0f); float3 point3 = rnd.NextFloat3(-1.0f, 1.0f); float t0 = rnd.NextFloat(0.0f, 1.0f); float t1 = rnd.NextFloat(0.0f, 1.0f); float3 e = point1 + point1 - point0; float3 a = math.lerp(point1, e, t0); float3 b = math.lerp(point3, point3 + point3 - point0, t0); float3 point2 = math.lerp(a, b, t1); return(PolygonCollider.CreateQuad(point0, point1, point2, point3)); } case ColliderType.Box: { float minSize = 0.05f; // TODO - work around hull builder problems with small faces, sometimes doesn't extend 1D->2D based on face area var boxGeometry = new BoxGeometry { Center = (rnd.NextInt(4) > 0) ? float3.zero : rnd.NextFloat3(-0.5f, 0.5f), Orientation = (rnd.NextInt(4) > 0) ? quaternion.identity : rnd.NextQuaternionRotation(), Size = rnd.NextFloat3(minSize, 1.0f) }; float maxBevelRadius = math.max(math.cmin((boxGeometry.Size - minSize) / (2.0f * (1.0f + float.Epsilon))), 0.0f); boxGeometry.BevelRadius = math.min(maxBevelRadius, convexRadius); return(BoxCollider.Create(boxGeometry)); } case ColliderType.Cylinder: { float minSize = 0.01f; // TODO - cylinder gets degenerate faces if radius-convexRadius=0 or height/2-convexRadius=0, decide how to handle this in CylinderCollider var cylinderGeometry = new CylinderGeometry { Center = (rnd.NextInt(4) > 0) ? float3.zero : rnd.NextFloat3(-0.5f, 0.5f), Orientation = (rnd.NextInt(4) > 0) ? quaternion.identity : rnd.NextQuaternionRotation(), Height = rnd.NextFloat(2.0f * minSize, 1f), Radius = rnd.NextFloat(minSize, 1.0f), SideCount = 20 }; var maxBevelRadius = math.max(math.min(cylinderGeometry.Height / 2, cylinderGeometry.Radius) - minSize, 0.0f); cylinderGeometry.BevelRadius = math.min(maxBevelRadius, convexRadius); return(CylinderCollider.Create(cylinderGeometry)); } default: throw new NotImplementedException(); } }
// Use this for initialization void Start() { //geo = new CylinderGeometry(10,10,10, 12,1); geo = new CylinderGeometry(25, 75, 100, 40, 5, false); meshFilter.mesh = geo.CreateAndGetMesh(); }