1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 04:12:02 +08:00
This commit is contained in:
Kevin Levron 2020-09-29 23:05:01 +02:00
parent 515a752c61
commit b4cdb68340
2 changed files with 26 additions and 15 deletions

View File

@ -8,10 +8,10 @@ export default {
extends: Mesh, extends: Mesh,
props: { props: {
...TextProps, ...TextProps,
color: { color: { type: [String, Number], default: 0xffffff },
type: [String, Number], timeCoef: { type: Number, default: 0.001 },
default: 0xffffff, noiseCoef: { type: Number, default: 0.015 },
}, zCoef: { type: Number, default: 10 },
}, },
created() { created() {
// add watchers // add watchers
@ -26,8 +26,14 @@ export default {
}); });
}); });
// uniform // uniforms
this.time = { value: 0 }; this.uTime = { value: 0 };
this.uNoiseCoef = { value: this.noiseCoef };
watch(() => this.noiseCoef, () => { this.uNoiseCoef.value = this.noiseCoef; });
this.uZCoef = { value: this.zCoef };
watch(() => this.zCoef, () => { this.uZCoef.value = this.zCoef; });
const loader = new FontLoader(); const loader = new FontLoader();
loader.load(this.fontSrc, (font) => { loader.load(this.fontSrc, (font) => {
@ -38,28 +44,32 @@ export default {
const startTime = Date.now(); const startTime = Date.now();
this.three.onBeforeRender(() => { this.three.onBeforeRender(() => {
this.time.value = (Date.now() - startTime) / 1000; this.uTime.value = (Date.now() - startTime) * this.timeCoef;
}); });
}); });
}, },
methods: { methods: {
createMaterial() { createMaterial() {
this.material = new MeshStandardMaterial({ color: this.color }); this.material = new MeshStandardMaterial({ color: this.color, metalness: 0.8, roughness: 0.5 });
this.material.onBeforeCompile = (shader) => { this.material.onBeforeCompile = (shader) => {
shader.uniforms.time = this.time; shader.uniforms.uTime = this.uTime;
shader.uniforms.uNoiseCoef = this.uNoiseCoef;
shader.uniforms.uZCoef = this.uZCoef;
shader.vertexShader = ` shader.vertexShader = `
uniform float time; uniform float uTime;
uniform float uNoiseCoef;
uniform float uZCoef;
${snoise2} ${snoise2}
` + shader.vertexShader; ` + shader.vertexShader;
shader.vertexShader = shader.vertexShader.replace( shader.vertexShader = shader.vertexShader.replace(
'#include <begin_vertex>', '#include <begin_vertex>',
` `
vec3 p = vec3(position * 0.01); vec3 p = vec3(position * uNoiseCoef);
p.x += time; p.x += uTime;
float noise = snoise(p.xy); float noise = snoise(p.xy);
vec3 transformed = vec3(position); vec3 transformed = vec3(position);
transformed.z += noise * 10.0; transformed.z += noise * uZCoef;
` `
); );
this.materialShader = shader; this.materialShader = shader;

View File

@ -24,8 +24,9 @@ export default {
if (this.geometry && !this.mesh) this.initMesh(); if (this.geometry && !this.mesh) this.initMesh();
}, },
unmounted() { unmounted() {
if (this.geometry) this.geometry.dispose();
if (this.mesh) this.scene.remove(this.mesh); if (this.mesh) this.scene.remove(this.mesh);
if (this.geometry) this.geometry.dispose();
if (this.material && !this.materialId) this.material.dispose();
}, },
methods: { methods: {
initMesh() { initMesh() {