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

textures refactoring

This commit is contained in:
Kevin Levron 2020-10-04 14:00:07 +02:00
parent 1c66a946d5
commit 7b7524d1dd
7 changed files with 25 additions and 45 deletions

View File

@ -2,6 +2,3 @@ export { default as Renderer } from './Renderer.js';
export { default as PerspectiveCamera } from './PerspectiveCamera.js'; export { default as PerspectiveCamera } from './PerspectiveCamera.js';
export { default as Camera } from './PerspectiveCamera.js'; export { default as Camera } from './PerspectiveCamera.js';
export { default as Scene } from './Scene.js'; export { default as Scene } from './Scene.js';
export { default as Texture } from './Texture.js';
export { default as CubeTexture } from './CubeTexture.js';

View File

@ -1,8 +1,8 @@
import { CubeTextureLoader } from 'three'; import { CubeTextureLoader, CubeRefractionMapping } from 'three';
import { watch } from 'vue'; import { watch } from 'vue';
export default { export default {
inject: ['three'], inject: ['material'],
emits: ['loaded'], emits: ['loaded'],
props: { props: {
path: String, path: String,
@ -13,9 +13,12 @@ export default {
onLoad: Function, onLoad: Function,
onProgress: Function, onProgress: Function,
onError: Function, onError: Function,
id: { type: String, default: 'envMap' },
refraction: Boolean,
refractionRatio: { type: Number, default: 0.98 },
}, },
created() { created() {
this.createTexture(); this.refreshTexture();
watch(() => this.path, this.refreshTexture); watch(() => this.path, this.refreshTexture);
watch(() => this.urls, this.refreshTexture); watch(() => this.urls, this.refreshTexture);
}, },
@ -30,6 +33,11 @@ export default {
}, },
refreshTexture() { refreshTexture() {
this.createTexture(); this.createTexture();
this.material.setTexture(this.texture, this.id);
if (this.refraction) {
this.texture.mapping = CubeRefractionMapping;
this.material.setProp('refractionRatio', this.refractionRatio);
}
}, },
onLoaded() { onLoaded() {
if (this.onLoad) this.onLoad(); if (this.onLoad) this.onLoad();

View File

@ -1,19 +0,0 @@
import CubeTexture from '../core/CubeTexture';
export default {
extends: CubeTexture,
inject: ['material'],
created() {
this.material.setEnvMap(this.texture);
},
unmounted() {
this.material.setEnvMap(null);
},
methods: {
refreshTexture() {
this.createTexture();
this.material.setEnvMap(this.texture);
},
},
__hmrId: 'EnvMap',
};

View File

@ -34,17 +34,12 @@ export default {
if (this.id) delete this.three.materials[this.id]; if (this.id) delete this.three.materials[this.id];
}, },
methods: { methods: {
setMap(texture) { setProp(key, value, needsUpdate = false) {
this.material.map = texture; this.material[key] = value;
this.material.needsUpdate = true; this.material.needsUpdate = needsUpdate;
}, },
setEnvMap(texture) { setTexture(texture, key = 'map') {
this.material.envMap = texture; this.setProp(key, texture, true);
this.material.needsUpdate = true;
},
setRefractionMap(texture, ratio) {
this.material.refractionRatio = ratio;
this.setEnvMap(texture);
}, },
_addWatchers() { _addWatchers() {
// don't work for flatShading // don't work for flatShading

View File

@ -2,16 +2,17 @@ import { TextureLoader } from 'three';
import { watch } from 'vue'; import { watch } from 'vue';
export default { export default {
inject: ['three'], inject: ['material'],
emits: ['loaded'], emits: ['loaded'],
props: { props: {
src: String, src: String,
onLoad: Function, onLoad: Function,
onProgress: Function, onProgress: Function,
onError: Function, onError: Function,
id: { type: String, default: 'map' },
}, },
created() { created() {
this.createTexture(); this.refreshTexture();
watch(() => this.src, this.refreshTexture); watch(() => this.src, this.refreshTexture);
}, },
unmounted() { unmounted() {
@ -23,6 +24,7 @@ export default {
}, },
refreshTexture() { refreshTexture() {
this.createTexture(); this.createTexture();
this.material.setTexture(this.texture, this.id);
}, },
onLoaded() { onLoaded() {
if (this.onLoad) this.onLoad(); if (this.onLoad) this.onLoad();

View File

@ -7,6 +7,6 @@ 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 Map } from './Map.js';
export { default as EnvMap } from './EnvMap.js'; export { default as Texture } from './Texture.js';
export { default as RefractionMap } from './RefractionMap.js'; export { default as CubeTexture } from './CubeTexture.js';

View File

@ -7,8 +7,6 @@ export const TroisJSVuePlugin = {
'PerspectiveCamera', 'PerspectiveCamera',
'Renderer', 'Renderer',
'Scene', 'Scene',
// 'Texture',
// 'CubeTexture',
'BoxGeometry', 'BoxGeometry',
'CircleGeometry', 'CircleGeometry',
@ -41,9 +39,8 @@ export const TroisJSVuePlugin = {
'SubSurfaceMaterial', 'SubSurfaceMaterial',
'ToonMaterial', 'ToonMaterial',
'Map', 'Texture',
'EnvMap', 'CubeTexture',
'RefractionMap',
'Box', 'Box',
'Circle', 'Circle',