import THREE from 'three'; var glslify = require('glslify'); const vertexShader = glslify('./meshline.vert'); const fragmentShader = glslify('./meshline.frag'); export default class MeshLineMaterial extends THREE.Material { constructor(parameters = {}) { super(); this.lineWidth = this.check(parameters.lineWidth, 1); this.map = this.check(parameters.map, null); this.useMap = this.check(parameters.useMap, 0); this.color = this.check(parameters.color, new THREE.Color(0xffffff)); this.opacity = this.check(parameters.opacity, 1); this.resolution = this.check(parameters.resolution, new THREE.Vector2(1, 1)); this.sizeAttenuation = this.check(parameters.sizeAttenuation, 1); this.near = this.check(parameters.near, 1); this.far = this.check(parameters.far, 1); this.dashArray = this.check(parameters.dashArray, []); this.useDash = (this.dashArray !== []) ? 1 : 0; let material = new THREE.RawShaderMaterial({ uniforms: { lineWidth: {type: 'f', value: this.lineWidth}, map: {type: 't', value: this.map}, useMap: {type: 'f', value: this.useMap}, color: {type: 'c', value: this.color}, opacity: {type: 'f', value: this.opacity}, resolution: {type: 'v2', value: this.resolution}, sizeAttenuation: {type: 'f', value: this.sizeAttenuation}, near: {type: 'f', value: this.near}, far: {type: 'f', value: this.far}, dashArray: {type: 'v2', value: new THREE.Vector2(this.dashArray[0], this.dashArray[1])}, useDash: {type: 'f', value: this.useDash} }, vertexShader: vertexShader, fragmentShader: fragmentShader }); delete parameters.lineWidth; delete parameters.map; delete parameters.useMap; delete parameters.color; delete parameters.opacity; delete parameters.resolution; delete parameters.sizeAttenuation; delete parameters.near; delete parameters.far; delete parameters.dashArray; material.type = 'MeshLineMaterial'; material.setValues(parameters); return material; } check(v, d) { return v === undefined ? d : v; } copy(source) { super.copy(this, source); this.lineWidth = source.lineWidth; this.map = source.map; this.useMap = source.useMap; this.color.copy( source.color ); this.opacity = source.opacity; this.resolution.copy( source.resolution ); this.sizeAttenuation = source.sizeAttenuation; this.near = source.near; this.far = source.far; return this; } }