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

update materials

This commit is contained in:
Kevin Levron 2020-10-01 18:22:01 +02:00
parent 78450ed106
commit fb83acdc2c
9 changed files with 77 additions and 129 deletions

View File

@ -1,12 +1,12 @@
import { MeshBasicMaterial } from 'three'; import { MeshBasicMaterial } from 'three';
import { propsValues } from '../tools.js';
import Material from './Material'; import Material from './Material';
export default { export default {
extends: Material, extends: Material,
created() { setup(props) {
this.material = new MeshBasicMaterial({ const material = new MeshBasicMaterial(propsValues(props, ['id']));
color: this.color, return { material };
});
}, },
__hmrId: 'BasicMaterial', __hmrId: 'BasicMaterial',
}; };

View File

@ -1,12 +1,12 @@
import { MeshLambertMaterial } from 'three'; import { MeshLambertMaterial } from 'three';
import { propsValues } from '../tools.js';
import Material from './Material'; import Material from './Material';
export default { export default {
extends: Material, extends: Material,
created() { setup(props) {
this.material = new MeshLambertMaterial({ const material = new MeshLambertMaterial(propsValues(props, ['id']));
color: this.color, return { material };
});
}, },
__hmrId: 'LambertMaterial', __hmrId: 'LambertMaterial',
}; };

View File

