Here is a cleaned up version of the 3D library that I put together for the Pico Fox game.
The demo code is commented in more detail, but here is the basic gist.
Copy the code between Begin Cut and End Cut. (It's a big chunk.) In _init() function include: --init_3d() --Need to call init_3d() to set up player, camera and lights --use load_object(object_vertices,object_faces,x,y,z,ax,ay,az,obstacle,color_mode,color) to load 3D models into the world --use read_vector_string and read_face_string to generate vertex list and face list from string data In _update() function include: --handle_buttons() -- handle default buttons for player-- this can be overwritten obviously. --update_player() -- update the player with default movement, stopping at obstacles --update_camera() -- update the camera based on player location and direction --update_particles() --update 3D particles if used. (I didn't add any for this demo.) --... --update_3d() -- call update_3d() at the end of the _update() function to transform etc. in _draw() function include: --draw_3d() --render objects into triangles, sort the triangles and draw them onto the screen
Please feel free to use this in your projects as well as to update it and make it work better.
New features for version 2:
--Significant speed increase (2X!)
--Load models from strings to save token space
--Python script to translate from ".obj" files to compressed string
Run the script in the folder with the files that you are converting.
python low_poly_compress-01.py filename.obj
Detailed instructions and script found on GitHub.
I optimized the rendering of the engine--more than double the speed for dynamically shaded models.
The most significant tweaks were:
--New triangle rendering code based on the results of the triangle thunderdome thread
--Faster quicksort replaces bubblesort
--Projecting points more efficiently (I was projecting the same vertex multiple times previously
These, combined with some in-lining and other optimizations pushed the speed up quite a bit so that "large=338 faces" models can be rendered in real time with shading.
I also added functions to load objects from strings as opposed to massive arrays of numbers. Those arrays are nice and readable, but they chew through tokens.
The conversion script can be downloaded here: https://github.com/electricgryphon/Pico-8-Gryphon-3D-Engine-Library
Try this: http://s000.tinyupload.com/index.php?file_id=56902566375777023329
I updated the link in the description as well.
Thank you, link works now.
Haven't been able to succesfully convert an .obj though.
Tried running the script with Python 2.7 and 3.6 but both don't work. I'm trying to convert a basic cube. (The one you get when you open Blender) I get two errors at the parts when num_text is stored in val. In the model_v line there is an "n" that can't be converted to float and in the model_f line all data looks like "1//2" and thus can't be converted to int.
Have tried simply filtering the n and // out but that doesn't work obviously. :-) Not really familiar with the .obj file structure so not sure what I'm doing wrong.
Here's a link to a tutorial on how to convert files using the script:
(This is also added to the cart description.)
Let me know if anything is unclear or if you have problems and I will update the document.
Hey electricgryphon, I'd love to see copies of the .obj files used for these demos and/or picofox as well if you've still got them. Do you think you could put them on Github? I'm new to blender, and it'd be cool to have something to play around with so I know I'm on the right track with the model creation. Thanks!
EDIT: previously, this said I couldn't access any of the tinyupload URLs, the issue has since resolved itself.
Tools and instructions are loaded on GitHub.
I have also included a demo blender file for a basic animal.
Currently there is no support for automatic importing blender files with individual face colors. (On the PicoFox program, I did this by hand :-/ ) This is definitely on the list of features to be added in the next version.
I'm trying to port the lib to C, and I'm running into a problem with some of the bitwise operations. Here in particular: https://github.com/computermouth/eg3d/blob/master/render.c#L2020
I can't quite figure out what value this is supposed to be retrieving. I wonder if it's some sort of floating point magic? I tweeted at zep here https://twitter.com/ComputerMouth/status/854203622095077378
At first I thought it was a bug, but the more I look at it, the more it seems like the library actually depends on this behaviour. Any tips?
Pico 8 stores numbers in a fixed point format of 0xFFFF.FFFF.
In this case, I'm using band(val,0xffff) as a way to speed up floor().
Zep reduced the function overhead CPU cost of bitwise operations so one way to speed up programs is to use these operations whenever possible.
electricgryphon, I've kept forgetting to reference something I've did months ago, was to work on something with your lib but sadly pico-8 is too limited for it and kinda moved to other non-pico8 related stuff =/
You can have a look at the converter for blender: https://github.com/justburn/EGryphon3DLibPanel
Oh... and as a bonus (Inspired by Picofox):
Your arrangements of the StarFox songs are great! Could I pull them into the game? Some proper menu music, like you provided, will work wonders.
The blender export will also come in handy--i've been writing offline conversion scripts, but this could save some steps.
Sure, I'll be glad if you use them into your game! (Corneria is 3 channels instead of 4 for a reason), it will need some tweak to join them together and add sfx.
If you're going to update your game don't forget to fix a bug in the center_text function and "cornerea" typo... could say to upgrade for v2 of the engine but that's asking too much ;)
One thing I'd change would be using checkerboard dithering instead of lines. IMO it would look better.
Also how do I add my models to it and what is reasonable vertex count?
//edit: I've walked into one of the floating tetrahedrons in that temple thingy (one with cube) and it looks like you're not doing backface culling. Any reasons for that?
Welcome back Darkhog.
Check the thread for details and tutorials of how to load models. (There is a script I posted on github.)
I run backface culling for most triangles, but turn it off when triangles are being clipped by the view plane. (The clipping code is a hacky mess, and it loses the winding/normal direction of the triangle.)
I have a raccoon demo posted that shows how to do checkered dithering on the 3D models. It's a little slower than the line shading, but does look cool.
I think I know why it's slower for checkerboard shading. You seem to be copying parts of spritesheet that seems to be a LUT texture which has checkerboard patterns backed in, instead of just storing the gradients somewhere and calculating shading at runitme using line (for # fill first fill with a solid color of internal dots, then plot perpendicular 90 deg. lines of second color, for crisscross just put 45 degrees lines in whatever color order). I think it's be faster that way.
Whelp, Just loaded the doggy model from your Github repo electricgryphon into my 3d engine and my pico8 cpu pegged at 2.2, in your engine the model tops out at a cpu of 1.1.
Even just as a wire frame that model pushes my engine to 1.3 while rotating!
Looks like I have lots of efficiencies to look for, about 0.5 CPU is spent just transforming the co-ords into world space.
Log in to post a comment