# Polygon Streaming Web Player for Three.js

To run the example you first need to make sure you have [Node.js](https://nodejs.org) installed. Then run the following in a terminal:

```shell
npm install
npm run dev
```

It will open a browser window and display the model.

## Explanation of the Code

Import the StreamController from the package:

```javascript
import { StreamController } from '@polygon-streaming/web-player-threejs';
```

Instanciate the stream controller:

```javascript
const streamController = new StreamController(camera, renderer, scene, controls.target, {
  cameraType: 'nonPlayer',
  triangleBudget: 5000000,
  mobileTriangleBudget: 3000000
});
```

Add a streaming model, passing it a model URL and a Group to act as a model parent:

```javascript
const modelParent = new THREE.Group();
modelParent.position.set(0, 1, 0);
scene.add(modelParent);

streamController.addModel('https://stream.viverse.com/demos/jet-engine-11m/', modelParent, {
  qualityPriority: 1
});
```

Call the stream controller's update method in the animation loop:

```javascript
function animate() {
  controls.update();
  renderer.render(scene, camera);
  streamController.update();
}

renderer.setAnimationLoop(animate);
```

## Files Required at the Web Root

Polygon Streaming expects the service worker that is used to cache model data to be available at the web root e.g. `/service-worker.js`. Models use KTX2 textures so you will need to make the basis transcoder files available at the web root e.g. `/lib/basis_transcoder.js` and `/lib/basis_transcoder.wasm`. Both of these requirements are handled in Vite's config file - vite.config.js.

## Using Your Own Model

* Upload your 3D model to the [console](https://stream.viverse.com/console).
* To get the model URL go to the models section of the [console](https://stream.viverse.com/console/models) and click on the three dots next to your model and select "Copy asset ID".
* Paste this URL as the first parameter of streamController.addModel() method.

## Stream Controller Parameters

* **camera** (Required): The camera used in the scene.
* **renderer** (Required): The WebGL renderer used in the scene.
* **scene** (Required): The scene object.
* **cameraTarget** (Required): The camera target which is a Vector3. If you are using orbit controls it would be controls.target.
* **options** (Optional): All options are optional. The options are:
  * **cameraType**:  'nonPlayer' | 'player', default: 'nonPlayer'
    * **nonPlayer**: A camera that is not attached to a player e.g. a camera that orbits an object.
    * **player**: A camera that is attached to a player.
  * **triangleBudget**: number, default: 5000000. The maximum amount of triangles that you want to be in the scene at any single point.
  * **mobileTriangleBudget**: number, default: 3000000. The triangle budget used on a mobile device. If it is set to 0 it will use the non-mobile triangle budget.
  * **minimumDistance**: number, default: 0.01. The smallest possible distance to the camera.
  * **distanceFactor**: number, default: 1.1. Preference for nearby objects over objects further away. Values above one mean a preference for nearby objects. Values below one mean a preference for objects further away. One is neutral.
  * **maximumQuality**: number, default: 15000. Stops improving geometry that exceeds the maximum quality. This can be used to stop far away objects from showing more detail which can be wasteful. Setting it to 0 means there is no maximum quality.
  * **closeUpDistance**: number, default: 3. The distance where it starts using close-up distance factor. Set it to 0 to not use close-up distance factor.
  * **closeUpDistanceFactor**: number, default: 5. The distance factor used when close-up to an object. Should be higher than the standard distance factor.
  * **iOS Memory Limit**: number, default 0. The maximum amount of memory in MB that meshes and textures can consume on iOS devices to avoid the page crashing. Use 0 to let Polygon Streaming determine the limit or -1 for no limit.

## Streaming Model Parameters

* **URL** (Required): URL of the XRG model. If it doesn't end with .xrg it will append model.xrg to the URL.
* **model parent** (Required): The scene object that the streaming model will be attached to.
* **options** (Optional): All options are optional. The options are:
  * **qualityPriority**: number, default: 1.  How much to prioritize the quality of this model relative to the quality of other models in the scene. This parameter does nothing if this is the only model in the scene.
  * **initialTrianglePercent**: number, default: 0.1. Percentage of triangle budget to initialize the model with.
  * **castShadows**: boolean, default: true. Whether the model should cast shadows.
  * **receiveShadows**: boolean, default: true. Whether the model should receive shadows.
  * **forceDoubleSided**: boolean, default: false. Render the model double sided regardless of the setting in the model file.
  * **useAlpha**: boolean, default: true. Whether to render semi-transparency in materials. You might turn this off to increase performance but all your materials will render opaque.
  * **playAnimationAutomatically**: boolean, default: true. Whether to play the embedded animation automatically.
  * **animation**: string or number, default: null. The name or index of the embedded animation to play. An index value of 0 is the first animation. If not value is supplied it will play the first animation.
  * **environmentMap**: texture, default: null. A cube map environment texture.
  * **hashCode**: string, default: ''. Hash code to validate streaming model.

