Skip to content

Commit ebbd1e6

Browse files
aidinabediwilleastcott
authored andcommitted
Allow different source images for different mipmap levels (playcanvas#1616)
* add mipLevel parameter to pc.Texture.setSource & .getSource * improve documentation for mipLevel parameter * fix conflicted changes after merge * Update texture.js * improve documentation for mipLevel parameter
1 parent e491418 commit ebbd1e6

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/graphics/texture.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -691,12 +691,16 @@ Object.assign(pc, function () {
691691
* texture is a cubemap, the supplied source must be an array of 6 canvases, images or videos.
692692
* @param {HTMLCanvasElement|HTMLImageElement|HTMLVideoElement|Array} source A canvas, image or video element,
693693
* or an array of 6 canvas, image or video elements.
694+
* @param {Number} mipLevel A non-negative integer specifying the image level of detail. Defaults to 0, which represents the base image source.
695+
* A level value of N, that is greater than 0, represents the image source for the Nth mipmap reduction level.
694696
*/
695-
setSource: function (source) {
697+
setSource: function (source, mipLevel) {
696698
var i;
697699
var invalid = false;
698700
var width, height;
699701

702+
mipLevel = mipLevel || 0;
703+
700704
if (this._cubemap) {
701705
if (source[0]) {
702706
// rely on first face sizes
@@ -724,8 +728,8 @@ Object.assign(pc, function () {
724728
if (!invalid) {
725729
// mark levels as updated
726730
for (i = 0; i < 6; i++) {
727-
if (this._levels[0][i] !== source[i])
728-
this._levelsUpdated[0][i] = true;
731+
if (this._levels[mipLevel][i] !== source[i])
732+
this._levelsUpdated[mipLevel][i] = true;
729733
}
730734
}
731735
} else {
@@ -735,8 +739,8 @@ Object.assign(pc, function () {
735739

736740
if (!invalid) {
737741
// mark level as updated
738-
if (source !== this._levels[0])
739-
this._levelsUpdated[0] = true;
742+
if (source !== this._levels[mipLevel])
743+
this._levelsUpdated[mipLevel] = true;
740744

741745
width = source.width;
742746
height = source.height;
@@ -754,20 +758,22 @@ Object.assign(pc, function () {
754758
// remove levels
755759
if (this._cubemap) {
756760
for (i = 0; i < 6; i++) {
757-
this._levels[0][i] = null;
758-
this._levelsUpdated[0][i] = true;
761+
this._levels[mipLevel][i] = null;
762+
this._levelsUpdated[mipLevel][i] = true;
759763
}
760764
} else {
761-
this._levels[0] = null;
762-
this._levelsUpdated[0] = true;
765+
this._levels[mipLevel] = null;
766+
this._levelsUpdated[mipLevel] = true;
763767
}
764768
} else {
765769
// valid texture
766-
this._width = width;
767-
this._height = height;
770+
if (mipLevel === 0) {
771+
this._width = width;
772+
this._height = height;
773+
}
768774
this._pot = pc.math.powerOfTwo(this._width) && pc.math.powerOfTwo(this._height);
769775

770-
this._levels[0] = source;
776+
this._levels[mipLevel] = source;
771777
}
772778

773779
// valid or changed state of validity
@@ -784,10 +790,13 @@ Object.assign(pc, function () {
784790
* @name pc.Texture#getSource
785791
* @description Get the pixel data of the texture. If this is a cubemap then an array of 6 images will be returned otherwise
786792
* a single image.
787-
* @returns {HTMLImageElement} The source image of this texture.
793+
* @param {Number} mipLevel A non-negative integer specifying the image level of detail. Defaults to 0, which represents the base image source.
794+
* A level value of N, that is greater than 0, represents the image source for the Nth mipmap reduction level.
795+
* @returns {HTMLImageElement} The source image of this texture. Can be null if source not assigned for specific image level.
788796
*/
789-
getSource: function () {
790-
return this._levels[0];
797+
getSource: function (mipLevel) {
798+
mipLevel = mipLevel || 0;
799+
return this._levels[mipLevel];
791800
},
792801

793802
/**

0 commit comments

Comments
 (0)