mirror of
https://github.com/troisjs/trois.git
synced 2024-11-24 04:12:02 +08:00
simplify textures
This commit is contained in:
parent
888b9e2e8d
commit
19eaa22fc5
@ -1,17 +1,17 @@
|
|||||||
import { defineComponent, PropType, watch } from 'vue'
|
import { defineComponent, PropType, watch } from 'vue'
|
||||||
import { CubeReflectionMapping, CubeTextureLoader, RGBFormat } from 'three'
|
import { CubeReflectionMapping, CubeTextureLoader } from 'three'
|
||||||
import Texture from './Texture'
|
import Texture from './Texture'
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
extends: Texture,
|
extends: Texture,
|
||||||
props: {
|
props: {
|
||||||
|
name: { type: String, default: 'envMap' },
|
||||||
path: { type: String, required: true },
|
path: { type: String, required: true },
|
||||||
urls: {
|
urls: {
|
||||||
type: Array as PropType<string[]>,
|
type: Array as PropType<string[]>,
|
||||||
default: () => ['px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg'],
|
default: () => ['px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg'],
|
||||||
},
|
},
|
||||||
// format: { type: Number, default: RGBFormat },
|
props: { type: Object, default: () => ({ mapping: CubeReflectionMapping }) },
|
||||||
mapping: { type: Number, default: CubeReflectionMapping },
|
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
watch(() => this.path, this.refreshTexture)
|
watch(() => this.path, this.refreshTexture)
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import { defineComponent, PropType, watch } from 'vue'
|
import { defineComponent, PropType, watch } from 'vue'
|
||||||
import { ClampToEdgeWrapping, LinearEncoding, LinearFilter, LinearMipmapLinearFilter, ShaderMaterial, Texture, TextureLoader, UVMapping } from 'three'
|
import { ShaderMaterial, Texture, TextureLoader } from 'three'
|
||||||
import { bindProp } from '../tools'
|
import { bindOptions } from '../tools'
|
||||||
import { MaterialInjectionKey, MaterialInterface } from './Material'
|
import { MaterialInjectionKey, MaterialInterface } from './Material'
|
||||||
import { Vector2PropInterface } from '../core/Object3D'
|
|
||||||
|
|
||||||
export interface TexureInterface {
|
export interface TexureInterface {
|
||||||
material?: MaterialInterface
|
material?: MaterialInterface
|
||||||
@ -20,16 +19,7 @@ export default defineComponent({
|
|||||||
onLoad: Function as PropType<(t: Texture) => void>,
|
onLoad: Function as PropType<(t: Texture) => void>,
|
||||||
onProgress: Function as PropType<(e: ProgressEvent) => void>,
|
onProgress: Function as PropType<(e: ProgressEvent) => void>,
|
||||||
onError: Function as PropType<(e: ErrorEvent) => void>,
|
onError: Function as PropType<(e: ErrorEvent) => void>,
|
||||||
encoding: { type: Number, default: LinearEncoding },
|
props: { type: Object, default: () => ({}) },
|
||||||
// format: { type: Number, default: RGBAFormat },
|
|
||||||
mapping: { type: Number, default: UVMapping },
|
|
||||||
wrapS: { type: Number, default: ClampToEdgeWrapping },
|
|
||||||
wrapT: { type: Number, default: ClampToEdgeWrapping },
|
|
||||||
magFilter: { type: Number, default: LinearFilter },
|
|
||||||
minFilter: { type: Number, default: LinearMipmapLinearFilter },
|
|
||||||
repeat: { type: Object as PropType<Vector2PropInterface>, default: () => ({ x: 1, y: 1 }) },
|
|
||||||
rotation: { type: Number, default: 0 },
|
|
||||||
center: { type: Object as PropType<Vector2PropInterface>, default: () => ({ x: 0, y: 0 }) },
|
|
||||||
},
|
},
|
||||||
setup(): TexureInterface {
|
setup(): TexureInterface {
|
||||||
return {}
|
return {}
|
||||||
@ -45,21 +35,23 @@ export default defineComponent({
|
|||||||
methods: {
|
methods: {
|
||||||
createTexture() {
|
createTexture() {
|
||||||
if (!this.src) return undefined
|
if (!this.src) return undefined
|
||||||
const texture = new TextureLoader().load(this.src, this.onLoaded, this.onProgress, this.onError)
|
return new TextureLoader().load(this.src, this.onLoaded, this.onProgress, this.onError)
|
||||||
// use format ? TextureLoader will automatically set format to THREE.RGBFormat for JPG images.
|
},
|
||||||
const wathProps = ['encoding', 'mapping', 'wrapS', 'wrapT', 'magFilter', 'minFilter', 'repeat', 'rotation', 'center']
|
initTexture() {
|
||||||
wathProps.forEach(prop => { bindProp(this, prop, texture) })
|
if (!this.texture) return
|
||||||
return texture
|
|
||||||
|
bindOptions(this.texture, this.props)
|
||||||
|
if (!this.material) return
|
||||||
|
|
||||||
|
this.material.setTexture(this.texture, this.name)
|
||||||
|
if (this.material.material instanceof ShaderMaterial && this.uniform) {
|
||||||
|
(this.material as any).uniforms[this.uniform] = { value: this.texture }
|
||||||
|
}
|
||||||
},
|
},
|
||||||
refreshTexture() {
|
refreshTexture() {
|
||||||
|
this.texture?.dispose()
|
||||||
this.texture = this.createTexture()
|
this.texture = this.createTexture()
|
||||||
|
this.initTexture()
|
||||||
if (this.texture && this.material) {
|
|
||||||
this.material.setTexture(this.texture, this.name)
|
|
||||||
if (this.material.material instanceof ShaderMaterial && this.uniform) {
|
|
||||||
(this.material as any).uniforms[this.uniform] = { value: this.texture }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
onLoaded(t: Texture) {
|
onLoaded(t: Texture) {
|
||||||
this.onLoad?.(t)
|
this.onLoad?.(t)
|
||||||
|
Loading…
Reference in New Issue
Block a user