Bare-bones Vue component wrapping a THREE.js instance.
Table of Contents
Main
Usage:
npm install vue-three-wrap --save
Then:
<!-- vue-three-wrap will stretch to fit its container by default -->
Props
Name | Type | Default | Notes |
---|---|---|---|
camera | Object | new THREE.PerspectiveCamera(75, 0.5625, 0.1, 1000) |
Main camera. |
cameraType | Object, Boolean, String | perspective |
perspective , orthographic , or ortho . Creates the desired camera as the scene default. |
fov | Number, String | 75 |
Camera field of view. |
height | Number | -1 | Height of the canvas. -1 to take up full height of container. |
injectShaders | Boolean | false |
Whether or not to inject custom shaders. See below. |
rendererOptions | Object | {} | Object of options to be passed directly to the WebGLRenderer. |
renderLoop | Boolean | true | Whether or not to call update every frame. |
renderType | String | webgl | webgl or css . Uses the CSS3DRenderer if set to css . (See below) |
start | Function({ scene, camera, renderer, slot, elements, CSS, vertexShader, fragmentShader }) | null | Function to be called once at scene creation. |
update | Function({ scene, camera, renderer, slot, elements, CSS, vertexShader, fragmentShader }) | null | Function called once per frame. |
width | Number | -1 | Width of the canvas. -1 to take up full width of container. |
start
and update
start
and update
functions accept one object with the following parameters:
// The THREE scene created by this VueThreeWrap scene // The main camera camera // The main renderer renderer // The contents of the default slot slot // An array of all valid elements in the default slot elements // an object containing CSS renderer objects (see below) CSS // the text of the first <script> tag in the default slot whose type is set to "shader/vertex" // (defaults to a standard vertex shader if none exists - see src/utils/shader-defaults.js) vertexShader // the text of the first <script> tag in the default slot whose type is set to "shader/fragment" // (defaults to a pink fragment shader if none exists - see src/utils/shader-defaults.js) fragmentShader
CSS Renderer
You can use THREE's CSS renderer with vue-three-wrap
:
I'm an h2 And I'm a paragraph
To do so:
- Set the
renderType
prop tocss
. - Use the
elements
argument in thestart
method to access elements in the default render slot. - Create new CSS3DObjects using the
CSS
property passed to thestart
andupdate
functions.
Otherwise, it's just like working with a normal THREE.js scene, just with usable DOM objects.
Shader Injection
Set the inject-shaders
prop to true
to inject some common noise functions. You can use THREE's #include
convention. All return a value of -1 to 1.
float snoise(vec2)
- 2D simplex noise.float cnoise(vec2)
- 2D Perlin noise.
An example fragment shader using 2D simplex noise:
Note that comments in your custom shaders must use /* this format */
, not // this format
.
Extras
Raycaster
A class that wraps Three's raycaster.
Example:
Constructor
Defaults shown.
// the area to check on mouseover el: document // the camera that will be doing the raycasting camera: null // whether or not to print debug messages debug: false // raycaster (optional - will create automatically if not specified) raycaster: null
Properties
Name | Type | Notes |
---|---|---|
interpolatedX | Number | The normalized relative mouse X position, from -1 to 1. |
interpolatedY | Number | The normalized relative mouse Y position, from -1 to 1. |
mouseX | Number | The latest mouseX position relative to the container. |
mouseY | Number | The latest mouseY position relative to the container. |
Methods
Name | Arguments | Notes |
---|---|---|
init | options (same as constructor) | Initializes this raycaster. Same method as called by constructor. |
updateMouse | mouse event | Updates relative mouse coordinates. Called internally. |
cast | { coordinates: { x: Number, y: Number }, camera: THREE.Camera } |
Raycast and save the results internally. If coordinates are unspecified, uses the last coordinates set by updateMouse . If camera is unspecified, uses the camera added during instantiation. |
intersectObject | (object, recursive, optionalTarget, coordinates, camera) | Calls cast() with the given coordinates and camera , then runs intersectObject with the first three arguments. |
intersectObjects | (objects, recursive, optionalTarget, coordinates, camera) | Calls cast() with the given coordinates and camera , then runs intersectObjects with the first three arguments. |
destroy | None | Destroys the event listener created during init . |
Postprocessing
You can use the postprocessor from the examples very easily with the custom-renderer
prop:
To use:
-
Import
QuickComposer
fromvue-three-wrap/extras/quick-composer
.- If you want more control of your composer, you can also import
EffectComposer
fromvue-three-wrap/extras/effect-composer
.
- If you want more control of your composer, you can also import
-
Set the
custom-renderer
prop invue-three-wrap
to an instance of the QuickComposer. -
Instantiate the QuickComposer with the following options as an object:
{ // these three can just be passed from your start/update functions scene, camera, renderer, // an array of shader objects (see below) passes: [] }
TODO: document EffectComposer and QuickComposer
Post Shaders
vue-three-wrap
comes with some complete shaders in the form of JS objects:
DotScreenShader
RGBShiftShader
You can import any existing shader from vue-three-wrap/shaders/YourDesiredShader
.
You can also create your own by making an object with uniforms
, vertexShader
, and fragmentShader
properties:
// ExampleShader.js
export default {
uniforms: {
// Each uniform ust be an object with a `value` property
yourUniform: { value: 0}
},
vertexShader: 'A string containing your WebGL vertex shader',
fragmentShader: 'A string containing your WebGL fragment shader'
}
You can then use this shader as a pass in the QuickComposer or EffectComposer. Writing shaders is beyond the scope of this readme - take a look at The Book of Shaders for more information.
Object Loader
You can import .gltf
and .glb
files, the format that Three prefers, using the load-gltf
extra.
BMFont
vue-three-wrap
comes with methods to handle loading and displaying text with BMFonts. To use:
- Convert your font to a BMFont with a tool like
msdf-bmfont
. Place the created.fnt
and.png
files in your project. - Import the
bmfont
method fromvue-three-wrap/extras/bm-font
and use like this: