@@ -369,6 +369,109 @@ var Material = new Class({
369369 * This property is automatically set to true when instancing a new material.
370370 */
371371 this . needsUpdate = true ;
372+
373+ // Scene-level Properties that control if the material needs updating, or not
374+
375+ this . program = null ;
376+
377+ this . sceneProperties = {
378+ numClippingPlanes : 0 ,
379+ fog : null ,
380+ outputEncoding : 'linear' ,
381+ gammaFactor : 2 ,
382+ lightsHash : null ,
383+ acceptLight : false ,
384+ receiveShadow : false ,
385+ shadowType : null
386+ } ;
387+ } ,
388+
389+ isDirty : function ( scene , camera , object )
390+ {
391+ if ( this . needsUpdate )
392+ {
393+ // We know it needs an update already, so skip any further checks
394+ return true ;
395+ }
396+
397+ var props = this . sceneProperties ;
398+
399+ if ( ! this . program || props . fog !== scene . fog || camera . outputEncoding !== props . outputEncoding || camera . gammaFactor !== props . gammaFactor )
400+ {
401+ this . needsUpdate = true ;
402+
403+ return true ;
404+ }
405+
406+ // if (scene.clippingPlanes && scene.clippingPlanes.length !== props.numClippingPlanes)
407+ // {
408+ // this.needsUpdate = true;
409+ // }
410+
411+ var acceptLight = this . acceptLight && ! ! scene . lights && ( scene . lights . totalNum > 0 ) ;
412+
413+ if ( acceptLight !== props . acceptLight )
414+ {
415+ this . needsUpdate = true ;
416+
417+ return true ;
418+ }
419+ else if ( acceptLight )
420+ {
421+ if ( object . receiveShadow !== props . receiveShadow || object . shadowType !== props . shadowType || ! scene . lights . hash . compare ( props . lightsHash ) )
422+ {
423+ this . needsUpdate = true ;
424+
425+ return true ;
426+ }
427+ }
428+
429+ return false ;
430+ } ,
431+
432+ update : function ( scene , camera , object )
433+ {
434+ if ( ! this . isDirty ( scene , camera , object ) )
435+ {
436+ // Camera doesn't need updating, so let's bail out
437+ return ;
438+ }
439+
440+ var props = this . sceneProperties ;
441+
442+ if ( ! this . program )
443+ {
444+ // this.addEventListener('dispose', this.onMaterialDispose, this);
445+ }
446+
447+ var oldProgram = this . program ;
448+
449+ this . program = this . programs . getProgram ( camera , this , object , scene ) ;
450+
451+ if ( oldProgram )
452+ {
453+ this . programs . releaseProgram ( oldProgram ) ;
454+ }
455+
456+ props . fog = scene . fog ;
457+
458+ if ( scene . lights )
459+ {
460+ props . acceptLight = this . acceptLight ;
461+ props . lightsHash = scene . lights . hash . copyTo ( props . lightsHash ) ;
462+ props . receiveShadow = object . receiveShadow ;
463+ props . shadowType = object . shadowType ;
464+ }
465+ else
466+ {
467+ props . acceptLight = false ;
468+ }
469+
470+ props . numClippingPlanes = scene . clippingPlanes ? scene . clippingPlanes . length : 0 ;
471+ props . outputEncoding = camera . outputEncoding ;
472+ props . gammaFactor = camera . gammaFactor ;
473+
474+ this . needsUpdate = false ;
372475 }
373476
374477} ) ;
0 commit comments