1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 04:12:02 +08:00

materials refactoring

This commit is contained in:
Kevin Levron 2020-10-03 12:34:14 +02:00
parent 13de74ec35
commit a66d4816b2
8 changed files with 82 additions and 74 deletions

View File

@ -4,9 +4,10 @@ import Material from './Material';
export default { export default {
extends: Material, extends: Material,
setup(props) { methods: {
const material = new MeshBasicMaterial(propsValues(props, ['id'])); createMaterial() {
return { material }; this.material = new MeshBasicMaterial(propsValues(this.$props, ['id']));
},
}, },
__hmrId: 'BasicMaterial', __hmrId: 'BasicMaterial',
}; };

View File

@ -4,9 +4,10 @@ import Material from './Material';
export default { export default {
extends: Material, extends: Material,
setup(props) { methods: {
const material = new MeshLambertMaterial(propsValues(props, ['id'])); createMaterial() {
return { material }; this.material = new MeshLambertMaterial(propsValues(this.$props, ['id']));
},
}, },
__hmrId: 'LambertMaterial', __hmrId: 'LambertMaterial',
}; };

View File

@ -15,24 +15,32 @@ export default {
transparent: Boolean, transparent: Boolean,
vertexColors: Boolean, vertexColors: Boolean,
}, },
created() { beforeMount() {
this.createMaterial();
if (this.id) this.three.materials[this.id] = this.material; if (this.id) this.three.materials[this.id] = this.material;
if (this.mesh) this.mesh.material = this.material; this.mesh.setMaterial(this.material);
},
// won't work for flatShading mounted() {
['color', 'depthTest', 'depthWrite', 'fog', 'opacity', 'transparent'].forEach(p => { this._addWatchers();
watch(() => this[p], () => { if (this.addWatchers) this.addWatchers();
if (p === 'color') {
this.material.color = new Color(this.color);
} else {
this.material[p] = this[p];
}
});
});
}, },
unmounted() { unmounted() {
this.material.dispose(); this.material.dispose();
delete this.three.materials[this.id]; if (this.id) delete this.three.materials[this.id];
},
methods: {
_addWatchers() {
// don't work for flatShading
['color', 'depthTest', 'depthWrite', 'fog', 'opacity', 'side', 'transparent'].forEach(p => {
watch(() => this[p], () => {
if (p === 'color') {
this.material.color.set(this.color);
} else {
this.material[p] = this[p];
}
});
});
},
}, },
render() { render() {
return []; return [];

View File

@ -4,9 +4,10 @@ import Material from './Material';
export default { export default {
extends: Material, extends: Material,
setup(props) { methods: {
const material = new MeshPhongMaterial(propsValues(props, ['id'])); createMaterial() {
return { material }; this.material = new MeshPhongMaterial(propsValues(this.$props, ['id']));
},
}, },
__hmrId: 'PhongMaterial', __hmrId: 'PhongMaterial',
}; };

View File

@ -1,22 +1,13 @@
import { Color, MeshPhysicalMaterial } from 'three'; import { MeshPhysicalMaterial } from 'three';
import { watch } from 'vue';
import { propsValues } from '../tools.js'; import { propsValues } from '../tools.js';
import StandardMaterial from './StandardMaterial'; import StandardMaterial from './StandardMaterial';
export default { export default {
extends: StandardMaterial, extends: StandardMaterial,
setup(props) { methods: {
const material = new MeshPhysicalMaterial(propsValues(props, ['id'])); createMaterial() {
['emissive', 'emissiveIntensity', 'metalness', 'roughness'].forEach(p => { this.material = new MeshPhysicalMaterial(propsValues(this.$props, ['id']));
watch(() => props[p], (value) => { },
if (p === 'emissive') {
material.emissive = new Color(value);
} else {
material[p] = value;
}
});
});
return { material };
}, },
__hmrId: 'PhysicalMaterial', __hmrId: 'PhysicalMaterial',
}; };

View File

@ -1,19 +1,22 @@
import { ShaderMaterial } from 'three';
export default { export default {
inject: ['three'], inject: ['three', 'mesh'],
props: { props: {
id: String, id: String,
uniforms: Object, uniforms: Object,
vertexShader: String, vertexShader: String,
fragmentShader: String, fragmentShader: String,
}, },
beforeMount() {
this.createMaterial();
if (this.id) this.three.materials[this.id] = this.material;
this.mesh.setMaterial(this.material);
},
mounted() { mounted() {
this.three.materials[this.id] = this.material; if (this.addWatchers) this.addWatchers();
}, },
unmounted() { unmounted() {
this.material.dispose(); this.material.dispose();
delete this.three.materials[this.id]; if (this.id) delete this.three.materials[this.id];
}, },
render() { render() {
return []; return [];

View File

@ -11,18 +11,21 @@ export default {
metalness: { type: Number, default: 0 }, metalness: { type: Number, default: 0 },
roughness: { type: Number, default: 1 }, roughness: { type: Number, default: 1 },
}, },
setup(props) { methods: {
const material = new MeshStandardMaterial(propsValues(props, ['id'])); createMaterial() {
['emissive', 'emissiveIntensity', 'metalness', 'roughness'].forEach(p => { this.material = new MeshStandardMaterial(propsValues(this.$props, ['id']));
watch(() => props[p], (value) => { },
if (p === 'emissive') { addWatchers() {
material.emissive = new Color(value); ['emissive', 'emissiveIntensity', 'metalness', 'roughness'].forEach(p => {
} else { watch(() => this[p], (value) => {
material[p] = value; if (p === 'emissive') {
} this.material.emissive = new Color(value);
} else {
this.material[p] = value;
}
});
}); });
}); },
return { material };
}, },
__hmrId: 'StandardMaterial', __hmrId: 'StandardMaterial',
}; };

View File

@ -1,6 +1,6 @@
import { Color, ShaderMaterial as TShaderMaterial, UniformsUtils } from 'three'; import { Color, ShaderMaterial as TShaderMaterial, UniformsUtils } from 'three';
import SubsurfaceScatteringShader from './SubsurfaceScatteringShader.js'; import SubsurfaceScatteringShader from './SubsurfaceScatteringShader.js';
import ShaderMaterial from './ShaderMaterial.js'; import ShaderMaterial from './ShaderMaterial';
export default { export default {
extends: ShaderMaterial, extends: ShaderMaterial,
@ -16,27 +16,27 @@ export default {
opacity: { type: Number, default: 1 }, opacity: { type: Number, default: 1 },
vertexColors: { type: Boolean, default: false }, vertexColors: { type: Boolean, default: false },
}, },
setup(props) { methods: {
const params = SubsurfaceScatteringShader; createMaterial() {
const uniforms = UniformsUtils.clone(params.uniforms); const params = SubsurfaceScatteringShader;
Object.entries(props).forEach(([key, value]) => { const uniforms = UniformsUtils.clone(params.uniforms);
if (key === 'diffuse' || key === 'thicknessColor') { Object.entries(this.$props).forEach(([key, value]) => {
value = new Color(value); if (key === 'diffuse' || key === 'thicknessColor') {
} value = new Color(value);
if (key !== 'id' && key !== 'transparent' && key !== 'vertexColors') { }
uniforms[key].value = value; if (key !== 'id' && key !== 'transparent' && key !== 'vertexColors') {
} uniforms[key].value = value;
}); }
});
const material = new TShaderMaterial({ this.material = new TShaderMaterial({
...params, ...params,
uniforms, uniforms,
lights: true, lights: true,
transparent: this.transparent, transparent: this.transparent,
vertexColors: this.vertexColors, vertexColors: this.vertexColors,
}); });
},
return { material };
}, },
__hmrId: 'SubSurfaceMaterial', __hmrId: 'SubSurfaceMaterial',
}; };