Skip to content

Instantly share code, notes, and snippets.

@leefsmp
Last active September 10, 2018 09:09
Show Gist options
  • Save leefsmp/f89be435f475b805780fe3ea4fd1fc54 to your computer and use it in GitHub Desktop.
Save leefsmp/f89be435f475b805780fe3ea4fd1fc54 to your computer and use it in GitHub Desktop.
Basic Shader Material Extension for Forge Viewer
/////////////////////////////////////////////////////////////////
// ShaderMaterial Extension - Part 1
// By Philippe Leefsma, January 2016
//
/////////////////////////////////////////////////////////////////
import ExtensionBase from 'Viewer.ExtensionBase'
const vertexShader = `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}
`
const fragmentShader = `
uniform vec4 color;
varying vec2 vUv;
void main() {
gl_FragColor = color;
}
`
class ShaderMaterialExtension1 extends ExtensionBase {
/////////////////////////////////////////////////////////////////
// Class constructor
//
/////////////////////////////////////////////////////////////////
constructor (viewer, options) {
super (viewer, options)
}
/////////////////////////////////////////////////////////////////
// Extension Id
//
/////////////////////////////////////////////////////////////////
static get ExtensionId() {
return 'Viewing.Extension.ShaderMaterial1'
}
/////////////////////////////////////////////////////////////////
// Load callback
//
/////////////////////////////////////////////////////////////////
load() {
this.selectionHandler = this.selectionHandler.bind(this)
this.viewer.addEventListener(
Autodesk.Viewing.AGGREGATE_SELECTION_CHANGED_EVENT,
this.selectionHandler)
this.material = this.createShaderMaterial({
name: 'shader-material',
fragmentShader,
vertexShader
})
this.randomUpdate ()
console.log('Viewing.Extension.ShaderMaterial1 loaded')
return true
}
/////////////////////////////////////////////////////////////////
// Unload callback
//
/////////////////////////////////////////////////////////////////
unload() {
console.log('Viewing.Extension.ShaderMaterial1 unloaded')
return true
}
/////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////
createShaderMaterial (data) {
const uniforms = {
color: {
value: new THREE.Vector4(0, 0, 1, 1),
type: 'v4'
}
}
const material = new THREE.ShaderMaterial({
fragmentShader: data.fragmentShader,
vertexShader: data.vertexShader,
uniforms
})
this._viewer.impl.matman().addMaterial(
data.name, material, true)
return material
}
/////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////
selectionHandler (event) {
if (event.selections && event.selections.length) {
const selection = event.selections[0]
const fragIds = selection.fragIdsArray
this.setMaterial(fragIds, this.material)
this._viewer.clearSelection()
}
}
/////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////
setMaterial(fragIds, material) {
const fragList = this._viewer.model.getFragmentList()
this.toArray(fragIds).forEach((fragId) => {
fragList.setMaterial(fragId, material)
})
this._viewer.impl.invalidate(true)
}
/////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////
randomUpdate() {
this.material.uniforms.color.value = new THREE.Vector4(
Math.random(),
Math.random(),
Math.random(),
1.0)
this.material.needsUpdate = true
this._viewer.impl.sceneUpdated(true)
window.setTimeout(() => this.randomUpdate(), 2000)
}
/////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////
toArray (obj) {
return obj ? (Array.isArray(obj) ? obj : [obj]) : []
}
}
Autodesk.Viewing.theExtensionManager.registerExtension(
ShaderMaterialExtension1.ExtensionId,
ShaderMaterialExtension1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment