Log In  

Cart #34798 | 2017-01-03 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

old version:

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.

P#32574 2016-11-17 01:56 ( Edited 2017-06-28 20:27)

awesome! thanks so much for sharing!

P#32577 2016-11-17 06:13 ( Edited 2016-11-17 11:13)

Great stuff! Thanks for that

P#32578 2016-11-17 08:22 ( Edited 2016-11-17 13:22)

This is fantastic. Thanks!

P#32579 2016-11-17 09:29 ( Edited 2016-11-17 14:29)

Tremendous stuff.

P#32586 2016-11-17 15:19 ( Edited 2016-11-17 20:19)

Thanks for the share, looking forward to seeing some projects using this.

P#32632 2016-11-19 18:11 ( Edited 2016-11-19 23:11)

Thanks for sharing! I made a tiny game with this. https://www.lexaloffle.com/bbs/?tid=28139

P#32960 2016-12-03 07:16 ( Edited 2016-12-03 12:16)

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

P#34802 2017-01-02 21:48 ( Edited 2017-04-01 16:50)

How do you do that? Seriously, how do you make a 3D engine? I realize this is probably a really broad question, but I wouldn't even know where to start :o

Also, if you can't tell, I'm super impressed. @.@

P#34865 2017-01-03 15:25 ( Edited 2017-01-03 20:25)

@electricgryphon: Great job! I loved this before, and this is a huge improvement!

@Lumiette: Definitely check out the code; it's well commented and messing with it a bit should help you start to understand how it works.

P#34870 2017-01-03 16:45 ( Edited 2017-01-03 21:45)

Great work! Want to try and make something with this library but the link to the Python script is broken. Could you please reupload it? Thanks!

P#35047 2017-01-05 21:54 ( Edited 2017-01-06 02:54)

Try this: http://s000.tinyupload.com/index.php?file_id=56902566375777023329
I updated the link in the description as well.

P#35053 2017-01-06 01:22 ( Edited 2017-01-06 06:22)

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.

P#35154 2017-01-07 10:09 ( Edited 2017-01-07 15:09)

Thanks for the heads up--I may have tweaked some blender settings.

I'll put together a proper tutorial.

P#35167 2017-01-07 12:55 ( Edited 2017-01-07 17:55)

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.


P#35175 2017-01-07 16:07 ( Edited 2017-01-07 21:07)

Thank you for the tutorial and the fast response! :) Works perfectly and have already started working on some simple player stuff. (Thinking of making a simple shooting gallery)

P#35199 2017-01-07 19:29 ( Edited 2017-01-08 00:29)

Hey this is great! I got some models loaded and it's pretty fast! What would be the best way to set the color of the individual faces?

P#38812 2017-03-28 10:18 ( Edited 2017-03-28 14:19)

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.

P#38873 2017-03-30 03:24 ( Edited 2017-03-31 06:30)

Awesome stuff.

P#38921 2017-03-31 15:31 ( Edited 2017-03-31 19:31)

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.

-Electric Gryphon

P#38981 2017-04-01 12:49 ( Edited 2017-04-01 16:53)

best doggo

P#39009 2017-04-02 14:56 ( Edited 2017-04-02 18:56)

Hi electricgryphon,

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?

-- Ben

P#39756 2017-04-18 13:12 ( Edited 2017-04-18 17:12)

Hi Computermouth,

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.


P#39767 2017-04-18 22:10 ( Edited 2017-04-19 02:10)

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):

Cart #39790 | 2017-04-20 | Code ▽ | Embed ▽ | No License

Cart #39791 | 2017-04-20 | Code ▽ | Embed ▽ | No License

P#39792 2017-04-20 02:56 ( Edited 2017-04-20 06:56)

Hi JustBurner,

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.


P#39802 2017-04-20 16:54 ( Edited 2017-04-20 20:54)

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 ;)

P#39803 2017-04-21 03:12 ( Edited 2017-04-21 07:12)

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?

P#40989 2017-05-26 08:18 ( Edited 2017-05-26 12:21)

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.

P#40996 2017-05-26 10:57 ( Edited 2017-05-26 14:57)

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.

P#41011 2017-05-26 16:58 ( Edited 2017-05-26 20:58)

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.

P#41982 2017-06-28 16:27 ( Edited 2017-06-28 20:27)

Hi, will you update this Engine to support textures with the new tline()?

P#77338 2020-05-28 22:33 ( Edited 2020-05-28 22:37)

Hi leissler, I'm not planning on adding to this particular engine now. There are a couple of folks who are doing great things with tline now, including Johan Peitz. https://twitter.com/johanpeitz/status/1266090587683422209

Back in the --ahem-- old days we didn't have tline() and made do with hacky cludges. https://www.lexaloffle.com/bbs/?tid=29819

P#77352 2020-05-29 04:37

@electricgryphon I'm trying to do make a 3d face in pico-8 but it gives me this error. here are the variables

P#79081 2020-07-09 04:23

Hey! i was testing your 3d engine when i got this error. after I looked at the code trying to understand what the error was, I realized that there is no such functions as read_vector_string; read_face_string in the engine code at all, but in your example it was. please correct the error, or correct me if I'm wrong.

P#121006 2022-11-19 19:51 ( Edited 2022-11-19 19:51)

So I figured out what was the problem. the read_vector_string function was outside of the part of the code that needed to be copied. well, okay, I copied the additional part of the code, but the problems did not end. inside the LOGIC OF THE ENGINE, the timer method was called, which was outside the logic of the engine. hmm... I commented out every mention of the timer that caused an error, and now everything works fine. however, that have to be fixed. I would also like a tutorial on setting up light, materials and other graphics settings.

P#121012 2022-11-19 20:21

Hi @ioil13. This may be of help. There are =3= keys to search for code in Pico-8.

CTRL+F = Search in this current TAB.
CTRL+G = Repeat the last search in this current TAB.
CTRL+H = Repeat the last search in ALL tabs.

The last one you may not be aware of as it is not included in the standard Pico-8 HELP file, either included with the system or the main Online help.


P#121043 2022-11-20 05:21

Hello @dw817! yes, these hotkeys could be useful in finding the missing code, I'll take note of them. however, I'm still surprised how the library ended up with a piece of code that was completely unrelated to it. plus a very important part of the code, which, on the contrary, is missing from the library. In any case, I'm glad that I managed to fix everything and start using the library. thanks again, cheers.

P#121059 2022-11-20 11:02

hello. i don't know if this engine still updates, but i found that changing AX, AY or AZ in load_model function arguments doesn't change anything. its only works when in "model.ay=[angle]" call after model load.

P#121193 2022-11-22 19:41

This is spectacular; thanks for sharing!

P#123272 2022-12-30 03:02

Wonderful library! I am currently trying to implement color values for the faces of the 3D-object. However in the read_face function I dont see any loading of the face colors (which should be assinged to f[4] and f[5]).

function read_face()
    --verts 1 to 3
    for i=1,3 do

    return f

If I am understanding this correctly, no face color values are included in the compressed string. Because of this I'll add a new data type which contain the face color values as a compressed string. I'll post this in the coming days. I'm also making a fork of your github repo for converting .obj files to strings with support for face color values.

P#142090 2024-02-27 23:17

Currently working on a tool to allow for coloring models in this library. This is just a little preview, I will implement exporting as string soon.

P#142134 2024-02-28 16:25 ( Edited 2024-02-28 16:26)

Finished it. The face painter can be found here. Let me know if you have any questions.

Cart #face_painter_3d_gryphon-2 | 2024-02-29 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

P#142193 2024-02-29 11:03 ( Edited 2024-02-29 11:04)

[Please log in to post a comment]