@@ -18,23 +18,20 @@ import ModelViewerElementBase, {$scene} from '../../model-viewer-base.js';
1818import { assetPath , textureMatchesMeta , timePasses , waitForEvent } from '../helpers.js' ;
1919
2020const expect = chai . expect ;
21- const BG_IMAGE_URL = assetPath ( 'equirectangular.png ' ) ;
21+ const BG_IMAGE_URL = assetPath ( 'spruit_sunrise_2k.jpg ' ) ;
2222const MODEL_URL = assetPath ( 'reflective-sphere.gltf' ) ;
2323
24- const skysphereUsingMap =
24+ const backgroundHasMap =
2525 ( scene , url ) => {
26- const material = scene . skysphere . material ;
27- const color = material . color . getHexString ( ) ;
28- return textureMatchesMeta ( material . map , { url : url } ) && color === 'ffffff' ;
26+ return textureMatchesMeta ( scene . background . texture , { url : url } ) ;
2927 }
3028
31- const skysphereUsingColor =
29+ const backgroundHasColor =
3230 ( scene , hex ) => {
33- const { color, map} = scene . skysphere . material ;
34- // Invert gamma correct to match passed in hex
35- const gammaCorrectedColor = color . clone ( ) . convertLinearToGamma ( 2.2 ) ;
36-
37- return map == null && gammaCorrectedColor . getHexString ( ) === hex ;
31+ if ( ! scene . background || ! scene . background . isColor ) {
32+ return false ;
33+ }
34+ return scene . background . getHexString ( ) === hex ;
3835 }
3936
4037/**
@@ -46,7 +43,7 @@ const skysphereUsingColor =
4643 * @param {THREE.Scene } scene
4744 * @param {Object } meta
4845 */
49- const modelUsingEnvmap = ( scene , meta ) => {
46+ const modelUsingEnvMap = ( scene , meta ) => {
5047 let found = false ;
5148 scene . model . traverse ( object => {
5249 if ( ! object . material || ! object . material . envMap ) {
@@ -70,18 +67,22 @@ const modelUsingEnvmap = (scene, meta) => {
7067 * @param {Model } model
7168 * @param {Object } meta
7269 */
73- const waitForEnvmap = ( model , meta ) => waitForEvent (
74- model ,
75- 'envmap-change' ,
76- e => textureMatchesMeta ( e . value , { ... meta , type : 'EnvironmentMap' } ) ) ;
70+ const waitForEnvMap = ( model , meta ) =>
71+ waitForEvent ( model , 'envmap-change' , event => {
72+ return textureMatchesMeta ( event . value , { ... meta } ) ;
73+ } ) ;
7774
7875/**
7976 * Returns a promise that resolves when a given element is loaded
8077 * and has an environment map set that matches the passed in meta.
8178 * @see textureMatchesMeta
8279 */
83- const waitForLoadAndEnvmap = ( scene , element , meta ) => Promise . all (
84- [ waitForEvent ( element , 'load' ) , waitForEnvmap ( scene . model , meta ) ] ) ;
80+ const waitForLoadAndEnvMap =
81+ ( scene , element , meta ) => {
82+ const load = waitForEvent ( element , 'load' ) ;
83+ const envMap = waitForEnvMap ( scene . model , meta ) ;
84+ return Promise . all ( [ load , envMap ] ) ;
85+ }
8586
8687suite ( 'ModelViewerElementBase with EnvironmentMixin' , ( ) => {
8788 let nextId = 0 ;
@@ -106,34 +107,34 @@ suite('ModelViewerElementBase with EnvironmentMixin', () => {
106107 teardown ( ( ) => element . parentNode && element . parentNode . removeChild ( element ) ) ;
107108
108109 test (
109- 'has default skysphere if no background-image or background-color' ,
110+ 'has default background if no background-image or background-color' ,
110111 ( ) => {
111- expect ( skysphereUsingColor ( scene , 'ffffff' ) ) . to . be . equal ( true ) ;
112+ expect ( backgroundHasColor ( scene , 'ffffff' ) ) . to . be . equal ( true ) ;
112113 } ) ;
113114
114115 test (
115- 'has default skysphere if no background-image or background-color when in DOM' ,
116+ 'has default background if no background-image or background-color when in DOM' ,
116117 async ( ) => {
117118 document . body . appendChild ( element ) ;
118119 await timePasses ( ) ;
119- expect ( skysphereUsingColor ( scene , 'ffffff' ) ) . to . be . equal ( true ) ;
120+ expect ( backgroundHasColor ( scene , 'ffffff' ) ) . to . be . equal ( true ) ;
120121 } ) ;
121122
122123 suite ( 'with a background-image property' , ( ) => {
123124 suite ( 'and a src property' , ( ) => {
124125 setup ( async ( ) => {
125- let onLoad = waitForLoadAndEnvmap ( scene , element , { url : BG_IMAGE_URL } ) ;
126+ let onLoad = waitForLoadAndEnvMap ( scene , element , { url : BG_IMAGE_URL } ) ;
126127 element . src = MODEL_URL ;
127128 element . backgroundImage = BG_IMAGE_URL ;
128129 await onLoad ;
129130 } ) ;
130131
131- test ( 'displays skysphere with the correct map' , async function ( ) {
132- expect ( skysphereUsingMap ( scene , element . backgroundImage ) ) . to . be . ok ;
132+ test ( 'displays background with the correct map' , async function ( ) {
133+ expect ( backgroundHasMap ( scene , element . backgroundImage ) ) . to . be . ok ;
133134 } ) ;
134135
135136 test ( 'applies the image as an environment map' , async function ( ) {
136- expect ( modelUsingEnvmap ( scene , {
137+ expect ( modelUsingEnvMap ( scene , {
137138 url : element . backgroundImage
138139 } ) ) . to . be . ok ;
139140 } ) ;
@@ -156,26 +157,30 @@ suite('ModelViewerElementBase with EnvironmentMixin', () => {
156157 suite ( 'with a background-color property' , ( ) => {
157158 suite ( 'and a src property' , ( ) => {
158159 setup ( async ( ) => {
159- let onLoad = waitForLoadAndEnvmap ( scene , element , { url : null } ) ;
160+ let onLoad = waitForLoadAndEnvMap ( scene , element , {
161+ url : null ,
162+ } ) ;
160163 element . src = MODEL_URL ;
161164 element . backgroundColor = '#ff0077' ;
162165 await onLoad ;
163166 } ) ;
164167
165- test ( 'displays skysphere with the correct color' , async function ( ) {
166- expect ( skysphereUsingColor ( scene , 'ff0077' ) ) . to . be . ok ;
168+ test ( 'displays background with the correct color' , async function ( ) {
169+ expect ( backgroundHasColor ( scene , 'ff0077' ) ) . to . be . ok ;
167170 } ) ;
168171
169172 test ( 'applies a generated environment map on model' , async function ( ) {
170- expect ( modelUsingEnvmap ( scene , { url : null } ) ) . to . be . ok ;
173+ expect ( modelUsingEnvMap ( scene , {
174+ url : null ,
175+ } ) ) . to . be . ok ;
171176 } ) ;
172177
173178 test (
174- 'displays skysphere with correct color after attaching to DOM' ,
179+ 'displays background with correct color after attaching to DOM' ,
175180 async function ( ) {
176181 document . body . appendChild ( element ) ;
177182 await timePasses ( ) ;
178- expect ( skysphereUsingColor ( scene , 'ff0077' ) ) . to . be . ok ;
183+ expect ( backgroundHasColor ( scene , 'ff0077' ) ) . to . be . ok ;
179184 } ) ;
180185 test ( 'the directional light is tinted' , ( ) => {
181186 const lightColor = scene . shadowLight . color . getHexString ( ) . toLowerCase ( ) ;
@@ -186,34 +191,34 @@ suite('ModelViewerElementBase with EnvironmentMixin', () => {
186191
187192 suite ( 'with background-color and background-image properties' , ( ) => {
188193 setup ( async ( ) => {
189- let onLoad = waitForLoadAndEnvmap ( scene , element , { url : BG_IMAGE_URL } ) ;
194+ let onLoad = waitForLoadAndEnvMap ( scene , element , { url : BG_IMAGE_URL } ) ;
190195 element . setAttribute ( 'src' , MODEL_URL ) ;
191196 element . setAttribute ( 'background-color' , '#ff0077' ) ;
192197 element . setAttribute ( 'background-image' , BG_IMAGE_URL ) ;
193198 await onLoad ;
194199 } ) ;
195200
196- test ( 'displays skysphere with background-image' , async function ( ) {
197- expect ( skysphereUsingMap ( scene , element . backgroundImage ) ) . to . be . ok ;
201+ test ( 'displays background with background-image' , async function ( ) {
202+ expect ( backgroundHasMap ( scene , element . backgroundImage ) ) . to . be . ok ;
198203 } ) ;
199204
200- test ( 'applies background-image envmap on model' , async function ( ) {
201- expect ( modelUsingEnvmap ( scene , { url : element . backgroundImage } ) ) . to . be . ok ;
205+ test ( 'applies background-image environment map on model' , async function ( ) {
206+ expect ( modelUsingEnvMap ( scene , { url : element . backgroundImage } ) ) . to . be . ok ;
202207 } ) ;
203208
204209 suite ( 'and background-image subsequently removed' , ( ) => {
205210 setup ( async ( ) => {
206- let envmapChanged = waitForEnvmap ( scene . model , { url : null } ) ;
211+ let envMapChanged = waitForEnvMap ( scene . model , { url : null } ) ;
207212 element . removeAttribute ( 'background-image' ) ;
208- await envmapChanged ;
213+ await envMapChanged ;
209214 } ) ;
210215
211- test ( 'displays skysphere with background-color' , async function ( ) {
212- expect ( skysphereUsingColor ( scene , 'ff0077' ) ) . to . be . ok ;
216+ test ( 'displays background with background-color' , async function ( ) {
217+ expect ( backgroundHasColor ( scene , 'ff0077' ) ) . to . be . ok ;
213218 } ) ;
214219
215- test ( 'reapplies generated envmap on model' , async function ( ) {
216- expect ( modelUsingEnvmap ( scene , { url : null } ) ) . to . be . ok ;
220+ test ( 'reapplies generated environment map on model' , async function ( ) {
221+ expect ( modelUsingEnvMap ( scene , { url : null } ) ) . to . be . ok ;
217222 } ) ;
218223 } ) ;
219224 } ) ;
0 commit comments