10
2014
Draw Ascii Sphere
Just for fun tried to convert this LUA script into unity c#,
Original source: http://rosettacode.org/wiki/Draw_a_sphere#Lua
*Note this source outputs the file into spheredump.txt file (at project root folder)
C# Source ( DrawSphere.cs ) *not optimized..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | // ORIGINAL SOURCE: http://rosettacode.org/wiki/Draw_a_sphere#Lua using UnityEngine; using System.Collections; public class DrawSphere : MonoBehaviour { char [] shades = new char []{ '.' , ':' , '!' , '*' , 'o' , 'e' , '&' , '#' , '%' , '@' }; Vector3 lightVec; void Start () { lightVec = normalizer(30, 30, -50); draw_sphere (20, 4, 0.1f); } void draw_sphere ( float radius, float k, float ambient) { string lineTemp = "" ; for ( int i = ( int )Mathf.Floor(-radius);i<-( int )Mathf.Floor(-radius);i++) { int x = ( int )(i + 0.5f); for ( int j = ( int )Mathf.Floor(-2*radius);j<( int )-Mathf.Floor(-2*radius);j++) { float y = (j / 2 + 0.5f); if ((Mathf.Pow(x,2) + Mathf.Pow(y,2)) <= (Mathf.Pow(radius,2))) { Vector3 vec = normalizer(x, y, Mathf.Sqrt( Mathf.Pow(radius,2)-Mathf.Pow(x,2)-Mathf.Pow(y,2))); float b = Mathf.Pow(dotter(lightVec,vec),k) + ambient; float intensity = ( int )(Mathf.Floor((1 - b) * shades.Length)); lineTemp = lineTemp + ((intensity>-1)?shades[( int )intensity]:shades[0]); } else { lineTemp = lineTemp + " " ; } } lineTemp = lineTemp+System.Environment.NewLine; } // output to textfile System.IO.File.WriteAllText( "spheredump.txt" , lineTemp); } Vector3 normalizer( float vec1, float vec2, float vec3) { float [] vec = new float []{0,vec1,vec2,vec3}; float len = Mathf.Sqrt( Mathf.Pow(vec[1],2) + Mathf.Pow(vec[2],2) + Mathf.Pow(vec[3],2)); return new Vector3 (vec[1]/len, vec[2]/len, vec[3]/len); } float dotter(Vector3 vec1, Vector3 vec2) { float d = (vec1[0]*vec2[0] + vec1[1]*vec2[1] + vec1[2]*vec2[2]); return d<0?-d:0; } } |
Related Posts
Leave a comment
Recent posts
- MotionVector Effect: Object “disappears” when paused
- [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
Recent Comments
- Using RenderDoc with Unity (graphics debugger) on
- UI Scroll View automatic Content height on
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- 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