How to trigger functionality in GLTF file that triggers functionality in Next.js project?

Not sure exactly how to word this, so sorry if it’s confusing.

  • My Needle project is exported as GLTF (i assume, not actually to that part yet).
  • Action happens in GLTF file, like a button being pressed
  • I want that action to trigger something to happen in Next.js project that GLTF is placed in (assuming GLTF is basically used like an HTML element). For example, i want the button to trigger the Next.js project to go to a different route. So from localhost:3000/scene1 to localhost:3000/sceneTest

Is this possible?

Original Post on Discord

by user 352282921056468993

Technically in the GLTF nothing happens - the GLTF is the data container and when you export your project from e.g. Unity using our tools then your whole project data is written to the glb (it’s similar to a fbx for example, it that holds all the mesh and rendering info and the GLTF has that PLUS your component data which we added)

When the glb is loaded in Needle Engine it is unpacked and we create the components for you again that you configured in (let’s assume Unity)

So what you want to do is something like:

  • Create a Needle Typescript component in your web project “src/scripts” directory, name it e.g. “MyNewComponent.ts” and add a method like start() { console.log("HELLO") } in that component
  • Add the Unity component that gets generated with the same nem (MyNewComponent.cs) and add it to a Object in your Unity scene
  • Now click play to re-export your scene
  • Assuming your have your exporter setup to export to the next.js project it will automatically refresh, load your glb and display “Hello” in the browser console log.

More indepth info here: Scripting Introduction | Needle Engine Documentation

Here’s also a short video about scripting in Needle Engine: https://www.youtube.com/watch?v=uf5UK0bLHlY&list=PLJ4BaFFEGP1GVTmPhKDC6QzL8Am9700Wo&index=5&t=2s&ab_channel=Needle

Note that we already have a builtin component to open an URL by clicking on an object. Have a look at the “OpenURL” component (I know it was just an example but just in case it wasnt :slightly_smiling_face: )

so is the Next.js project/code separate from the Needle web project/code inside “src/scripts”? For example, i have a personal site and want some pages to have Needle exported content and some to not have anything Needle-related. Would i have to find a way to turn the code of my personal site (Next.js project) into a Needle project?

by user 352282921056468993

You can use your existing site yep - It’s up to you on which pages you include the component. Have you seen our next template?

It’s a single page example but you get the idea hopefully! nextjs-sample/src/pages/index.tsx at 5c29eff6169f44761f555a1611f2857f4fa1a71f · needle-engine/nextjs-sample · GitHub

i did see that was recently added. Ahh okay, well glad to know this is all possible! I will try to figure out how to convert a regular Next.js project into something that can have a mix of Needle content and non-Needle content. Not currently sure how to bridge the gap, but this seems like good starts thanks!

by user 352282921056468993

Technically it’s not different to how you would add something like react three fiber :slightly_smiling_face: install the npm package + add the component

Good luck!

I tried hooking up my project to a new empty needle project created in unity, but getting some issues when clicking the
“Install” button in Unity. Here’s the errors:

warning Error running install script for optional dependency: "C:\\Users\\jackj\\Projects\\shmoji\\node_modules\\cpu-features: Command failed.

throw new Error('Unable to detect compiler type');

You can see in image i picked the root folder of Next project. I went through the nextjs-sample you sent and copied all those files line for line basically. My best guess is there is some sort of dependency conflict. Before adding Needle, here is the project dependencies: shmoji/package.json at main · Shmoji/shmoji · GitHub
Screenshot_2023-08-05_131015.png

by user 352282921056468993

The package cpu-features is not known to me, never seen it
Perhaps run npm ls --depth=5 in the web project directory to figure out which package is pulling this in

You can also run npm install via the commandline in the project directory to get more information (usually the npm log file is also printed in the console)

Figured it out! thanks for all the help. Will say what fixed things for me in case will help others:

had to remove babel.config.js from my next project - idk much about babel and compilers, but i think that prevented the correct needle compiler from being used

by user 352282921056468993