{"action":"created","issue":{"url":"https://api.github.com/repos/AnalyticalGraphicsInc/cesium/issues/2633","labels_url":"https://api.github.com/repos/AnalyticalGraphicsInc/cesium/issues/2633/labels{/name}","comments_url":"https://api.github.com/repos/AnalyticalGraphicsInc/cesium/issues/2633/comments","events_url":"https://api.github.com/repos/AnalyticalGraphicsInc/cesium/issues/2633/events","html_url":"https://github.com/AnalyticalGraphicsInc/cesium/pull/2633","id":68409516,"number":2633,"title":"Fix Material caching issue","user":{"login":"mramato","id":1495196,"avatar_url":"https://avatars.githubusercontent.com/u/1495196?v=3","gravatar_id":"","url":"https://api.github.com/users/mramato","html_url":"https://github.com/mramato","followers_url":"https://api.github.com/users/mramato/followers","following_url":"https://api.github.com/users/mramato/following{/other_user}","gists_url":"https://api.github.com/users/mramato/gists{/gist_id}","starred_url":"https://api.github.com/users/mramato/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mramato/subscriptions","organizations_url":"https://api.github.com/users/mramato/orgs","repos_url":"https://api.github.com/users/mramato/repos","events_url":"https://api.github.com/users/mramato/events{/privacy}","received_events_url":"https://api.github.com/users/mramato/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":11,"created_at":"2015-04-14T15:21:42Z","updated_at":"2015-06-23T18:44:59Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/AnalyticalGraphicsInc/cesium/pulls/2633","html_url":"https://github.com/AnalyticalGraphicsInc/cesium/pull/2633","diff_url":"https://github.com/AnalyticalGraphicsInc/cesium/pull/2633.diff","patch_url":"https://github.com/AnalyticalGraphicsInc/cesium/pull/2633.patch"},"body":"Run the below code in Sandcastle, hit the first button, wait for it to load, then his the second button.  In master the second polygon will be gray, in this branch both polygons will have the proper texture.  The problem is that in master, neither the `_loadedImages` or `_loadedCubeMaps` arrays are being properly populated when the image was already in the texture cache.  In order to fix this I had to keep the source images around in the cache as well and add them to the proper collection when a cache hit occurs.\r\n\r\nThere also appears to be a race condition in Material, which I'll write up a second issue for.  Basically if you hit the buttons below quickly (or just add them both in the same function instead of having 2 buttons) everything works.  This is because Material actually loads the same image twice, rather than keeping a promise to the image from the first request.  Looking at the Material code, there are most likely additional bugs as well, we should really look into refactoring the entire object.\r\n\r\n```javascript\r\nCesium.Material._materialCache.addMaterial('Wallpaper', {\r\n    fabric : {\r\n        type : 'Wallpaper',\r\n        uniforms : {\r\n            image : Cesium.Material.DefaultImageId,\r\n            anchor : new Cesium.Cartesian2(0, 0)\r\n        },\r\n        components : {\r\n            diffuse : 'texture2D(image, fract((gl_FragCoord.xy - anchor.xy) / vec2(imageDimensions.xy))).rgb',\r\n            alpha : 'texture2D(image, fract((gl_FragCoord.xy - anchor.xy) / vec2(imageDimensions.xy))).a'\r\n        }\r\n    },\r\n    translucent : false\r\n});\r\n\r\nvar WallPaperMaterialProperty = function(scene, image, anchor) {\r\n    this._scene = scene;\r\n    this._image = image;\r\n    this._anchor = anchor;\r\n    this.definitionChanged = new Cesium.Event();\r\n    this.isConstant = true;\r\n};\r\n\r\nWallPaperMaterialProperty.prototype.getType = function(time) {\r\n    return 'Wallpaper';\r\n};\r\n\r\nWallPaperMaterialProperty.prototype.getValue = function(time, result) {\r\n    if (!Cesium.defined(result)) {\r\n        result = {\r\n            image : undefined,\r\n            anchor : undefined\r\n        };\r\n    }\r\n    result.image = this._image;\r\n    result.anchor = Cesium.SceneTransforms.wgs84ToDrawingBufferCoordinates(this._scene, this._anchor, result.anchor);\r\n    if (Cesium.defined(result.anchor)) {\r\n        result.anchor.x = Math.floor(result.anchor.x);\r\n        result.anchor.y = Math.floor(result.anchor.y);\r\n    } else {\r\n        result.anchor = new Cesium.Cartesian2(0, 0);\r\n    }\r\n    return result;\r\n};\r\n\r\nWallPaperMaterialProperty.prototype.equals = function(other) {\r\n    return this === other || //\r\n    (other instanceof WallPaperMaterialProperty && //\r\n    this._image === other._image);\r\n};\r\n\r\nvar viewer = new Cesium.Viewer('cesiumContainer');\r\n\r\nSandcastle.addToolbarButton('Test1', function() {\r\n    var customDataSource1 = new Cesium.CustomDataSource(\"testing\");\r\n    viewer.dataSources.add(customDataSource1);\r\n    var customRectangle = customDataSource1.entities.add({\r\n        id : \"rect1\",\r\n        rectangle : {\r\n            coordinates : Cesium.Rectangle.fromDegrees(-119, 44, -112, 47),\r\n            material : new WallPaperMaterialProperty(viewer.scene, \"../images/checkerboard.png\", Cesium.Cartesian3.fromDegrees(-119, 44))\r\n        }\r\n    });\r\n\r\n});\r\n\r\nSandcastle.addToolbarButton('Test2', function() {\r\n    var customRectangle2 = viewer.entities.add({\r\n        id : \"rect2\",\r\n        rectangle : {\r\n            coordinates : Cesium.Rectangle.fromDegrees(-110.0, 20.0, -80.0, 25.0),\r\n            material : new WallPaperMaterialProperty(viewer.scene, \"../images/checkerboard.png\", Cesium.Cartesian3.fromDegrees(-110, 20))\r\n        }\r\n    });\r\n});\r\n```"},"comment":{"url":"https://api.github.com/repos/AnalyticalGraphicsInc/cesium/issues/comments/114604537","html_url":"https://github.com/AnalyticalGraphicsInc/cesium/pull/2633#issuecomment-114604537","issue_url":"https://api.github.com/repos/AnalyticalGraphicsInc/cesium/issues/2633","id":114604537,"user":{"login":"mramato","id":1495196,"avatar_url":"https://avatars.githubusercontent.com/u/1495196?v=3","gravatar_id":"","url":"https://api.github.com/users/mramato","html_url":"https://github.com/mramato","followers_url":"https://api.github.com/users/mramato/followers","following_url":"https://api.github.com/users/mramato/following{/other_user}","gists_url":"https://api.github.com/users/mramato/gists{/gist_id}","starred_url":"https://api.github.com/users/mramato/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mramato/subscriptions","organizations_url":"https://api.github.com/users/mramato/orgs","repos_url":"https://api.github.com/users/mramato/repos","events_url":"https://api.github.com/users/mramato/events{/privacy}","received_events_url":"https://api.github.com/users/mramato/received_events","type":"User","site_admin":false},"created_at":"2015-06-23T18:44:59Z","updated_at":"2015-06-23T18:44:59Z","body":"The most recent one is: https://groups.google.com/d/msg/cesium-dev/Qhos2CpA1cY/Q2X_jTNDktUJ\r\n\r\nBut the original is: https://groups.google.com/d/msg/cesium-dev/laVtluz3VRY/qjHdbUQ2Uj8J\r\n\r\nYou are correct that we have to keep the texture around, but our approach to image based uniforms seems fundamentally broken otherwise, since you can never use the same texture more than once.\r\n\r\nAnother possible solution would be to remove the Material texture cache completely, since I think that's where the problem lies.  This should fix the bug but also prevent us from keeping around the source in memory.  I'm not sure what benefit the cache provides in its current state."}}