Просмотр исходного кода

Texture: Introduce `width`, `height`, `depth` (#31025)

sunag 10 месяцев назад
Родитель
Сommit
4a02e5768c
2 измененных файлов с 52 добавлено и 0 удалено
  1. 22 0
      src/textures/Source.js
  2. 30 0
      src/textures/Texture.js

+ 22 - 0
src/textures/Source.js

@@ -73,6 +73,28 @@ class Source {
 
 	}
 
+	getSize( target ) {
+
+		const data = this.data;
+
+		if ( data instanceof HTMLVideoElement ) {
+
+			target.set( data.videoWidth, data.videoHeight );
+
+		} else if ( data !== null ) {
+
+			target.set( data.width, data.height, data.depth || 0 );
+
+		} else {
+
+			target.set( 0, 0, 0 );
+
+		}
+
+		return target;
+
+	}
+
 	/**
 	 * When the property is set to `true`, the engine allocates the memory
 	 * for the texture (if necessary) and triggers the actual texture upload

+ 30 - 0
src/textures/Texture.js

@@ -12,11 +12,14 @@ import {
 } from '../constants.js';
 import { generateUUID } from '../math/MathUtils.js';
 import { Vector2 } from '../math/Vector2.js';
+import { Vector3 } from '../math/Vector3.js';
 import { Matrix3 } from '../math/Matrix3.js';
 import { Source } from './Source.js';
 
 let _textureId = 0;
 
+const _tempVec3 = /*@__PURE__*/ new Vector3();
+
 /**
  * Base class for all textures.
  *
@@ -358,6 +361,33 @@ class Texture extends EventDispatcher {
 
 	}
 
+	/**
+	 * The width of the texture in pixels.
+	 */
+	get width() {
+
+		return this.source.getSize( _tempVec3 ).x;
+
+	}
+
+	/**
+	 * The height of the texture in pixels.
+	 */
+	get height() {
+
+		return this.source.getSize( _tempVec3 ).y;
+
+	}
+
+	/**
+	 * The depth of the texture in pixels.
+	 */
+	get depth() {
+
+		return this.source.getSize( _tempVec3 ).z;
+
+	}
+
 	/**
 	 * The image object holding the texture data.
 	 *

粤ICP备19079148号