How to import a GLSL file into a GLSL file in ReactJS & ThreeJS

Oscar de la Hera Gomez
Three flowers that represent ReactJS, ThreeJS & ViteJS side by side. Beneath them sits the text "Include in GLSL."

To create a structured GLSL project in ReactJS & ThreeJS, create a GLSL ViteJS enabled project and use the #include statement along with the files path.

We recommend that you clone our Open Source React, Typescript & ViteJS Starter Project, checking out the main branch and carrying out the steps below. The changes can be found on the tutorial/three-js/include branch.

git clone git@github.com:delasign/reactjs-3js-vitejs-starter-project.git

Step One: Setup the Project

A screenshot of Chrome showing that the plane is now using the shader that we offer in this tutorial.

Follow the tutorial below to learn how to setup a ViteJS project to work with GLSL, ReactJS, Typescript & ThreeJS.

Step Two: Create the shared GLSL file

A screenshot of VSCode showing how we created a BaseLight.glsl file that holds the struct. All the elements of the struct end in a ";" this is important as without it, the file will not load.

Create the shared GLSL functionality in a new file.

We recommend that you place this within a shared folder.

If the functionality is not a function, make sure that it ends in a semi-colon (i.e. ;), otherwise the project will produce errors.

Step Three: Include the GLSL File

A screenshot of VSCode showing how we have included the GLSL file in a separate file using the include statement.

In the file that you wish to import the functionality that you created in Step Two, add the following line at the top:

Please replace ./path/to/file with the path to your file.

#include "./path/to/file";

Step Four: Test

A screenshot of Chrome showing that the plane is now using the shader that we offer in this tutorial.

Run the code and confirm that everything works as expected.

Looking to learn more about ReactJS and ThreeJS ?

Search our blog to find educational content on learning how to use ReactJS and ThreeJS.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details