mirror of
https://github.com/troisjs/trois.git
synced 2024-11-24 04:12:02 +08:00
commit
9e062155f6
@ -1,18 +1,33 @@
|
|||||||
|
import { ShaderMaterial } from 'three';
|
||||||
|
import { watch } from 'vue';
|
||||||
|
import { propsValues, defaultFragmentShader, defaultVertexShader } from '../tools.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
inject: ['three', 'mesh'],
|
inject: ['three', 'mesh'],
|
||||||
props: {
|
props: {
|
||||||
uniforms: Object,
|
uniforms: { type: Object, default: () => {} },
|
||||||
vertexShader: String,
|
vertexShader: { type: String, default: defaultVertexShader },
|
||||||
fragmentShader: String,
|
fragmentShader: { type: String, default: defaultFragmentShader },
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
this.createMaterial();
|
this.createMaterial();
|
||||||
this.mesh.setMaterial(this.material);
|
['vertexShader', 'fragmentShader'].forEach(p => {
|
||||||
if (this.addWatchers) this.addWatchers();
|
watch(() => this[p], () => {
|
||||||
|
// recreate material if we change either shader
|
||||||
|
this.material.dispose();
|
||||||
|
this.createMaterial();
|
||||||
|
});
|
||||||
|
});
|
||||||
},
|
},
|
||||||
unmounted() {
|
unmounted() {
|
||||||
this.material.dispose();
|
this.material.dispose();
|
||||||
},
|
},
|
||||||
|
methods: {
|
||||||
|
createMaterial() {
|
||||||
|
this.material = new ShaderMaterial(propsValues(this.$props));
|
||||||
|
this.mesh.setMaterial(this.material);
|
||||||
|
},
|
||||||
|
},
|
||||||
render() {
|
render() {
|
||||||
return [];
|
return [];
|
||||||
},
|
},
|
||||||
|
@ -3,10 +3,10 @@ export { default as LambertMaterial } from './LambertMaterial.js';
|
|||||||
export { default as MatcapMaterial } from './MatcapMaterial.js';
|
export { default as MatcapMaterial } from './MatcapMaterial.js';
|
||||||
export { default as PhongMaterial } from './PhongMaterial.js';
|
export { default as PhongMaterial } from './PhongMaterial.js';
|
||||||
export { default as PhysicalMaterial } from './PhysicalMaterial.js';
|
export { default as PhysicalMaterial } from './PhysicalMaterial.js';
|
||||||
|
export { default as ShaderMaterial } from './ShaderMaterial.js';
|
||||||
export { default as StandardMaterial } from './StandardMaterial.js';
|
export { default as StandardMaterial } from './StandardMaterial.js';
|
||||||
export { default as SubSurfaceMaterial } from './SubSurfaceMaterial.js';
|
export { default as SubSurfaceMaterial } from './SubSurfaceMaterial.js';
|
||||||
export { default as ToonMaterial } from './ToonMaterial.js';
|
export { default as ToonMaterial } from './ToonMaterial.js';
|
||||||
|
|
||||||
|
|
||||||
export { default as Texture } from './Texture.js';
|
export { default as Texture } from './Texture.js';
|
||||||
export { default as CubeTexture } from './CubeTexture.js';
|
export { default as CubeTexture } from './CubeTexture.js';
|
||||||
|
16
src/tools.js
16
src/tools.js
@ -73,3 +73,19 @@ function getMatcapFormatString(format) {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// shader defaults
|
||||||
|
export const defaultVertexShader = `
|
||||||
|
varying vec2 vUv;
|
||||||
|
|
||||||
|
void main(){
|
||||||
|
vUv = uv;
|
||||||
|
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
|
||||||
|
}`;
|
||||||
|
|
||||||
|
export const defaultFragmentShader = `
|
||||||
|
varying vec2 vUv;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_FragColor = vec4(vUv.x, vUv.y, 0., 1.0);
|
||||||
|
}`;
|
Loading…
Reference in New Issue
Block a user