3D engine thing

I’ll write this eventually, so I might as well write this now…

How to use this thing

First off, you need to get the engine into your project obviously. Then make a new frame. You are going to use this frame to write some code.

Then make a clip with a rectangle in it, put in the frame you just created. Name it something so you can actually access it. In this example I will name it “tri”.

Then, in the frame, put this as its default script:

project.renderer = new pk3d.Renderer(tri);
project.camera = new pk3d.Camera(project.height / project.width, 70);
project.scene = [];

This code

  1. creates a Renderer, and passes the clip “tri” as the renderer’s triangle clip. So now when the renderer wants to render some triangles, it will create a clone of that clip to make a triangle with it.
  2. creates a Camera. The first parameter is the project’s aspect ratio. The second is its field of view, in degrees. (There are third and fourth parameters which are the camera’s near and far respectively (which determines the limit of how near or far away objects can be from the camera), but they can be omitted and when they are, default to 0.1 and 1000)
  3. Creates an empty array for the scene. This is where all the 3D objects will be stored for render.

It also creates all of these newly assigned objects as project variables, so other scripts can access them.

Then, in the update script of that same frame, insert the following code:

project.renderer.render(project.camera, project.scene);

This simply renders the scene using the camera on every frame.

Now, once you are done, when previewing the project you should get an empty scene and hopefully no errors as I haven’t tested it, I’m just looking at the main code in the pk3d project.

To add some meshes you can import some .obj files. I do not have functions to create shapes such as cubes and spheres so you will either need to manually put all the mesh data or import .obj files. When importing .obj files you need to make sure it only stores vertex, material, and face data, and not things like UV coordinates. Only the basics. It will probably error if you don’t do so.

To actually get it into the project you will need to disguise the files (files in plural you can import .mtl files since they are paired with .obj files). Do this by changing the file extension to a supported type, like .wickobj. Then, once they are in the projects, you can change their file extension back so they show up as stuff like “model.obj” and “model.mtl” (they don’t have to be named model you can name thing anything you want).

If you don’t want to have to get an .obj file and .mtl file, you can just steal some of the object files from my pk3d project. (Oh wait you I think you can’t export files from wick editor… umm… Oh shoot i can’t upload .obj or .mtl files onto here um… archmtl.wick (229 Bytes) archobj.wick (1.4 KB) aha yes that take wick editor. ok now download them and replace their file extensions with .wickobj is this legal this is like smuggling)

Then, in the previously mentioned frame, put the following code:

this.mesh = pk3d.createMeshFromAsset( this.project.getAssetByName("model.obj") );
project.scene.push(this.mesh);

It creates a mesh from the .obj file and adds it to the scene. Now when you preview it now, you probably won’t see your mesh beacuse the camera is inside the mesh, and because when you look at triangles from behind they disappear, you probably don’t see any triangles (or maybe detached triangles floating in mid-air)

To fix this you will move your camera. Put the following code in the frame:

project.camera.position = new pk3d.Vector3(0, 0, -10);

This will hopefully move the camera far enough backwards for you to see the mesh.

If you want to make it rotate you can make rotate. In the Update script of the frame (preferably before the line which renders the scene), add this code:

this.mesh.rotateY(0.01);

It makes the mesh rotate which is pretty cool. You can also do it using this way:

var rot = pk3d.CFrame.fromRotation(new pk3d.Vector3(0, 0.01, 0));
this.mesh.cframe = rot.mul(this.mesh.cframe);

You’re probably asking what the heck is this cframe thing? CFrame is short for Coordinate Frame, a name I did not steal from Roblox because they also use the exact same thing and has a better name than “point-at matrix”. And I know that because I sometimes program stuff in roblox studio. Anyway, CFrames store positional and rotational data and they’re kinda tricky and hard to understand also they’re really 4x4 matrices.

