13
2013
Three.js PlaneGeometry.js to Unity C#

Converted this THREE.js PlaneGeometry.js (by mr.doob) to Unity c# (its similar to CreatePlane from unity wiki, but more limited)
Current version:
– Using MeshTopology.Quads
– Still some problems with the UV’s, if using more than one segments
– Also currently all the variables are as ints, so need to use even numbers
– And missing normals..
Source: ** Use the finished source from alexzzzz on comment section links instead **
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlaneGeometry : MonoBehaviour
{
void Start()
{
CreatePlane(4,4,4,4);
}
void CreatePlane(int width, int height, int segmentsWidth, int segmentsHeight)
{
int ix, iy,
width_half = width / 2,
height_half = height / 2,
gridX = segmentsWidth,
gridY = segmentsHeight,
gridX1 = gridX + 1,
gridY1 = gridY + 1,
segment_width = width / gridX,
segment_height = height / gridY;
Vector3 normal = new Vector3( 0, 0, 1 );
List<Vector3> vertices = new List<Vector3>();
List<Vector3> vertexNormals = new List<Vector3>();
List<int> faces = new List<int>();
List<Vector2> faceVertexUvs = new List<Vector2>();
for ( iy = 0; iy < gridY1; iy++ ) {
for ( ix = 0; ix < gridX1; ix++ ) {
float x = ix * segment_width - width_half;
float y = iy * segment_height - height_half;
vertices.Add(new Vector3(x, -y, 0));
}
}
for ( iy = 0; iy < gridY; iy++ ) {
for ( ix = 0; ix < gridX; ix++ ) {
int a = ix + gridX1 * iy;
int b = ix + gridX1 * (iy + 1);
int c = (ix + 1) + gridX1 * (iy + 1);
int d = (ix + 1) + gridX1 * iy;
faces.Add(a);
faces.Add(b);
faces.Add(c);
faces.Add(d);
faceVertexUvs.Add(new Vector2(ix / gridX, iy / gridY));
faceVertexUvs.Add(new Vector2(ix / gridX, (iy + 1) / gridY));
faceVertexUvs.Add(new Vector2((ix + 1 ) / gridX, ( iy + 1 ) / gridY));
faceVertexUvs.Add(new Vector2((ix + 1 ) / gridX, iy / gridY));
}
}
// create mesh
Mesh m = GetComponent<MeshFilter>().mesh;
m.vertices = vertices.ToArray();
m.SetIndices(faces.ToArray(), MeshTopology.Quads, 0);
m.uv = faceVertexUvs.ToArray();
}
}
Related Posts
22 Comments + Add Comment
Leave a comment
Recent posts
- Favorites in PackageManager
- LudumDare59 : Signal
- Unity Editor: Tree Generator
- Leaf/Foliage Generator Tools (Runs in Browser)
- Testing Unity AI Beta
- Ways to Support UnityCoder Development
- Using UI Slider to Create 5-Star Rating Element
- Game Music Library For Unity (editor plugin)
- Fontastic : Easily Test Fonts in Unity Editor!
- GeoTiff Importer & Terrain Generator for Unity
- Create Baked DropShadow for UI images
- .JP2 Ortho Image Converter to PNG/JPG/TIFF
Recent Comments
- on Mesh Exploder (sources)
- on Sprite Sheet Flip Book Shader
- on Sprite Sheet Flip Book Shader
- on [Asset Store] PolygonCollider2D Optimizer
- on Trajectory Test Scene 2.0
- on Vector3 maths for dummies!
- on UnityHub 3.6.0: Remove Version Control & Cloud Dashboard columns
- on Using RenderDoc with Unity (graphics debugger)
Coin:
CUgDSbRqFcAumDSAcdKDvuXsw26VdkJe8C8WGUQHBAGS
An article by












Here is mine:
plane script: http://pastebin.com/Tb3cD34D
plane editor script: http://pastebin.com/dcBTtWJB
picture: http://piccy.info/view3/4267481/3ed176dcd81a4bdd7a199285f991f3be/orig/
Thanks!
Cube maker in javascript (with shared vertices)
http://forum.unity3d.com/threads/142908-Procedural-Cube-Generation
I edited my procedural plane script. Now it creates tangents and also allows to save the final mesh as an asset.
http://sdrv.ms/1cgtimP
Create a quad mesh asset:
https://gist.github.com/mstevenson/4384385
create random landscape (just a mesh terrain, with source)
http://forum.unity3d.com/threads/generating-sc2000-terrain-performance.255570/
My set of procedural shapes: https://bitbucket.org/alexzzzz/procedural-shapes-for-unity
Just started looking for a Möbius strip mesh generation from script, so far havent founded anyting “simple”.. any tips? : )
Would prefer this kind:
http://www.palmyria.co.uk/illusions/geometry/mobius90solid.jpg
But single sided plane is also fine:
http://www.umich.edu/~vrl/project2/moebius/moeb_scr.jpg
I think, if you manage to build a square tube like that – http://piccy.info/view3/7480462/465598b8a9785a0e4184183f8bfb6c40/
– the rest is the matter of proper vertex rotation.
founded some real formulas, trying to extract the parts from here:
http://www.java2s.com/Open-Source/CSharp_Free_Code/3D/Download_Helix_3D_Toolkit.htm
(would have some nice shapes to add to your procedural meshes too 🙂
http://www.palmyria.co.uk/illusions/geometry/mobius90solid.jpg
I guess, I will add a basic torus shape with options to make it look like this möbius thing.
Done
https://bitbucket.org/alexzzzz/procedural-shapes-for-unity
wow thanks! saved me a lot of time 🙂
only got this far,
http://unitycoder.com/blog/2014/12/24/mobius-strip-mesh-generator-wip/
Octahedron Sphere (sources)
http://catlikecoding.com/unity/tutorials/octahedron-sphere/
Modified CreatePlane.cs to add “flipYZ” option (needed for creating billboard mesh plane, default quad faced camera sideways and doing a +90’deg matrix rotation in shader would take me years..)
https://gist.github.com/unitycoder/c8c679ad82a02dd83b3c
Procedural Toolkit (free)
https://www.assetstore.unity3d.com/en/#!/content/16508
https://github.com/Syomus/ProceduralToolkit/
“Computational Geometry Algorithms Library (CGAL) is a C++ library that aims to provide easy access to efficient and reliable algorithms in computational geometry”
https://github.com/CGAL/cgal
Mesh Welding:
http://forum.unity3d.com/threads/procedural-path-generating-advice-on-merging-geometry-where-paths-intersect-lofting.351016/#post-2272229
Emgen is a library for creating meshes of basic geometric shapes.
https://github.com/keijiro/Emgen
generate primitive meshes
https://github.com/keijiro/Metamesh
high performance dynamic mesh generation of 2D and 3D shapes with rounded edges/corners
https://github.com/brandf/SliceyMesh