Cristian Barzaghi
Cristian Barzaghi

Reputation: 41

Forge Viewer, raster images

Is possible to directly load a raster image (PNG, JPG, TIFF) to Forge Viewer?

I see the Autodesk.PDF add-in that can load PDF, I cant find any Autodesk.IMAGE add-in... Otherwise I need to prior convert Image into PDF and than load it through Autodesk.PDF.

Upvotes: 0

Views: 563

Answers (1)

cyrille
cyrille

Reputation: 2659

The Autodesk Forge Viewer is based on Three.js - therefore you can use the Three.js API to load an image/texture, there is no need of a Viewer extension for that.

However it depends what you want to do. In case you just want to load an image in the scene, that code is enough.

const texture = THREE.ImageUtils.loadTexture( "thumbnail256.png" );
const material = new THREE.MeshBasicMaterial({ map : texture });
const geometry = new THREE.PlaneGeometry(5, 20, 32);
const planeMesh = new THREE.Mesh(geometry, material);
const planeMesh.position.set(1, 2, 3);
NOP_VIEWER.overlays.addScene('custom-scene');
NOP_VIEWER.overlays.addMesh(planeMesh, 'custom-scene');

But if you want to apply the texture on an existing element in the loaded scene, you need to proceed like this:

const texture = THREE.ImageUtils.loadTexture( "thumbnail256.png" );
const material = new THREE.MeshBasicMaterial({ map : texture, side: THREE.DoubleSide });
NOP_VIEWER.impl.matman().addMaterial('custom-material', material, true);
const model = NOP_VIEWER.model;
model.unconsolidate(); // If the model is consolidated, material changes won't have any effect
const tree = model.getInstanceTree();
const frags = model.getFragmentList();
const dbids = NOP_VIEWER.getSelection();
for (const dbid of dbids) {
  tree.enumNodeFragments(dbid, (fragid) => {
    frags.setMaterial(fragid, material);
  });
}
NOP_VIEWER.impl.invalidate(true, true, false);

Note you may need to work out the texture uv, depending on the geometry.

Upvotes: 1

Related Questions