/// <summary> /// re/generate mesh geometry based on parameters /// </summary> /// <param name="radius0">fist radius of cone</param> /// <param name="radius1">second radius of cone</param> /// <param name="height">height of cone</param> /// <param name="sides">number of triangle segments in radius</param> /// <param name="heightSegments">number of triangle segments in height</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> /// <returns>Cone class with Cone game object</returns> public void GenerateGeometry(float radius0, float radius1, float thickness, float height, int sides, int heightSegments, NormalsType normalsType, PivotPosition pivotPosition) { // generate new mesh and clear old one var meshFilter = GetComponent<MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; // generate geometry if (thickness >= 0) { GenerationTimeMS = Primitives.HollowConePrimitive.GenerateGeometry(mesh, radius0, radius1, thickness, height, sides, heightSegments, normalsType, pivotPosition); } else { GenerationTimeMS = Primitives.ConePrimitive.GenerateGeometry(mesh, radius0, radius1, height, sides, heightSegments, normalsType, pivotPosition); } this.radius0 = radius0; this.radius1 = radius1; this.height = height; this.thickness = thickness; this.sides = sides; this.heightSegments = heightSegments; this.normalsType = normalsType; this.flipNormals = false; this.pivotPosition = pivotPosition; }
/// <summary> /// re/generate mesh geometry based on parameters /// </summary> /// <param name="radius0">fist radius of tube</param> /// <param name="radius1">second radius of tube</param> /// <param name="torusSegments">number of triangle of torusKnot</param> /// <param name="coneSegments">number of triangle of torusKnot cone</param> /// <param name="P">knot parameter</param> /// <param name="Q">knot parameter</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public void GenerateGeometry(float radius0, float radius1, int torusSegments, int coneSegments, int P, int Q, NormalsType normalsType, PivotPosition pivotPosition) { // generate new mesh and clear old one var meshFilter = GetComponent<MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; // generate geometry GenerationTimeMS = Primitives.TorusKnotPrimitive.GenerateGeometry(mesh, radius0, radius1, torusSegments, coneSegments, P, Q, normalsType, pivotPosition); this.radius0 = radius0; this.radius1 = radius1; this.torusSegments = torusSegments; this.coneSegments = coneSegments; this.P = P; this.Q = Q; this.normalsType = normalsType; this.flipNormals = false; this.pivotPosition = pivotPosition; }
/// <summary> /// Creates the geometry. /// </summary> protected void CreateGeometry(float radius0, float radius1, float thickness, float height, int sides, int heightSegments, NormalsType normalsType, PivotPosition pivotPosition) { // Clear the old mesh and generate a new one. var meshFilter = GetComponent<MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; _meshGenerationTime = ShapesFactory.CreateDiamond(mesh, radius0, radius1, height, sides, heightSegments, normalsType, pivotPosition); }
public ElementPanel(ModelPanel modelPanel, PivotPosition position) { _modelPanel = modelPanel; Position = position; BackColor = Color.White; DragEnter += new DragEventHandler(onDragEnter); DragOver += new DragEventHandler(onDragOver); DragDrop += new DragEventHandler(onDragDrop); Paint += new PaintEventHandler(ElementPanel_Paint); AutoScroll = true; AllowDrop = true; }
/// <summary> /// create Capsule game object /// </summary> /// <param name="radius">radius of capsule</param> /// <param name="sides">number of segments</param> /// <param name="heightSegments">number of segments of central cylinder</param> /// <param name="normals">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> /// <returns>Capsule class with Capsule game object</returns> public static Capsule Create(float radius, float height, int sides, int heightSegments, bool preserveHeight, NormalsType normals, PivotPosition pivotPosition) { var capsuleObject = new GameObject("CapsulePro"); capsuleObject.AddComponent<MeshFilter>(); var renderer = capsuleObject.AddComponent<MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var capsule = capsuleObject.AddComponent<Capsule>(); capsule.GenerateGeometry(radius, height, sides, heightSegments, preserveHeight, normals, pivotPosition); return capsule; }
/// <summary> /// create Cone game object /// </summary> /// <param name="radius0">first radius of cone</param> /// <param name="radius1">second radius of cone</param> /// <param name="thickness">thickness</param> /// <param name="height">height of cone</param> /// <param name="sides">number of triangle segments in radius direction</param> /// <param name="heightSegments">number of triangle segments in height direction</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> /// <returns>Cone class with Cone game object</returns> public static Cone Create(float radius0, float radius1, float thickness, float height, int sides, int heightSegments, NormalsType normalsType, PivotPosition pivotPosition) { var cylinderObject = new GameObject("ConePro"); cylinderObject.AddComponent<MeshFilter>(); var renderer = cylinderObject.AddComponent<MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var cone = cylinderObject.AddComponent<Cone>(); cone.GenerateGeometry(radius0, radius1, thickness, height, sides, heightSegments, normalsType, pivotPosition); return cone; }
/// <summary> /// create Box game object /// </summary> /// <param name="width">width of cube</param> /// <param name="height">height of cube</param> /// <param name="depth">depth of cube</param> /// <param name="widthSegments">number of triangle segments in width direction</param> /// <param name="heightSegments">number of triangle segments in height direction</param> /// <param name="depthSegments">number of triangle segments in depth direction</param> /// <param name="pivot">position of the model pivot</param> /// <param name="cubeMap">enable 6-sides cube map uv mapping</param> /// <returns>Box class with Box game object</returns> public static Box Create(float width, float height, float depth, int widthSegments, int heightSegments, int depthSegments, bool cubeMap, PivotPosition pivot) { var planeObject = new GameObject("BoxPro"); planeObject.AddComponent<MeshFilter>(); var renderer = planeObject.AddComponent<MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var cube = planeObject.AddComponent<Box>(); cube.GenerateGeometry(width, height, depth, widthSegments, heightSegments, depthSegments, cubeMap, pivot); return cube; }
/// <summary> /// create TorusKnot game object /// </summary> /// <param name="radius0">first radius of tube</param> /// <param name="radius1">second radius of tube</param> /// <param name="torusSegments">number of triangle segments of torusKnot</param> /// <param name="coneSegments">number of triangle segments or torusKnot cone</param> /// <returns>TorusKnot class with TorusKnot game object</returns> /// <param name="P">knot parameter</param> /// <param name="Q">knot parameter</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public static TorusKnot Create(float radius0, float radius1, int torusSegments, int coneSegments, int P, int Q, NormalsType normalsType, PivotPosition pivotPosition) { var cylinderObject = new GameObject("TorusKnotPro"); cylinderObject.AddComponent<MeshFilter>(); var renderer = cylinderObject.AddComponent<MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var torusKnot = cylinderObject.AddComponent<TorusKnot>(); torusKnot.GenerateGeometry(radius0, radius1, torusSegments, coneSegments, P, Q, normalsType, pivotPosition); return torusKnot; }
/// <summary> /// create RoundedCube game object /// </summary> /// <param name="width">width of the cube</param> /// <param name="height">height of the cube</param> /// <param name="length">length of the cube</param> /// <param name="segments">number of segments</param> /// <param name="roundness">roudness coefficient</param> /// <param name="normals">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> /// <returns>RoundedCube class with RoundedCube game object</returns> public static RoundedCube Create(float width, float height, float length, int segments, float roundness, NormalsType normals, PivotPosition pivotPosition) { var sphereObject = new GameObject("RoundedCubePro"); sphereObject.AddComponent<MeshFilter>(); var renderer = sphereObject.AddComponent<MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var superellipsoid = sphereObject.AddComponent<RoundedCube>(); superellipsoid.GenerateGeometry(width, height, length, segments, roundness, normals, pivotPosition); return superellipsoid; }
/// <summary> /// create Tube game object /// </summary> /// <param name="radius0">first radius of tube</param> /// <param name="radius1">second radius of tube</param> /// <param name="height">height of tube</param> /// <param name="sides">number of triangle segments in radius direction</param> /// <param name="heightSegments">number of triangle segments in height direction</param> /// <returns>Tube class with Tube game object</returns> /// <param name="slice">slicing parameter</param> /// <param name="radialMapping">using radial uv mapping on the top/bottom of the tube</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public static Tube Create(float radius0, float radius1, float height, int sides, int heightSegments, float slice, bool radialMapping, NormalsType normalsType, PivotPosition pivotPosition) { var cylinderObject = new GameObject("TubePro"); cylinderObject.AddComponent<MeshFilter>(); var renderer = cylinderObject.AddComponent<MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var tube = cylinderObject.AddComponent<Tube>(); tube.GenerateGeometry(radius0, radius1, height, sides, heightSegments, slice, radialMapping, normalsType, pivotPosition); return tube; }
/// <summary> /// create Sphere game object /// </summary> /// <param name="radius">radius of sphere</param> /// <param name="segments">number of segments</param> /// <param name="hemisphere">hemisphere, 0 ... complete sphere, 0.5 ... half-sphere</param> /// <param name="normals">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> /// <param name="innerRadius">radius of the inner sphere</param> /// <param name="slice">vertical slice parameter</param> /// <returns>Sphere class with Sphere game object</returns> public static Sphere Create(float radius, int segments, float hemisphere, float innerRadius, float slice, NormalsType normals, PivotPosition pivotPosition) { var sphereObject = new GameObject("SpherePro"); sphereObject.AddComponent<MeshFilter>(); var renderer = sphereObject.AddComponent<MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var sphere = sphereObject.AddComponent<Sphere>(); sphere.GenerateGeometry(radius, segments, hemisphere, innerRadius, slice, normals, pivotPosition); return sphere; }
/// <summary> /// create SuperEllipsoid game object /// </summary> /// <param name="width">width of the cube</param> /// <param name="height">height of the cube</param> /// <param name="length">length of the cube</param> /// <param name="segments">number of segments</param> /// <param name="normals">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> /// <param name="n1">first parameter</param> /// <param name="n2">second parameter</param> /// <returns>SuperEllipsoid class with SuperEllipsoid game object</returns> public static SuperEllipsoid Create(float width, float height, float length, int segments, float n1, float n2, NormalsType normals, PivotPosition pivotPosition) { var sphereObject = new GameObject("SuperEllipsoidPro"); sphereObject.AddComponent<MeshFilter>(); var renderer = sphereObject.AddComponent<MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var roundedCube = sphereObject.AddComponent<SuperEllipsoid>(); roundedCube.GenerateGeometry(width, height, length, segments, n1, n2, normals, pivotPosition); return roundedCube; }
/// <summary> /// create GeoSphere game object /// </summary> /// <param name="radius">radius of sphere</param> /// <param name="subdivision">number of subdivision</param> /// <param name="baseType">type of generation primitive</param> /// <param name="normals">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> /// <returns>GeoSphere class with GeoSphere game object</returns> public static GeoSphere Create(float radius, int subdivision, Primitives.GeoSpherePrimitive.BaseType baseType, NormalsType normals, PivotPosition pivotPosition) { var sphereObject = new GameObject("GeoSpherePro"); sphereObject.AddComponent<MeshFilter>(); var renderer = sphereObject.AddComponent<MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var sphere = sphereObject.AddComponent<GeoSphere>(); sphere.GenerateGeometry(radius, subdivision, baseType, normals, pivotPosition); return sphere; }
/// <summary> /// create Cylinder game object /// </summary> /// <param name="radius">radius of cylinder</param> /// <param name="height">height of cylinder</param> /// <param name="sides">number of triangle segments in radius direction</param> /// <param name="heightSegments">number of triangle segments in height direction</param> /// <returns>Cylinder class with Cylinder game object</returns> /// <param name="pivotPosition">position of the model pivot</param> /// <param name="normals">type of normals to be generated</param> public static Cylinder Create(float radius, float height, int sides, int heightSegments, NormalsType normals, PivotPosition pivotPosition) { var cylinderObject = new GameObject("CylinderPro"); cylinderObject.AddComponent<MeshFilter>(); var renderer = cylinderObject.AddComponent<MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var cylinder = cylinderObject.AddComponent<Cylinder>(); cylinder.GenerateGeometry(radius, height, sides, heightSegments, normals, pivotPosition); return cylinder; }
/// <summary> /// create Pyramid game object /// </summary> /// <param name="width">width of pyramid</param> /// <param name="height">height of pyramid</param> /// <param name="depth">depth of pyramid</param> /// <param name="widthSegments">number of triangle segments in width direction</param> /// <param name="heightSegments">number of triangle segments in height direction</param> /// <param name="depthSegments">number of triangle segments in depth direction</param> /// <param name="pivotPosition">position of the model pivot</param> /// <param name="pyramidMap">enable pyramid map uv mapping</param> /// <returns>Pyramid class with Pyramid game object</returns> public static Pyramid Create(float width, float height, float depth, int widthSegments, int heightSegments, int depthSegments, bool pyramidMap, PivotPosition pivotPosition) { var planeObject = new GameObject("PyramidPro"); planeObject.AddComponent<MeshFilter>(); var renderer = planeObject.AddComponent<MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var pyramid = planeObject.AddComponent<Pyramid>(); pyramid.GenerateGeometry(width, height, depth, widthSegments, heightSegments, depthSegments, pyramidMap, pivotPosition); return pyramid; }
/// <summary> /// create Arc game object /// </summary> /// <param name="width">width of cube</param> /// <param name="height1">height1 of cube</param> /// <param name="height2">height2 of cube</param> /// <param name="depth">depth of cube</param> /// <param name="arcSegments">depth of the </param> /// <param name="pivot">position of the model pivot</param> /// <returns>Arc class with Arc game object</returns> public static Arc Create(float width, float height1, float height2, float depth, int arcSegments, PivotPosition pivot) { var planeObject = new GameObject("ArcPro"); planeObject.AddComponent<MeshFilter>(); var renderer = planeObject.AddComponent<MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var cube = planeObject.AddComponent<Arc>(); cube.gizmo = ArcGizmo.Create(); cube.gizmo.transform.parent = planeObject.transform; cube.GenerateGeometry(width, height1, height2, depth, arcSegments, pivot); return cube; }
/// <summary> /// create GeoSphere game object /// </summary> /// <param name="radius">radius of sphere</param> /// <param name="subdivision">number of subdivision</param> /// <param name="baseType">type of generation primitive</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public void GenerateGeometry(float radius, int subdivision, Primitives.GeoSpherePrimitive.BaseType baseType, NormalsType normalsType, PivotPosition pivotPosition) { // generate new mesh and clear old one var meshFilter = GetComponent<MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; // geneate geometry GenerationTimeMS = Primitives.GeoSpherePrimitive.GenerateGeometry(mesh, radius, subdivision, baseType, normalsType, pivotPosition); this.radius = radius; this.subdivision = subdivision; this.baseType = baseType; this.normalsType = normalsType; this.flipNormals = false; this.pivotPosition = pivotPosition; }
/// <summary> /// create SphericalCone game object /// </summary> /// <param name="radius">radius of sphere</param> /// <param name="segments">number of segments</param> /// <param name="coneAngle">angle of conus in DEG, 360 ... complete sphere, 180 ... half-sphere</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public void GenerateGeometry(float radius, int segments, float coneAngle, NormalsType normalsType, PivotPosition pivotPosition) { // generate new mesh and clear old one var meshFilter = GetComponent<MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; // generate geometry GenerationTimeMS = Primitives.SphericalConePrimitive.GenerateGeometry(mesh, radius, segments, coneAngle, normalsType, pivotPosition); this.radius = radius; this.segments = segments; this.coneAngle = coneAngle; this.normalsType = normalsType; this.flipNormals = false; this.pivotPosition = pivotPosition; }
/// <summary> /// create Ellipsoid game object /// </summary> /// <param name="width">width of ellipsoid</param> /// <param name="height">height of ellipsoid</param> /// <param name="length">length of ellipsoid</param> /// <param name="segments">number of segments</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public void GenerateGeometry(float width, float height, float length, int segments, NormalsType normalsType, PivotPosition pivotPosition) { // generate new mesh and clear old one var meshFilter = GetComponent<MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; // generate geometry GenerationTimeMS = Primitives.EllipsoidPrimitive.GenerateGeometry(mesh, width, height, length, segments, normalsType, pivotPosition); this.width = width; this.height = height; this.length = length; this.segments = segments; this.normalsType = normalsType; this.flipNormals = false; this.pivotPosition = pivotPosition; }
/// <summary> /// re/generate mesh geometry based on parameters /// </summary> /// <param name="radius">radius of cylinder</param> /// <param name="height">height of cylinder</param> /// <param name="sides">number of triangle segments in radius</param> /// <param name="heightSegments">number of triangle segments in height</param> /// <param name="pivotPosition">position of the model pivot</param> /// <param name="normalsType">type of normals to be generated</param> public void GenerateGeometry(float radius, float height, int sides, int heightSegments, NormalsType normalsType, PivotPosition pivotPosition) { // generate new mesh and clear old one var meshFilter = GetComponent<MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; // generate cone with same radiuses GenerationTimeMS = Primitives.ConePrimitive.GenerateGeometry(mesh, radius, radius, height, sides, heightSegments, normalsType, pivotPosition); this.radius = radius; this.height = height; this.sides = sides; this.heightSegments = heightSegments; this.normalsType = normalsType; this.flipNormals = false; this.pivotPosition = pivotPosition; }
/// <summary> /// re/generate mesh geometry based on parameters /// </summary> /// <param name="width">width of cube</param> /// <param name="height1">height1 of cube</param> /// <param name="height2">height2 of cube</param> /// <param name="depth">depth of cube</param> /// <param name="arcSegments">depth of the </param> /// <param name="pivot">position of the model pivot</param> public void GenerateGeometry(float width, float height1, float height2, float depth, int arcSegments, PivotPosition pivot) { // generate new mesh and clear old one var meshFilter = GetComponent<MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; mesh.Clear(); // generate geometry GenerationTimeMS = Primitives.ArcPrimitive.GenerateGeometry(mesh, width, height1, height2, depth, arcSegments, gizmo.transform.localPosition, pivot); this.width = width; this.height1 = height1; this.height2 = height2; this.depth = depth; this.arcSegments = arcSegments; this.flipNormals = false; this.pivotPosition = pivot; }
/// <summary> /// create Sphere game object /// </summary> /// <param name="radius">radius of sphere</param> /// <param name="segments">number of segments</param> /// <param name="hemisphere">hemisphere, 0 ... complete sphere, 0.5 ... half-sphere</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="innerRadius">radius of the inner sphere</param> /// <param name="pivotPosition">position of the model pivot</param> /// <param name="slice">vertical slice parameter</param> public void GenerateGeometry(float radius, int segments, float hemisphere, float innerRadius, float slice, NormalsType normalsType, PivotPosition pivotPosition) { // generate new mesh and clear old one var meshFilter = GetComponent<MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; // generate geometry GenerationTimeMS = Primitives.SpherePrimitive.GenerateGeometry(mesh, radius, segments, hemisphere, innerRadius, slice, normalsType, pivotPosition); this.radius = radius; this.segments = segments; this.hemisphere = hemisphere; this.normalsType = normalsType; this.flipNormals = false; this.innerRadius = innerRadius; this.slice = slice; this.pivotPosition = pivotPosition; }
/// <summary> /// create Cone game object /// </summary> /// <param name="radius0">first radius of cone</param> /// <param name="radius1">second radius of cone</param> /// <param name="thickness">thickness</param> /// <param name="height">height of cone</param> /// <param name="sides">number of triangle segments in radius direction</param> /// <param name="heightSegments">number of triangle segments in height direction</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> /// <returns>Cone class with Cone game object</returns> public static Cone Create(float radius0, float radius1, float thickness, float height, int sides, int heightSegments, NormalsType normalsType, PivotPosition pivotPosition) { var cylinderObject = new GameObject("ConePro"); cylinderObject.AddComponent <MeshFilter>(); var renderer = cylinderObject.AddComponent <MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var cone = cylinderObject.AddComponent <Cone>(); cone.GenerateGeometry(radius0, radius1, thickness, height, sides, heightSegments, normalsType, pivotPosition); return(cone); }
// Token: 0x06004241 RID: 16961 RVA: 0x00150AE8 File Offset: 0x0014EEE8 public static RoundedCube Create(float width, float height, float length, int segments, float roundness, NormalsType normals, PivotPosition pivotPosition) { GameObject gameObject = new GameObject("RoundedCubePro"); gameObject.AddComponent <MeshFilter>(); MeshRenderer meshRenderer = gameObject.AddComponent <MeshRenderer>(); meshRenderer.sharedMaterial = new Material(Shader.Find("Diffuse")); RoundedCube roundedCube = gameObject.AddComponent <RoundedCube>(); roundedCube.GenerateGeometry(width, height, length, segments, roundness, normals, pivotPosition); return(roundedCube); }
/// <summary> /// create Capsule game object /// </summary> /// <param name="radius">radius of capsule</param> /// <param name="sides">number of segments</param> /// <param name="heightSegments">number of segments of central cylinder</param> /// <param name="normals">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> /// <returns>Capsule class with Capsule game object</returns> public static Capsule Create(float radius, float height, int sides, int heightSegments, bool preserveHeight, NormalsType normals, PivotPosition pivotPosition) { var capsuleObject = new GameObject("CapsulePro"); capsuleObject.AddComponent <MeshFilter>(); var renderer = capsuleObject.AddComponent <MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var capsule = capsuleObject.AddComponent <Capsule>(); capsule.GenerateGeometry(radius, height, sides, heightSegments, preserveHeight, normals, pivotPosition); return(capsule); }
// Token: 0x06004202 RID: 16898 RVA: 0x0014F9F0 File Offset: 0x0014DDF0 public void GenerateGeometry(float radius, float height, int sides, int heightSegments, NormalsType normalsType, PivotPosition pivotPosition) { MeshFilter component = base.GetComponent <MeshFilter>(); if (component.sharedMesh == null) { component.sharedMesh = new Mesh(); } Mesh sharedMesh = component.sharedMesh; base.GenerationTimeMS = ConePrimitive.GenerateGeometry(sharedMesh, radius, radius, height, sides, heightSegments, normalsType, pivotPosition); this.radius = radius; this.height = height; this.sides = sides; this.heightSegments = heightSegments; this.normalsType = normalsType; this.flipNormals = false; this.pivotPosition = pivotPosition; }
/// <summary> /// create RoundedCube game object /// </summary> /// <param name="width">width of the cube</param> /// <param name="height">height of the cube</param> /// <param name="length">length of the cube</param> /// <param name="segments">number of segments</param> /// <param name="roundness">roudness coefficient</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public void GenerateGeometry(float width, float height, float length, int segments, float roundness, NormalsType normalsType, PivotPosition pivotPosition) { // generate new mesh and clear old one var meshFilter = GetComponent <MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; // generate geometry GenerationTimeMS = Primitives.SuperEllipsoidPrimitive.GenerateGeometry(mesh, width, height, length, segments, roundness, roundness, normalsType, pivotPosition); this.width = width; this.height = height; this.length = length; this.segments = segments; this.roundness = roundness; this.normalsType = normalsType; this.flipNormals = false; this.pivotPosition = pivotPosition; }
/// <summary> /// generate mesh geometry for Spherical cone /// /// references: /// http://mathworld.wolfram.com/SphericalCone.html /// http://en.wikipedia.org/wiki/Spherical_sector /// /// </summary> /// <param name="mesh">mesh to be generated</param> /// <param name="radius">radius of sphere</param> /// <param name="segments">number of segments</param> /// <param name="coneAngle">angle of conus in DEG, 360 ... complete sphere, 180 ... half-sphere</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public static float GenerateGeometry(Mesh mesh, float radius, int segments, float coneAngle, NormalsType normalsType, PivotPosition pivotPosition) { var stopWatch = new Stopwatch(); stopWatch.Start(); radius = Mathf.Clamp(radius, 0, 100); segments = Mathf.Clamp(segments, 4, 100); coneAngle = Mathf.Clamp(coneAngle, 0, 360); var hemisphere = 1.0f - (coneAngle/360.0f); mesh.Clear(); int rings = segments - 1; int sectors = segments; var hemisphereCapY = -1 + hemisphere * 2; int hemisphereCapRing = rings; // var hemisphereYpos = -radius; float R = 1 / (float)(rings - 1); float S = 1 / (float)(sectors - 1); // calculate hemisphere parameters // var lastY = 0.0f; for (int r = 0; r < rings; r++) { var y = Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * r * R); if (y < hemisphereCapY) { hemisphereCapRing = r; // hemisphereYpos = lastY * radius; break; } // lastY = y; } int verticesNum = 0; var trianglesNum = (hemisphereCapRing) * (sectors) * 6; var verticesHemisphereNum = segments + 1; var trianglesHemisphereNum = segments * 3; if (hemisphereCapRing == rings) { trianglesNum -= sectors * 3; } if (normalsType == NormalsType.Vertex) { verticesNum = (hemisphereCapRing + 1) * (sectors + 1); } else { verticesNum = trianglesNum; } if (hemisphereCapRing == rings) { verticesNum -= sectors + 1; } var pivotOffset = Vector3.zero; switch (pivotPosition) { case PivotPosition.Botttom: pivotOffset = new Vector3(0.0f, radius, 0.0f); break; case PivotPosition.Top: pivotOffset = new Vector3(0.0f, -radius, 0.0f); break; case PivotPosition.Center: pivotOffset = Vector3.zero; break; } if (verticesNum + verticesHemisphereNum > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return 0.0f; } var vertices = new Vector3[verticesNum + verticesHemisphereNum]; var normals = new Vector3[verticesNum + verticesHemisphereNum]; var uvs = new Vector2[verticesNum + verticesHemisphereNum]; var triangles = new int[trianglesNum + trianglesHemisphereNum]; var vertIndex = 0; var triIndex = 0; for (int r = 0; r < hemisphereCapRing; r++) { var y = -Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * r * R); var sinR = Mathf.Sin(Mathf.PI * r * R); for (int s = 0; s < sectors; s++) { float x = Mathf.Sin(2 * Mathf.PI * s * S) * sinR; float z = Mathf.Cos(2 * Mathf.PI * s * S) * sinR; vertices[vertIndex + 0] = new Vector3(x, y, z) * radius + pivotOffset; normals[vertIndex + 0] = new Vector3(x, y, z); uvs[vertIndex + 0] = new Vector2(1.0f - (s * S), (r * R)); if (r < hemisphereCapRing - 1 && s < sectors - 1) { //543 triangles[triIndex + 5] = (r + 1) * sectors + (s); triangles[triIndex + 4] = r * sectors + (s + 1); triangles[triIndex + 3] = r * sectors + s; //210 triangles[triIndex + 2] = (r + 1) * sectors + (s + 1); triangles[triIndex + 1] = r * sectors + (s + 1); triangles[triIndex + 0] = (r + 1) * sectors + (s); triIndex += 6; } vertIndex += 1; } } var hemisphereVertOffset = vertIndex; // calculate hemisphere plane if (hemisphereCapRing < rings) { // duplicate triangles in face case if (normalsType == NormalsType.Face) { MeshUtils.DuplicateSharedVertices(ref vertices, ref uvs, triangles, triIndex); hemisphereVertOffset = triIndex; } var triVert = hemisphereVertOffset; vertIndex = hemisphereVertOffset; var y = -Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * (hemisphereCapRing - 1) * R); var sinR = Mathf.Sin(Mathf.PI * (hemisphereCapRing - 1) * R); var zeroIndex = 0; for (int i = 0; i < sectors; i++) { var x = Mathf.Sin(2 * Mathf.PI * i * S) * sinR; var z = Mathf.Cos(2 * Mathf.PI * i * S) * sinR; var v = new Vector3(x, y, z); vertices[vertIndex + 0] = v * radius + pivotOffset; // normals[vertIndex + 0] = new Vector3(0.0f, 1.0f, 0.0f); if (i > 0) { normals[vertIndex + 0] = -MeshUtils.ComputePolygonNormal(pivotOffset, vertices[vertIndex], vertices[vertIndex - 1]); if (i == sectors - 1) { normals[zeroIndex] = MeshUtils.ComputePolygonNormal(pivotOffset, vertices[zeroIndex], vertices[zeroIndex+1]); } } else { zeroIndex = vertIndex; } // make planar uv mapping for hemisphere plane var uvV = new Vector2(v.x * 0.5f, v.z * .5f); var uvCenter = new Vector2(0.5f, 0.5f); uvs[vertIndex + 0] = uvCenter + uvV; vertIndex += 1; } vertices[vertIndex + 0] = pivotOffset; normals[vertIndex + 0] = new Vector3(0, 1, 0); uvs[vertIndex + 0] = new Vector2(0.5f, 0.5f); vertIndex += 1; for (int i = 0; i < rings; i++) { triangles[triIndex + 2] = triVert + 0; triangles[triIndex + 1] = vertIndex - 1; if (i == rings - 1) { triangles[triIndex + 0] = hemisphereVertOffset; } else { triangles[triIndex + 0] = triVert + 1; } triIndex += 3; triVert += 1; } } mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; // generate normals by unity in face case if (normalsType == NormalsType.Face) { mesh.RecalculateNormals(); } else { mesh.normals = normals; } mesh.RecalculateBounds(); MeshUtils.CalculateTangents(mesh); mesh.Optimize(); stopWatch.Stop(); return stopWatch.ElapsedMilliseconds; }
// Token: 0x060042EB RID: 17131 RVA: 0x00157B40 File Offset: 0x00155F40 public static float GenerateGeometry(Mesh mesh, float width, float height, float depth, int widthSegments, int heightSegments, int depthSegments, bool cubeMap, float[] edgeOffsets, bool flipUV, PivotPosition pivot) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); width = Mathf.Clamp(width, 0f, 100f); height = Mathf.Clamp(height, 0f, 100f); depth = Mathf.Clamp(depth, 0f, 100f); heightSegments = Mathf.Clamp(heightSegments, 1, 100); widthSegments = Mathf.Clamp(widthSegments, 1, 100); depthSegments = Mathf.Clamp(depthSegments, 1, 100); mesh.Clear(); int num = widthSegments * depthSegments * 6 + widthSegments * heightSegments * 6 + depthSegments * heightSegments * 6; int num2 = (widthSegments + 1) * (depthSegments + 1) + (widthSegments + 1) * (heightSegments + 1) + (depthSegments + 1) * (heightSegments + 1); num *= 2; num2 *= 2; Vector3 zero = Vector3.zero; if (pivot != PivotPosition.Top) { if (pivot == PivotPosition.Botttom) { zero = new Vector3(0f, height / 2f, 0f); } } else { zero = new Vector3(0f, -height / 2f, 0f); } if (num2 > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return(0f); } Vector3[] vertices = new Vector3[num2]; Vector2[] array = new Vector2[num2]; int[] triangles = new int[num]; int num3 = 0; int num4 = 0; Vector3 vector = new Vector3(-width / 2f, zero.y - height / 2f, -depth / 2f); Vector3 vector2 = new Vector3(-width / 2f, zero.y - height / 2f, depth / 2f); Vector3 vector3 = new Vector3(width / 2f, zero.y - height / 2f, depth / 2f); Vector3 vector4 = new Vector3(width / 2f, zero.y - height / 2f, -depth / 2f); Vector3 vector5 = new Vector3(-width / 2f, height / 2f + zero.y, -depth / 2f); Vector3 vector6 = new Vector3(-width / 2f, height / 2f + zero.y, depth / 2f); Vector3 vector7 = new Vector3(width / 2f, height / 2f + zero.y, depth / 2f); Vector3 vector8 = new Vector3(width / 2f, height / 2f + zero.y, -depth / 2f); if (edgeOffsets != null && edgeOffsets.Length > 3) { vector6.x += edgeOffsets[0]; vector5.x += edgeOffsets[0]; vector2.x += edgeOffsets[1]; vector.x += edgeOffsets[1]; vector3.x += edgeOffsets[3]; vector7.x += edgeOffsets[2]; vector4.x += edgeOffsets[3]; vector8.x += edgeOffsets[2]; } BoxPrimitive.CreatePlane(0, vector, vector2, vector3, vector4, widthSegments, depthSegments, cubeMap, ref vertices, ref array, ref triangles, ref num3, ref num4); BoxPrimitive.CreatePlane(1, vector6, vector5, vector8, vector7, widthSegments, depthSegments, cubeMap, ref vertices, ref array, ref triangles, ref num3, ref num4); BoxPrimitive.CreatePlane(2, vector2, vector6, vector7, vector3, widthSegments, heightSegments, cubeMap, ref vertices, ref array, ref triangles, ref num3, ref num4); BoxPrimitive.CreatePlane(3, vector4, vector8, vector5, vector, widthSegments, heightSegments, cubeMap, ref vertices, ref array, ref triangles, ref num3, ref num4); BoxPrimitive.CreatePlane(4, vector, vector5, vector6, vector2, depthSegments, heightSegments, cubeMap, ref vertices, ref array, ref triangles, ref num3, ref num4); BoxPrimitive.CreatePlane(5, vector3, vector7, vector8, vector4, depthSegments, heightSegments, cubeMap, ref vertices, ref array, ref triangles, ref num3, ref num4); if (flipUV) { for (int i = 0; i < array.Length; i++) { array[i].x = 1f - array[i].x; } } mesh.vertices = vertices; mesh.uv = array; mesh.triangles = triangles; mesh.RecalculateNormals(); MeshUtils.CalculateTangents(mesh); mesh.RecalculateBounds(); stopwatch.Stop(); return((float)stopwatch.ElapsedMilliseconds); }
/// <summary> /// re/generate mesh geometry based on parameters /// </summary> /// <param name="width">width of pyramid</param> /// <param name="height">height of pyramid</param> /// <param name="depth">depth of pyramid</param> /// <param name="widthSegments">number of triangle segments in width direction</param> /// <param name="heightSegments">number of triangle segments in height direction</param> /// <param name="depthSegments">number of triangle segments in depth direction</param> /// <param name="pyramidMap">enable pyramid map uv mapping</param> /// <param name="pivotPosition">position of the model pivot</param> public void GenerateGeometry(float width, float height, float depth, int widthSegments, int heightSegments, int depthSegments, bool pyramidMap, PivotPosition pivotPosition) { // generate new mesh and clear old one var meshFilter = GetComponent <MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; // generate geometry GenerationTimeMS = Primitives.PyramidPrimitive.GenerateGeometry(mesh, width, height, depth, widthSegments, heightSegments, depthSegments, pyramidMap, pivotPosition); this.width = width; this.height = height; this.depth = depth; this.widthSegments = widthSegments; this.heightSegments = heightSegments; this.depthSegments = depthSegments; this.flipNormals = false; this.pyramidMap = pyramidMap; this.pivotPosition = pivotPosition; }
/// <summary> /// create Pyramid game object /// </summary> /// <param name="width">width of pyramid</param> /// <param name="height">height of pyramid</param> /// <param name="depth">depth of pyramid</param> /// <param name="widthSegments">number of triangle segments in width direction</param> /// <param name="heightSegments">number of triangle segments in height direction</param> /// <param name="depthSegments">number of triangle segments in depth direction</param> /// <param name="pivotPosition">position of the model pivot</param> /// <param name="pyramidMap">enable pyramid map uv mapping</param> /// <returns>Pyramid class with Pyramid game object</returns> public static Pyramid Create(float width, float height, float depth, int widthSegments, int heightSegments, int depthSegments, bool pyramidMap, PivotPosition pivotPosition) { var planeObject = new GameObject("PyramidPro"); planeObject.AddComponent <MeshFilter>(); var renderer = planeObject.AddComponent <MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var pyramid = planeObject.AddComponent <Pyramid>(); pyramid.GenerateGeometry(width, height, depth, widthSegments, heightSegments, depthSegments, pyramidMap, pivotPosition); return(pyramid); }
// Token: 0x060042EF RID: 17135 RVA: 0x00158264 File Offset: 0x00156664 public static float GenerateGeometry(Mesh mesh, float radius, float height, int sides, int heightSegments, bool preserveHeight, NormalsType normalsType, PivotPosition pivotPosition) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); radius = Mathf.Clamp(radius, 0f, 100f); height = Mathf.Clamp(height, 0f, 100f); heightSegments = Mathf.Clamp(heightSegments, 1, 250); sides = Mathf.Clamp(sides, 4, 250); mesh.Clear(); if (preserveHeight) { height -= radius * 2f; if (height < 0f) { height = 0f; } } int num = sides; int num2 = sides + 1; if ((num & 1) == 0) { num++; num2 = sides + 1; } float num3 = 1f / (float)(num - 1); float num4 = 1f / (float)(num2 - 1); int num5 = num / 2 + 1; int num6 = (num - 1) * (num2 - 1) * 6; int num7 = (sides + 1) * (heightSegments + 1); int num8 = sides * 6 * heightSegments; int num9; if (normalsType == NormalsType.Vertex) { num9 = num * num2 + num2; } else { num9 = (num5 - 1) * (num2 - 1) * 4 + (num - 1 - (num5 - 1)) * (num2 - 1) * 4; num7 = sides * (4 + (heightSegments - 1) * 2); } if (num9 + num7 > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return(0f); } Vector3[] array = new Vector3[num9 + num7]; Vector3[] array2 = new Vector3[num9 + num7]; Vector2[] array3 = new Vector2[num9 + num7]; int[] array4 = new int[num6 + num8]; float num10 = radius + height / 2f; Vector3 zero = Vector3.zero; if (pivotPosition != PivotPosition.Botttom) { if (pivotPosition == PivotPosition.Top) { zero = new Vector3(0f, -num10, 0f); } } else { zero = new Vector3(0f, num10, 0f); } int num11 = 0; int num12 = 0; if (normalsType == NormalsType.Vertex) { for (int i = 0; i < num5; i++) { float y = Mathf.Cos(-6.28318548f + 3.14159274f * (float)i * num3); float num13 = Mathf.Sin(3.14159274f * (float)i * num3); for (int j = 0; j < num2; j++) { float x = Mathf.Sin(6.28318548f * (float)j * num4) * num13; float z = Mathf.Cos(6.28318548f * (float)j * num4) * num13; array[num11] = new Vector3(x, y, z) * radius + zero; array2[num11] = new Vector3(x, y, z); Vector3[] array5 = array; int num14 = num11; array5[num14].y = array5[num14].y + height / 2f; Vector2 sphericalUV = CapsulePrimitive.GetSphericalUV(array[num11] - zero); array3[num11] = new Vector2(1f - (float)j * num4, sphericalUV.y); if (i < num5 - 1 && j < num2 - 1) { array4[num12] = (i + 1) * num2 + j; array4[num12 + 1] = i * num2 + (j + 1); array4[num12 + 2] = i * num2 + j; array4[num12 + 3] = (i + 1) * num2 + (j + 1); array4[num12 + 4] = i * num2 + (j + 1); array4[num12 + 5] = (i + 1) * num2 + j; num12 += 6; } num11++; } } if (height > 0f) { int num15 = num9; int num16 = num6; int num17 = num9; float num18 = height / (float)heightSegments; Vector3 a = new Vector3(0f, -height / 2f, 0f); float num19 = Mathf.Sin(3.14159274f * (float)(num5 - 1) * num3); for (int k = 0; k <= sides; k++) { float x2 = Mathf.Sin(6.28318548f * (float)k * num4) * num19; float z2 = Mathf.Cos(6.28318548f * (float)k * num4) * num19; Vector3 vector = new Vector3(x2, 0f, z2); float num20 = 0f; for (int l = 0; l <= heightSegments; l++) { array[num15] = a + vector * radius + new Vector3(0f, num20, 0f) + zero; array2[num15] = vector; Vector2 sphericalUV2 = CapsulePrimitive.GetSphericalUV(array[num15] - zero); array3[num15] = new Vector2(1f - (float)k * num4, sphericalUV2.y); num15++; num20 += num18; } } for (int m = 0; m < sides; m++) { int num21 = num9 + (m + 1) * (heightSegments + 1); for (int n = 0; n < heightSegments; n++) { array4[num16] = num21; array4[num16 + 1] = num21 + 1; array4[num16 + 2] = num17; array4[num16 + 3] = num21 + 1; array4[num16 + 4] = num17 + 1; array4[num16 + 5] = num17; num16 += 6; num17++; num21++; } num17++; } } for (int num22 = num5 - 1; num22 < num; num22++) { float y2 = Mathf.Cos(-6.28318548f + 3.14159274f * (float)num22 * num3); float num23 = Mathf.Sin(3.14159274f * (float)num22 * num3); for (int num24 = 0; num24 < num2; num24++) { float x3 = Mathf.Sin(6.28318548f * (float)num24 * num4) * num23; float z3 = Mathf.Cos(6.28318548f * (float)num24 * num4) * num23; array[num11] = new Vector3(x3, y2, z3) * radius; array2[num11] = new Vector3(x3, y2, z3); array[num11] += zero; Vector3[] array6 = array; int num25 = num11; array6[num25].y = array6[num25].y - height / 2f; Vector2 sphericalUV3 = CapsulePrimitive.GetSphericalUV(array[num11] - zero); array3[num11] = new Vector2(1f - (float)num24 * num4, sphericalUV3.y); if (num22 < num - 1 && num24 < num2 - 1) { array4[num12] = (num22 + 1 + 1) * num2 + num24; array4[num12 + 1] = (num22 + 1) * num2 + (num24 + 1); array4[num12 + 2] = (num22 + 1) * num2 + num24; array4[num12 + 3] = (num22 + 1 + 1) * num2 + (num24 + 1); array4[num12 + 4] = (num22 + 1) * num2 + (num24 + 1); array4[num12 + 5] = (num22 + 1 + 1) * num2 + num24; num12 += 6; } num11++; } } } else { for (int num26 = 0; num26 < num5 - 1; num26++) { float y3 = Mathf.Cos(-6.28318548f + 3.14159274f * (float)num26 * num3); float y4 = Mathf.Cos(-6.28318548f + 3.14159274f * (float)(num26 + 1) * num3); float num27 = Mathf.Sin(3.14159274f * (float)num26 * num3); float num28 = Mathf.Sin(3.14159274f * (float)(num26 + 1) * num3); for (int num29 = 0; num29 < num2 - 1; num29++) { float num30 = Mathf.Sin(6.28318548f * (float)num29 * num4); float num31 = Mathf.Sin(6.28318548f * (float)(num29 + 1) * num4); float num32 = Mathf.Cos(6.28318548f * (float)num29 * num4); float num33 = Mathf.Cos(6.28318548f * (float)(num29 + 1) * num4); float x4 = num30 * num27; float x5 = num31 * num27; float x6 = num30 * num28; float x7 = num31 * num28; float z4 = Mathf.Cos(6.28318548f * (float)num29 * num4) * num27; float z5 = num33 * num27; float z6 = num32 * num28; float z7 = num33 * num28; array[num11] = new Vector3(x4, y3, z4) * radius + zero; Vector3[] array7 = array; int num34 = num11; array7[num34].y = array7[num34].y + height / 2f; Vector2 sphericalUV4 = CapsulePrimitive.GetSphericalUV(array[num11] - zero); array3[num11] = new Vector2(1f - (float)num29 * num4, sphericalUV4.y); array[num11 + 1] = new Vector3(x6, y4, z6) * radius + zero; Vector3[] array8 = array; int num35 = num11 + 1; array8[num35].y = array8[num35].y + height / 2f; sphericalUV4 = CapsulePrimitive.GetSphericalUV(array[num11 + 1] - zero); array3[num11 + 1] = new Vector2(1f - (float)num29 * num4, sphericalUV4.y); array[num11 + 2] = new Vector3(x5, y3, z5) * radius + zero; Vector3[] array9 = array; int num36 = num11 + 2; array9[num36].y = array9[num36].y + height / 2f; sphericalUV4 = CapsulePrimitive.GetSphericalUV(array[num11 + 2] - zero); array3[num11 + 2] = new Vector2(1f - (float)(num29 + 1) * num4, sphericalUV4.y); array[num11 + 3] = new Vector3(x7, y4, z7) * radius + zero; Vector3[] array10 = array; int num37 = num11 + 3; array10[num37].y = array10[num37].y + height / 2f; sphericalUV4 = CapsulePrimitive.GetSphericalUV(array[num11 + 3] - zero); array3[num11 + 3] = new Vector2(1f - (float)(num29 + 1) * num4, sphericalUV4.y); array4[num12] = num11 + 1; array4[num12 + 1] = num11 + 2; array4[num12 + 2] = num11; array4[num12 + 3] = num11 + 3; array4[num12 + 4] = num11 + 2; array4[num12 + 5] = num11 + 1; num12 += 6; num11 += 4; } } if (height > 0f) { int num15 = num9; int num16 = num6; float num38 = height / (float)heightSegments; Vector3 a2 = new Vector3(0f, -height / 2f, 0f); float num39 = Mathf.Sin(3.14159274f * (float)(num5 - 1) * num3); for (int num40 = 0; num40 < sides; num40++) { Vector3 a3 = new Vector3(Mathf.Sin(6.28318548f * (float)num40 * num4) * num39, 0f, Mathf.Cos(6.28318548f * (float)num40 * num4) * num39); Vector3 vector2 = new Vector3(Mathf.Sin(6.28318548f * (float)(num40 + 1) * num4) * num39, 0f, Mathf.Cos(6.28318548f * (float)(num40 + 1) * num4) * num39); float num41 = 0f; int num42 = num15; Vector3 normalized = (a3 + vector2).normalized; for (int num43 = 0; num43 <= heightSegments; num43++) { array[num15] = a2 + a3 * radius + new Vector3(0f, num41, 0f) + zero; array[num15 + 1] = a2 + vector2 * radius + new Vector3(0f, num41, 0f) + zero; array2[num15] = normalized; array2[num15 + 1] = normalized; Vector2 sphericalUV5 = CapsulePrimitive.GetSphericalUV(array[num15] - zero); array3[num15] = new Vector2(1f - (float)num40 * num4, sphericalUV5.y); array3[num15 + 1] = new Vector2(1f - (float)(num40 + 1) * num4, sphericalUV5.y); num15 += 2; num41 += num38; } for (int num44 = 0; num44 < heightSegments; num44++) { array4[num16] = num42; array4[num16 + 1] = num42 + 1; array4[num16 + 2] = num42 + 3; array4[num16 + 3] = num42 + 3; array4[num16 + 4] = num42 + 2; array4[num16 + 5] = num42; num16 += 6; num42 += 2; } } } for (int num45 = num5 - 1; num45 < num - 1; num45++) { float y5 = Mathf.Cos(-6.28318548f + 3.14159274f * (float)num45 * num3); float y6 = Mathf.Cos(-6.28318548f + 3.14159274f * (float)(num45 + 1) * num3); float num46 = Mathf.Sin(3.14159274f * (float)num45 * num3); float num47 = Mathf.Sin(3.14159274f * (float)(num45 + 1) * num3); for (int num48 = 0; num48 < num2 - 1; num48++) { float num49 = Mathf.Sin(6.28318548f * (float)num48 * num4); float num50 = Mathf.Sin(6.28318548f * (float)(num48 + 1) * num4); float num51 = Mathf.Cos(6.28318548f * (float)num48 * num4); float num52 = Mathf.Cos(6.28318548f * (float)(num48 + 1) * num4); float x8 = num49 * num46; float x9 = num50 * num46; float x10 = num49 * num47; float x11 = num50 * num47; float z8 = Mathf.Cos(6.28318548f * (float)num48 * num4) * num46; float z9 = num52 * num46; float z10 = num51 * num47; float z11 = num52 * num47; array[num11] = new Vector3(x8, y5, z8) * radius + zero; Vector3[] array11 = array; int num53 = num11; array11[num53].y = array11[num53].y - height / 2f; Vector2 sphericalUV6 = CapsulePrimitive.GetSphericalUV(array[num11] - zero); array3[num11] = new Vector2(1f - (float)num48 * num4, sphericalUV6.y); array[num11 + 1] = new Vector3(x10, y6, z10) * radius + zero; Vector3[] array12 = array; int num54 = num11 + 1; array12[num54].y = array12[num54].y - height / 2f; sphericalUV6 = CapsulePrimitive.GetSphericalUV(array[num11 + 1] - zero); array3[num11 + 1] = new Vector2(1f - (float)num48 * num4, sphericalUV6.y); array[num11 + 2] = new Vector3(x9, y5, z9) * radius + zero; Vector3[] array13 = array; int num55 = num11 + 2; array13[num55].y = array13[num55].y - height / 2f; sphericalUV6 = CapsulePrimitive.GetSphericalUV(array[num11 + 2] - zero); array3[num11 + 2] = new Vector2(1f - (float)(num48 + 1) * num4, sphericalUV6.y); array[num11 + 3] = new Vector3(x11, y6, z11) * radius + zero; Vector3[] array14 = array; int num56 = num11 + 3; array14[num56].y = array14[num56].y - height / 2f; sphericalUV6 = CapsulePrimitive.GetSphericalUV(array[num11 + 3] - zero); array3[num11 + 3] = new Vector2(1f - (float)(num48 + 1) * num4, sphericalUV6.y); array4[num12] = num11 + 1; array4[num12 + 1] = num11 + 2; array4[num12 + 2] = num11; array4[num12 + 3] = num11 + 3; array4[num12 + 4] = num11 + 2; array4[num12 + 5] = num11 + 1; num12 += 6; num11 += 4; } } } mesh.vertices = array; mesh.normals = array2; mesh.uv = array3; mesh.triangles = array4; if (normalsType == NormalsType.Face) { mesh.RecalculateNormals(); } mesh.RecalculateBounds(); MeshUtils.CalculateTangents(mesh); stopwatch.Stop(); return((float)stopwatch.ElapsedMilliseconds); }
// Token: 0x0600427F RID: 17023 RVA: 0x00151D7C File Offset: 0x0015017C public void GenerateGeometry(float radius0, float radius1, float height, int sides, int heightSegments, float slice, bool radialMapping, NormalsType normalsType, PivotPosition pivotPosition) { MeshFilter component = base.GetComponent <MeshFilter>(); if (component.sharedMesh == null) { component.sharedMesh = new Mesh(); } Mesh sharedMesh = component.sharedMesh; base.GenerationTimeMS = TubePrimitive.GenerateGeometry(sharedMesh, radius0, radius1, height, sides, heightSegments, slice, radialMapping, normalsType, pivotPosition); this.radius0 = radius0; this.radius1 = radius1; this.height = height; this.sides = sides; this.heightSegments = heightSegments; this.slice = slice; this.radialMapping = radialMapping; this.normalsType = normalsType; this.flipNormals = false; }
// Token: 0x0600427E RID: 17022 RVA: 0x00151D24 File Offset: 0x00150124 public static Tube Create(float radius0, float radius1, float height, int sides, int heightSegments, float slice, bool radialMapping, NormalsType normalsType, PivotPosition pivotPosition) { GameObject gameObject = new GameObject("TubePro"); gameObject.AddComponent <MeshFilter>(); MeshRenderer meshRenderer = gameObject.AddComponent <MeshRenderer>(); meshRenderer.sharedMaterial = new Material(Shader.Find("Diffuse")); Tube tube = gameObject.AddComponent <Tube>(); tube.GenerateGeometry(radius0, radius1, height, sides, heightSegments, slice, radialMapping, normalsType, pivotPosition); return(tube); }
/// <summary> /// generate mesh geometry for Sphere /// </summary> /// <param name="mesh">mesh to be generated</param> /// <param name="radius">radius of sphere</param> /// <param name="segments">number of segments</param> /// <param name="hemisphere">hemisphere, 0 ... complete sphere, 0.5 ... half-sphere</param> /// <param name="innerRadius">radius of the inner sphere</param> /// <param name="slice">vertical slice parameter</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public static float GenerateGeometry(Mesh mesh, float radius, int segments, float hemisphere, float innerRadius, float slice, NormalsType normalsType, PivotPosition pivotPosition) { var stopWatch = new Stopwatch(); stopWatch.Start(); radius = Mathf.Clamp(radius, 0, 100); segments = Mathf.Clamp(segments, 4, 100); hemisphere = Mathf.Clamp(hemisphere, 0.0f, 1.0f); innerRadius = Mathf.Clamp01(innerRadius)*radius; slice = Mathf.Clamp(slice, 0.0f, 360.0f); mesh.Clear(); var rings = segments-1; var sectors = segments; slice = slice / 360.0f; var sidesSlice = (int)(sectors * (1.0f - slice)); if (sidesSlice == 0) { sidesSlice = 1; } var hemisphereCapY = -1 + hemisphere * 2; var hemisphereCapRing = rings; var hemisphereYpos = -radius; var R = 1 / (float)(rings - 1); var S = 1 / (float)(sectors - 1); // calculate hemisphere parameters var lastY = 0.0f; for (var r = 0; r < rings; r++) { var y = Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * r * R); if (y < hemisphereCapY) { hemisphereCapRing = r; hemisphereYpos = lastY * radius; break; } lastY = y; } var verticesNum = 0; var trianglesNum = (hemisphereCapRing/* - 1*/) * (sectors/* - 1*/) * 6; var verticesHemisphereNum = segments + 1; var trianglesHemisphereNum = segments * 3; if (hemisphereCapRing == rings) { trianglesNum -= sectors * 3; } if (normalsType == NormalsType.Vertex) { verticesNum = (hemisphereCapRing + 1) * (sectors + 1); } else { verticesNum = trianglesNum; } if (hemisphereCapRing == rings) { verticesNum -= sectors + 1; } if (innerRadius > 0 && hemisphereCapRing < rings) { verticesNum *= 2; trianglesNum *= 2; verticesNum += sectors * 2; trianglesNum += sectors * 3; } var pivotOffset = Vector3.zero; var height = radius-hemisphereYpos; switch (pivotPosition) { case PivotPosition.Botttom: pivotOffset = new Vector3(0.0f, radius, 0.0f); break; case PivotPosition.Top: pivotOffset = new Vector3(0.0f, hemisphereYpos, 0.0f); break; case PivotPosition.Center: pivotOffset = new Vector3(0.0f, (hemisphereYpos + height/2), 0.0f); break; } if (verticesNum + verticesHemisphereNum > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return 0.0f; } var vertices = new Vector3[verticesNum + verticesHemisphereNum]; var normals = new Vector3[verticesNum + verticesHemisphereNum]; var uvs = new Vector2[verticesNum + verticesHemisphereNum]; var triangles = new int[trianglesNum + trianglesHemisphereNum]; var vertIndex = 0; var triIndex = 0; // MeshUtils.Log(sectors); for (var r = 0; r < hemisphereCapRing; r++) { var y = -Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * r * R); var sinR = Mathf.Sin(Mathf.PI*r*R); for (var s = 0; s < sectors; s++) { var x = Mathf.Sin(2 * Mathf.PI * s * S) * sinR; var z = Mathf.Cos(2 * Mathf.PI * s * S) * sinR; // if (s < sidesSlice) // { // continue; // } vertices[vertIndex + 0] = new Vector3(x, y, z) * radius + pivotOffset; normals[vertIndex + 0] = new Vector3(x, y, z); uvs[vertIndex + 0] = new Vector2(1.0f - (s * S), (r * R)); if (r < hemisphereCapRing - 1 && s < sectors - 1) { //543 triangles[triIndex + 5] = (r + 1) * sectors + (s); triangles[triIndex + 4] = r * sectors + (s + 1); triangles[triIndex + 3] = r * sectors + s; //210 triangles[triIndex + 2] = (r + 1) * sectors + (s + 1); triangles[triIndex + 1] = r * sectors + (s + 1); triangles[triIndex + 0] = (r + 1) * sectors + (s); triIndex += 6; } vertIndex += 1; } } var hemisphereVertOffset = vertIndex; // calculate hemisphere plane if (hemisphereCapRing < rings) { // generate inner sphere if (innerRadius > 0) { var vertOffset = hemisphereVertOffset; var outerY = -Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * (hemisphereCapRing - 1) * R) * radius; var innertY = -Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * (hemisphereCapRing-1) * R) * innerRadius; var diff = new Vector3(0, outerY - innertY, 0); for (var r = 0; r < hemisphereCapRing; r++) { var y = -Mathf.Cos(-Mathf.PI*2.0f + Mathf.PI*r*R); var sinR = Mathf.Sin(Mathf.PI*r*R); for (var s = 0; s < sectors; s++) { var x = Mathf.Sin(2*Mathf.PI*s*S)*sinR; var z = Mathf.Cos(2*Mathf.PI*s*S)*sinR; vertices[vertIndex + 0] = new Vector3(x, y, z)*innerRadius + pivotOffset + diff; normals[vertIndex + 0] = -new Vector3(x, y, z); uvs[vertIndex + 0] = new Vector2(1.0f - (s*S), (r*R)); if (r < hemisphereCapRing - 1 && s < sectors - 1) { triangles[triIndex + 0] = vertOffset + (r + 1) * sectors + (s); triangles[triIndex + 1] = vertOffset + r * sectors + (s + 1); triangles[triIndex + 2] = vertOffset + r * sectors + s; triangles[triIndex + 3] = vertOffset + (r + 1) * sectors + (s + 1); triangles[triIndex + 4] = vertOffset + r * sectors + (s + 1); triangles[triIndex + 5] = vertOffset + (r + 1) * sectors + (s); triIndex += 6; } vertIndex += 1; } } hemisphereVertOffset = vertIndex; // duplicate triangles in face case if (normalsType == NormalsType.Face) { MeshUtils.DuplicateSharedVertices(ref vertices, ref uvs, triangles, triIndex); hemisphereVertOffset = triIndex; } // connect two hemi-spheres { var y = -Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * (hemisphereCapRing - 1) * R); var sinR = Mathf.Sin(Mathf.PI * (hemisphereCapRing - 1) * R); var triVert = hemisphereVertOffset; vertIndex = triVert; for (var i = 0; i < sectors; i++) { var x = Mathf.Sin(2 * Mathf.PI * i * S) * sinR; var z = Mathf.Cos(2 * Mathf.PI * i * S) * sinR; var v = new Vector3(x, y, z); v.Normalize(); vertices[vertIndex + 0] = v * radius + pivotOffset; vertices[vertIndex + 1] = v*innerRadius + pivotOffset + diff; normals[vertIndex + 0] = new Vector3(0.0f, 1.0f, 0.0f); normals[vertIndex + 1] = new Vector3(0.0f, 1.0f, 0.0f); // make planar uv mapping for hemisphere plane var uvV = new Vector2(v.x * 0.5f, v.z * .5f); var uvCenter = new Vector2(0.5f, 0.5f); var uvV2 = new Vector2(v.x * innerRadius / radius * 0.5f, v.z * innerRadius /radius * 0.5f); uvs[vertIndex + 0] = uvCenter + uvV; uvs[vertIndex + 1] = uvCenter + uvV2; vertIndex += 2; } for (var i = 0; i < rings; i++) { triangles[triIndex + 2] = triVert + 1; triangles[triIndex + 1] = triVert + 2; triangles[triIndex + 0] = triVert + 0; triangles[triIndex + 4] = triVert + 3; triangles[triIndex + 5] = triVert + 1; triangles[triIndex + 3] = triVert + 2; triIndex += 6; triVert += 2; } } } else { // duplicate triangles in face case if (normalsType == NormalsType.Face) { MeshUtils.DuplicateSharedVertices(ref vertices, ref uvs, triangles, triIndex); hemisphereVertOffset = triIndex; } var triVert = hemisphereVertOffset; vertIndex = hemisphereVertOffset; var y = -Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * (hemisphereCapRing - 1) * R); var sinR = Mathf.Sin(Mathf.PI * (hemisphereCapRing - 1) * R); for (var i = 0; i < sectors; i++) { var x = Mathf.Sin(2 * Mathf.PI * i * S) * sinR; var z = Mathf.Cos(2 * Mathf.PI * i * S) * sinR; var v = new Vector3(x, y, z); vertices[vertIndex + 0] = v * radius + pivotOffset; normals[vertIndex + 0] = new Vector3(0.0f, 1.0f, 0.0f); // make planar uv mapping for hemisphere plane var uvV = new Vector2(v.x * 0.5f, v.z * .5f); var uvCenter = new Vector2(0.5f, 0.5f); uvs[vertIndex + 0] = uvCenter + uvV; vertIndex += 1; } vertices[vertIndex + 0] = new Vector3(0, -hemisphereYpos, 0) + pivotOffset; normals[vertIndex + 0] = new Vector3(0, 1, 0); uvs[vertIndex + 0] = new Vector2(0.5f, 0.5f); vertIndex += 1; for (var i = 0; i < rings; i++) { triangles[triIndex + 2] = triVert + 0; triangles[triIndex + 1] = vertIndex - 1; if (i == rings - 1) { triangles[triIndex + 0] = hemisphereVertOffset; } else { triangles[triIndex + 0] = triVert + 1; } triIndex += 3; triVert += 1; } } } else { // duplicate triangles in face case if (normalsType == NormalsType.Face) { MeshUtils.DuplicateSharedVertices(ref vertices, ref uvs, triangles, triIndex); } } mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; // generate normals by unity in face case if (normalsType == NormalsType.Face) { mesh.RecalculateNormals(); } else { mesh.normals = normals; } mesh.RecalculateBounds(); MeshUtils.CalculateTangents(mesh); ; stopWatch.Stop(); return stopWatch.ElapsedMilliseconds; }
public IEnumerable<ReportElement> GetElements(PivotPosition position) { return Elements.Where(i => i.PivotPosition == position); }
ResultCell[] GetResultCells(PivotPosition position, DataRow row, ReportModel model) { ReportElement[] elements = model.Elements.Where(i => i.PivotPosition == position).ToArray(); return GetResultCells(elements, row); }
/// <summary> /// generate mesh geometry for Sphere /// </summary> /// <param name="mesh">mesh to be generated</param> /// <param name="radius">radius of sphere</param> /// <param name="segments">number of segments</param> /// <param name="hemisphere">hemisphere, 0 ... complete sphere, 0.5 ... half-sphere</param> /// <param name="innerRadius">radius of the inner sphere</param> /// <param name="slice">vertical slice parameter</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public static float GenerateGeometry(Mesh mesh, float radius, int segments, float hemisphere, float innerRadius, float slice, NormalsType normalsType, PivotPosition pivotPosition) { var stopWatch = new Stopwatch(); stopWatch.Start(); radius = Mathf.Clamp(radius, 0, 100); segments = Mathf.Clamp(segments, 4, 100); hemisphere = Mathf.Clamp(hemisphere, 0.0f, 1.0f); innerRadius = Mathf.Clamp01(innerRadius)*radius; slice = Mathf.Clamp(slice, 0.0f, 360.0f); mesh.Clear(); int rings = segments-1; int sectors = segments; slice = slice / 360.0f; int sidesSlice = (int)(sectors * (1.0f - slice)); if (sidesSlice == 0) { sidesSlice = 1; } var hemisphereCapY = -1 + hemisphere * 2; int hemisphereCapRing = rings; var hemisphereYpos = -radius; float R = 1 / (float)(rings - 1); float S = 1 / (float)(sectors - 1); // calculate hemisphere parameters var lastY = 0.0f; for (int r = 0; r < rings; r++) { var y = Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * r * R); if (y < hemisphereCapY) { hemisphereCapRing = r; hemisphereYpos = lastY * radius; break; } lastY = y; } int verticesNum = 0; var trianglesNum = (hemisphereCapRing/* - 1*/) * (sectors/* - 1*/) * 6; var verticesHemisphereNum = segments + 1; var trianglesHemisphereNum = segments * 3; if (hemisphereCapRing == rings) { trianglesNum -= sectors * 3; } if (normalsType == NormalsType.Vertex) { verticesNum = (hemisphereCapRing + 1) * (sectors + 1); } else { verticesNum = trianglesNum; } if (hemisphereCapRing == rings) { verticesNum -= sectors + 1; } if (innerRadius > 0 && hemisphereCapRing < rings) { verticesNum *= 2; trianglesNum *= 2; verticesNum += sectors * 2; trianglesNum += sectors * 3; } var pivotOffset = Vector3.zero; var height = radius-hemisphereYpos; switch (pivotPosition) { case PivotPosition.Botttom: pivotOffset = new Vector3(0.0f, radius, 0.0f); break; case PivotPosition.Top: pivotOffset = new Vector3(0.0f, hemisphereYpos, 0.0f); break; case PivotPosition.Center: pivotOffset = new Vector3(0.0f, (hemisphereYpos + height/2), 0.0f); break; } if (verticesNum + verticesHemisphereNum > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return 0.0f; } var vertices = new Vector3[verticesNum + verticesHemisphereNum]; var normals = new Vector3[verticesNum + verticesHemisphereNum]; var uvs = new Vector2[verticesNum + verticesHemisphereNum]; var triangles = new int[trianglesNum + trianglesHemisphereNum]; var vertIndex = 0; var triIndex = 0; // MeshUtils.Log(sectors); for (int r = 0; r < hemisphereCapRing; r++) { var y = -Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * r * R); var sinR = Mathf.Sin(Mathf.PI*r*R); for (int s = 0; s < sectors; s++) { float x = Mathf.Sin(2 * Mathf.PI * s * S) * sinR; float z = Mathf.Cos(2 * Mathf.PI * s * S) * sinR; // if (s < sidesSlice) // { // continue; // } vertices[vertIndex + 0] = new Vector3(x, y, z) * radius + pivotOffset; normals[vertIndex + 0] = new Vector3(x, y, z); uvs[vertIndex + 0] = new Vector2(1.0f - (s * S), (r * R)); if (r < hemisphereCapRing - 1 && s < sectors - 1) { //543 triangles[triIndex + 5] = (r + 1) * sectors + (s); triangles[triIndex + 4] = r * sectors + (s + 1); triangles[triIndex + 3] = r * sectors + s; //210 triangles[triIndex + 2] = (r + 1) * sectors + (s + 1); triangles[triIndex + 1] = r * sectors + (s + 1); triangles[triIndex + 0] = (r + 1) * sectors + (s); triIndex += 6; } vertIndex += 1; } } var hemisphereVertOffset = vertIndex; // calculate hemisphere plane if (hemisphereCapRing < rings) { // generate inner sphere if (innerRadius > 0) { var vertOffset = hemisphereVertOffset; var outerY = -Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * (hemisphereCapRing - 1) * R) * radius; var innertY = -Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * (hemisphereCapRing-1) * R) * innerRadius; var diff = new Vector3(0, outerY - innertY, 0); for (int r = 0; r < hemisphereCapRing; r++) { var y = -Mathf.Cos(-Mathf.PI*2.0f + Mathf.PI*r*R); var sinR = Mathf.Sin(Mathf.PI*r*R); for (int s = 0; s < sectors; s++) { float x = Mathf.Sin(2*Mathf.PI*s*S)*sinR; float z = Mathf.Cos(2*Mathf.PI*s*S)*sinR; vertices[vertIndex + 0] = new Vector3(x, y, z)*innerRadius + pivotOffset + diff; normals[vertIndex + 0] = -new Vector3(x, y, z); uvs[vertIndex + 0] = new Vector2(1.0f - (s*S), (r*R)); if (r < hemisphereCapRing - 1 && s < sectors - 1) { triangles[triIndex + 0] = vertOffset + (r + 1) * sectors + (s); triangles[triIndex + 1] = vertOffset + r * sectors + (s + 1); triangles[triIndex + 2] = vertOffset + r * sectors + s; triangles[triIndex + 3] = vertOffset + (r + 1) * sectors + (s + 1); triangles[triIndex + 4] = vertOffset + r * sectors + (s + 1); triangles[triIndex + 5] = vertOffset + (r + 1) * sectors + (s); triIndex += 6; } vertIndex += 1; } } hemisphereVertOffset = vertIndex; // duplicate triangles in face case if (normalsType == NormalsType.Face) { MeshUtils.DuplicateSharedVertices(ref vertices, ref uvs, triangles, triIndex); hemisphereVertOffset = triIndex; } // connect two hemi-spheres { var y = -Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * (hemisphereCapRing - 1) * R); var sinR = Mathf.Sin(Mathf.PI * (hemisphereCapRing - 1) * R); var triVert = hemisphereVertOffset; vertIndex = triVert; for (int i = 0; i < sectors; i++) { var x = Mathf.Sin(2 * Mathf.PI * i * S) * sinR; var z = Mathf.Cos(2 * Mathf.PI * i * S) * sinR; var v = new Vector3(x, y, z); v.Normalize(); vertices[vertIndex + 0] = v * radius + pivotOffset; vertices[vertIndex + 1] = v*innerRadius + pivotOffset + diff; normals[vertIndex + 0] = new Vector3(0.0f, 1.0f, 0.0f); normals[vertIndex + 1] = new Vector3(0.0f, 1.0f, 0.0f); // make planar uv mapping for hemisphere plane var uvV = new Vector2(v.x * 0.5f, v.z * .5f); var uvCenter = new Vector2(0.5f, 0.5f); var uvV2 = new Vector2(v.x * innerRadius / radius * 0.5f, v.z * innerRadius /radius * 0.5f); uvs[vertIndex + 0] = uvCenter + uvV; uvs[vertIndex + 1] = uvCenter + uvV2; vertIndex += 2; } for (int i = 0; i < rings; i++) { triangles[triIndex + 2] = triVert + 1; triangles[triIndex + 1] = triVert + 2; triangles[triIndex + 0] = triVert + 0; triangles[triIndex + 4] = triVert + 3; triangles[triIndex + 5] = triVert + 1; triangles[triIndex + 3] = triVert + 2; triIndex += 6; triVert += 2; } } } else { // duplicate triangles in face case if (normalsType == NormalsType.Face) { MeshUtils.DuplicateSharedVertices(ref vertices, ref uvs, triangles, triIndex); hemisphereVertOffset = triIndex; } var triVert = hemisphereVertOffset; vertIndex = hemisphereVertOffset; var y = -Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * (hemisphereCapRing - 1) * R); var sinR = Mathf.Sin(Mathf.PI * (hemisphereCapRing - 1) * R); for (int i = 0; i < sectors; i++) { var x = Mathf.Sin(2 * Mathf.PI * i * S) * sinR; var z = Mathf.Cos(2 * Mathf.PI * i * S) * sinR; var v = new Vector3(x, y, z); vertices[vertIndex + 0] = v * radius + pivotOffset; normals[vertIndex + 0] = new Vector3(0.0f, 1.0f, 0.0f); // make planar uv mapping for hemisphere plane var uvV = new Vector2(v.x * 0.5f, v.z * .5f); var uvCenter = new Vector2(0.5f, 0.5f); uvs[vertIndex + 0] = uvCenter + uvV; vertIndex += 1; } vertices[vertIndex + 0] = new Vector3(0, -hemisphereYpos, 0) + pivotOffset; normals[vertIndex + 0] = new Vector3(0, 1, 0); uvs[vertIndex + 0] = new Vector2(0.5f, 0.5f); vertIndex += 1; for (int i = 0; i < rings; i++) { triangles[triIndex + 2] = triVert + 0; triangles[triIndex + 1] = vertIndex - 1; if (i == rings - 1) { triangles[triIndex + 0] = hemisphereVertOffset; } else { triangles[triIndex + 0] = triVert + 1; } triIndex += 3; triVert += 1; } } } else { // duplicate triangles in face case if (normalsType == NormalsType.Face) { MeshUtils.DuplicateSharedVertices(ref vertices, ref uvs, triangles, triIndex); } } mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; // generate normals by unity in face case if (normalsType == NormalsType.Face) { mesh.RecalculateNormals(); } else { mesh.normals = normals; } mesh.RecalculateBounds(); MeshUtils.CalculateTangents(mesh); mesh.Optimize(); stopWatch.Stop(); return stopWatch.ElapsedMilliseconds; }
/// <summary> /// generate mesh geometry for Pyramid /// </summary> /// <param name="mesh">mesh to be generated</param> /// <param name="width">width of pyramid</param> /// <param name="height">height of pyramid</param> /// <param name="depth">depth of pyramid</param> /// <param name="widthSegments">number of triangle segments in width direction</param> /// <param name="heightSegments">number of triangle segments in height direction</param> /// <param name="depthSegments">number of triangle segments in depth direction</param> /// <param name="pyramidMap">enable pyramid map uv mapping</param> /// <param name="pivotPosition">position of the model pivot</param> public static float GenerateGeometry(Mesh mesh, float width, float height, float depth, int widthSegments, int heightSegments, int depthSegments, bool pyramidMap, PivotPosition pivotPosition) { var stopWatch = new Stopwatch(); stopWatch.Start(); width = Mathf.Clamp(width, 0, 100); height = Mathf.Clamp(height, 0, 100); width = Mathf.Clamp(width, 0, 100); widthSegments = Mathf.Clamp(widthSegments, 1, 100); heightSegments = Mathf.Clamp(heightSegments, 1, 100); depthSegments = Mathf.Clamp(depthSegments, 1, 100); mesh.Clear(); int numTriangles = widthSegments * heightSegments * 6 * 2 + depthSegments * heightSegments * 6 * 2 + widthSegments * depthSegments * 6; int numVertices = (widthSegments + 1) * (heightSegments + 1) * 2 + (depthSegments + 1) * (heightSegments + 1) * 2 + (widthSegments + 1) * (depthSegments + 1); if (numVertices > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return(0.0f); } var vertices = new Vector3[numVertices]; var uvs = new Vector2[numVertices]; var triangles = new int[numTriangles]; var apex = new Vector3(0, height, 0); var pivotOffset = Vector3.zero; switch (pivotPosition) { case PivotPosition.Center: pivotOffset = new Vector3(0.0f, -height / 2, 0.0f); break; case PivotPosition.Top: pivotOffset = new Vector3(0.0f, -height, 0.0f); break; } int vertIndex = 0; float widthRatio = width / widthSegments; var offsetTris = widthSegments * heightSegments * 6; var offsetVert = (widthSegments + 1) * (heightSegments + 1); var a0 = new Vector3(depth / 2, 0.0f, 0 * widthRatio - width / 2) + pivotOffset; var b0 = new Vector3(depth / 2, 0.0f, widthSegments * widthRatio - width / 2) + pivotOffset; var a1 = new Vector3(-depth / 2, 0.0f, 0 * widthRatio - width / 2) + pivotOffset; var b1 = new Vector3(-depth / 2, 0.0f, widthSegments * widthRatio - width / 2) + pivotOffset; for (int i = 0; i < widthSegments + 1; i++) { var bottom0 = new Vector3(depth / 2, 0.0f, i * widthRatio - width / 2); var v0 = apex - bottom0; var bottom1 = new Vector3(-depth / 2, 0.0f, i * widthRatio - width / 2); var v1 = apex - bottom1; for (int j = 0; j < heightSegments + 1; j++) { vertices[vertIndex + 0] = bottom0 + (float)j / heightSegments * v0 + pivotOffset; vertices[offsetVert + vertIndex + 0] = bottom1 + (float)j / heightSegments * v1 + pivotOffset; if (pyramidMap) { var baryCoordinates = MeshUtils.ComputeBarycentricCoordinates(apex + pivotOffset, a0, b0, vertices[vertIndex]); uvs[vertIndex + 0] = GetPyramidUVMap(PyramidSide.side0, baryCoordinates); baryCoordinates = MeshUtils.ComputeBarycentricCoordinates(apex + pivotOffset, b1, a1, vertices[offsetVert + vertIndex + 0]); uvs[offsetVert + vertIndex + 0] = GetPyramidUVMap(PyramidSide.side2, baryCoordinates); } else { uvs[vertIndex + 0] = new Vector2(vertices[vertIndex + 0].z / width, (float)j / heightSegments); uvs[offsetVert + vertIndex + 0] = new Vector2(vertices[offsetVert + vertIndex + 0].z / width, (float)j / heightSegments); } vertIndex++; } } int triIndex = 0; int triVert = 0; for (int i = 0; i < widthSegments; i++) { var nextSegment = (heightSegments + 1); for (int j = 0; j < heightSegments; j++) { triangles[triIndex + 0] = triVert + 0; triangles[triIndex + 1] = triVert + 1; triangles[triIndex + 2] = triVert + nextSegment + 0; triangles[triIndex + 3] = triVert + nextSegment + 0; triangles[triIndex + 4] = triVert + 1; triangles[triIndex + 5] = triVert + nextSegment + 1; triangles[offsetTris + triIndex + 0] = offsetVert + triVert + nextSegment + 0; triangles[offsetTris + triIndex + 1] = offsetVert + triVert + 1; triangles[offsetTris + triIndex + 2] = offsetVert + triVert + 0; triangles[offsetTris + triIndex + 3] = offsetVert + triVert + nextSegment + 1; triangles[offsetTris + triIndex + 4] = offsetVert + triVert + 1; triangles[offsetTris + triIndex + 5] = offsetVert + triVert + nextSegment + 0; triIndex += 6; triVert += 1; } triVert += 1; } widthRatio = depth / depthSegments; vertIndex = (widthSegments + 1) * (heightSegments + 1) * 2; triIndex = widthSegments * heightSegments * 6 * 2; triVert = vertIndex; offsetTris = depthSegments * heightSegments * 6; offsetVert = (depthSegments + 1) * (heightSegments + 1); a0 = new Vector3(0 * widthRatio - depth / 2, 0.0f, width / 2) + pivotOffset; b0 = new Vector3(depthSegments * widthRatio - depth / 2, 0.0f, width / 2) + pivotOffset; a1 = new Vector3(0 * widthRatio - depth / 2, 0.0f, -width / 2) + pivotOffset; b1 = new Vector3(depthSegments * widthRatio - depth / 2, 0.0f, -width / 2) + pivotOffset; for (int i = 0; i < depthSegments + 1; i++) { var bottom0 = new Vector3(i * widthRatio - depth / 2, 0.0f, width / 2); var v0 = apex - bottom0; var bottom1 = new Vector3(i * widthRatio - depth / 2, 0.0f, -width / 2); var v1 = apex - bottom1; for (int j = 0; j < heightSegments + 1; j++) { vertices[vertIndex + 0] = bottom0 + (float)j / heightSegments * v0 + pivotOffset; vertices[offsetVert + vertIndex + 0] = bottom1 + (float)j / heightSegments * v1 + pivotOffset; if (pyramidMap) { var baryCoordinates = MeshUtils.ComputeBarycentricCoordinates(apex + pivotOffset, b0, a0, vertices[vertIndex]); uvs[vertIndex + 0] = GetPyramidUVMap(PyramidSide.side1, baryCoordinates); baryCoordinates = MeshUtils.ComputeBarycentricCoordinates(apex + pivotOffset, a1, b1, vertices[offsetVert + vertIndex + 0]); uvs[offsetVert + vertIndex + 0] = GetPyramidUVMap(PyramidSide.side3, baryCoordinates); } else { uvs[vertIndex + 0] = new Vector2(vertices[vertIndex + 0].x / depth, (float)j / heightSegments); uvs[offsetVert + vertIndex + 0] = new Vector2(vertices[offsetVert + vertIndex + 0].x / depth, (float)j / heightSegments); } vertIndex++; } } for (int i = 0; i < depthSegments; i++) { var nextSegment = (heightSegments + 1); for (int j = 0; j < heightSegments; j++) { triangles[triIndex + 0] = triVert + nextSegment + 0; triangles[triIndex + 1] = triVert + 1; triangles[triIndex + 2] = triVert + 0; triangles[triIndex + 3] = triVert + nextSegment + 1; triangles[triIndex + 4] = triVert + 1; triangles[triIndex + 5] = triVert + nextSegment + 0; triangles[offsetTris + triIndex + 0] = offsetVert + triVert + 0; triangles[offsetTris + triIndex + 1] = offsetVert + triVert + 1; triangles[offsetTris + triIndex + 2] = offsetVert + triVert + nextSegment + 0; triangles[offsetTris + triIndex + 3] = offsetVert + triVert + nextSegment + 0; triangles[offsetTris + triIndex + 4] = offsetVert + triVert + 1; triangles[offsetTris + triIndex + 5] = offsetVert + triVert + nextSegment + 1; triIndex += 6; triVert += 1; } triVert += 1; } widthRatio = width / widthSegments; var depthRatio = depth / depthSegments; vertIndex = (widthSegments + 1) * (heightSegments + 1) * 2 + (depthSegments + 1) * (heightSegments + 1) * 2; triIndex = widthSegments * heightSegments * 6 * 2 + depthSegments * heightSegments * 6 * 2; triVert = vertIndex; for (int i = 0; i < depthSegments + 1; i++) { for (int j = 0; j < widthSegments + 1; j++) { vertices[vertIndex + 0] = new Vector3(depth / 2 - depthRatio * i, 0.0f, width / 2 - j * widthRatio) + pivotOffset; if (pyramidMap) { uvs[vertIndex + 0] = GetPyramidUVMap(PyramidSide.bottom, new Vector2((float)j / widthSegments, (float)i / depthSegments)); } else { uvs[vertIndex + 0] = new Vector2((float)j / widthSegments, (float)i / depthSegments); } vertIndex++; } } for (int i = 0; i < depthSegments; i++) { var nextSegment = (widthSegments + 1); for (int j = 0; j < widthSegments; j++) { triangles[triIndex + 0] = triVert + nextSegment + 0; triangles[triIndex + 1] = triVert + 1; triangles[triIndex + 2] = triVert + 0; triangles[triIndex + 3] = triVert + nextSegment + 1; triangles[triIndex + 4] = triVert + 1; triangles[triIndex + 5] = triVert + nextSegment + 0; triIndex += 6; triVert += 1; } triVert += 1; } mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; mesh.RecalculateNormals(); MeshUtils.CalculateTangents(mesh); ; mesh.RecalculateBounds(); stopWatch.Stop(); return(stopWatch.ElapsedMilliseconds); }
/// <summary> /// generate mesh geometry for cone /// </summary> /// <param name="mesh">mesh to be generated</param> /// <param name="radius0">fist radius of cone</param> /// <param name="radius1">second radius of cone</param> /// <param name="height">height of cone</param> /// <param name="sides">number of triangle segments in radius</param> /// <param name="heightSegments">number of triangle segments in height</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public static float GenerateGeometry(Mesh mesh, float radius0, float radius1, float thickness, float height, int sides, int heightSegments, NormalsType normalsType, PivotPosition pivotPosition) { var stopWatch = new Stopwatch(); stopWatch.Start(); radius0 = Mathf.Clamp(radius0, 0, 100); radius1 = Mathf.Clamp(radius1, 0, 100); height = Mathf.Clamp(height, 0, 100); sides = Mathf.Clamp(sides, 3, 100); heightSegments = Mathf.Clamp(heightSegments, 1, 100); var slice = 0.0f; // normalize slice slice = slice / 360.0f; mesh.Clear(); heightSegments = Mathf.Clamp(heightSegments, 1, 250); sides = Mathf.Clamp(sides, 3, 250); var minRadius = Mathf.Min(radius0, radius1); if (thickness > minRadius) { thickness = minRadius; } var thicknessRadius = radius0 - thickness; var radialMapping = false; int sidesPipe = (int)(sides * (1.0f - slice)); if (sidesPipe == 0) { sidesPipe = 1; } int numVertices = 0; int numTriangles = sidesPipe * 6 * heightSegments * 2; int numVerticesCaps = ((sidesPipe + 1) * 2 * 2); int numTrianglesCaps = (sidesPipe) * 6 * 2; int numTrianglesPipeCaps = 0; int numVerticesPipeCaps = 0; if (sidesPipe < sides) { numTrianglesPipeCaps = 12; numVerticesPipeCaps = 8; } if (normalsType == NormalsType.Face) { numVertices = sidesPipe * (4 + (heightSegments - 1) * 2) * 2; } else { numVertices = (sidesPipe + 1) * (heightSegments + 1) * 2; } if (numVertices + numVerticesCaps > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return(0.0f); } var vertices = new Vector3[numVertices + numVerticesCaps + numVerticesPipeCaps]; var normals = new Vector3[numVertices + numVerticesCaps + numVerticesPipeCaps]; var uvs = new Vector2[numVertices + numVerticesCaps + numVerticesPipeCaps]; var triangles = new int[numTriangles + numTrianglesCaps + numTrianglesPipeCaps]; var bottomCenter = Vector3.zero; var innerVertOffset = numVertices / 2; var innerTriOffset = numTriangles / 2; var coneHeight = (new Vector3(bottomCenter.x + radius0, bottomCenter.y, bottomCenter.z) - new Vector3(bottomCenter.x + radius1, bottomCenter.y + height, bottomCenter.z)).magnitude; var pivotOffset = Vector3.zero; switch (pivotPosition) { case PivotPosition.Center: pivotOffset = new Vector3(0.0f, -height / 2, 0.0f); break; case PivotPosition.Top: pivotOffset = new Vector3(0.0f, -height, 0.0f); break; } if (normalsType == NormalsType.Face) { var vertIndex = 0; var triIndex = 0; var heightRatio = coneHeight / heightSegments; for (int i = 0; i < sidesPipe; i++) { float angle0 = ((float)i / sides) * Mathf.PI * 2.0f; var v0 = new Vector3(Mathf.Cos(angle0), 0.0f, Mathf.Sin(angle0)).normalized; var upVector = ((bottomCenter + new Vector3(0, height, 0)) + v0 * radius1) - (bottomCenter + v0 * radius0); upVector.Normalize(); float angle1 = ((float)(i + 1) / sides) * Mathf.PI * 2.0f; var v1 = new Vector3(Mathf.Cos(angle1), 0.0f, Mathf.Sin(angle1)).normalized; var upVector1 = ((bottomCenter + new Vector3(0, height, 0)) + v1 * radius1) - (bottomCenter + v1 * radius0); upVector1.Normalize(); var currHeight = 0.0f; var triVert = vertIndex; var normal = (v0 + v1).normalized; for (int j = 0; j <= heightSegments; j++) { // generate vertices for outer side vertices[vertIndex + 0] = bottomCenter + v0 * radius0 + upVector * currHeight + pivotOffset; vertices[vertIndex + 1] = bottomCenter + v1 * radius0 + upVector1 * currHeight + pivotOffset; normals[vertIndex + 0] = normal; normals[vertIndex + 1] = normal; uvs[vertIndex + 0] = new Vector2((float)i / sides, (float)j / heightSegments); uvs[vertIndex + 1] = new Vector2((float)(i + 1) / sides, (float)j / heightSegments); // generate vertices for inner side vertices[vertIndex + innerVertOffset + 0] = bottomCenter + v0 * thicknessRadius + upVector * currHeight + pivotOffset; vertices[vertIndex + innerVertOffset + 1] = bottomCenter + v1 * thicknessRadius + upVector1 * currHeight + pivotOffset; normals[vertIndex + innerVertOffset + 0] = -normal; normals[vertIndex + innerVertOffset + 1] = -normal; uvs[vertIndex + innerVertOffset + 0] = new Vector2((float)i / sides, (float)j / heightSegments); uvs[vertIndex + innerVertOffset + 1] = new Vector2((float)(i + 1) / sides, (float)j / heightSegments); vertIndex += 2; currHeight += heightRatio; } for (int j = 0; j < heightSegments; j++) { // generate triangles for outer side triangles[triIndex + 0] = triVert + 0; triangles[triIndex + 1] = triVert + 2; triangles[triIndex + 2] = triVert + 1; triangles[triIndex + 3] = triVert + 2; triangles[triIndex + 4] = triVert + 3; triangles[triIndex + 5] = triVert + 1; // generate triangles for inner side triangles[triIndex + innerTriOffset + 0] = triVert + innerVertOffset + 1; triangles[triIndex + innerTriOffset + 1] = triVert + innerVertOffset + 2; triangles[triIndex + innerTriOffset + 2] = triVert + innerVertOffset + 0; triangles[triIndex + innerTriOffset + 3] = triVert + innerVertOffset + 1; triangles[triIndex + innerTriOffset + 4] = triVert + innerVertOffset + 3; triangles[triIndex + innerTriOffset + 5] = triVert + innerVertOffset + 2; triIndex += 6; triVert += 2; } } } else { var vertIndex = 0; var triIndex = 0; var triVert = 0; var heightRatio = coneHeight / heightSegments; for (int i = 0; i <= sidesPipe; i++) { float angle0 = ((float)i / sides) * Mathf.PI * 2.0f; var v0 = new Vector3(Mathf.Cos(angle0), 0.0f, Mathf.Sin(angle0)).normalized; var upVector = ((bottomCenter + new Vector3(0, height, 0)) + v0 * radius1) - (bottomCenter + v0 * radius0); upVector.Normalize(); var currHeight = 0.0f; for (int j = 0; j <= heightSegments; j++) { // generate vertices for outer side vertices[vertIndex + 0] = bottomCenter + v0 * radius0 + upVector * currHeight + pivotOffset; normals[vertIndex + 0] = v0; uvs[vertIndex + 0] = new Vector2((float)i / sides, (float)j / heightSegments); // generate vertices for inner side vertices[vertIndex + innerVertOffset + 0] = bottomCenter + v0 * thicknessRadius + upVector * currHeight + pivotOffset; normals[vertIndex + innerVertOffset + 0] = -v0; uvs[vertIndex + innerVertOffset + 0] = new Vector2((float)i / sides, (float)j / heightSegments); vertIndex += 1; currHeight += heightRatio; } } for (int i = 0; i < sidesPipe; i++) { var triVertNext = (i + 1) * (heightSegments + 1); for (int j = 0; j < heightSegments; j++) { triangles[triIndex + 0] = triVert + 0; triangles[triIndex + 1] = triVert + 1; triangles[triIndex + 2] = triVertNext + 0; triangles[triIndex + 3] = triVertNext + 0; triangles[triIndex + 4] = triVert + 1; triangles[triIndex + 5] = triVertNext + 1; triangles[triIndex + innerTriOffset + 0] = triVertNext + innerVertOffset + 0; triangles[triIndex + innerTriOffset + 1] = triVert + innerVertOffset + 1; triangles[triIndex + innerTriOffset + 2] = triVert + innerVertOffset + 0; triangles[triIndex + innerTriOffset + 3] = triVertNext + innerVertOffset + 1; triangles[triIndex + innerTriOffset + 4] = triVert + innerVertOffset + 1; triangles[triIndex + innerTriOffset + 5] = triVertNext + innerVertOffset + 0; triIndex += 6; triVert += 1; triVertNext += 1; } triVert += 1; } } // generate caps { var vertIndex = numVertices; var triIndex = numTriangles; var triVert = vertIndex; var downVertOffset = numVerticesCaps / 2; var downTriOffset = numTrianglesCaps / 2; var downUVRatio = 0.5f * (radius0 / radius1); for (int i = 0; i <= sidesPipe; i++) { float angle0 = ((float)i / sides) * Mathf.PI * 2.0f; var v0 = new Vector3(Mathf.Cos(angle0), 0.0f, Mathf.Sin(angle0)).normalized; var upVector = ((bottomCenter + new Vector3(0, height, 0)) + v0 * radius1) - (bottomCenter + v0 * radius0); upVector.Normalize(); // generate top caps vertices[vertIndex + 1] = bottomCenter + v0 * radius0 + upVector * coneHeight + pivotOffset; normals[vertIndex + 1] = new Vector3(0, 1, 0); vertices[vertIndex + 0] = bottomCenter + v0 * thicknessRadius + upVector * coneHeight + pivotOffset; normals[vertIndex + 0] = new Vector3(0, 1, 0); var uvV = new Vector2(v0.x * 0.5f, v0.z * .5f); var uvVInner = new Vector2(v0.x * downUVRatio, v0.z * downUVRatio); var uvCenter = new Vector2(0.5f, 0.5f); var ratio = (float)i / sides; if (radialMapping) { uvs[vertIndex + 0] = new Vector2(ratio, 1.0f); uvs[vertIndex + 1] = new Vector2(ratio, 0.0f); } else { uvs[vertIndex + 0] = uvCenter + uvVInner; uvs[vertIndex + 1] = uvCenter + uvV; } // generate down caps vertices[vertIndex + downVertOffset + 1] = bottomCenter + v0 * radius0 + pivotOffset; normals[vertIndex + downVertOffset + 1] = new Vector3(0, -1, 0); vertices[vertIndex + downVertOffset + 0] = bottomCenter + v0 * thicknessRadius + pivotOffset; normals[vertIndex + downVertOffset + 0] = new Vector3(0, -1, 0); if (radialMapping) { uvs[vertIndex + downVertOffset + 0] = new Vector2(ratio, 1.0f); uvs[vertIndex + downVertOffset + 1] = new Vector2(ratio, 0.0f); } else { uvs[vertIndex + downVertOffset + 0] = uvCenter + uvVInner; uvs[vertIndex + downVertOffset + 1] = uvCenter + uvV; } vertIndex += 2; } for (int i = 0; i < sidesPipe; i++) { var triVertNext = numVertices + (i + 1) * 2; var triVertNextDown = numVertices + downVertOffset + (i + 1) * 2; triangles[triIndex + 0] = triVertNext + 0; triangles[triIndex + 1] = triVert + 1; triangles[triIndex + 2] = triVert + 0; triangles[triIndex + 3] = triVertNext + 1; triangles[triIndex + 4] = triVert + 1; triangles[triIndex + 5] = triVertNext + 0; triangles[triIndex + downTriOffset + 0] = triVert + downVertOffset + 0; triangles[triIndex + downTriOffset + 1] = triVert + downVertOffset + 1; triangles[triIndex + downTriOffset + 2] = triVertNextDown + 0; triangles[triIndex + downTriOffset + 3] = triVertNextDown + 0; triangles[triIndex + downTriOffset + 4] = triVert + downVertOffset + 1; triangles[triIndex + downTriOffset + 5] = triVertNextDown + 1; triIndex += 6; triVert += 2; } } // generate pipe caps if (sidesPipe < sides) { var triIndex = numTriangles + numTrianglesCaps; var vertIndex = numVertices + numVerticesCaps; // vertices if (normalsType == NormalsType.Vertex) { vertices[vertIndex + 0] = vertices[0]; vertices[vertIndex + 1] = vertices[innerVertOffset]; vertices[vertIndex + 2] = vertices[heightSegments]; vertices[vertIndex + 3] = vertices[innerVertOffset + heightSegments]; vertices[vertIndex + 4] = vertices[(sidesPipe) * (heightSegments + 1) + heightSegments]; vertices[vertIndex + 5] = vertices[(sidesPipe) * (heightSegments + 1) + innerVertOffset]; vertices[vertIndex + 6] = vertices[(sidesPipe) * (heightSegments + 1)]; vertices[vertIndex + 7] = vertices[(sidesPipe) * (heightSegments + 1) + innerVertOffset + heightSegments]; } else { vertices[vertIndex + 0] = vertices[0]; vertices[vertIndex + 1] = vertices[innerVertOffset]; vertices[vertIndex + 2] = vertices[heightSegments * 2]; vertices[vertIndex + 3] = vertices[innerVertOffset + heightSegments * 2]; vertices[vertIndex + 4] = vertices[(sidesPipe - 1) * ((heightSegments + 1) * 2) + (heightSegments) * 2 + 1]; vertices[vertIndex + 5] = vertices[(sidesPipe - 1) * ((heightSegments + 1) * 2) + innerVertOffset + 1]; vertices[vertIndex + 6] = vertices[(sidesPipe - 1) * ((heightSegments + 1) * 2) + 1]; vertices[vertIndex + 7] = vertices[(sidesPipe - 1) * ((heightSegments + 1) * 2) + innerVertOffset + (heightSegments) * 2 + 1]; } // triangles triangles[triIndex + 0] = vertIndex + 0; triangles[triIndex + 1] = vertIndex + 1; triangles[triIndex + 2] = vertIndex + 2; triangles[triIndex + 3] = vertIndex + 3; triangles[triIndex + 4] = vertIndex + 2; triangles[triIndex + 5] = vertIndex + 1; triangles[triIndex + 6] = vertIndex + 4; triangles[triIndex + 7] = vertIndex + 5; triangles[triIndex + 8] = vertIndex + 6; triangles[triIndex + 9] = vertIndex + 7; triangles[triIndex + 10] = vertIndex + 5; triangles[triIndex + 11] = vertIndex + 4; // normals var normal0 = Vector3.Cross(vertices[vertIndex + 1] - vertices[vertIndex + 0], vertices[vertIndex + 2] - vertices[vertIndex + 0]); normals[vertIndex + 0] = normal0; normals[vertIndex + 1] = normal0; normals[vertIndex + 2] = normal0; normals[vertIndex + 3] = normal0; var normal1 = Vector3.Cross(vertices[vertIndex + 5] - vertices[vertIndex + 4], vertices[vertIndex + 6] - vertices[vertIndex + 4]); normals[vertIndex + 4] = normal1; normals[vertIndex + 5] = normal1; normals[vertIndex + 6] = normal1; normals[vertIndex + 7] = normal1; // uvs // var uvInnerRatio = radius0/radius1; // uvs[vertIndex + 0] = new Vector2(0.0f, uvInnerRatio); // uvs[vertIndex + 1] = new Vector2(0.0f, 1.0f); // uvs[vertIndex + 2] = new Vector2(1.0f, uvInnerRatio); // uvs[vertIndex + 3] = new Vector2(1.0f, 1.0f); // // uvs[vertIndex + 4] = new Vector2(0.0f, 0.0f); // uvs[vertIndex + 5] = new Vector2(1.0f, 1.0f-uvInnerRatio); // uvs[vertIndex + 6] = new Vector2(1.0f, 0.0f); // uvs[vertIndex + 7] = new Vector2(0.0f, 1.0f-uvInnerRatio); // this code is responsible for uv mapping of the inside of tube slice cap // VERTICAL MAPPING FIX: uvs[vertIndex + 0] = new Vector2(1.0f, 1.0f); uvs[vertIndex + 1] = new Vector2(0.0f, 1.0f); uvs[vertIndex + 2] = new Vector2(1.0f, 0.0f); uvs[vertIndex + 3] = new Vector2(0.0f, 0.0f); uvs[vertIndex + 4] = new Vector2(1.0f, 1.0f); uvs[vertIndex + 5] = new Vector2(0.0f, 0.0f); uvs[vertIndex + 6] = new Vector2(1.0f, 0.0f); uvs[vertIndex + 7] = new Vector2(0.0f, 1.0f); } mesh.vertices = vertices; mesh.normals = normals; mesh.uv = uvs; mesh.triangles = triangles; mesh.RecalculateBounds(); MeshUtils.CalculateTangents(mesh); mesh.Optimize(); stopWatch.Stop(); return(stopWatch.ElapsedMilliseconds); }
// Token: 0x06004201 RID: 16897 RVA: 0x0014F99C File Offset: 0x0014DD9C public static Cylinder Create(float radius, float height, int sides, int heightSegments, NormalsType normals, PivotPosition pivotPosition) { GameObject gameObject = new GameObject("CylinderPro"); gameObject.AddComponent <MeshFilter>(); MeshRenderer meshRenderer = gameObject.AddComponent <MeshRenderer>(); meshRenderer.sharedMaterial = new Material(Shader.Find("Diffuse")); Cylinder cylinder = gameObject.AddComponent <Cylinder>(); cylinder.GenerateGeometry(radius, height, sides, heightSegments, normals, pivotPosition); return(cylinder); }
// Token: 0x06004318 RID: 17176 RVA: 0x0015EF24 File Offset: 0x0015D324 public static float GenerateGeometry(Mesh mesh, float torusRadius, float coneRadius, int torusSegments, int coneSegments, float slice, NormalsType normalsType, PivotPosition pivotPosition) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); torusRadius = Mathf.Clamp(torusRadius, 0f, 100f); coneRadius = Mathf.Clamp(coneRadius, 0f, 100f); torusSegments = Mathf.Clamp(torusSegments, 3, 250); coneSegments = Mathf.Clamp(coneSegments, 3, 100); slice = Mathf.Clamp(slice, 0f, 360f); mesh.Clear(); slice /= 360f; int num = (int)((float)torusSegments * (1f - slice)); if (num == 0) { num = 1; } int num2 = 2 * coneSegments * num; int num3; if (normalsType == NormalsType.Vertex) { num3 = (num + 1) * (coneSegments + 1); } else { num3 = num2 * 3; } int num4 = 0; int num5 = 0; if (num < torusSegments) { num4 = (coneSegments + 1) * 2; num5 = (coneSegments + 2) * 2; } if (num3 > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return(0f); } Vector3[] array = new Vector3[num3 + num5]; Vector3[] array2 = new Vector3[num3 + num5]; Vector2[] array3 = new Vector2[num3 + num5]; int[] array4 = new int[num2 * 3 + num4 * 3]; float num6 = 0f; float num7 = 6.28318548f / (float)torusSegments; Vector3 b = default(Vector3); Vector3 vector = default(Vector3); int num8 = 0; int num9 = 0; int num10 = num2 * 3; int num11 = num3 + 1; int num12 = array.Length - 1; Vector3 zero = Vector3.zero; if (pivotPosition != PivotPosition.Botttom) { if (pivotPosition == PivotPosition.Top) { zero = new Vector3(0f, -coneRadius, 0f); } } else { zero = new Vector3(0f, coneRadius, 0f); } for (int i = 0; i <= num + 1; i++) { num6 += num7; b = vector; vector = new Vector3(torusRadius * Mathf.Cos(num6), 0f, torusRadius * Mathf.Sin(num6)); if (i > 0) { Vector3 vector2 = vector - b; Vector3 vector3 = vector + b; Vector3 vector4 = Vector3.Cross(vector2, vector3); vector3 = Vector3.Cross(vector4, vector2); vector3.Normalize(); vector4.Normalize(); float num13 = 0f; float num14 = 6.28318548f / (float)coneSegments; for (int j = 0; j <= coneSegments; j++) { num13 += num14; float d = coneRadius * Mathf.Sin(num13); float d2 = coneRadius * Mathf.Cos(num13); Vector3 b2 = vector3 * d + vector4 * d2; array[num8] = vector + b2 + zero; array2[num8] = b2.normalized; array3[num8] = new Vector2(1f - (float)(i - 1) / (float)torusSegments, (float)j / (float)coneSegments); num8++; if (num < torusSegments) { if (i == num + 1) { array[num3] = vector + zero; array3[num3] = new Vector2(0.5f, 0.5f); array2[num3] = vector2.normalized; array[num11] = vector + b2 + zero; array2[num11] = vector2.normalized; array3[num11] = new Vector2(0.5f, 0.5f) + new Vector2(b2.x * 0.5f, b2.y * 0.5f); if (j < coneSegments) { array4[num10] = num11 + 1; array4[num10 + 1] = num11; array4[num10 + 2] = num3; num10 += 3; } num11++; } else if (i == 1) { Vector3 a = vector; array[num12] = a + zero; array3[num12] = new Vector2(0.5f, 0.5f); array2[num12] = -vector2.normalized; array[num11] = a + b2 + zero; array2[num11] = -vector2.normalized; array3[num11] = new Vector2(0.5f, 0.5f) + new Vector2(b2.x * 0.5f, b2.y * 0.5f); if (j < coneSegments) { array4[num10] = num12; array4[num10 + 1] = num11; array4[num10 + 2] = num11 + 1; num10 += 3; } num11++; } } } if (i <= num) { int num15 = (i - 1) * (coneSegments + 1); int num16 = i * (coneSegments + 1); int num17 = 0; for (int k = 0; k < coneSegments; k++) { array4[num9] = num16 + num17; array4[num9 + 1] = num15 + 1 + num17; array4[num9 + 2] = num15 + num17; array4[num9 + 3] = num16 + 1 + num17; array4[num9 + 4] = num15 + 1 + num17; array4[num9 + 5] = num16 + num17; num9 += 6; num17++; } } } } if (normalsType == NormalsType.Face) { MeshUtils.DuplicateSharedVertices(ref array, ref array3, array4, -1); } mesh.vertices = array; mesh.triangles = array4; if (normalsType == NormalsType.Vertex) { mesh.normals = array2; } else { mesh.RecalculateNormals(); } mesh.uv = array3; mesh.RecalculateBounds(); MeshUtils.CalculateTangents(mesh); stopwatch.Stop(); return((float)stopwatch.ElapsedMilliseconds); }
/// <summary> /// create Capsule game object /// </summary> /// <param name="radius">radius of capsule</param> /// <param name="sides">number of segments</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="heightSegments">number of segments of central cylinder</param> /// <param name="pivotPosition">position of the model pivot</param> public void GenerateGeometry(float radius, float height, int sides, int heightSegments, bool preserverHeight, NormalsType normalsType, PivotPosition pivotPosition) { // generate new mesh and clear old one var meshFilter = GetComponent <MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; // generate geometry GenerationTimeMS = Primitives.CapsulePrimitive.GenerateGeometry(mesh, radius, height, sides, heightSegments, preserverHeight, normalsType, pivotPosition); this.radius = radius; this.height = height; this.heightSegments = heightSegments; this.sides = sides; this.preserveHeight = preserverHeight; this.normalsType = normalsType; this.flipNormals = false; this.pivotPosition = pivotPosition; }
/// <summary> /// generate mesh geometry for SuperEllipsoid /// </summary> /// <param name="mesh">mesh to be generated</param> /// <param name="width">width of roundedCube</param> /// <param name="height">height of roundedCube</param> /// <param name="length">length of roundedCube</param> /// <param name="segments">number of segments</param> /// <param name="n2">second parameter of superellipsoid</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> /// <param name="n1">second parameter of superellipsoid</param> public static float GenerateGeometry(Mesh mesh, float width, float height, float length, int segments, float n1, float n2, NormalsType normalsType, PivotPosition pivotPosition) { var stopWatch = new Stopwatch(); stopWatch.Start(); width = Mathf.Clamp(width, 0, 100); length = Mathf.Clamp(length, 0, 100); height = Mathf.Clamp(height, 0, 100); segments = Mathf.Clamp(segments, 1, 100); n1 = Mathf.Clamp(n1, 0.01f, 5.0f); n2 = Mathf.Clamp(n2, 0.01f, 5.0f); mesh.Clear(); // to fix spherical uv generation use only segments % 3 segments = segments * 4 - 1; segments += 5; var numVertices = (segments + 1) * (segments / 2 + 1); var numTriangles = segments * (segments / 2) * 6; if (normalsType == NormalsType.Face) { numVertices = numTriangles; } if (numVertices > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return(0.0f); } var vertices = new Vector3[numVertices]; var uvs = new Vector2[numVertices]; var triangles = new int[numTriangles]; var pivotOffset = Vector3.zero; switch (pivotPosition) { case PivotPosition.Botttom: pivotOffset = new Vector3(0.0f, height, 0.0f); break; case PivotPosition.Top: pivotOffset = new Vector3(0.0f, -height, 0.0f); break; } var vertIndex = 0; for (int j = 0; j <= segments / 2; j++) { for (int i = 0; i <= segments; i++) { var index = j * (segments + 1) + i; var theta = i * 2.0f * Mathf.PI / segments; var phi = -0.5f * Mathf.PI + Mathf.PI * j / (segments / 2.0f); // make unit sphere, power determines roundness vertices[index].x = RPower(Mathf.Cos(phi), n1) * RPower(Mathf.Cos(theta), n2) * width; vertices[index].z = RPower(Mathf.Cos(phi), n1) * RPower(Mathf.Sin(theta), n2) * length; vertices[index].y = RPower(Mathf.Sin(phi), n1) * height; // compute uv spherical mapping uvs[index].x = Mathf.Atan2(vertices[index].z, vertices[index].x) / (2.0f * Mathf.PI); if (uvs[index].x < 0) { uvs[index].x = 1 + uvs[index].x; } uvs[index].y = 0.5f + Mathf.Atan2(vertices[index].y, Mathf.Sqrt(vertices[index].x * vertices[index].x + vertices[index].z * vertices[index].z)) / Mathf.PI; // fix seams if (j == 0) { vertices[index].x = 0; vertices[index].y = -height; vertices[index].z = 0; uvs[index].y = 0.0f; uvs[index].x = 0; } if (j == segments / 2) { vertices[index].x = 0; vertices[index].y = height; vertices[index].z = 0; uvs[index].y = 1.0f; uvs[index].x = uvs[(j - 1) * (segments + 1) + i].x; } if (i == segments) { vertices[index].x = vertices[j * (segments + 1)].x; vertices[index].z = vertices[j * (segments + 1)].z; uvs[index].x = 1.0f; } vertices[index] += pivotOffset; if (vertIndex < index) { vertIndex = index; } } } // fix uv seam for (int i = 0; i <= segments; i++) { var indexNext = (segments + 1) + i; uvs[i].x = uvs[indexNext].x; } var triIndex = 0; for (int j = 0; j < segments / 2; j++) { for (int i = 0; i < segments; i++) { var i1 = j * (segments + 1) + i; var i2 = j * (segments + 1) + (i + 1); var i3 = (j + 1) * (segments + 1) + (i + 1); var i4 = (j + 1) * (segments + 1) + i; triangles[triIndex + 0] = i3; triangles[triIndex + 1] = i2; triangles[triIndex + 2] = i1; triangles[triIndex + 3] = i4; triangles[triIndex + 4] = i3; triangles[triIndex + 5] = i1; triIndex += 6; } } if (normalsType == NormalsType.Face) { MeshUtils.DuplicateSharedVertices(ref vertices, ref uvs, triangles, -1); } mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; if (normalsType == NormalsType.Vertex) { Vector3[] normals = null; MeshUtils.ComputeVertexNormals(vertices, triangles, out normals); // fix normals seam for (int j = 0; j < segments / 2; j++) { var index0 = j * (segments + 1); var index = j * (segments + 1) + segments; normals[index] = normals[index0]; } mesh.normals = normals; } else { mesh.RecalculateNormals(); } mesh.RecalculateBounds(); MeshUtils.CalculateTangents(mesh); ; stopWatch.Stop(); return(stopWatch.ElapsedMilliseconds); }
// Token: 0x06004242 RID: 16962 RVA: 0x00150B3C File Offset: 0x0014EF3C public void GenerateGeometry(float width, float height, float length, int segments, float roundness, NormalsType normalsType, PivotPosition pivotPosition) { MeshFilter component = base.GetComponent <MeshFilter>(); if (component.sharedMesh == null) { component.sharedMesh = new Mesh(); } Mesh sharedMesh = component.sharedMesh; base.GenerationTimeMS = SuperEllipsoidPrimitive.GenerateGeometry(sharedMesh, width, height, length, segments, roundness, roundness, normalsType, pivotPosition); this.width = width; this.height = height; this.length = length; this.segments = segments; this.roundness = roundness; this.normalsType = normalsType; this.flipNormals = false; this.pivotPosition = pivotPosition; }
/// <summary> /// re/generate mesh geometry based on parameters /// </summary> /// <param name="width">width of cube</param> /// <param name="height">height of cube</param> /// <param name="depth">depth of cube</param> /// <param name="widthSegments">number of triangle segments in width direction</param> /// <param name="heightSegments">number of triangle segments in height direction</param> /// <param name="depthSegments">number of triangle segments in depth direction</param> /// <param name="cubeMap">enable 6-sides cube map uv mapping</param> /// <param name="edgeOffset"></param> /// <param name="pivot">position of the model pivot</param> public void GenerateGeometry(float width, float height, float depth, int widthSegments, int heightSegments, int depthSegments, bool cubeMap, float[] edgeOffset, PivotPosition pivot) { // generate new mesh and clear old one var meshFilter = GetComponent <MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; mesh.Clear(); // generate geometry GenerationTimeMS = Primitives.BoxPrimitive.GenerateGeometry(mesh, width, height, depth, widthSegments, heightSegments, depthSegments, cubeMap, edgeOffset, flipUVMapping, pivot); this.width = width; this.height = height; this.depth = depth; this.widthSegments = widthSegments; this.heightSegments = heightSegments; this.depthSegments = depthSegments; this.cubeMap = cubeMap; this.flipNormals = false; this.pivotPosition = pivot; this.edgeOffsets = edgeOffset; }
/// <summary> /// re/generate mesh geometry based on parameters /// </summary> /// <param name="radius0">fist radius of cone</param> /// <param name="radius1">second radius of cone</param> /// <param name="height">height of cone</param> /// <param name="sides">number of triangle segments in radius</param> /// <param name="heightSegments">number of triangle segments in height</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> /// <returns>Cone class with Cone game object</returns> public void GenerateGeometry(float radius0, float radius1, float thickness, float height, int sides, int heightSegments, NormalsType normalsType, PivotPosition pivotPosition) { // generate new mesh and clear old one var meshFilter = GetComponent <MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; // generate geometry if (thickness >= 0) { GenerationTimeMS = Primitives.HollowConePrimitive.GenerateGeometry(mesh, radius0, radius1, thickness, height, sides, heightSegments, normalsType, pivotPosition); } else { GenerationTimeMS = Primitives.ConePrimitive.GenerateGeometry(mesh, radius0, radius1, height, sides, heightSegments, normalsType, pivotPosition); } this.radius0 = radius0; this.radius1 = radius1; this.height = height; this.thickness = thickness; this.sides = sides; this.heightSegments = heightSegments; this.normalsType = normalsType; this.flipNormals = false; this.pivotPosition = pivotPosition; }
/// <summary> /// create Box game object /// </summary> /// <param name="width">width of cube</param> /// <param name="height">height of cube</param> /// <param name="depth">depth of cube</param> /// <param name="widthSegments">number of triangle segments in width direction</param> /// <param name="heightSegments">number of triangle segments in height direction</param> /// <param name="depthSegments">number of triangle segments in depth direction</param> /// <param name="edgeOffset"></param> /// <param name="pivot">position of the model pivot</param> /// <param name="cubeMap">enable 6-sides cube map uv mapping</param> /// <returns>Box class with Box game object</returns> public static Box Create(float width, float height, float depth, int widthSegments, int heightSegments, int depthSegments, bool cubeMap, float[] edgeOffset, PivotPosition pivot) { var planeObject = new GameObject("BoxPro"); planeObject.AddComponent <MeshFilter>(); var renderer = planeObject.AddComponent <MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); if (edgeOffset == null || edgeOffset.Length != 4) { edgeOffset = new float[4]; } var cube = planeObject.AddComponent <Box>(); cube.GenerateGeometry(width, height, depth, widthSegments, heightSegments, depthSegments, cubeMap, edgeOffset, pivot); return(cube); }
/// <summary> /// generate geometry for capsule /// </summary> /// <param name="mesh">mesh to be generated</param> /// <param name="radius">radius of capsule</param> /// <param name="sides">number of segments</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="heightSegments">number of segments of central cylinder</param> /// <param name="pivotPosition">position of the model pivot</param> public static float GenerateGeometry(Mesh mesh, float radius, float height, int sides, int heightSegments, bool preserveHeight, NormalsType normalsType, PivotPosition pivotPosition) { var stopWatch = new Stopwatch(); stopWatch.Start(); radius = Mathf.Clamp(radius, 0, 100); height = Mathf.Clamp(height, 0, 100); heightSegments = Mathf.Clamp(heightSegments, 1, 250); sides = Mathf.Clamp(sides, 4, 250); mesh.Clear(); if (preserveHeight) { height = height - radius * 2; if (height < 0) { height = 0; } } int rings = sides; int sectors = sides+1; if ((rings&1) == 0) { rings += 1; sectors = sides+1; } float R = 1 / (float)(rings - 1); float S = 1 / (float)(sectors - 1); var midRing = rings/2 + 1; int verticesNum = 0; var trianglesNum = (rings - 1) * (sectors - 1) * 6; var verticesCylinder = (sides + 1) * (heightSegments + 1); var trianglesCylinder = sides * 6 * heightSegments; if (normalsType == NormalsType.Vertex) { verticesNum = rings*sectors +sectors; } else { verticesNum = (midRing - 1)*(sectors - 1)*4 + ((rings - 1) - (midRing - 1))*(sectors - 1)*4; verticesCylinder = sides * (4 + (heightSegments - 1) * 2); } if (verticesNum + verticesCylinder > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return 0.0f; } var vertices = new Vector3[verticesNum + verticesCylinder]; var normals = new Vector3[verticesNum + verticesCylinder]; var uvs = new Vector2[verticesNum + verticesCylinder]; var triangles = new int[trianglesNum + trianglesCylinder]; var capsuleRadius = radius + height/2; var pivotOffset = Vector3.zero; switch (pivotPosition) { case PivotPosition.Botttom: pivotOffset = new Vector3(0.0f, capsuleRadius, 0.0f); break; case PivotPosition.Top: pivotOffset = new Vector3(0.0f, -capsuleRadius, 0.0f); break; } var vertIndex = 0; var triIndex = 0; var vertIndexCyl = 0; var triIndexCyl = 0; // calculate capsule with vertex normals if (normalsType == NormalsType.Vertex) { // generate upper hemisphere for (int r = 0; r < midRing; r++) { var y = Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * r * R); var sinR = Mathf.Sin(Mathf.PI * r * R); for (int s = 0; s < sectors; s++) { float x = Mathf.Sin(2*Mathf.PI*s*S)*sinR; float z = Mathf.Cos(2 * Mathf.PI * s * S) * sinR; vertices[vertIndex + 0] = new Vector3(x, y, z) * radius + pivotOffset; normals[vertIndex + 0] = new Vector3(x, y, z); vertices[vertIndex + 0].y += height / 2; var uv = GetSphericalUV(vertices[vertIndex + 0] - pivotOffset); //uvs[vertIndex + 0] = new Vector2(s * S, 1.0f - (r * R)); uvs[vertIndex + 0] = new Vector2(1.0f - s * S, uv.y); if (r < midRing-1 && s < sectors - 1) { triangles[triIndex + 0] = (r + 1) * sectors + (s); triangles[triIndex + 1] = r * sectors + (s + 1); triangles[triIndex + 2] = r * sectors + s; triangles[triIndex + 3] = (r + 1) * sectors + (s + 1); triangles[triIndex + 4] = r * sectors + (s + 1); triangles[triIndex + 5] = (r + 1) * sectors + (s); triIndex += 6; } vertIndex += 1; } } // generate central cylinder if (height > 0) { vertIndexCyl = verticesNum; triIndexCyl = trianglesNum; var triVertCyl = verticesNum; var heightRatio = height/heightSegments; var bottomCenter = new Vector3(0.0f, -height/2, 0.0f); var sinR = Mathf.Sin(Mathf.PI * (midRing-1) * R); for (int s = 0; s <= sides; s++) { float x = Mathf.Sin(2 * Mathf.PI * s * S) * sinR; float z = Mathf.Cos(2 * Mathf.PI * s * S) * sinR; var v0 = new Vector3(x, 0.0f, z); // var texV = (midRing - 1) * R; var currHeight = 0.0f; for (int j = 0; j <= heightSegments; j++) { // generate vertices vertices[vertIndexCyl + 0] = bottomCenter + v0 * radius + new Vector3(0.0f, currHeight, 0.0f) + pivotOffset; normals[vertIndexCyl + 0] = v0; // uvs[vertIndexCyl + 0] = new Vector2(s * S, texV/2); // // texV += 1.0f/heightSegments; var uv = GetSphericalUV(vertices[vertIndexCyl + 0] - pivotOffset); // uvs[vertIndex + 0] = new Vector2(s * S, 1.0f - (r * R)); uvs[vertIndexCyl + 0] = new Vector2(1.0f - s * S, uv.y); vertIndexCyl += 1; currHeight += heightRatio; } } for (int i = 0; i < sides; i++) { var triVertNext = verticesNum + (i + 1)*(heightSegments + 1); for (int j = 0; j < heightSegments; j++) { triangles[triIndexCyl + 0] = triVertNext + 0; triangles[triIndexCyl + 1] = triVertNext + 1; triangles[triIndexCyl + 2] = triVertCyl + 0; triangles[triIndexCyl + 3] = triVertNext + 1; triangles[triIndexCyl + 4] = triVertCyl + 1; triangles[triIndexCyl + 5] = triVertCyl + 0; triIndexCyl += 6; triVertCyl += 1; triVertNext += 1; } triVertCyl += 1; } } // generate bottom hemisphere for (int r = midRing-1; r < rings; r++) { var y = Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * r * R); var sinR = Mathf.Sin(Mathf.PI * r * R); for (int s = 0; s < sectors; s++) { float x = Mathf.Sin(2 * Mathf.PI * s * S) * sinR; float z = Mathf.Cos(2 * Mathf.PI * s * S) * sinR; vertices[vertIndex + 0] = new Vector3(x, y, z) * radius; normals[vertIndex + 0] = new Vector3(x, y, z); vertices[vertIndex] += pivotOffset; vertices[vertIndex + 0].y -= height/2; var uv = GetSphericalUV(vertices[vertIndex + 0] - pivotOffset); // uvs[vertIndex + 0] = new Vector2(s * S, 1.0f - (r * R)); uvs[vertIndex + 0] = new Vector2(1.0f - s * S, uv.y); if (r < rings-1 && s < sectors - 1) { triangles[triIndex + 0] = ((r+1) + 1) * sectors + (s); triangles[triIndex + 1] = (r+1) * sectors + (s + 1); triangles[triIndex + 2] = (r+1) * sectors + s; triangles[triIndex + 3] = ((r+1) + 1) * sectors + (s + 1); triangles[triIndex + 4] = (r+1) * sectors + (s + 1); triangles[triIndex + 5] = ((r+1) + 1) * sectors + (s); triIndex += 6; } vertIndex += 1; } } } else { // generate upper hemisphere for (int r = 0; r < midRing-1; r++) { var y = Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * r * R); var yNext = Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * (r+1) * R); var sinR = Mathf.Sin(Mathf.PI * r * R); var sinR1 = Mathf.Sin(Mathf.PI * (r + 1) * R); for (int s = 0; s < sectors-1; s++) { var sinS = Mathf.Sin(2*Mathf.PI*s*S); var sinS1 = Mathf.Sin(2*Mathf.PI*(s + 1)*S); var cosS = Mathf.Cos(2*Mathf.PI*(s)*S); var cosS1 = Mathf.Cos(2*Mathf.PI*(s + 1)*S); var x = sinS*sinR; var xNext = sinS1*sinR; var xNextR = sinS*sinR1; var xNextRS = sinS1*sinR1; var z = Mathf.Cos(2*Mathf.PI*s*S)*sinR; var zNext = cosS1*sinR; var zNextR = cosS*sinR1; var zNextRS = cosS1*sinR1; // r, s vertices[vertIndex + 0] = new Vector3(x, y, z) * radius + pivotOffset; // uvs[vertIndex + 0] = new Vector2(s * S, 1.0f - r * R); vertices[vertIndex + 0].y += height/2; var uv = GetSphericalUV(vertices[vertIndex + 0] - pivotOffset); uvs[vertIndex + 0] = new Vector2(1.0f - s * S, uv.y); // r+1, s vertices[vertIndex + 1] = new Vector3(xNextR, yNext, zNextR) * radius + pivotOffset; // uvs[vertIndex + 1] = new Vector2(s * S, 1.0f - (r+1) * R); vertices[vertIndex + 1].y += height / 2; uv = GetSphericalUV(vertices[vertIndex + 1] - pivotOffset); uvs[vertIndex + 1] = new Vector2(1.0f - s * S, uv.y); // r, s+1 vertices[vertIndex + 2] = new Vector3(xNext, y, zNext) * radius + pivotOffset; // uvs[vertIndex + 2] = new Vector2((s+1) * S, 1.0f - (r) * R); vertices[vertIndex + 2].y += height / 2; uv = GetSphericalUV(vertices[vertIndex + 2] - pivotOffset); uvs[vertIndex + 2] = new Vector2(1.0f - (s+1) * S, uv.y); // r+1, s+1 vertices[vertIndex + 3] = new Vector3(xNextRS, yNext, zNextRS) * radius + pivotOffset; // uvs[vertIndex + 3] = new Vector2((s+1) * S, 1.0f - (r+1) * R); vertices[vertIndex + 3].y += height / 2; uv = GetSphericalUV(vertices[vertIndex + 3] - pivotOffset); uvs[vertIndex + 3] = new Vector2(1.0f - (s+1) * S, uv.y); triangles[triIndex + 0] = vertIndex + 1; triangles[triIndex + 1] = vertIndex + 2; triangles[triIndex + 2] = vertIndex + 0; triangles[triIndex + 3] = vertIndex + 3; triangles[triIndex + 4] = vertIndex + 2; triangles[triIndex + 5] = vertIndex + 1; triIndex += 6; vertIndex += 4; } } // generate central cylinder if (height > 0) { vertIndexCyl = verticesNum; triIndexCyl = trianglesNum; var heightRatio = height / heightSegments; var bottomCenter = new Vector3(0.0f, -height / 2, 0.0f); var sinR = Mathf.Sin(Mathf.PI * (midRing - 1) * R); for (int s = 0; s < sides; s++) { var v0 = new Vector3(Mathf.Sin(2 * Mathf.PI * s * S) * sinR, 0.0f, Mathf.Cos(2 * Mathf.PI * s * S) * sinR); var v1 = new Vector3(Mathf.Sin(2 * Mathf.PI * (s+1) * S) * sinR, 0.0f, Mathf.Cos(2 * Mathf.PI * (s+1) * S) * sinR); // var texV = (midRing - 1) * R; var currHeight = 0.0f; var triVertCyl = vertIndexCyl; var normal = (v0 + v1).normalized; for (int j = 0; j <= heightSegments; j++) { // generate vertices vertices[vertIndexCyl + 0] = bottomCenter + v0 * radius + new Vector3(0.0f, currHeight, 0.0f) + pivotOffset; vertices[vertIndexCyl + 1] = bottomCenter + v1 * radius + new Vector3(0.0f, currHeight, 0.0f) + pivotOffset; normals[vertIndexCyl + 0] = normal; normals[vertIndexCyl + 1] = normal; // uvs[vertIndexCyl + 0] = new Vector2(s * S, texV/2); // uvs[vertIndexCyl + 1] = new Vector2((s + 1) * S, texV/2); // texV += 1.0f/heightSegments; var uv = GetSphericalUV(vertices[vertIndexCyl + 0] - pivotOffset); uvs[vertIndexCyl + 0] = new Vector2(1.0f - s * S, uv.y); uvs[vertIndexCyl + 1] = new Vector2(1.0f - (s+1) * S, uv.y); vertIndexCyl += 2; currHeight += heightRatio; } for (int j = 0; j < heightSegments; j++) { triangles[triIndexCyl + 0] = triVertCyl + 0; triangles[triIndexCyl + 1] = triVertCyl + 1; triangles[triIndexCyl + 2] = triVertCyl + 3; triangles[triIndexCyl + 3] = triVertCyl + 3; triangles[triIndexCyl + 4] = triVertCyl + 2; triangles[triIndexCyl + 5] = triVertCyl + 0; triIndexCyl += 6; triVertCyl += 2; } } } // generate bottom hemisphere for (int r = midRing-1; r < rings - 1; r++) { var y = Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * r * R); var yNext = Mathf.Cos(-Mathf.PI * 2.0f + Mathf.PI * (r + 1) * R); var sinR = Mathf.Sin(Mathf.PI * r * R); var sinR1 = Mathf.Sin(Mathf.PI * (r + 1) * R); for (int s = 0; s < sectors - 1; s++) { var sinS = Mathf.Sin(2 * Mathf.PI * s * S); var sinS1 = Mathf.Sin(2 * Mathf.PI * (s + 1) * S); var cosS = Mathf.Cos(2 * Mathf.PI * (s) * S); var cosS1 = Mathf.Cos(2 * Mathf.PI * (s + 1) * S); var x = sinS * sinR; var xNext = sinS1 * sinR; var xNextR = sinS * sinR1; var xNextRS = sinS1 * sinR1; var z = Mathf.Cos(2 * Mathf.PI * s * S) * sinR; var zNext = cosS1 * sinR; var zNextR = cosS * sinR1; var zNextRS = cosS1 * sinR1; // r, s vertices[vertIndex + 0] = new Vector3(x, y, z) * radius + pivotOffset; // uvs[vertIndex + 0] = new Vector2(s * S, 1.0f - r * R); vertices[vertIndex + 0].y -= height / 2; var uv = GetSphericalUV(vertices[vertIndex + 0] - pivotOffset); uvs[vertIndex + 0] = new Vector2(1.0f - s * S, uv.y); // r+1, s vertices[vertIndex + 1] = new Vector3(xNextR, yNext, zNextR) * radius + pivotOffset; // uvs[vertIndex + 1] = new Vector2(s * S, 1.0f - (r + 1) * R); vertices[vertIndex + 1].y -= height / 2; uv = GetSphericalUV(vertices[vertIndex + 1] - pivotOffset); uvs[vertIndex + 1] = new Vector2(1.0f - s * S, uv.y); // r, s+1 vertices[vertIndex + 2] = new Vector3(xNext, y, zNext) * radius + pivotOffset; // uvs[vertIndex + 2] = new Vector2((s + 1) * S, 1.0f - (r) * R); vertices[vertIndex + 2].y -= height / 2; uv = GetSphericalUV(vertices[vertIndex + 2] - pivotOffset); uvs[vertIndex + 2] = new Vector2(1.0f - (s+1) * S, uv.y); // r+1, s+1 vertices[vertIndex + 3] = new Vector3(xNextRS, yNext, zNextRS) * radius + pivotOffset; // uvs[vertIndex + 3] = new Vector2((s + 1) * S, 1.0f - (r + 1) * R); vertices[vertIndex + 3].y -= height / 2; uv = GetSphericalUV(vertices[vertIndex + 3] - pivotOffset); uvs[vertIndex + 3] = new Vector2(1.0f - (s + 1) * S, uv.y); triangles[triIndex + 0] = vertIndex + 1; triangles[triIndex + 1] = vertIndex + 2; triangles[triIndex + 2] = vertIndex + 0; triangles[triIndex + 3] = vertIndex + 3; triangles[triIndex + 4] = vertIndex + 2; triangles[triIndex + 5] = vertIndex + 1; triIndex += 6; vertIndex += 4; } } } mesh.vertices = vertices; mesh.normals = normals; mesh.uv = uvs; mesh.triangles = triangles; // generate normals by unity in face case if (normalsType == NormalsType.Face) { mesh.RecalculateNormals(); } mesh.RecalculateBounds(); MeshUtils.CalculateTangents(mesh); mesh.Optimize(); stopWatch.Stop(); return stopWatch.ElapsedMilliseconds; }
// Token: 0x06004305 RID: 17157 RVA: 0x0015B1AC File Offset: 0x001595AC public static float GenerateGeometry(Mesh mesh, float radius0, float radius1, float thickness, float height, int sides, int heightSegments, NormalsType normalsType, PivotPosition pivotPosition) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); radius0 = Mathf.Clamp(radius0, 0f, 100f); radius1 = Mathf.Clamp(radius1, 0f, 100f); height = Mathf.Clamp(height, 0f, 100f); sides = Mathf.Clamp(sides, 3, 100); heightSegments = Mathf.Clamp(heightSegments, 1, 100); float num = 0f; num /= 360f; mesh.Clear(); heightSegments = Mathf.Clamp(heightSegments, 1, 250); sides = Mathf.Clamp(sides, 3, 250); float num2 = Mathf.Min(radius0, radius1); if (thickness > num2) { thickness = num2; } float d = radius0 - thickness; bool flag = false; int num3 = (int)((float)sides * (1f - num)); if (num3 == 0) { num3 = 1; } int num4 = num3 * 6 * heightSegments * 2; int num5 = (num3 + 1) * 2 * 2; int num6 = num3 * 6 * 2; int num7 = 0; int num8 = 0; if (num3 < sides) { num7 = 12; num8 = 8; } int num9; if (normalsType == NormalsType.Face) { num9 = num3 * (4 + (heightSegments - 1) * 2) * 2; } else { num9 = (num3 + 1) * (heightSegments + 1) * 2; } if (num9 + num5 > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return(0f); } Vector3[] array = new Vector3[num9 + num5 + num8]; Vector3[] array2 = new Vector3[num9 + num5 + num8]; Vector2[] array3 = new Vector2[num9 + num5 + num8]; int[] array4 = new int[num4 + num6 + num7]; Vector3 zero = Vector3.zero; int num10 = num9 / 2; int num11 = num4 / 2; float magnitude = (new Vector3(zero.x + radius0, zero.y, zero.z) - new Vector3(zero.x + radius1, zero.y + height, zero.z)).magnitude; Vector3 zero2 = Vector3.zero; if (pivotPosition != PivotPosition.Center) { if (pivotPosition == PivotPosition.Top) { zero2 = new Vector3(0f, -height, 0f); } } else { zero2 = new Vector3(0f, -height / 2f, 0f); } if (normalsType == NormalsType.Face) { int num12 = 0; int num13 = 0; float num14 = magnitude / (float)heightSegments; for (int i = 0; i < num3; i++) { float f = (float)i / (float)sides * 3.14159274f * 2f; Vector3 vector = new Vector3(Mathf.Cos(f), 0f, Mathf.Sin(f)); Vector3 normalized = vector.normalized; Vector3 a = zero + new Vector3(0f, height, 0f) + normalized * radius1 - (zero + normalized * radius0); a.Normalize(); float f2 = (float)(i + 1) / (float)sides * 3.14159274f * 2f; Vector3 vector2 = new Vector3(Mathf.Cos(f2), 0f, Mathf.Sin(f2)); Vector3 normalized2 = vector2.normalized; Vector3 a2 = zero + new Vector3(0f, height, 0f) + normalized2 * radius1 - (zero + normalized2 * radius0); a2.Normalize(); float num15 = 0f; int num16 = num12; Vector3 normalized3 = (normalized + normalized2).normalized; for (int j = 0; j <= heightSegments; j++) { array[num12] = zero + normalized * radius0 + a * num15 + zero2; array[num12 + 1] = zero + normalized2 * radius0 + a2 * num15 + zero2; array2[num12] = normalized3; array2[num12 + 1] = normalized3; array3[num12] = new Vector2((float)i / (float)sides, (float)j / (float)heightSegments); array3[num12 + 1] = new Vector2((float)(i + 1) / (float)sides, (float)j / (float)heightSegments); array[num12 + num10] = zero + normalized * d + a * num15 + zero2; array[num12 + num10 + 1] = zero + normalized2 * d + a2 * num15 + zero2; array2[num12 + num10] = -normalized3; array2[num12 + num10 + 1] = -normalized3; array3[num12 + num10] = new Vector2((float)i / (float)sides, (float)j / (float)heightSegments); array3[num12 + num10 + 1] = new Vector2((float)(i + 1) / (float)sides, (float)j / (float)heightSegments); num12 += 2; num15 += num14; } for (int k = 0; k < heightSegments; k++) { array4[num13] = num16; array4[num13 + 1] = num16 + 2; array4[num13 + 2] = num16 + 1; array4[num13 + 3] = num16 + 2; array4[num13 + 4] = num16 + 3; array4[num13 + 5] = num16 + 1; array4[num13 + num11] = num16 + num10 + 1; array4[num13 + num11 + 1] = num16 + num10 + 2; array4[num13 + num11 + 2] = num16 + num10; array4[num13 + num11 + 3] = num16 + num10 + 1; array4[num13 + num11 + 4] = num16 + num10 + 3; array4[num13 + num11 + 5] = num16 + num10 + 2; num13 += 6; num16 += 2; } } } else { int num17 = 0; int num18 = 0; int num19 = 0; float num20 = magnitude / (float)heightSegments; for (int l = 0; l <= num3; l++) { float f3 = (float)l / (float)sides * 3.14159274f * 2f; Vector3 vector3 = new Vector3(Mathf.Cos(f3), 0f, Mathf.Sin(f3)); Vector3 normalized4 = vector3.normalized; Vector3 a3 = zero + new Vector3(0f, height, 0f) + normalized4 * radius1 - (zero + normalized4 * radius0); a3.Normalize(); float num21 = 0f; for (int m = 0; m <= heightSegments; m++) { array[num17] = zero + normalized4 * radius0 + a3 * num21 + zero2; array2[num17] = normalized4; array3[num17] = new Vector2((float)l / (float)sides, (float)m / (float)heightSegments); array[num17 + num10] = zero + normalized4 * d + a3 * num21 + zero2; array2[num17 + num10] = -normalized4; array3[num17 + num10] = new Vector2((float)l / (float)sides, (float)m / (float)heightSegments); num17++; num21 += num20; } } for (int n = 0; n < num3; n++) { int num22 = (n + 1) * (heightSegments + 1); for (int num23 = 0; num23 < heightSegments; num23++) { array4[num18] = num19; array4[num18 + 1] = num19 + 1; array4[num18 + 2] = num22; array4[num18 + 3] = num22; array4[num18 + 4] = num19 + 1; array4[num18 + 5] = num22 + 1; array4[num18 + num11] = num22 + num10; array4[num18 + num11 + 1] = num19 + num10 + 1; array4[num18 + num11 + 2] = num19 + num10; array4[num18 + num11 + 3] = num22 + num10 + 1; array4[num18 + num11 + 4] = num19 + num10 + 1; array4[num18 + num11 + 5] = num22 + num10; num18 += 6; num19++; num22++; } num19++; } } int num24 = num9; int num25 = num4; int num26 = num24; int num27 = num5 / 2; int num28 = num6 / 2; float num29 = 0.5f * (radius0 / radius1); for (int num30 = 0; num30 <= num3; num30++) { float f4 = (float)num30 / (float)sides * 3.14159274f * 2f; Vector3 vector4 = new Vector3(Mathf.Cos(f4), 0f, Mathf.Sin(f4)); Vector3 normalized5 = vector4.normalized; Vector3 a4 = zero + new Vector3(0f, height, 0f) + normalized5 * radius1 - (zero + normalized5 * radius0); a4.Normalize(); array[num24 + 1] = zero + normalized5 * radius0 + a4 * magnitude + zero2; array2[num24 + 1] = new Vector3(0f, 1f, 0f); array[num24] = zero + normalized5 * d + a4 * magnitude + zero2; array2[num24] = new Vector3(0f, 1f, 0f); Vector2 b = new Vector2(normalized5.x * 0.5f, normalized5.z * 0.5f); Vector2 b2 = new Vector2(normalized5.x * num29, normalized5.z * num29); Vector2 a5 = new Vector2(0.5f, 0.5f); float x = (float)num30 / (float)sides; if (flag) { array3[num24] = new Vector2(x, 1f); array3[num24 + 1] = new Vector2(x, 0f); } else { array3[num24] = a5 + b2; array3[num24 + 1] = a5 + b; } array[num24 + num27 + 1] = zero + normalized5 * radius0 + zero2; array2[num24 + num27 + 1] = new Vector3(0f, -1f, 0f); array[num24 + num27] = zero + normalized5 * d + zero2; array2[num24 + num27] = new Vector3(0f, -1f, 0f); if (flag) { array3[num24 + num27] = new Vector2(x, 1f); array3[num24 + num27 + 1] = new Vector2(x, 0f); } else { array3[num24 + num27] = a5 + b2; array3[num24 + num27 + 1] = a5 + b; } num24 += 2; } for (int num31 = 0; num31 < num3; num31++) { int num32 = num9 + (num31 + 1) * 2; int num33 = num9 + num27 + (num31 + 1) * 2; array4[num25] = num32; array4[num25 + 1] = num26 + 1; array4[num25 + 2] = num26; array4[num25 + 3] = num32 + 1; array4[num25 + 4] = num26 + 1; array4[num25 + 5] = num32; array4[num25 + num28] = num26 + num27; array4[num25 + num28 + 1] = num26 + num27 + 1; array4[num25 + num28 + 2] = num33; array4[num25 + num28 + 3] = num33; array4[num25 + num28 + 4] = num26 + num27 + 1; array4[num25 + num28 + 5] = num33 + 1; num25 += 6; num26 += 2; } if (num3 < sides) { int num34 = num4 + num6; int num35 = num9 + num5; if (normalsType == NormalsType.Vertex) { array[num35] = array[0]; array[num35 + 1] = array[num10]; array[num35 + 2] = array[heightSegments]; array[num35 + 3] = array[num10 + heightSegments]; array[num35 + 4] = array[num3 * (heightSegments + 1) + heightSegments]; array[num35 + 5] = array[num3 * (heightSegments + 1) + num10]; array[num35 + 6] = array[num3 * (heightSegments + 1)]; array[num35 + 7] = array[num3 * (heightSegments + 1) + num10 + heightSegments]; } else { array[num35] = array[0]; array[num35 + 1] = array[num10]; array[num35 + 2] = array[heightSegments * 2]; array[num35 + 3] = array[num10 + heightSegments * 2]; array[num35 + 4] = array[(num3 - 1) * ((heightSegments + 1) * 2) + heightSegments * 2 + 1]; array[num35 + 5] = array[(num3 - 1) * ((heightSegments + 1) * 2) + num10 + 1]; array[num35 + 6] = array[(num3 - 1) * ((heightSegments + 1) * 2) + 1]; array[num35 + 7] = array[(num3 - 1) * ((heightSegments + 1) * 2) + num10 + heightSegments * 2 + 1]; } array4[num34] = num35; array4[num34 + 1] = num35 + 1; array4[num34 + 2] = num35 + 2; array4[num34 + 3] = num35 + 3; array4[num34 + 4] = num35 + 2; array4[num34 + 5] = num35 + 1; array4[num34 + 6] = num35 + 4; array4[num34 + 7] = num35 + 5; array4[num34 + 8] = num35 + 6; array4[num34 + 9] = num35 + 7; array4[num34 + 10] = num35 + 5; array4[num34 + 11] = num35 + 4; Vector3 vector5 = Vector3.Cross(array[num35 + 1] - array[num35], array[num35 + 2] - array[num35]); array2[num35] = vector5; array2[num35 + 1] = vector5; array2[num35 + 2] = vector5; array2[num35 + 3] = vector5; Vector3 vector6 = Vector3.Cross(array[num35 + 5] - array[num35 + 4], array[num35 + 6] - array[num35 + 4]); array2[num35 + 4] = vector6; array2[num35 + 5] = vector6; array2[num35 + 6] = vector6; array2[num35 + 7] = vector6; array3[num35] = new Vector2(1f, 1f); array3[num35 + 1] = new Vector2(0f, 1f); array3[num35 + 2] = new Vector2(1f, 0f); array3[num35 + 3] = new Vector2(0f, 0f); array3[num35 + 4] = new Vector2(1f, 1f); array3[num35 + 5] = new Vector2(0f, 0f); array3[num35 + 6] = new Vector2(1f, 0f); array3[num35 + 7] = new Vector2(0f, 1f); } mesh.vertices = array; mesh.normals = array2; mesh.uv = array3; mesh.triangles = array4; mesh.RecalculateBounds(); MeshUtils.CalculateTangents(mesh); stopwatch.Stop(); return((float)stopwatch.ElapsedMilliseconds); }
// Token: 0x06004316 RID: 17174 RVA: 0x0015EB34 File Offset: 0x0015CF34 public static float GenerateGeometry(Mesh mesh, float torusRadius, float coneRadius, int torusSegments, int coneSegments, int P, int Q, NormalsType normalsType, PivotPosition pivotPosition) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); torusRadius = Mathf.Clamp(torusRadius, 0f, 100f); coneRadius = Mathf.Clamp(coneRadius, 0f, 100f); torusSegments = Mathf.Clamp(torusSegments, 3, 250); coneSegments = Mathf.Clamp(coneSegments, 3, 100); P = Mathf.Clamp(P, 1, 20); Q = Mathf.Clamp(Q, 1, 20); mesh.Clear(); int num = 2 * coneSegments * torusSegments; int num2; if (normalsType == NormalsType.Vertex) { num2 = (torusSegments + 1) * (coneSegments + 1); } else { num2 = num * 3; } if (num2 > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return(0f); } Vector3[] array = new Vector3[num2]; Vector3[] array2 = new Vector3[num2]; Vector2[] array3 = new Vector2[num2]; int[] array4 = new int[num * 3]; float num3 = 0f; float num4 = 6.28318548f / (float)torusSegments; Vector3 b = default(Vector3); Vector3 vector = default(Vector3); int num5 = 0; int num6 = 0; float num7 = float.MaxValue; float num8 = float.MinValue; for (int i = 0; i <= torusSegments + 1; i++) { num3 += num4; float num9 = torusRadius * 0.5f * (2f + Mathf.Sin((float)Q * num3)); b = vector; vector = new Vector3(num9 * Mathf.Cos((float)P * num3), num9 * Mathf.Cos((float)Q * num3), num9 * Mathf.Sin((float)P * num3)); if (i > 0) { Vector3 vector2 = vector - b; Vector3 vector3 = vector + b; Vector3 vector4 = Vector3.Cross(vector2, vector3); vector3 = Vector3.Cross(vector4, vector2); vector3.Normalize(); vector4.Normalize(); float num10 = 0f; float num11 = 6.28318548f / (float)coneSegments; for (int j = 0; j <= coneSegments; j++) { num10 += num11; float d = coneRadius * Mathf.Sin(num10); float d2 = coneRadius * Mathf.Cos(num10); Vector3 b2 = vector3 * d + vector4 * d2; array[num5] = vector + b2; array2[num5] = b2.normalized; array3[num5] = new Vector2((float)(i - 1) / (float)torusSegments, (float)j / (float)coneSegments); if (array[num5].y < num7) { num7 = array[num5].y; } if (array[num5].y > num8) { num8 = array[num5].y; } num5++; } if (i <= torusSegments) { int num12 = (i - 1) * (coneSegments + 1); int num13 = i * (coneSegments + 1); int num14 = 0; for (int k = 0; k < coneSegments; k++) { array4[num6] = num13 + num14; array4[num6 + 1] = num12 + 1 + num14; array4[num6 + 2] = num12 + num14; array4[num6 + 3] = num13 + 1 + num14; array4[num6 + 4] = num12 + 1 + num14; array4[num6 + 5] = num13 + num14; num6 += 6; num14++; } } } } if (pivotPosition != PivotPosition.Center) { float num15 = (pivotPosition != PivotPosition.Botttom) ? (-num8) : (-num7); for (int l = 0; l < array.Length; l++) { Vector3[] array5 = array; int num16 = l; array5[num16].y = array5[num16].y + num15; } } if (normalsType == NormalsType.Face) { MeshUtils.DuplicateSharedVertices(ref array, ref array3, array4, -1); } mesh.vertices = array; mesh.triangles = array4; if (normalsType == NormalsType.Vertex) { mesh.normals = array2; } else { mesh.RecalculateNormals(); } mesh.uv = array3; mesh.RecalculateBounds(); MeshUtils.CalculateTangents(mesh); stopwatch.Stop(); return((float)stopwatch.ElapsedMilliseconds); }
/// <summary> /// generate mesh geometry for box /// </summary> /// <param name="mesh">mesh to be generated</param> /// <param name="width">width of cube</param> /// <param name="height">height of cube</param> /// <param name="depth">depth of cube</param> /// <param name="widthSegments">number of triangle segments in width direction</param> /// <param name="heightSegments">number of triangle segments in height direction</param> /// <param name="depthSegments">number of triangle segments in depth direction</param> /// <param name="cubeMap">enable 6-sides cube map uv mapping</param> /// <param name="edgeOffsets">offsets on edges for creating a ramp</param> /// <param name="flipUV">flag to flip uv mapping</param> /// <param name="pivot">position of the model pivot</param> public static float GenerateGeometry(Mesh mesh, float width, float height, float depth, int widthSegments, int heightSegments, int depthSegments, bool cubeMap, float[] edgeOffsets, bool flipUV, PivotPosition pivot) { var stopWatch = new Stopwatch(); stopWatch.Start(); width = Mathf.Clamp(width, 0, 100); height = Mathf.Clamp(height, 0, 100); depth = Mathf.Clamp(depth, 0, 100); heightSegments = Mathf.Clamp(heightSegments, 1, 100); widthSegments = Mathf.Clamp(widthSegments, 1, 100); depthSegments = Mathf.Clamp(depthSegments, 1, 100); mesh.Clear(); var numTriangles = widthSegments * depthSegments * 6 + widthSegments * heightSegments * 6 + depthSegments * heightSegments * 6; var numVertices = (widthSegments + 1) * (depthSegments + 1) + (widthSegments + 1) * (heightSegments + 1) + (depthSegments + 1) * (heightSegments + 1); numTriangles *= 2; numVertices *= 2; var pivotOffset = Vector3.zero; switch (pivot) { case PivotPosition.Top: pivotOffset = new Vector3(0.0f, -height / 2, 0.0f); break; case PivotPosition.Botttom: pivotOffset = new Vector3(0.0f, height / 2, 0.0f); break; } if (numVertices > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return(0.0f); } var vertices = new Vector3[numVertices]; var uvs = new Vector2[numVertices]; var triangles = new int[numTriangles]; var vertIndex = 0; var triIndex = 0; var a0 = new Vector3(-width / 2, pivotOffset.y - height / 2, -depth / 2); var b0 = new Vector3(-width / 2, pivotOffset.y - height / 2, depth / 2); var c0 = new Vector3(width / 2, pivotOffset.y - height / 2, depth / 2); var d0 = new Vector3(width / 2, pivotOffset.y - height / 2, -depth / 2); var a1 = new Vector3(-width / 2, height / 2 + pivotOffset.y, -depth / 2); var b1 = new Vector3(-width / 2, height / 2 + pivotOffset.y, depth / 2); var c1 = new Vector3(width / 2, height / 2 + pivotOffset.y, depth / 2); var d1 = new Vector3(width / 2, height / 2 + pivotOffset.y, -depth / 2); if (edgeOffsets != null && edgeOffsets.Length > 3) { b1.x += edgeOffsets[0]; a1.x += edgeOffsets[0]; b0.x += edgeOffsets[1]; a0.x += edgeOffsets[1]; c0.x += edgeOffsets[3]; c1.x += edgeOffsets[2]; d0.x += edgeOffsets[3]; d1.x += edgeOffsets[2]; } CreatePlane(0, a0, b0, c0, d0, widthSegments, depthSegments, cubeMap, ref vertices, ref uvs, ref triangles, ref vertIndex, ref triIndex); CreatePlane(1, b1, a1, d1, c1, widthSegments, depthSegments, cubeMap, ref vertices, ref uvs, ref triangles, ref vertIndex, ref triIndex); CreatePlane(2, b0, b1, c1, c0, widthSegments, heightSegments, cubeMap, ref vertices, ref uvs, ref triangles, ref vertIndex, ref triIndex); CreatePlane(3, d0, d1, a1, a0, widthSegments, heightSegments, cubeMap, ref vertices, ref uvs, ref triangles, ref vertIndex, ref triIndex); CreatePlane(4, a0, a1, b1, b0, depthSegments, heightSegments, cubeMap, ref vertices, ref uvs, ref triangles, ref vertIndex, ref triIndex); CreatePlane(5, c0, c1, d1, d0, depthSegments, heightSegments, cubeMap, ref vertices, ref uvs, ref triangles, ref vertIndex, ref triIndex); if (flipUV) { for (var i = 0; i < uvs.Length; i++) { uvs[i].x = 1.0f - uvs[i].x; } } mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; mesh.RecalculateNormals(); MeshUtils.CalculateTangents(mesh); ; mesh.RecalculateBounds(); stopWatch.Stop(); return(stopWatch.ElapsedMilliseconds); }
ResultCell[] GetHeaderCells(PivotPosition position, ReportModel model) { return (from element in model.Elements where element.PivotPosition == position select new ResultCell() { Element = element, IsTitle = true, Value = element.DisplayNameEl }).ToArray(); }
/// <summary> /// generate mesh geometry for TorusKnot /// </summary> /// <param name="mesh">mesh to be generated</param> /// <param name="torusRadius">radius of torus</param> /// <param name="coneRadius">radius of cone</param> /// <param name="torusSegments">number of triangle of torus</param> /// <param name="coneSegments">number of triangle of torus cone</param> /// <param name="P">Knot parameter</param> /// <param name="Q">Knot parameter</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public static float GenerateGeometry(Mesh mesh, float torusRadius, float coneRadius, int torusSegments, int coneSegments, int P, int Q, NormalsType normalsType, PivotPosition pivotPosition) { var stopWatch = new Stopwatch(); stopWatch.Start(); torusRadius = Mathf.Clamp(torusRadius, 0, 100); coneRadius = Mathf.Clamp(coneRadius, 0, 100); torusSegments = Mathf.Clamp(torusSegments, 3, 250); coneSegments = Mathf.Clamp(coneSegments, 3, 100); P = Mathf.Clamp(P, 1, 20); Q = Mathf.Clamp(Q, 1, 20); mesh.Clear(); int numTriangles = 2 * (coneSegments) * (torusSegments); int numVertices = 0; if (normalsType == NormalsType.Vertex) { numVertices = (torusSegments + 1) * (coneSegments + 1); } else { numVertices = numTriangles * 3; } if (numVertices > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return(0.0f); } var vertices = new Vector3[numVertices]; var normals = new Vector3[numVertices]; var uvs = new Vector2[numVertices]; var triangles = new int[numTriangles * 3]; var theta = 0.0f; float step = 2.0f * Mathf.PI / torusSegments; var p = new Vector3(); var pNext = new Vector3(); var vertIndex = 0; var triIndex = 0; var minY = float.MaxValue; var maxY = float.MinValue; for (int i = 0; i <= torusSegments + 1; i++) { theta += step; var r = torusRadius * 0.5f * (2.0f + Mathf.Sin(Q * theta)); p = pNext; // compute point on torus pNext = new Vector3(r * Mathf.Cos(P * theta), r * Mathf.Cos(Q * theta), r * Mathf.Sin(P * theta)); if (i > 0) { var T = pNext - p; var N = pNext + p; // find vectors B and N perpendicular to tangent point at torus circle point p var B = Vector3.Cross(T, N); N = Vector3.Cross(B, T); N.Normalize(); B.Normalize(); var theta2 = 0.0f; var step2 = 2.0f * Mathf.PI / coneSegments; for (int j = 0; j <= coneSegments; j++) { theta2 += step2; var s = coneRadius * Mathf.Sin(theta2); var t = coneRadius * Mathf.Cos(theta2); // find point u on cone radius var u = (N * s) + (B * t); vertices[vertIndex + 0] = pNext + u; normals[vertIndex + 0] = u.normalized; uvs[vertIndex + 0] = new Vector2((float)(i - 1) / torusSegments, (float)j / coneSegments); if (vertices[vertIndex].y < minY) { minY = vertices[vertIndex].y; } if (vertices[vertIndex].y > maxY) { maxY = vertices[vertIndex].y; } vertIndex += 1; } if (i <= torusSegments) { var torSeg = (i - 1) * (coneSegments + 1); var nextTorSeg = (i) * (coneSegments + 1); var trivert = 0; for (int j = 0; j < coneSegments; j++) { triangles[triIndex + 0] = nextTorSeg + 0 + trivert; triangles[triIndex + 1] = torSeg + 1 + trivert; triangles[triIndex + 2] = torSeg + 0 + trivert; triangles[triIndex + 3] = nextTorSeg + 1 + trivert; triangles[triIndex + 4] = torSeg + 1 + trivert; triangles[triIndex + 5] = nextTorSeg + 0 + trivert; triIndex += 6; trivert += 1; } } } } // adjust pivot position if (pivotPosition != PivotPosition.Center) { var pivotOffset = pivotPosition == PivotPosition.Botttom ? -minY : -maxY; for (int i = 0; i < vertices.Length; i++) { vertices[i].y += pivotOffset; } } // duplicate shared vertices for face vertices if (normalsType == NormalsType.Face) { MeshUtils.DuplicateSharedVertices(ref vertices, ref uvs, triangles, -1); } mesh.vertices = vertices; mesh.triangles = triangles; if (normalsType == NormalsType.Vertex) { mesh.normals = normals; } else { mesh.RecalculateNormals(); } mesh.uv = uvs; mesh.RecalculateBounds(); MeshUtils.CalculateTangents(mesh); mesh.Optimize(); stopWatch.Stop(); return(stopWatch.ElapsedMilliseconds); }
/// <summary> /// generate mesh geometry for box /// </summary> /// <param name="mesh">mesh to be generated</param> /// <param name="width">width of cube</param> /// <param name="height1">height1 of cube</param> /// <param name="height2">height2 of cube</param> /// <param name="depth">depth of cube</param> /// <param name="arcSegments">depth of the </param> /// <param name="controlPoint">control point of arc curve</param> /// <param name="pivot">position of the model pivot</param> public static float GenerateGeometry(Mesh mesh, float width, float height1, float height2, float depth, int arcSegments, Vector3 controlPoint, PivotPosition pivot) { var stopWatch = new Stopwatch(); stopWatch.Start(); width = Mathf.Clamp(width, 0, 100); height1 = Mathf.Clamp(height1, 0, 100); height2 = Mathf.Clamp(height2, 0, 100); depth = Mathf.Clamp(depth, 0, 100); arcSegments = Mathf.Clamp(arcSegments, 1, 100); var height = Mathf.Max(height1, height2); mesh.Clear(); int numTriangles = 36 + (arcSegments * 6) + (arcSegments * 3) * 2 + 6; int numVertices = 24 + (arcSegments * 6) * 2 + 2 + 2; var pivotOffset = Vector3.zero; switch (pivot) { case PivotPosition.Center: pivotOffset = new Vector3(0.0f, -height/2, 0.0f); break; case PivotPosition.Top: pivotOffset = new Vector3(0.0f, -height, 0.0f); break; } if (numVertices > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return 0.0f; } var vertices = new Vector3[numVertices]; var uvs = new Vector2[numVertices]; var triangles = new int[numTriangles]; var vertIdx = 0; var triIdx = 0; // make bottom points var p0 = vertices[0] = new Vector3(-width/2, 0.0f, -depth/2); var p1 = vertices[1] = new Vector3(width / 2, 0.0f, -depth / 2); var p2 = vertices[2] = new Vector3(width / 2, 0.0f, depth / 2); var p3 = vertices[3] = new Vector3(-width / 2, 0.0f, depth / 2); uvs[0] = new Vector2(0, 1); uvs[1] = new Vector2(1, 1); uvs[2] = new Vector2(1, 0); uvs[3] = new Vector2(0, 0); var p4 = p0; var p5 = p1; var p6 = p3; var p7 = p2; triangles[0] = 0; triangles[1] = 1; triangles[2] = 2; triangles[3] = 0; triangles[4] = 2; triangles[5] = 3; vertIdx = 4; triIdx = 6; // make left side if (height1 > 0) { vertices[vertIdx + 0] = p0; vertices[vertIdx + 1] = p3; p4 = vertices[vertIdx + 2] = new Vector3(-width / 2, height1, -depth / 2); p6 = vertices[vertIdx + 3] = new Vector3(-width / 2, height1, depth / 2); uvs[vertIdx + 3] = new Vector2(0, height1/height); uvs[vertIdx + 2] = new Vector2(1, height1 / height); uvs[vertIdx + 1] = new Vector2(0, 0); uvs[vertIdx + 0] = new Vector2(1, 0); triangles[triIdx + 0] = vertIdx + 3; triangles[triIdx + 1] = vertIdx + 2; triangles[triIdx + 2] = vertIdx + 0; triangles[triIdx + 3] = vertIdx + 1; triangles[triIdx + 4] = vertIdx + 3; triangles[triIdx + 5] = vertIdx + 0; vertIdx += 4; triIdx += 6; } // make right side if (height2 > 0) { vertices[vertIdx + 0] = p1; vertices[vertIdx + 1] = p2; p5 = vertices[vertIdx + 2] = new Vector3(width / 2, height2, -depth / 2); p7 = vertices[vertIdx + 3] = new Vector3(width / 2, height2, depth / 2); uvs[vertIdx + 3] = new Vector2(1, height2 / height); uvs[vertIdx + 2] = new Vector2(0, height2 / height); uvs[vertIdx + 1] = new Vector2(1, 0); uvs[vertIdx + 0] = new Vector2(0, 0); triangles[triIdx + 0] = vertIdx + 2; triangles[triIdx + 1] = vertIdx + 1; triangles[triIdx + 2] = vertIdx + 0; triangles[triIdx + 3] = vertIdx + 1; triangles[triIdx + 4] = vertIdx + 2; triangles[triIdx + 5] = vertIdx + 3; vertIdx += 4; triIdx += 6; } vertices[vertIdx++] = p0; vertices[vertIdx++] = p1; vertices[vertIdx++] = p2; vertices[vertIdx++] = p3; var tri0 = vertIdx - 4; var tri1 = vertIdx - 3; var tri2 = vertIdx - 2; var tri3 = vertIdx - 1; uvs[tri0] = new Vector2(0.0f, 0.0f); uvs[tri1] = new Vector2(1.0f, 0.0f); uvs[tri2] = new Vector2(0.0f, 0.0f); uvs[tri3] = new Vector2(1.0f, 0.0f); var frontSideVertOffset = vertIdx + arcSegments * 2; var frontSideTriOffset = triIdx + arcSegments*6; var ctrl0 = 0; var ctrl1 = 3; var halfT = 0.5f; // make arc side for (int i = 0; i <= arcSegments; i++) { var t = (float) i/arcSegments; var v0 = MeshUtils.BezierQuadratic(p4, p5, new Vector3(controlPoint.x, controlPoint.y, -depth / 2), t); var v1 = MeshUtils.BezierQuadratic(p6, p7, new Vector3(controlPoint.x, controlPoint.y, depth / 2), t); vertices[vertIdx + 0] = v0; vertices[vertIdx + 1] = v1; uvs[vertIdx + 0] = new Vector2(t, 0.0f); uvs[vertIdx + 1] = new Vector2(t, 1.0f); if (i < arcSegments) { // make top triangle quad triangles[triIdx + 0] = vertIdx + 0; triangles[triIdx + 1] = vertIdx + 1; triangles[triIdx + 2] = vertIdx + 3; triangles[triIdx + 3] = vertIdx + 3; triangles[triIdx + 4] = vertIdx + 2; triangles[triIdx + 5] = vertIdx + 0; // make front side and back triangle triangles[frontSideTriOffset + triIdx + 0] = frontSideVertOffset + vertIdx + 0; triangles[frontSideTriOffset + triIdx + 1] = frontSideVertOffset + vertIdx + 2; triangles[frontSideTriOffset + triIdx + 5] = frontSideVertOffset + vertIdx + 1; triangles[frontSideTriOffset + triIdx + 4] = frontSideVertOffset + vertIdx + 3; if (height1 > Mathf.Epsilon && height2 > Mathf.Epsilon) { if (t < 0.5f) { triangles[frontSideTriOffset + triIdx + 2] = tri0; triangles[frontSideTriOffset + triIdx + 3] = tri3; } else { triangles[frontSideTriOffset + triIdx + 2] = tri1; triangles[frontSideTriOffset + triIdx + 3] = tri2; } } else { if (height1 > Mathf.Epsilon) { triangles[frontSideTriOffset + triIdx + 2] = tri0; triangles[frontSideTriOffset + triIdx + 3] = tri3; } if (height2 > Mathf.Epsilon) { triangles[frontSideTriOffset + triIdx + 2] = tri1; triangles[frontSideTriOffset + triIdx + 3] = tri2; } } triIdx += 6; } // make front side vertices[frontSideVertOffset + vertIdx + 0] = v0; vertices[frontSideVertOffset + vertIdx + 1] = v1; uvs[frontSideVertOffset + vertIdx + 0] = new Vector2(t, v0.y/height); uvs[frontSideVertOffset + vertIdx + 1] = new Vector2(1-t, v0.y/height); if (t < 0.5f && t + (float)(i + 1)/(arcSegments) >= 0.5f) { ctrl0 = vertIdx + 2; ctrl1 = vertIdx + 3; halfT = t; if (arcSegments % 2 == 0) { halfT = 0.5f; } } vertIdx += 2; } if (height1 > 0 && height2 > 0) { vertices[frontSideVertOffset + vertIdx + 0] = vertices[ctrl0]; vertices[frontSideVertOffset + vertIdx + 1] = vertices[ctrl1]; uvs[frontSideVertOffset + vertIdx + 0] = new Vector2(1.0f - halfT, vertices[ctrl0].y / height); uvs[frontSideVertOffset + vertIdx + 1] = new Vector2(halfT, vertices[ctrl1].y / height); // make closing triangle front triangles[frontSideTriOffset + triIdx + 2] = tri0; triangles[frontSideTriOffset + triIdx + 1] = tri1; triangles[frontSideTriOffset + triIdx + 0] = frontSideVertOffset + vertIdx + 0; // make closing triangle back triangles[frontSideTriOffset + triIdx + 3] = tri3; triangles[frontSideTriOffset + triIdx + 4] = tri2; triangles[frontSideTriOffset + triIdx + 5] = frontSideVertOffset + vertIdx + 1; } for (int i = 0; i < vertices.Length; i++) { vertices[i] += pivotOffset; } mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; mesh.RecalculateNormals(); MeshUtils.CalculateTangents(mesh); mesh.Optimize(); mesh.RecalculateBounds(); stopWatch.Stop(); return stopWatch.ElapsedMilliseconds; }
/// <summary> /// create RoundedCube game object /// </summary> /// <param name="width">width of the cube</param> /// <param name="height">height of the cube</param> /// <param name="length">length of the cube</param> /// <param name="segments">number of segments</param> /// <param name="roundness">roudness coefficient</param> /// <param name="normals">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> /// <returns>RoundedCube class with RoundedCube game object</returns> public static RoundedCube Create(float width, float height, float length, int segments, float roundness, NormalsType normals, PivotPosition pivotPosition) { var sphereObject = new GameObject("RoundedCubePro"); sphereObject.AddComponent <MeshFilter>(); var renderer = sphereObject.AddComponent <MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var superellipsoid = sphereObject.AddComponent <RoundedCube>(); superellipsoid.GenerateGeometry(width, height, length, segments, roundness, normals, pivotPosition); return(superellipsoid); }
/// <summary> /// create Torus game object /// </summary> /// <param name="radius0">first radius of tube</param> /// <param name="radius1">second radius of tube</param> /// <param name="torusSegments">number of triangle segments of torus</param> /// <param name="coneSegments">number of triangle segments or torus cone</param> /// <returns>Torus class with Torus game object</returns> /// <param name="slice">slice</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public static Torus Create(float radius0, float radius1, int torusSegments, int coneSegments, float slice, NormalsType normalsType, PivotPosition pivotPosition) { var cylinderObject = new GameObject("TorusPro"); cylinderObject.AddComponent <MeshFilter>(); var renderer = cylinderObject.AddComponent <MeshRenderer>(); renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); var torus = cylinderObject.AddComponent <Torus>(); torus.GenerateGeometry(radius0, radius1, torusSegments, coneSegments, slice, normalsType, pivotPosition); return(torus); }
/// <summary> /// generate mesh geometry for box /// </summary> /// <param name="mesh">mesh to be generated</param> /// <param name="width">width of cube</param> /// <param name="height1">height1 of cube</param> /// <param name="height2">height2 of cube</param> /// <param name="depth">depth of cube</param> /// <param name="arcSegments">depth of the </param> /// <param name="controlPoint">control point of arc curve</param> /// <param name="pivot">position of the model pivot</param> public static float GenerateGeometry(Mesh mesh, float width, float height1, float height2, float depth, int arcSegments, Vector3 controlPoint, PivotPosition pivot) { var stopWatch = new Stopwatch(); stopWatch.Start(); width = Mathf.Clamp(width, 0, 100); height1 = Mathf.Clamp(height1, 0, 100); height2 = Mathf.Clamp(height2, 0, 100); depth = Mathf.Clamp(depth, 0, 100); arcSegments = Mathf.Clamp(arcSegments, 1, 100); var height = Mathf.Max(height1, height2); mesh.Clear(); var numTriangles = 36 + (arcSegments * 6) + (arcSegments * 3) * 2 + 6; var numVertices = 24 + (arcSegments * 6) * 2 + 2 + 2; var pivotOffset = Vector3.zero; switch (pivot) { case PivotPosition.Center: pivotOffset = new Vector3(0.0f, -height / 2, 0.0f); break; case PivotPosition.Top: pivotOffset = new Vector3(0.0f, -height, 0.0f); break; } if (numVertices > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return(0.0f); } var vertices = new Vector3[numVertices]; var uvs = new Vector2[numVertices]; var triangles = new int[numTriangles]; var vertIdx = 0; var triIdx = 0; // make bottom points var p0 = vertices[0] = new Vector3(-width / 2, 0.0f, -depth / 2); var p1 = vertices[1] = new Vector3(width / 2, 0.0f, -depth / 2); var p2 = vertices[2] = new Vector3(width / 2, 0.0f, depth / 2); var p3 = vertices[3] = new Vector3(-width / 2, 0.0f, depth / 2); uvs[0] = new Vector2(0, 1); uvs[1] = new Vector2(1, 1); uvs[2] = new Vector2(1, 0); uvs[3] = new Vector2(0, 0); var p4 = p0; var p5 = p1; var p6 = p3; var p7 = p2; triangles[0] = 0; triangles[1] = 1; triangles[2] = 2; triangles[3] = 0; triangles[4] = 2; triangles[5] = 3; vertIdx = 4; triIdx = 6; // make left side if (height1 > 0) { vertices[vertIdx + 0] = p0; vertices[vertIdx + 1] = p3; p4 = vertices[vertIdx + 2] = new Vector3(-width / 2, height1, -depth / 2); p6 = vertices[vertIdx + 3] = new Vector3(-width / 2, height1, depth / 2); uvs[vertIdx + 3] = new Vector2(0, height1 / height); uvs[vertIdx + 2] = new Vector2(1, height1 / height); uvs[vertIdx + 1] = new Vector2(0, 0); uvs[vertIdx + 0] = new Vector2(1, 0); triangles[triIdx + 0] = vertIdx + 3; triangles[triIdx + 1] = vertIdx + 2; triangles[triIdx + 2] = vertIdx + 0; triangles[triIdx + 3] = vertIdx + 1; triangles[triIdx + 4] = vertIdx + 3; triangles[triIdx + 5] = vertIdx + 0; vertIdx += 4; triIdx += 6; } // make right side if (height2 > 0) { vertices[vertIdx + 0] = p1; vertices[vertIdx + 1] = p2; p5 = vertices[vertIdx + 2] = new Vector3(width / 2, height2, -depth / 2); p7 = vertices[vertIdx + 3] = new Vector3(width / 2, height2, depth / 2); uvs[vertIdx + 3] = new Vector2(1, height2 / height); uvs[vertIdx + 2] = new Vector2(0, height2 / height); uvs[vertIdx + 1] = new Vector2(1, 0); uvs[vertIdx + 0] = new Vector2(0, 0); triangles[triIdx + 0] = vertIdx + 2; triangles[triIdx + 1] = vertIdx + 1; triangles[triIdx + 2] = vertIdx + 0; triangles[triIdx + 3] = vertIdx + 1; triangles[triIdx + 4] = vertIdx + 2; triangles[triIdx + 5] = vertIdx + 3; vertIdx += 4; triIdx += 6; } vertices[vertIdx++] = p0; vertices[vertIdx++] = p1; vertices[vertIdx++] = p2; vertices[vertIdx++] = p3; var tri0 = vertIdx - 4; var tri1 = vertIdx - 3; var tri2 = vertIdx - 2; var tri3 = vertIdx - 1; uvs[tri0] = new Vector2(0.0f, 0.0f); uvs[tri1] = new Vector2(1.0f, 0.0f); uvs[tri2] = new Vector2(0.0f, 0.0f); uvs[tri3] = new Vector2(1.0f, 0.0f); var frontSideVertOffset = vertIdx + arcSegments * 2; var frontSideTriOffset = triIdx + arcSegments * 6; var ctrl0 = 0; var ctrl1 = 3; var halfT = 0.5f; // make arc side for (var i = 0; i <= arcSegments; i++) { var t = (float)i / arcSegments; var v0 = MeshUtils.BezierQuadratic(p4, p5, new Vector3(controlPoint.x, controlPoint.y, -depth / 2), t); var v1 = MeshUtils.BezierQuadratic(p6, p7, new Vector3(controlPoint.x, controlPoint.y, depth / 2), t); vertices[vertIdx + 0] = v0; vertices[vertIdx + 1] = v1; uvs[vertIdx + 0] = new Vector2(t, 0.0f); uvs[vertIdx + 1] = new Vector2(t, 1.0f); if (i < arcSegments) { // make top triangle quad triangles[triIdx + 0] = vertIdx + 0; triangles[triIdx + 1] = vertIdx + 1; triangles[triIdx + 2] = vertIdx + 3; triangles[triIdx + 3] = vertIdx + 3; triangles[triIdx + 4] = vertIdx + 2; triangles[triIdx + 5] = vertIdx + 0; // make front side and back triangle triangles[frontSideTriOffset + triIdx + 0] = frontSideVertOffset + vertIdx + 0; triangles[frontSideTriOffset + triIdx + 1] = frontSideVertOffset + vertIdx + 2; triangles[frontSideTriOffset + triIdx + 5] = frontSideVertOffset + vertIdx + 1; triangles[frontSideTriOffset + triIdx + 4] = frontSideVertOffset + vertIdx + 3; if (height1 > Mathf.Epsilon && height2 > Mathf.Epsilon) { if (t < 0.5f) { triangles[frontSideTriOffset + triIdx + 2] = tri0; triangles[frontSideTriOffset + triIdx + 3] = tri3; } else { triangles[frontSideTriOffset + triIdx + 2] = tri1; triangles[frontSideTriOffset + triIdx + 3] = tri2; } } else { if (height1 > Mathf.Epsilon) { triangles[frontSideTriOffset + triIdx + 2] = tri0; triangles[frontSideTriOffset + triIdx + 3] = tri3; } if (height2 > Mathf.Epsilon) { triangles[frontSideTriOffset + triIdx + 2] = tri1; triangles[frontSideTriOffset + triIdx + 3] = tri2; } } triIdx += 6; } // make front side vertices[frontSideVertOffset + vertIdx + 0] = v0; vertices[frontSideVertOffset + vertIdx + 1] = v1; uvs[frontSideVertOffset + vertIdx + 0] = new Vector2(t, v0.y / height); uvs[frontSideVertOffset + vertIdx + 1] = new Vector2(1 - t, v0.y / height); if (t < 0.5f && t + (float)(i + 1) / (arcSegments) >= 0.5f) { ctrl0 = vertIdx + 2; ctrl1 = vertIdx + 3; halfT = t; if (arcSegments % 2 == 0) { halfT = 0.5f; } } vertIdx += 2; } if (height1 > 0 && height2 > 0) { vertices[frontSideVertOffset + vertIdx + 0] = vertices[ctrl0]; vertices[frontSideVertOffset + vertIdx + 1] = vertices[ctrl1]; uvs[frontSideVertOffset + vertIdx + 0] = new Vector2(1.0f - halfT, vertices[ctrl0].y / height); uvs[frontSideVertOffset + vertIdx + 1] = new Vector2(halfT, vertices[ctrl1].y / height); // make closing triangle front triangles[frontSideTriOffset + triIdx + 2] = tri0; triangles[frontSideTriOffset + triIdx + 1] = tri1; triangles[frontSideTriOffset + triIdx + 0] = frontSideVertOffset + vertIdx + 0; // make closing triangle back triangles[frontSideTriOffset + triIdx + 3] = tri3; triangles[frontSideTriOffset + triIdx + 4] = tri2; triangles[frontSideTriOffset + triIdx + 5] = frontSideVertOffset + vertIdx + 1; } for (var i = 0; i < vertices.Length; i++) { vertices[i] += pivotOffset; } mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; mesh.RecalculateNormals(); MeshUtils.CalculateTangents(mesh); ; mesh.RecalculateBounds(); stopWatch.Stop(); return(stopWatch.ElapsedMilliseconds); }
/// <summary> /// generate mesh geometry for Pyramid /// </summary> /// <param name="mesh">mesh to be generated</param> /// <param name="width">width of pyramid</param> /// <param name="height">height of pyramid</param> /// <param name="depth">depth of pyramid</param> /// <param name="widthSegments">number of triangle segments in width direction</param> /// <param name="heightSegments">number of triangle segments in height direction</param> /// <param name="depthSegments">number of triangle segments in depth direction</param> /// <param name="pyramidMap">enable pyramid map uv mapping</param> /// <param name="pivotPosition">position of the model pivot</param> public static float GenerateGeometry(Mesh mesh, float width, float height, float depth, int widthSegments, int heightSegments, int depthSegments, bool pyramidMap, PivotPosition pivotPosition) { var stopWatch = new Stopwatch(); stopWatch.Start(); width = Mathf.Clamp(width, 0, 100); height = Mathf.Clamp(height, 0, 100); width = Mathf.Clamp(width, 0, 100); widthSegments = Mathf.Clamp(widthSegments, 1, 100); heightSegments = Mathf.Clamp(heightSegments, 1, 100); depthSegments = Mathf.Clamp(depthSegments, 1, 100); mesh.Clear(); int numTriangles = widthSegments*heightSegments*6*2 + depthSegments*heightSegments*6*2 + widthSegments*depthSegments*6; int numVertices = (widthSegments + 1)*(heightSegments + 1)*2 + (depthSegments + 1)*(heightSegments + 1)*2 + (widthSegments + 1)*(depthSegments + 1); if (numVertices > 60000) { UnityEngine.Debug.LogError("Too much vertices!"); return 0.0f; } var vertices = new Vector3[numVertices]; var uvs = new Vector2[numVertices]; var triangles = new int[numTriangles]; var apex = new Vector3(0, height, 0); var pivotOffset = Vector3.zero; switch (pivotPosition) { case PivotPosition.Center: pivotOffset = new Vector3(0.0f, -height/2, 0.0f); break; case PivotPosition.Top: pivotOffset = new Vector3(0.0f, -height, 0.0f); break; } int vertIndex = 0; float widthRatio = width/widthSegments; var offsetTris = widthSegments*heightSegments*6; var offsetVert = (widthSegments + 1)*(heightSegments + 1); var a0 = new Vector3(depth / 2, 0.0f, 0 * widthRatio - width / 2) + pivotOffset; var b0 = new Vector3(depth / 2, 0.0f, widthSegments * widthRatio - width / 2) + pivotOffset; var a1 = new Vector3(-depth / 2, 0.0f, 0 * widthRatio - width / 2) + pivotOffset; var b1 = new Vector3(-depth / 2, 0.0f, widthSegments * widthRatio - width / 2) + pivotOffset; for (int i = 0; i < widthSegments+1; i++) { var bottom0 = new Vector3(depth / 2, 0.0f, i * widthRatio - width / 2); var v0 = apex - bottom0; var bottom1 = new Vector3(-depth / 2, 0.0f, i * widthRatio - width / 2); var v1 = apex - bottom1; for (int j=0; j < heightSegments+1; j++) { vertices[vertIndex + 0] = bottom0 + (float)j / heightSegments * v0 + pivotOffset; vertices[offsetVert + vertIndex + 0] = bottom1 + (float)j / heightSegments * v1 + pivotOffset; if (pyramidMap) { var baryCoordinates = MeshUtils.ComputeBarycentricCoordinates(apex + pivotOffset, a0, b0, vertices[vertIndex]); uvs[vertIndex + 0] = GetPyramidUVMap(PyramidSide.side0, baryCoordinates); baryCoordinates = MeshUtils.ComputeBarycentricCoordinates(apex + pivotOffset, b1, a1, vertices[offsetVert + vertIndex + 0]); uvs[offsetVert + vertIndex + 0] = GetPyramidUVMap(PyramidSide.side2, baryCoordinates); } else { uvs[vertIndex + 0] = new Vector2(vertices[vertIndex + 0].z/width, (float)j/heightSegments); uvs[offsetVert + vertIndex + 0] = new Vector2(vertices[offsetVert + vertIndex + 0].z / width, (float)j / heightSegments); } vertIndex ++; } } int triIndex = 0; int triVert = 0; for (int i = 0; i < widthSegments; i++) { var nextSegment = (heightSegments + 1); for (int j = 0; j < heightSegments; j++) { triangles[triIndex + 0] = triVert + 0; triangles[triIndex + 1] = triVert + 1; triangles[triIndex + 2] = triVert + nextSegment + 0; triangles[triIndex + 3] = triVert + nextSegment + 0; triangles[triIndex + 4] = triVert + 1; triangles[triIndex + 5] = triVert + nextSegment + 1; triangles[offsetTris + triIndex + 0] = offsetVert + triVert + nextSegment + 0; triangles[offsetTris + triIndex + 1] = offsetVert + triVert + 1; triangles[offsetTris + triIndex + 2] = offsetVert + triVert + 0; triangles[offsetTris + triIndex + 3] = offsetVert + triVert + nextSegment + 1; triangles[offsetTris + triIndex + 4] = offsetVert + triVert + 1; triangles[offsetTris + triIndex + 5] = offsetVert + triVert + nextSegment + 0; triIndex += 6; triVert += 1; } triVert += 1; } widthRatio = depth / depthSegments; vertIndex = (widthSegments + 1) * (heightSegments + 1) * 2; triIndex = widthSegments*heightSegments*6*2; triVert = vertIndex; offsetTris = depthSegments * heightSegments * 6; offsetVert = (depthSegments + 1) * (heightSegments + 1); a0 = new Vector3(0*widthRatio - depth/2, 0.0f, width/2) + pivotOffset; b0 = new Vector3(depthSegments * widthRatio - depth / 2, 0.0f, width / 2) + pivotOffset; a1 = new Vector3(0 * widthRatio - depth / 2, 0.0f, -width / 2) + pivotOffset; b1 = new Vector3(depthSegments * widthRatio - depth / 2, 0.0f, -width / 2) + pivotOffset; for (int i = 0; i < depthSegments + 1; i++) { var bottom0 = new Vector3(i * widthRatio - depth / 2, 0.0f, width/2); var v0 = apex - bottom0; var bottom1 = new Vector3(i * widthRatio - depth / 2, 0.0f, -width/2); var v1 = apex - bottom1; for (int j = 0; j < heightSegments + 1; j++) { vertices[vertIndex + 0] = bottom0 + (float)j / heightSegments * v0 + pivotOffset; vertices[offsetVert + vertIndex + 0] = bottom1 + (float)j / heightSegments * v1 + pivotOffset; if (pyramidMap) { var baryCoordinates = MeshUtils.ComputeBarycentricCoordinates(apex + pivotOffset, b0, a0, vertices[vertIndex]); uvs[vertIndex + 0] = GetPyramidUVMap(PyramidSide.side1, baryCoordinates); baryCoordinates = MeshUtils.ComputeBarycentricCoordinates(apex + pivotOffset, a1, b1, vertices[offsetVert + vertIndex + 0]); uvs[offsetVert + vertIndex + 0] = GetPyramidUVMap(PyramidSide.side3, baryCoordinates); } else { uvs[vertIndex + 0] = new Vector2(vertices[vertIndex + 0].x / depth, (float)j / heightSegments); uvs[offsetVert + vertIndex + 0] = new Vector2(vertices[offsetVert + vertIndex + 0].x / depth, (float)j / heightSegments); } vertIndex++; } } for (int i = 0; i < depthSegments; i++) { var nextSegment = (heightSegments + 1); for (int j = 0; j < heightSegments; j++) { triangles[triIndex + 0] = triVert + nextSegment + 0; triangles[triIndex + 1] = triVert + 1; triangles[triIndex + 2] = triVert + 0; triangles[triIndex + 3] = triVert + nextSegment + 1; triangles[triIndex + 4] = triVert + 1; triangles[triIndex + 5] = triVert + nextSegment + 0; triangles[offsetTris + triIndex + 0] = offsetVert + triVert + 0; triangles[offsetTris + triIndex + 1] = offsetVert + triVert + 1; triangles[offsetTris + triIndex + 2] = offsetVert + triVert + nextSegment + 0; triangles[offsetTris + triIndex + 3] = offsetVert + triVert + nextSegment + 0; triangles[offsetTris + triIndex + 4] = offsetVert + triVert + 1; triangles[offsetTris + triIndex + 5] = offsetVert + triVert + nextSegment + 1; triIndex += 6; triVert += 1; } triVert += 1; } widthRatio = width / widthSegments; var depthRatio = depth/depthSegments; vertIndex = (widthSegments + 1)*(heightSegments + 1)*2 + (depthSegments + 1)*(heightSegments + 1)*2; triIndex = widthSegments*heightSegments*6*2 + depthSegments*heightSegments*6*2; triVert = vertIndex; for (int i = 0; i < depthSegments + 1; i++) { for (int j = 0; j < widthSegments + 1; j++) { vertices[vertIndex + 0] = new Vector3(depth / 2 - depthRatio * i, 0.0f, width / 2 - j * widthRatio) + pivotOffset; if (pyramidMap) { uvs[vertIndex + 0] = GetPyramidUVMap(PyramidSide.bottom, new Vector2((float)j / widthSegments, (float)i / depthSegments)); } else { uvs[vertIndex + 0] = new Vector2((float) j/widthSegments, (float) i/depthSegments); } vertIndex++; } } for (int i = 0; i < depthSegments; i++) { var nextSegment = (widthSegments + 1); for (int j = 0; j < widthSegments; j++) { triangles[triIndex + 0] = triVert + nextSegment + 0; triangles[triIndex + 1] = triVert + 1; triangles[triIndex + 2] = triVert + 0; triangles[triIndex + 3] = triVert + nextSegment + 1; triangles[triIndex + 4] = triVert + 1; triangles[triIndex + 5] = triVert + nextSegment + 0; triIndex += 6; triVert += 1; } triVert += 1; } mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; mesh.RecalculateNormals(); MeshUtils.CalculateTangents(mesh); mesh.Optimize(); mesh.RecalculateBounds(); stopWatch.Stop(); return stopWatch.ElapsedMilliseconds; }
/// <summary> /// re/generate mesh geometry based on parameters /// </summary> /// <param name="radius0">fist radius of tube</param> /// <param name="radius1">second radius of tube</param> /// <param name="torusSegments">number of triangle of torus</param> /// <param name="coneSegments">number of triangle of torus cone</param> /// <param name="slice">slice</param> /// <param name="normalsType">type of normals to be generated</param> /// <param name="pivotPosition">position of the model pivot</param> public void GenerateGeometry(float radius0, float radius1, int torusSegments, int coneSegments, float slice, NormalsType normalsType, PivotPosition pivotPosition) { // generate new mesh and clear old one var meshFilter = GetComponent <MeshFilter>(); if (meshFilter.sharedMesh == null) { meshFilter.sharedMesh = new Mesh(); } var mesh = meshFilter.sharedMesh; // generate geometry GenerationTimeMS = Primitives.TorusPrimitive.GenerateGeometry(mesh, radius0, radius1, torusSegments, coneSegments, slice, normalsType, pivotPosition); this.radius0 = radius0; this.radius1 = radius1; this.torusSegments = torusSegments; this.coneSegments = coneSegments; this.normalsType = normalsType; this.slice = slice; this.flipNormals = false; this.pivotPosition = pivotPosition; }