Jan
2
2012
2
2012
Precalculated Sin Cos Arrays
An article by mgear
11 Comments
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
- [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
@unitycoder_com
My TweetsSubscribe to Blog via Email
Tag Cloud
2d
3D
AI
algorithm
android
asset
build
color
custom
demo
editor
effect
error
fake
free
game
generator
greasemonkey
indie
javascript
light
line
ludumdare
mesh
paint
particles
physics
plugin
proto
prototype
script
sea
shader
shadow
sprite
terrain
texture
tutorial
ui
unity
vertex
visibility
water
waves
webgl
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.
Tricks With the Floating-Point Format.
https://randomascii.wordpress.com/2012/01/11/tricks-with-the-floating-point-format/