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.
This is fantastic. Thanks!
Thanks for sharing! I made a tiny game with this. https://www.lexaloffle.com/bbs/?tid=28139
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
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. @[email protected]
@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.
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!
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.
Thanks for the heads up--I may have tweaked some blender settings.
I'll put together a proper tutorial.
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.
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)
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?
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.
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
@electricgryphon I'm trying to do make a 3d face in pico-8 but it gives me this error. here are the variables
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.
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.
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.
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.
[Please log in to post a comment]