2
2012
Precalculated Sin Cos Arrays
In the old days (blitz basic, qbasic..) these lookup tables were quite useful!
Download test scene: (moving sphere..Note! saved with Unity3D v3.5)
mSinCosTables.unitypackage
Source: (javascript, attach it to some object to see it moving..)
// Precalculated SinCos - Unity3D version - mgear - http://unitycoder.com/blog/
// ORIGINAL SOURCE: "Jim" http://www.dbfinteractive.com/forum/index.php?topic=4772.0
#pragma strict
private var lookupTableSize:int=65536; // or 1024
private var mSin:float[]; // using native .NET arrays (see unity help page for ARRAY)
private var mCos:float[];
private var mconst:float;
private var c:float = 0;
private var maxrad:float = 2*Mathf.PI;
function Start ()
{
// init arrays
mCos = new float[lookupTableSize];
mSin = new float[lookupTableSize];
mconst = lookupTableSize / (2*Mathf.PI); // array index constant
// build lookup tables
var angle:float = 0;
for (var i:int=0;i<lookupTableSize;i++)
{
mSin[i] = Mathf.Sin(angle);
mCos[i] = Mathf.Cos(angle);
angle += 2*Mathf.PI/lookupTableSize;
}
}
// mainloop
function Update ()
{
// normal maths
// transform.position.x = Mathf.Cos(c)*5;
// transform.position.y = Mathf.Sin(c)*5;
// c+=0.05;
// precalculated
transform.position.x = getCos(c)*5;
transform.position.y = getSin(c)*5;
c = Mathf.Repeat(c+0.05,maxrad);
}
// helper functions
function getSin(radians:float) {return mSin[radians * mconst];}
function getCos(radians:float) {return mCos[radians * mconst];}
Related Posts
11 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












Benchmarking Mathf with System.Math:
http://forum.unity3d.com/threads/194938-Benchmarking-Mathf-with-System-Math-%28big-performance-differences%29
Fast Estimated Sine and Cosine
http://www.xnawiki.com/index.php/Fast_Estimated_Sine_and_Cosine
Hello can you help me. I want to return an array with 10 elements in it between -1 and 1. Using Sin or PingPong. Please help. Thanks 🙂
^ sorry havent had time to test that out..any progress yet?
optimize Bounds:
http://forum.unity3d.com/threads/204243-Slow-Unity-Math-Please-Unity-Tech-keep-core-math-fast
Vector3:
http://forum.unity3d.com/threads/224251-How-do-I-fill-a-vector-in-one-line-of-code?p=1496384&viewfull=1#post1496384
“transform.position is way slower than transform.localPosition”
http://forum.unity3d.com/threads/could-batched-vector-ops-speed-up-unity.253924/#post-1677880
this shader has FastSinCos function :
http://forum.unity3d.com/threads/230900-Shader-Release-Moving-Trees-amp-Grass-in-Wind-Outside-of-Terrain
thanks! changed the last line to
static function Sin(radians:float) {return mSin[radians * mconst];}
and the file name to m.js
so i can simply call m.Sin() in any other js.
Okay i changed the code abit to access it anywere with m.sin m.cos… made loop run in Awake then it can access from other scripts in Start.
#pragma strict private static var lookupTableSize:int=100; // or 1024//16384 private static var mSin:float[];// private static var mCos:float[]; private static var mconst:float; function Awake () { // init arrays mCos = new float[lookupTableSize]; mSin = new float[lookupTableSize]; mconst = lookupTableSize / (2*Mathf.PI); // array index constant // build lookup tables var angle:float = 0; for (var i:int=0;i<lookupTableSize;i++) { mSin[i] = Mathf.Sin(angle); mCos[i] = Mathf.Cos(angle); angle += 2*Mathf.PI/lookupTableSize; } } // helper functions static function sin (rr:float) :float {return mSin[(rr * mconst)%lookupTableSize];} static function cos (rr:float) :float {return mCos[(rr * mconst)%lookupTableSize];}Tricks With the Floating-Point Format.
https://randomascii.wordpress.com/2012/01/11/tricks-with-the-floating-point-format/