@ -5,42 +5,20 @@ export default {
inject: ['three'], inject: ['three'],
props: { props: {
id: String, id: String,
color: { color: { type: [String, Number], default: '#ffffff' },
type: [String, Number], depthTest: { type: Boolean, default: true },
default: '#ffffff', depthWrite: { type: Boolean, default: true },
flatShading: Boolean,
fog: { type: Boolean, default: true },
opacity: { type: Number, default: 1 },
side: { type: Number, default: FrontSide },
transparent: Boolean,
vertexColors: Boolean,
}, },
depthTest: { created() {
type: Boolean,
default: true,
},
depthWrite: {
type: Boolean,
default: true,
},
fog: {
type: Boolean,
default: true,
},
opacity: {
type: Number,
default: 1,
},
side: {
type: Number,
default: FrontSide,
},
transparent: {
type: Boolean,
default: false,
},
vertexColors: {
type: Boolean,
default: false,
},
},
mounted() {
this.three.materials[this.id] = this.material; this.three.materials[this.id] = this.material;
// won't work for flatShading
['color', 'depthTest', 'depthWrite', 'fog', 'opacity', 'transparent'].forEach(p => { ['color', 'depthTest', 'depthWrite', 'fog', 'opacity', 'transparent'].forEach(p => {
watch(() => this[p], () => { watch(() => this[p], () => {
if (p === 'color') { if (p === 'color') {
@ -53,15 +31,7 @@ export default {
}, },
unmounted() { unmounted() {
this.material.dispose(); this.material.dispose();
}, delete this.three.materials[this.id];
methods: {
propsValues() {
const props = {};
Object.entries(this.$props).forEach(([key, value]) => {
if (key !== 'id') props[key] = value;
});
return props;
},
}, },
render() { render() {
return []; return [];

View File

@ -1,12 +1,12 @@
import { MeshPhongMaterial } from 'three'; import { MeshPhongMaterial } from 'three';
import { propsValues } from '../tools.js';
import Material from './Material'; import Material from './Material';
export default { export default {
extends: Material, extends: Material,
created() { setup(props) {
this.material = new MeshPhongMaterial({ const material = new MeshPhongMaterial(propsValues(props, ['id']));
color: this.color, return { material };
});
}, },
__hmrId: 'PhongMaterial', __hmrId: 'PhongMaterial',
}; };

View File

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

View File

@ -9,15 +9,11 @@ export default {
fragmentShader: String, fragmentShader: String,
}, },
mounted() { mounted() {
if (!this.material) {
// this.material = new ShaderMaterial(this.$props);
}
this.three.materials[this.id] = this.material; this.three.materials[this.id] = this.material;
}, },
unmounted() { unmounted() {
this.material.dispose(); this.material.dispose();
}, delete this.three.materials[this.id];
methods: {
}, },
render() { render() {
return []; return [];

View File

@ -1,38 +1,28 @@
import { Color, MeshStandardMaterial } from 'three'; import { Color, MeshStandardMaterial } from 'three';
import { watch } from 'vue'; import { watch } from 'vue';
import { propsValues } from '../tools.js';
import Material from './Material'; import Material from './Material';
export default { export default {
extends: Material, extends: Material,
props: { props: {
emissive: { emissive: { type: [Number, String], default: 0 },
type: [Number, String], emissiveIntensity: { type: Number, default: 1 },
default: 0, metalness: { type: Number, default: 0 },
roughness: { type: Number, default: 1 },
}, },
emissiveIntensity: { setup(props) {
type: Number, const material = new MeshStandardMaterial(propsValues(props, ['id']));
default: 1,
},
metalness: {
type: Number,
default: 0,
},
roughness: {
type: Number,
default: 1,
},
},
created() {
this.material = new MeshStandardMaterial(this.propsValues());
['emissive', 'emissiveIntensity', 'metalness', 'roughness'].forEach(p => { ['emissive', 'emissiveIntensity', 'metalness', 'roughness'].forEach(p => {
watch(() => this[p], () => { watch(() => props[p], (value) => {
if (p === 'emissive') { if (p === 'emissive') {
this.material.emissive = new Color(this.emissive); material.emissive = new Color(value);
} else { } else {
this.material[p] = this[p]; material[p] = value;
} }
}); });
}); });
return { material };
}, },
__hmrId: 'StandardMaterial', __hmrId: 'StandardMaterial',
}; };

View File

@ -5,51 +5,21 @@ import ShaderMaterial from './ShaderMaterial.js';
export default { export default {
extends: ShaderMaterial, extends: ShaderMaterial,
props: { props: {
diffuse: { diffuse: { type: String, default: '#ffffff' },
type: String, thicknessColor: { type: String, default: '#ffffff' },
default: '#ffffff', thicknessDistortion: { type: Number, default: 0.4 },
thicknessAmbient: { type: Number, default: 0.01 },
thicknessAttenuation: { type: Number, default: 0.7 },
thicknessPower: { type: Number, default: 2 },
thicknessScale: { type: Number, default: 4 },
transparent: { type: Boolean, default: false },
opacity: { type: Number, default: 1 },
vertexColors: { type: Boolean, default: false },
}, },
thicknessColor: { setup(props) {
type: String,
default: '#ffffff',
},
thicknessDistortion: {
type: Number,
default: 0.4,
},
thicknessAmbient: {
type: Number,
default: 0.01,
},
thicknessAttenuation: {
type: Number,
default: 0.7,
},
thicknessPower: {
type: Number,
default: 2,
},
thicknessScale: {
type: Number,
default: 4,
},
transparent: {
type: Boolean,
default: false,
},
opacity: {
type: Number,
default: 1,
},
vertexColors: {
type: Boolean,
default: false,
},
},
created() {
const params = SubsurfaceScatteringShader; const params = SubsurfaceScatteringShader;
const uniforms = UniformsUtils.clone(params.uniforms); const uniforms = UniformsUtils.clone(params.uniforms);
Object.entries(this.$props).forEach(([key, value]) => { Object.entries(props).forEach(([key, value]) => {
if (key === 'diffuse' || key === 'thicknessColor') { if (key === 'diffuse' || key === 'thicknessColor') {
value = new Color(value); value = new Color(value);
} }
@ -58,13 +28,15 @@ export default {
} }
}); });
this.material = new TShaderMaterial({ const 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',
}; };

View File

@ -6,6 +6,16 @@ export function setFromProp(o, prop) {
} }
}; };
export function propsValues(props, exclude) {
const values = {};
Object.entries(props).forEach(([key, value]) => {
if (!exclude || (exclude && !exclude.includes(key))) {
values[key] = value;
}
});
return values;
};
export function lerp(value1, value2, amount) { export function lerp(value1, value2, amount) {
amount = amount < 0 ? 0 : amount; amount = amount < 0 ? 0 : amount;
amount = amount > 1 ? 1 : amount; amount = amount > 1 ? 1 : amount;