@@ -1168,7 +1168,7 @@ THREE.WebGLRenderer = function ( parameters ) {
1168
1168
if ( renderTarget ) {
1169
1169
1170
1170
var texture = renderTarget . texture ;
1171
- var isTargetPowerOfTwo = THREE . Math . isPowerOfTwo ( renderTarget . width ) && THREE . Math . isPowerOfTwo ( renderTarget . height ) ;
1171
+ var isTargetPowerOfTwo = isPowerOfTwo ( renderTarget ) ;
1172
1172
if ( texture . generateMipmaps && isTargetPowerOfTwo && texture . minFilter !== THREE . NearestFilter && texture . minFilter !== THREE . LinearFilter ) {
1173
1173
1174
1174
updateRenderTargetMipmap ( renderTarget ) ;
@@ -2724,8 +2724,14 @@ THREE.WebGLRenderer = function ( parameters ) {
2724
2724
2725
2725
texture . image = clampToMaxSize ( texture . image , capabilities . maxTextureSize ) ;
2726
2726
2727
+ if ( textureNeedsPowerOfTwo ( texture ) && isPowerOfTwo ( texture . image ) === false ) {
2728
+
2729
+ texture . image = makePowerOfTwo ( texture . image ) ;
2730
+
2731
+ }
2732
+
2727
2733
var image = texture . image ,
2728
- isImagePowerOfTwo = THREE . Math . isPowerOfTwo ( image . width ) && THREE . Math . isPowerOfTwo ( image . height ) ,
2734
+ isImagePowerOfTwo = isPowerOfTwo ( image ) , // TODO: Always true at this step?
2729
2735
glFormat = paramThreeToGL ( texture . format ) ,
2730
2736
glType = paramThreeToGL ( texture . type ) ;
2731
2737
@@ -2875,6 +2881,36 @@ THREE.WebGLRenderer = function ( parameters ) {
2875
2881
2876
2882
}
2877
2883
2884
+ function isPowerOfTwo ( image ) {
2885
+
2886
+ return THREE . Math . isPowerOfTwo ( image . width ) && THREE . Math . isPowerOfTwo ( image . height ) ;
2887
+
2888
+ }
2889
+
2890
+ function textureNeedsPowerOfTwo ( texture ) {
2891
+
2892
+ if ( texture . wrapS !== THREE . ClampToEdgeWrapping || texture . wrapT !== THREE . ClampToEdgeWrapping ) return true ;
2893
+ if ( texture . minFilter !== THREE . NearestFilter && texture . minFilter !== THREE . LinearFilter ) return true ;
2894
+
2895
+ return false ;
2896
+
2897
+ }
2898
+
2899
+ function makePowerOfTwo ( image ) {
2900
+
2901
+ var canvas = document . createElement ( 'canvas' ) ;
2902
+ canvas . width = THREE . Math . nearestPowerOfTwo ( image . width ) ;
2903
+ canvas . height = THREE . Math . nearestPowerOfTwo ( image . height ) ;
2904
+
2905
+ var context = canvas . getContext ( '2d' ) ;
2906
+ context . drawImage ( image , 0 , 0 , canvas . width , canvas . height ) ;
2907
+
2908
+ console . warn ( 'THREE.WebGLRenderer: image is not power of two (' + image . width + 'x' + image . height + '). Resized to ' + canvas . width + 'x' + canvas . height , image ) ;
2909
+
2910
+ return canvas ;
2911
+
2912
+ }
2913
+
2878
2914
function setCubeTexture ( texture , slot ) {
2879
2915
2880
2916
var textureProperties = properties . get ( texture ) ;
@@ -2918,7 +2954,7 @@ THREE.WebGLRenderer = function ( parameters ) {
2918
2954
}
2919
2955
2920
2956
var image = cubeImage [ 0 ] ,
2921
- isImagePowerOfTwo = THREE . Math . isPowerOfTwo ( image . width ) && THREE . Math . isPowerOfTwo ( image . height ) ,
2957
+ isImagePowerOfTwo = isPowerOfTwo ( image ) ,
2922
2958
glFormat = paramThreeToGL ( texture . format ) ,
2923
2959
glType = paramThreeToGL ( texture . type ) ;
2924
2960
@@ -3056,7 +3092,7 @@ THREE.WebGLRenderer = function ( parameters ) {
3056
3092
3057
3093
// Setup texture, create render and frame buffers
3058
3094
3059
- var isTargetPowerOfTwo = THREE . Math . isPowerOfTwo ( renderTarget . width ) && THREE . Math . isPowerOfTwo ( renderTarget . height ) ,
3095
+ var isTargetPowerOfTwo = isPowerOfTwo ( renderTarget ) ,
3060
3096
glFormat = paramThreeToGL ( renderTarget . texture . format ) ,
3061
3097
glType = paramThreeToGL ( renderTarget . texture . type ) ;
3062
3098
0 commit comments