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
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
- Custom Unity Hub Project Template Preview Image/Video (using HTML+CSS in package description)
Recent Comments
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- ffmpeg stream raw video into Unity Texture2D on
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