3D web starter kit with Three.js and TypeScript.
This is a 3D web starter kit for Gatsby.js websites written in TypeScript. 3D scenes are expressed by a pure Three.js library. It includes the bare essentials for you to get started (styling, minimal toolset).
:white_check_mark: Three.js
:white_check_mark: TypeScript
:white_check_mark: Gatsby
:white_check_mark: Netlify
:white_check_mark: TailwindCSS
:white_check_mark: Emotion
A nodejs >= 6.0.0 setup with yarn is recommended.
Create a Gatsby site.
Install gatsby-cli
package globally on your machine.
# using NPM
npm install -g gatsby-cli
# using YARN
yarn global add gatsby-cli
Use the gatsby-cli
to create a new site and install its dependencies.
gatsby new project-name https://github.com/shunp/gatsby-three-ts-plus
Or you can use this command if you are familiar with degit
.
degit [email protected]:shunp/gatsby-three-ts-plus
Start developing.
Move to project's directory.
cd project-name/
Start your site.
# using npm
npm start
# using yarn
yarn start
Open source code using your favorite IDE/Text editor and navigate to src/
directory, this is where your application live.
Build your application for production.
Once you're finished, you can make production build of your app using:
# using npm
npm run build
# using yarn
yarn build
Deploy your app to Github pages!
After building your application in step 3, you're ready to publish your app and go online!
# using npm
npm run deploy
# using yarn
yarn deploy
A quick look at the top-level files and directories you'll see in a Gatsby project.
```
.
βββ node_modules
βββ src
βββ static
βββ .gitignore
βββ .prettierrc
βββ gatsby-browser.js
βββ gatsby-config.js
βββ gatsby-node.js
βββ gatsby-ssr.js
βββ LICENSE
βββ yarn.lock
βββ package.json
βββ README.md
```
/node_modules
: This directory contains all of the modules of code that your project depends on (npm packages) are automatically installed.
/src
: This directory will contain all of the code related to what you will see on the front-end of your site (what you see in the browser) such as your site header or a page template. src
is a convention for βsource codeβ.
.gitignore
: This file tells git which files it should not track / not maintain a version history for.
.prettierrc
: This is a configuration file for Prettier. Prettier is a tool to help keep the formatting of your code consistent.
gatsby-browser.js
: This file is where Gatsby expects to find any usage of the Gatsby browser APIs (if any). These allow customization/extension of default Gatsby settings affecting the browser.
gatsby-config.js
: This is the main configuration file for a Gatsby site. This is where you can specify information about your site (metadata) like the site title and description, which Gatsby plugins youβd like to include, etc. (Check out the config docs for more detail).
gatsby-node.js
: This file is where Gatsby expects to find any usage of the Gatsby Node APIs (if any). These allow customization/extension of default Gatsby settings affecting pieces of the site build process.
gatsby-ssr.js
: This file is where Gatsby expects to find any usage of the Gatsby server-side rendering APIs (if any). These allow customization of default Gatsby settings affecting server-side rendering.
LICENSE
: This Gatsby starter is licensed under the 0BSD license. This means that you can see this file as a placeholder and replace it with your own license.
yarn.lock
(See package.json
below, first). This is an automatically generated file based on the exact versions of your npm dependencies that were installed for your project. (You wonβt change this file directly).
package.json
: A manifest file for Node.js projects, which includes things like metadata (the projectβs name, author, etc). This manifest is how npm knows which packages to install for your project.
README.md
: A text file containing useful reference information about your project.
You can find a simple scene at src/scenes/BaseScene.tsx
. The basic components such as a camera
and scene
have been decleared. You can customize your scene as you want, referencing other scenes placed on the same directry.
import React, { useEffect, createRef } from 'react'
import * as THREE from 'three'
import { css } from '@emotion/core'
const newScene = () => {
const scene = new THREE.Scene()
return scene
}
const newCamera = () => {
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
camera.position.x = 100
camera.position.y = 100
camera.position.z = 400
return camera
}
const newRenderer = (mount: React.RefObject<HTMLInputElement>) => {
const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.autoClear = true
if (mount.current) {
mount.current.appendChild(renderer.domElement)
}
return renderer
}
const BaseScene = () => {
const mount = createRef<HTMLInputElement>()
useEffect(() => {
// scene
const scene = newScene()
// camera
const camera = newCamera()
// renderer
const renderer = newRenderer(mount)
// mesh
const geometry = new THREE.BoxGeometry(50, 50, 50)
const material = new THREE.MeshNormalMaterial()
const mesh = new THREE.Mesh(geometry, material)
scene.add(mesh)
// render
const render = () => {
renderer.render(scene, camera)
}
// animation
const animate = () => {
requestAnimationFrame(animate)
render()
}
animate()
}, [])
return (
<>
<div css={css``} ref={mount} />
</>
)
}
export default BaseScene
Built with Gatsby - the blazing-fast static site generator for React.