Anyway, the code creates a CFrame rotated 0.01 radians in the Y axis, then multiplies that with the mesh’s CFrame. This makes the mesh rotate. So multiplying CFrames with other CFrames will create a transformed CFrame. Also CFrame multiplication isn’t commutative, since matrix multiplication isn’t either, so cf1 * cf2 is not the same as cf2 * cf1.

umm yeah and raycasting and sprites and i jeez i really gotta go now i’ll edit in more stuff later

1 Like

Aha, I think I might have figured out why clipping wasn’t working properly. My guess as to why it wasn’t working was because the triangle wouldn’t get clipped on multiple edges. Now I figured out why it wasn’t doing that: it’s because when a triangle gets clipped, its results get put at the end of the clipped triangles list. However, the problem with this is that the program clips the triangle at the end of the list. So, for example, if a triangle needed to be clipped into two triangles, it will clip the two. But then at the next edge, it will clip the last triangle in the list then put the results at the end of the list. Then it will clip another triangle at the end of the list (which are the new triangles) with the same edge and then ignore the other triangle, because it already clipped two triangles (which is the amount of triangles in the list).

I fixed it by putting the new triangles at the beginning of the list instead of the end. Here’s the fix: pk3d.wick (27.8 KB) The not-clipping problem is completely gone. The change of a single line of code that fixed this problem is in line 1101 of the engine code.

Edit:
oh. and i can see that javidx9 actually wrote code to take the triangle at the front of the list, not at the back… but i made it so it gets the triangle at the end of the list… which is wrong as i stated… lol… cries

1 Like

This is so Cool when you can ad Phyics and a Playabel charckter you can make a Verry cool 3D Platformer

1 Like

image
Whoa.

2 Likes

yup, that is a gift from our forum member @pumpkinhead

1 Like

pretty cool engine but i would like to know how to make walls

please don’t revive old posts.

revive power jk also i wanna revive it cause its awesome

how do I include my own 3d model? @pumpkinhead

No reason to not to :|

@pumpkinhead

maybe look here…

It looks like you have to import some custom assets and then call some code ?

2 Likes

Thank you!

Why can’t I open the wick files?

Idk, maybe it no longer works on the newest version or it doesn’t like the custom files ?
edit: didn’t work for me either

1 Like

we could wait for @pumpkinhead to reply

1 Like

Bruh moment.

reviving posts on its own isn’t bad, as long as the reply is relevant. it’s better than making a brand-new topic.

also @Jordy you don’t need to ping pumpkinhead 3 times.

so first what you wanna do is you wanna get your model’s files. it has to be an .obj or .mtl file since the built-in helper function only supports that. (btw an .mtl file are the materials for an .obj file) then you have to change the file extension of those files to .wickobj so that you can actually import them into the project. once you have done that,

to create the mesh you use the following code:

// here the file name would probably be model.wickobj
// but its name in the project is "model.obj"
// you can rename it to anything this is just an example
var mesh = pk3d.createMeshFromAsset( this.project.getAssetByName("model.obj") );

// you can also pass the .mtl file as a second argument 
var meshWithMaterial = pk3d.createMeshFromAsset(
  this.project.getAssetByName("model.obj"),
  this.project.getAssetByName("model.mtl")
);

then you have to push that mesh into an array which in this example would be called scene.

to render the scene you use this code:

// run this on initialization
project.renderer = new pk3d.Renderer(triangleClip);

// run this on update
// camera is a pk3d.Camera instance
project.renderer.render(camera, scene);

but honestly i wouldn’t use this 3d engine for practical use. it’s way too slow cuz it’s in wick editor, and also the triangle sorting is bad for large surfaces because i’m forced to use painter’s algorithm.

i could make a better 3d engine in wick using opengl, but then there would be no point having it in wick.

1 Like

i’m assuming you’re talking about not being able to open archobj.wick and archmtl.wick

that’s because they’re not actually wick files i just changed their file extensions to wick to bypass the file extension restrictions

1 Like