Small vector classes for mruby. Primarily for game developers. Provides a set of functions similar to shading languages (GLSL/HLSL/Cg).
Add
conf.gem github: 'dabroz/mruby-float4'
to your build_config.rb
.
There are two ways to use this library. First is the "Ruby" way - operate on vector instances like they were regular classes:
a = Vec4.new(1.0, 2.0, 3.0, 4.0)
b = Vec4.new(2.0 ,4.0, 5.0, 6.0)
c = (a * 2.0 + b).normalize.dot(Vec4.new(0, 0, 1, 1))
Another is a typical shading language syntax, using an optional module import:
extend Vec4::GLSL
a = vec4(1.0, 2.0, 3.0, 4.0)
b = vec4(2.0 ,4.0, 5.0, 6.0)
c = dot(normalize(a * 2.0 + b), vec4(0, 0, 1, 1))
All vector instances are immutable (there is no x=
and similar functions). This is done intentionally. Since Ruby has no "by value" structures it is very for someone used to shading languages to not create a duplicate of vector and modify multiple instances incorrectly.
Vec4
(4xfloat
),Vec3
(3xfloat
),Vec2
(2xfloat
)IVec4
(4xmrb_int
),IVec3
(3xmrb_int
),IVec2
(2xmrb_int
)BVec4
(4xmrb_bool
),BVec3
(3xmrb_bool
),BVec2
(2xmrb_bool
)
+
-
*
/
-@
acos
asin
atan2
atan
ceil
clamp
cos
cross
degrees
dot
exp2
floor
fract
inversesqrt
length_squared
length
lerp
log2
log
max
min
mix
normalize
radians
reflect
refract
saturate
sin
smoothstep
sqrt
step
tan
- all possible swizzle combinations (
.xyz
,.xyz
,.xxx
etc)
Helper functions:
data_size
(4
forVec4
)data_class
(Float
forVec4
)
It is written completly in C (although C code is autogenerated from Ruby template) for performace reasons. C functions do not usually call Ruby, so things like overridden Math.floor
won't change this gem behavior.
Data is stored withing the object as simple C structure (for size and performance), which has several side-effects:
- any overrides on
Float
won't change this gem behavior VecX
family uses single-precision float numbers (mruby uses double-precision by default)
In 64-bit mode, inline structures (istruct
) is used to store data to increate performance and avoid extra allocation.
2.0 * Vec2.new(1.0, 2.0)
won't work, because mruby doesn't currently support Fixnum/Float operators overloading. There is however a pull request for that in the works.
benchmark/bm_ao_render.rb (pure Ruby) [256x256] -> 1m40.712s
benchmark/bm_ao_render_float4.rb (32-bit) [256x256] -> 0m59.265s
benchmark/bm_ao_render_float4.rb (64-bit, w/istruct) [256x256] -> 0m53.674s
So there is a slight performance bonus of using this gem. 64-bit version is even faster, thanks to inline structure storage.
MIT License
Copyright (c) 2016 Tomasz Dąbrowski
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- Fork it ( https://github.com/dabroz/mruby-float4/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request