0.2.6 • Published 5 years ago

@twoavy/gltf-viewer v0.2.6

Weekly downloads
-
License
-
Repository
github
Last release
5 years ago

gltf-viewer

A Vue component to view GLTF-3D-Models, incl. OrbitControls and v-model position

Installation

npm i @twoavy/gltf-viewer
OR
yarn add @twoavy/gltf-viewer
import GltfViewer from 'gltf-viewer';

Usage

Once installed, it can be used in a template as simply as:

<gltf-viewer url="path/to/model.gltf" 
             :options="options"
             :points="points"
              @point-clicked="onClick($event)"/>

or with multiple Models:

<gltf-viewer :urls="['path/to/model1.gltf', 'path/to/model2.gltf']" 
              @object-loaded="onLoaded" />

Options

{
    bgColor: 0xcccccc,
    maxClickAngle: 25,
    lights: {
        exposure: 1.0,
        textureEncoding: 'sRGB',
        ambientIntensity: 0.3,
        ambientColor: 0xFFFFFF,
        directIntensity: 0.8 * Math.PI,
        directColor: 0xFFFFFF
    },
    controls: {
        enabled: true,
        minPolarAngle: 0,
        maxPolarAngle: Math.PI,
        minAzimuthAngle: -Infinity,
        maxAzimuthAngle: Infinity,
        minDistance: 0,
        maxDistance: Infinity,
        autoRotate: false,
        autoRotateSpeed: -10,
        enableZoom: true,
        zoom: 1 // note that zoom and min/max distance affect each other!
    }
}

Points

[
    {
        id: 0,
        position: { x: 1, y: 1, z: 1 },
        rotation: { x: 0, y: 0, z: 0 },
        // payload can be anything (e.g. target camera position)
        payload: {
            x: -0.9,
            y: 0.5,
            z: -1.1
        }
    },
    {
        id: 1,
        position: { x: 2, y: 4, z: -1 },
        rotation: { x: 1, y: 2, z: 0 },
        // payload can be anything
        payload: {
            x: -0.9,
            y: 0.5,
            z: -1.1
        }
    }
]

v-model

Animate positoin with e.g. Greensock:

<gltf-viewer url="path/to/model.gltf" 
             v-model="position" />
data() {
    return {
        position: {}
    }
},
methods: {
    tweenPosition() {
        let formPos = { ...this.position }
        let targetPos = { x: 1, y: 2, z: 3 }
        TweenLite.to(fromPos, 0.6, {
            ...targetPos,
            onUpdate: () => {
                this.position = fromPos
            }
        })
    }
}

Note that you cannot change the camera position via v-model if autoRotate is true!

Events

NamePayload
start-drag-
end-drag-
point-clickedPoint
object-loadad{ url: String, gltf: Object }

Point

ProperyTypeDefault
idNumber-
positionObject{ x: 0, y: 0, z: 0 }
rotationObjectnull
colorHex0xffffff
opacityNumber0.5
radiusNumber0.1
scaleNumber0.5
objectObjectnull
onClickFunctionnull
payloadanynull
doubleSideBooleantrue