News for hackers. Stuff that matters.
Re:Voxel.css by yoklov on Wednesday January 27, 2016 @02:23AM (#10978743)
As someone with two Unity WebGL games in production, I wouldn't recommend this route. A fairly small game (~20kloc, so not a toy by any stretch, but small nonetheless) has 26mb of JavaScript code exported This kills the browser.

Only good desktop or laptop machines can run it well. Runs like shit on average machines (lets not even mention mobile). This is after several months of dedicated optimization time to reduce resource usage and minimize build size.

Overall, you are faaaar better off writing it in JavaScript with one of the pure js engines if you are going to go this route.

Re:Voxel.css by irrlichthn on Wednesday January 27, 2016 @03:10AM (#10978872)
Can't upvote this comment enough. I'm also currently in the process of creating a full featured WebGL game, and nothing is faster than simply using pure JavaScript there. Most people won't accept this, but when you ask them, those never actually tried creating a complex WebGL game.
Re:Voxel.css by vanderZwan on Wednesday January 27, 2016 @08:09AM (#10979807)
What about compiles-to-JavaScript languages like TypeScript?
Re:Voxel.css by yoklov on Wednesday January 27, 2016 @11:23AM (#10980887)
These are totally fine most of the time. Typescript in particular looks like a good choice for game development.

Sometimes the language is less of a good choice. E.g. i did a small toy project in coffeescript about two years ago, and found to my horror, that if your function ended in a loop, it would return an array. Always. This isn't possible to accidentally do in the host language, so I'd say it is a bad fit (for game development, where you care about such things).

Re:Voxel.css by tdy721 on Wednesday January 27, 2016 @12:29PM (#10981401)
Coffeescript returns the last line always, all you needed to do was add a 'return null' (or just 'return') as the last line in the function.

You have to be explicit in Vanilla JS to return things or it just return null. Coffeescript explicitly returns the last line every time.

Re:Voxel.css by tdy721 on Wednesday January 27, 2016 @12:33PM (#10981426)
I believe that point is that JS always returns something for every object. Many users of JS just don't realize it. (I know I didn't till I started drinking the coffee flavored kool-aid)
Re:Voxel.css by gsnedders on Wednesday January 27, 2016 @02:47PM (#10982557)
JavaScript functions always return something, yes—but unless you have a return statement, that something is `undefined`.
Re:Voxel.css by odbol_ on Wednesday January 27, 2016 @12:29PM (#10981398)
wtf coffeescript. What is the point of that?
Re:Voxel.css by matthewmacleod on Wednesday January 27, 2016 @04:50PM (#10983766)
This is a deliberate design decision in CoffeeScript – "everything is an expression" (see http://coffeescript.org/#expressions).

This is probably as a result of CoffeeScript's Ruby-esque syntax and semantics. It does mean you can write some very clear and succinct code at times, but the tradeoff is that it can have unforeseen performance implications.

Re:Voxel.css by goldbrick on Wednesday January 27, 2016 @03:59PM (#10983297)
It's what ruby does and coffeescript is basically an exercise in making js more like ruby.
Re:Voxel.css by spoiler on Wednesday January 27, 2016 @08:55AM (#10979992)
They don't add much overhead during runtime; most of the time there is zero overhead, or sometimes they can (theoretically, not sure if it's done in practice) produce more optimal code than a "literal translation".
Re:Voxel.css by klodolph on Wednesday January 27, 2016 @07:59PM (#10985098)
It depends on the language. TypeScript is basically JavaScript with added bits. Unity compiles to JavaScript, but it does so through a crazy chain of C# -> CIL (via Mono) -> C++ (via il2cpp) -> JavaScript (via Emscripten). And then remember that your browser then converts that JavaScript to assembly. Holy convoluted toolchain, batman.
Re:Voxel.css by flohofwoe on Wednesday January 27, 2016 @05:35AM (#10979327)
There's also the alternative of using C/C++ for portability and performance, but without a huge engine runtime you'll have to drag along.

There is no inherent size advantage of doing things in Javascript vs. cross-compiled to asm.js. For instance, three.js is 100kByte minified and compressed, that's in the same ballpark as the (admittedly simple) demos here: http://floooh.github.io/oryol/

Once you are over the static overhead for the C runtime and web-API-wrappers of roughly 30..50kByte, emscripten demos don't grow much faster than minified and compressed Javascript code.

[edit: typos]

Re:Voxel.css by yoklov on Wednesday January 27, 2016 @11:39AM (#10980995)
If you write in C you are probably fine for code size. Usage of the c++ stdlib can easily cause a lot of code bloat though if you are not careful, but I don't actually know how bad this gets (it used to be quite bad for native apps).

There are other issues as well. The biggest offender being the inflexible heap size, e.g. when you launch an asm.js module, you pass in an array buffer as the heap which cannot be resized. If you need more memory than this, you are SOL.

This wouldn't be that bad, except in practice it needs to be ridiculously small if you want to run on most machines. 256MB, which not much memory for a game at all, will fail to allocate, or kill the tab on about 33% of machines (being generous. For us its probably more like 50% of machines, but we make educational games that need to run on university machines, which are uh, on the lower end).

Either way, as a result, a great deal of the optimization time I mentioned was spent getting all of our games to run with under 128MB heap.

That said it's definitely possible if you're careful (I'm not sure this is quite as true for a voxel game though, but I could be wrong). My complaint wasn't about the idea of using asm.js, it was the idea that Unity would produce a good result. It just doesn't.

Re:Voxel.css by fulafel on Wednesday January 27, 2016 @06:14AM (#10979458)
Are there ready made engines suitable for this? Unity has taken 11 years and Three.js 6 years to get to their current estabilished status, so let's keep fingers crossed that something gets started soon on the asm.js/emscripten front.
Re:Voxel.css by flohofwoe on Wednesday January 27, 2016 @08:08AM (#10979800)
I think that's a bit of a chicken egg problem, the large game engines need to be everything to everybody, and trimming the executable size down probably wasn't a priority in the past since mostly you'll have a big upfront download anyway.

However, I remember that the first Unity HTML5/WebGL demos a few years ago were much smaller, a complete zombie FPS had a 3.5MB upfront download which is actually pretty decent.

Re:Voxel.css by larsiusprime on Wednesday January 27, 2016 @08:00AM (#10979762)
There's also Haxe, which has a lot of great 2D and 3D frameworks, and compiles to minimal javascript really well.
Re:Voxel.css by avodonosov on Wednesday January 27, 2016 @03:56AM (#10979009)
How much of the 26mb JS is actually invoked at run-time?

I tested several real world web sites, like google search, github, google docs - only around 30% of javascript loaded into browser is actually invoked at run-time. Maybe in your case the usage ratio is much lower?

Would it be helpful to remove unused code with a tool like this https://github.com/avodonosov/pocl ?

Re:Voxel.css by flohofwoe on Wednesday January 27, 2016 @05:41AM (#10979347)
emscripten-compiled code already has a very aggressive dead-code elimination, so this wouldn't help I guess. It is most likely the cross-compiled Mono runtime plus required Unity modules which produce the code bloat on top of the actual game play code.
Re:Voxel.css by avodonosov on Wednesday January 27, 2016 @06:44AM (#10979536)
The emscripten dead code elimination uses static analysis which is limited only to the code totally impossible to be reached during execution even in theory. But POCL leaves only code which is actually invoked at run time (keeping all other code on server in the "load at first invocation" form). The difference may be very significant.

Google products (google docs for example) use Closure Compiler for dead code elimination, but anyway, after clicking almost every menu item in document editor, only around 45% of code was invoked.

If that 26mb web site was available online, I would be interested to measure it's code usage ratio.

Re:Voxel.css by flohofwoe on Wednesday January 27, 2016 @08:10AM (#10979809)
Hmm that's very interesting, a bit risky, but worth experimenting with... thanks for the pointer!
Re:Voxel.css by avodonosov on Wednesday January 27, 2016 @01:31PM (#10981897)
What risks do you see?

BTW, the idea is not radically new. Java loads classes only when they are accessed first time.

Operating systems load native applications by mapping executable file into virtual memory. The pages are loaded into physical memory only when CPU tries to access them.

Re:Voxel.css by flohofwoe on Wednesday January 27, 2016 @03:34PM (#10983052)
Ok yes, if there's a mechanism that dynamically loads the missing parts then it could work (actually emscripten supports dynamically loading scripts similar to DLLs). But 'naively' deleting functions from an asm.js blob would result in some 'unknown function' exception if the analysis was wrong and a removed function is still called somewhere.
Re:Voxel.css by wwuww on Wednesday January 27, 2016 @09:55AM (#10980266)
But that might change drasticallyin the (hopefully near) future, when we see WebAssembly in the browser ...