{"size": 3304, "ext": "js", "max_stars_repo_path": "browser/scripts/abstractPlugins/abstractThreeMeshPlugin.js", "max_stars_repo_name": "waskosky/patches", "max_stars_repo_stars_event_min_datetime": "2015-09-21T15:08:57.000Z", "max_stars_repo_stars_event_max_datetime": "2017-07-31T08:01:22.000Z", "max_issues_repo_path": "browser/scripts/abstractPlugins/abstractThreeMeshPlugin.js", "max_issues_repo_name": "waskosky/patches", "max_issues_repo_issues_event_min_datetime": "2015-09-15T23:49:33.000Z", "max_issues_repo_issues_event_max_datetime": "2017-08-01T08:52:00.000Z", "max_forks_repo_path": "browser/scripts/abstractPlugins/abstractThreeMeshPlugin.js", "max_forks_repo_name": "waskosky/patches", "max_forks_repo_forks_event_min_datetime": "2015-10-11T10:42:50.000Z", "max_forks_repo_forks_event_max_datetime": "2017-07-16T22:31:42.000Z"}
{"max_stars_count": 187, "max_issues_count": 1533, "max_forks_count": 52, "avg_line_length": 27.305785124, "max_line_length": 103, "alphanum_fraction": 0.7024818402}
function AbstractThreeMeshPlugin(core) {
	ThreeObject3DPlugin.apply(this, arguments)

	this.desc = 'THREE.js Mesh'

	this.input_slots = [
		{
			name: 'geometry',
			dt: core.datatypes.GEOMETRY,
			array: true,
			def: [new THREE.Geometry()]
		},
		{
			name: 'material',
			dt: core.datatypes.MATERIAL,
			array: true,
			def: [new THREE.MeshBasicMaterial({color: 0x00FF00})]
		}
	].concat(this.input_slots)

	// meshes are create via a helper object
	this.createMeshRoot = function() {}
	this.createMesh = function() {}
	this.createAnimatedMesh = function() {}
}

AbstractThreeMeshPlugin.prototype = Object.create(ThreeObject3DPlugin.prototype)

AbstractThreeMeshPlugin.prototype.reset = function() {
	ThreeObject3DPlugin.prototype.reset.apply(this)

	this.setObject3D(this.createMeshRoot())

	this.geoms = [new THREE.Geometry()]
	this.mats = [new THREE.MeshBasicMaterial({color: 0x00ff00})]

	var mesh = this.createMesh(this.geoms[0], this.mats[0])
	this.object3d.add(mesh)

	// back reference for object picking
	this.object3d.backReference = this
}

AbstractThreeMeshPlugin.prototype.update_mesh = function()
{
	var i
	var mesh

	// tbd: optimize this so that we don't always reconstruct meshes
	// but update the ones we can just update
	if (this.geoms_dirty && this.geoms) {
		this.always_update = false

		// non-recursive clone of the root, to preserve transform
		this.setObject3D(this.object3d.clone(/*recursive = */false))

		for (i = 0; i < this.geoms.length; ++i) {
			if (this.geoms[i].morphTargets && this.geoms[i].morphTargets.length > 0) {
				// clone the material, because we don't want to edit properties on some random material
				var mat = (this.mats && this.mats.length > 0) ? this.mats[i % this.mats.length].clone() : undefined
				mesh = this.createAnimatedMesh(this.geoms[i], mat)

				var animName = this.geoms[i].animations.length > 0 ? this.geoms[i].animations[0].name : undefined
				if (animName) {
					mesh.playAnimation(animName, 100)
				}

				mesh.material.morphTargets = true
				this.always_update = true
			}
			else {
				mesh = this.createMesh(this.geoms[i], this.mats[i % this.mats.length])
			}
			mesh.backReference = this

			this.object3d.add(mesh)
		}

		this.geoms_dirty = this.mats_dirty = false

		this.node.emit('meshChanged')
	}

	if (this.mats_dirty && this.mats && this.mats.length > 0) {
		for (i = 0; i < this.object3d.children.length; ++i) {
			if (this.object3d.children[i] instanceof THREE.MorphAnimMesh) {
				this.object3d.children[i].material = this.mats[i % this.mats.length].clone()
				this.object3d.children[i].material.morphTargets = true
			}
			else {
				this.object3d.children[i].material = this.mats[i % this.mats.length]
			}
		}

		this.mats_dirty = false
	}
}

AbstractThreeMeshPlugin.prototype.update_input = function(slot, data) {
	if (slot.name === 'geometry') { // geometry
		this.geoms = data
		this.geoms_dirty = true
	}
	else if (slot.name === 'material') { // material
		this.mats = data
		this.mats_dirty = true
	}
	else {
		ThreeObject3DPlugin.prototype.update_input.apply(this, arguments)
	}
}

AbstractThreeMeshPlugin.prototype.update_state = function() {
	this.update_mesh()

	ThreeObject3DPlugin.prototype.update_state.apply(this)
}

if (typeof(module) !== 'undefined')
	module.exports = AbstractThreeMeshPlugin
