From a3ad5f1b92890fd4eebc34b2db7411927ac33c4f Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 28 Feb 2014 17:30:19 -0500 Subject: [PATCH 1/4] Add second parameter to Material.fromType to override default uniforms. --- Apps/Sandcastle/gallery/Animations.html | 6 +- .../gallery/Geometry and Appearances.html | 6 +- Apps/Sandcastle/gallery/Materials.html | 10 ++- Apps/Sandcastle/gallery/Polygons.html | 10 ++- Apps/Sandcastle/gallery/Polylines.html | 78 ++++++++++--------- Source/Scene/ExtentPrimitive.js | 5 +- Source/Scene/Material.js | 20 ++++- Source/Scene/Polygon.js | 5 +- Source/Scene/Polyline.js | 5 +- Source/Scene/ViewportQuad.js | 5 +- Specs/Scene/MaterialSpec.js | 19 ++++- 11 files changed, 102 insertions(+), 67 deletions(-) diff --git a/Apps/Sandcastle/gallery/Animations.html b/Apps/Sandcastle/gallery/Animations.html index 2b913d32acd4..013e1e884028 100644 --- a/Apps/Sandcastle/gallery/Animations.html +++ b/Apps/Sandcastle/gallery/Animations.html @@ -67,15 +67,15 @@ Cesium.Matrix4.fromTranslation(new Cesium.Cartesian3(200000.0, 0.0, -3000000.0)), modelMatrix); - var material = Cesium.Material.fromType('Stripe'); // Use default colors - material.uniforms.repeat = 10; var sensors = new Cesium.SensorVolumeCollection(); rectangularSensor = sensors.addRectangularPyramid({ modelMatrix : modelMatrix, radius : 10000000.0, xHalfAngle : Cesium.Math.toRadians(30.0), yHalfAngle : Cesium.Math.toRadians(20.0), - material : material + material : Cesium.Material.fromType('Stripe', { + repeat : 10 + }) }); primitives.add(sensors); } diff --git a/Apps/Sandcastle/gallery/Geometry and Appearances.html b/Apps/Sandcastle/gallery/Geometry and Appearances.html index e11146e8ed6d..a717e9597a22 100644 --- a/Apps/Sandcastle/gallery/Geometry and Appearances.html +++ b/Apps/Sandcastle/gallery/Geometry and Appearances.html @@ -817,13 +817,13 @@ positions : positions }) }); - var wallMaterial = Cesium.Material.fromType('Checkerboard'); - wallMaterial.uniforms.repeat = new Cesium.Cartesian2(20.0, 6.0); scene.primitives.add(new Cesium.Primitive({ geometryInstances : wallInstance, appearance : new Cesium.MaterialAppearance({ - material : wallMaterial + material : Cesium.Material.fromType('Checkerboard', { + repeat : new Cesium.Cartesian2(20.0, 6.0) + }) }) })); diff --git a/Apps/Sandcastle/gallery/Materials.html b/Apps/Sandcastle/gallery/Materials.html index 51b95364868d..d39d15d52db9 100644 --- a/Apps/Sandcastle/gallery/Materials.html +++ b/Apps/Sandcastle/gallery/Materials.html @@ -372,15 +372,17 @@ function applyPolylineGlowMaterial(primitive, scene) { Sandcastle.declare(applyPolylineGlowMaterial); // For highlighting in Sandcastle. - var material = Cesium.Material.fromType('PolylineGlow'); - material.uniforms.innerWidth = primitive.getWidth() / 2.0; + var material = Cesium.Material.fromType('PolylineGlow', { + innerWidth : primitive.getWidth() / 2.0 + }); primitive.setMaterial(material); } function applyPolylineOutlineMaterial(primitive, scene) { Sandcastle.declare(applyPolylineOutlineMaterial); // For highlighting in Sandcastle. - var material = Cesium.Material.fromType('PolylineOutline'); - material.uniforms.outlineWidth = primitive.getWidth() / 2.0; + var material = Cesium.Material.fromType('PolylineOutline', { + outlineWidth : primitive.getWidth() / 2.0 + }); primitive.setMaterial(material); } diff --git a/Apps/Sandcastle/gallery/Polygons.html b/Apps/Sandcastle/gallery/Polygons.html index 244f98697ebb..191da2184257 100644 --- a/Apps/Sandcastle/gallery/Polygons.html +++ b/Apps/Sandcastle/gallery/Polygons.html @@ -48,9 +48,10 @@ Cesium.Math.toRadians(-90.0), Cesium.Math.toRadians(20.0)), rotation : Cesium.Math.toRadians(45), - material : Cesium.Material.fromType(Cesium.Material.ColorType) + material : Cesium.Material.fromType(Cesium.Material.ColorType, { + color : new Cesium.Color(1.0, 0.0, 1.0, 0.75) + }) })); - polygon.material.uniforms.color = new Cesium.Color(1.0, 0.0, 1.0, 0.75); Sandcastle.declare(polygon); // Create a nested polygon with holes @@ -88,9 +89,10 @@ }; var polygonHierarchy = primitives.add(new Cesium.Polygon({ polygonHierarchy : hierarchy, - material : Cesium.Material.fromType('Color') + material : Cesium.Material.fromType('Color', { + color : new Cesium.Color(0.0, 1.0, 1.0, 0.75) + }) })); - polygonHierarchy.material.uniforms.color = new Cesium.Color(0.0, 1.0, 1.0, 0.75); Sandcastle.declare(polygonHierarchy); // For highlighting on mouseover in Sandcastle. // Create a polygon from an extent diff --git a/Apps/Sandcastle/gallery/Polylines.html b/Apps/Sandcastle/gallery/Polylines.html index f91ddee502b6..23bbaf5d35ae 100644 --- a/Apps/Sandcastle/gallery/Polylines.html +++ b/Apps/Sandcastle/gallery/Polylines.html @@ -32,42 +32,45 @@ var polylines = new Cesium.PolylineCollection(); // A simple polyline with two points. - var polyline = polylines.add(); + var polyline = polylines.add({ + positions : ellipsoid.cartographicArrayToCartesianArray([ + Cesium.Cartographic.fromDegrees(-120.0, 40.0), + Cesium.Cartographic.fromDegrees(-110.0, 30.0) + ]), + material : Cesium.Material.fromType('Color', { + color : new Cesium.Color(1.0, 1.0, 1.0, 1.0) + }) + }); Sandcastle.declare(polyline); // For highlighting on mouseover in Sandcastle. - polyline.setPositions(ellipsoid.cartographicArrayToCartesianArray([ - Cesium.Cartographic.fromDegrees(-120.0, 40.0), - Cesium.Cartographic.fromDegrees(-110.0, 30.0) - ])); - var colorMaterial = Cesium.Material.fromType('Color'); - colorMaterial.uniforms.color = new Cesium.Color(1.0, 1.0, 1.0, 1.0); - polyline.setMaterial(colorMaterial); // Apply a polyline outline material - var widePolyline = polylines.add(); + var widePolyline = polylines.add({ + positions : ellipsoid.cartographicArrayToCartesianArray([ + Cesium.Cartographic.fromDegrees(-105.0, 40.0), + Cesium.Cartographic.fromDegrees(-100.0, 38.0), + Cesium.Cartographic.fromDegrees(-105.0, 35.0), + Cesium.Cartographic.fromDegrees(-100.0, 32.0) + ]), + material : Cesium.Material.fromType(Cesium.Material.PolylineOutlineType, { + outlineWidth : 5.0 + }), + width : 10.0 + }); Sandcastle.declare(widePolyline); // For highlighting on mouseover in Sandcastle. - widePolyline.setPositions(ellipsoid.cartographicArrayToCartesianArray([ - Cesium.Cartographic.fromDegrees(-105.0, 40.0), - Cesium.Cartographic.fromDegrees(-100.0, 38.0), - Cesium.Cartographic.fromDegrees(-105.0, 35.0), - Cesium.Cartographic.fromDegrees(-100.0, 32.0) - ])); - var outlineMaterial = Cesium.Material.fromType(Cesium.Material.PolylineOutlineType); - outlineMaterial.uniforms.outlineWidth = 5.0; - widePolyline.setMaterial(outlineMaterial); - widePolyline.setWidth(10.0); // Apply a polyline glow material - var coloredPolyline = polylines.add(); + var coloredPolyline = polylines.add({ + positions : ellipsoid.cartographicArrayToCartesianArray([ + Cesium.Cartographic.fromDegrees(-95.0, 30.0), + Cesium.Cartographic.fromDegrees(-85.0, 40.0) + ]), + material : Cesium.Material.fromType(Cesium.Material.PolylineGlowType, { + innerWidth : 3.0, + color : new Cesium.Color(1.0, 0.5, 0.0, 1.0) + }), + width : 10.0 + }); Sandcastle.declare(coloredPolyline); // For highlighting on mouseover in Sandcastle. - coloredPolyline.setPositions(ellipsoid.cartographicArrayToCartesianArray([ - Cesium.Cartographic.fromDegrees(-95.0, 30.0), - Cesium.Cartographic.fromDegrees(-85.0, 40.0) - ])); - var glowMaterial = Cesium.Material.fromType(Cesium.Material.PolylineGlowType); - glowMaterial.uniforms.innerWidth = 3.0; - glowMaterial.uniforms.color = new Cesium.Color(1.0, 0.5, 0.0, 1.0); - coloredPolyline.setMaterial(glowMaterial); - coloredPolyline.setWidth(10.0); primitives.add(polylines); @@ -76,15 +79,16 @@ var localPolylines = new Cesium.PolylineCollection(); var center = ellipsoid.cartographicToCartesian(Cesium.Cartographic.fromDegrees(-80.0, 35.0)); localPolylines.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(center); - var localPolyline = localPolylines.add(); + + var localPolyline = localPolylines.add({ + positions : [ + new Cesium.Cartesian3(0.0, 0.0, 0.0), + new Cesium.Cartesian3(1000000.0, 0.0, 0.0) + ], + width : 10.0, + material : Cesium.Material.fromType(Cesium.Material.PolylineArrowType) + }); Sandcastle.declare(localPolyline); // For highlighting on mouseover in Sandcastle. - localPolyline.setPositions([ - new Cesium.Cartesian3(0.0, 0.0, 0.0), - new Cesium.Cartesian3(1000000.0, 0.0, 0.0) - ]); - localPolyline.setWidth(10.0); - var arrowMaterial = Cesium.Material.fromType(Cesium.Material.PolylineArrowType); - localPolyline.setMaterial(arrowMaterial); primitives.add(localPolylines); } diff --git a/Source/Scene/ExtentPrimitive.js b/Source/Scene/ExtentPrimitive.js index 51d47067961e..28733342b527 100644 --- a/Source/Scene/ExtentPrimitive.js +++ b/Source/Scene/ExtentPrimitive.js @@ -129,8 +129,9 @@ define([ */ this.show = defaultValue(options.show, true); - var material = Material.fromType(Material.ColorType); - material.uniforms.color = new Color(1.0, 1.0, 0.0, 0.5); + var material = Material.fromType(Material.ColorType, { + color : new Color(1.0, 1.0, 0.0, 0.5) + }); /** * The surface appearance of the primitive. This can be one of several built-in {@link Material} objects or a custom material, scripted with diff --git a/Source/Scene/Material.js b/Source/Scene/Material.js index 45bd534b2cc7..825bc712a60f 100644 --- a/Source/Scene/Material.js +++ b/Source/Scene/Material.js @@ -351,27 +351,39 @@ define([ * Shorthand for: new Material({fabric : {type : type}}); * * @param {String} type The base material type. + * @param {Object} [uniforms] Overrides for the default uniforms. * * @returns {Material} New material object. * * @exception {DeveloperError} material with that type does not exist. * * @example - * var material = Cesium.Material.fromType('Color'); - * material.uniforms.color = vec4(1.0, 0.0, 0.0, 1.0); + * var material = Cesium.Material.fromType('Color', { + * color : new Cesium.Color(1.0, 0.0, 0.0, 1.0) + * }); */ - Material.fromType = function(type) { + Material.fromType = function(type, uniforms) { //>>includeStart('debug', pragmas.debug); if (!defined(Material._materialCache.getMaterial(type))) { throw new DeveloperError('material with type \'' + type + '\' does not exist.'); } //>>includeEnd('debug'); - return new Material({ + var material = new Material({ fabric : { type : type } }); + + if (defined(uniforms)) { + for (var name in uniforms) { + if (uniforms.hasOwnProperty(name)) { + material.uniforms[name] = uniforms[name]; + } + } + } + + return material; }; Material.prototype.isTranslucent = function() { diff --git a/Source/Scene/Polygon.js b/Source/Scene/Polygon.js index 481088e3c378..dc7b4e3ff2b9 100644 --- a/Source/Scene/Polygon.js +++ b/Source/Scene/Polygon.js @@ -128,8 +128,9 @@ define([ */ this.show = defaultValue(options.show, true); - var material = Material.fromType(Material.ColorType); - material.uniforms.color = new Color(1.0, 1.0, 0.0, 0.5); + var material = Material.fromType(Material.ColorType, { + color : new Color(1.0, 1.0, 0.0, 0.5) + }); /** * The surface appearance of the primitive. This can be one of several built-in {@link Material} objects or a custom material, scripted with diff --git a/Source/Scene/Polyline.js b/Source/Scene/Polyline.js index 14eeea1bae41..afffdc8f7eb9 100644 --- a/Source/Scene/Polyline.js +++ b/Source/Scene/Polyline.js @@ -35,8 +35,9 @@ define([ this._material = options.material; if (!defined(this._material)) { - this._material = Material.fromType(Material.ColorType); - this._material.uniforms.color = new Color(1.0, 1.0, 1.0, 1.0); + this._material = Material.fromType(Material.ColorType, { + color : new Color(1.0, 1.0, 1.0, 1.0) + }); } var positions = options.positions; diff --git a/Source/Scene/ViewportQuad.js b/Source/Scene/ViewportQuad.js index 8935404fb1f5..10e449ff926f 100644 --- a/Source/Scene/ViewportQuad.js +++ b/Source/Scene/ViewportQuad.js @@ -81,8 +81,9 @@ define([ this.rectangle = BoundingRectangle.clone(rectangle); if (!defined(material)) { - material = Material.fromType(Material.ColorType); - material.uniforms.color = new Color(1.0, 1.0, 1.0, 1.0); + material = Material.fromType(Material.ColorType, { + color : new Color(1.0, 1.0, 1.0, 1.0) + }); } /** diff --git a/Specs/Scene/MaterialSpec.js b/Specs/Scene/MaterialSpec.js index dc062449838f..4f79450bc400 100644 --- a/Specs/Scene/MaterialSpec.js +++ b/Specs/Scene/MaterialSpec.js @@ -451,12 +451,23 @@ defineSuite([ expect(pixel).not.toEqual([0, 0, 0, 0]); }); + it('create material using fromType and overide default uniforms', function() { + var material1 = Material.fromType('Color', { + color : new Color(0.0, 1.0, 0.0, 1.0) + }); + + var pixel = renderMaterial(material1); + expect(pixel).toEqual([0, 255, 0, 255]); + }); + it('create multiple materials from the same type', function() { - var material1 = Material.fromType('Color'); - material1.uniforms.color = new Color(0.0, 1.0, 0.0, 1.0); + var material1 = Material.fromType('Color', { + color : new Color(0.0, 1.0, 0.0, 1.0) + }); - var material2 = Material.fromType('Color'); - material2.uniforms.color = new Color(0.0, 0.0, 1.0, 1.0); + var material2 = Material.fromType('Color', { + color : new Color(0.0, 0.0, 1.0, 1.0) + }); expect(material1.shaderSource).toEqual(material2.shaderSource); From 3595f6c9edf0e06654022406e130e06c408e467c Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 28 Feb 2014 17:42:48 -0500 Subject: [PATCH 2/4] Update after merge. --- Apps/Sandcastle/gallery/Polylines.html | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Apps/Sandcastle/gallery/Polylines.html b/Apps/Sandcastle/gallery/Polylines.html index d03fa42809ae..81d0c2c622ab 100644 --- a/Apps/Sandcastle/gallery/Polylines.html +++ b/Apps/Sandcastle/gallery/Polylines.html @@ -73,16 +73,17 @@ Sandcastle.declare(coloredPolyline); // For highlighting on mouseover in Sandcastle. // A polyline loop - var loopPolyline = polylines.add(); + var loopPolyline = polylines.add({ + positions : ellipsoid.cartographicArrayToCartesianArray([ + Cesium.Cartographic.fromDegrees(-105.0, 30.0), + Cesium.Cartographic.fromDegrees(-105.0, 25.0), + Cesium.Cartographic.fromDegrees(-100.0, 22.0), + Cesium.Cartographic.fromDegrees(-100.0, 28.0) + ]), + width : 3.0, + loop : true + }); Sandcastle.declare(loopPolyline); // For highlighting on mouseover in Sandcastle. - loopPolyline.setPositions(ellipsoid.cartographicArrayToCartesianArray([ - Cesium.Cartographic.fromDegrees(-105.0, 30.0), - Cesium.Cartographic.fromDegrees(-105.0, 25.0), - Cesium.Cartographic.fromDegrees(-100.0, 22.0), - Cesium.Cartographic.fromDegrees(-100.0, 28.0) - ])); - loopPolyline.setWidth(3.0); - loopPolyline.setLoop(true); primitives.add(polylines); From d685f0a97d6f1d197b73b0c794caac9c82d034dd Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 28 Feb 2014 17:45:03 -0500 Subject: [PATCH 3/4] Update CHANGES.md. --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 3e30ef559557..91528a63d39c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -120,6 +120,7 @@ Beta Releases * Added `TimeIntervalCollection.getChangedEvent` which returns an event that will be raised whenever intervals are updated. * Added `Intersections2D` class containing operations on 2D triangles. * Added `czm_inverseViewProjection` and `czm_inverseModelViewProjection` automatic GLSL uniform. +* Added a second parameter to `Material.fromType` to override default uniforms. [#1522](https://github.com/AnalyticalGraphicsInc/cesium/pull/1522) ### b25 - 2014-02-03 From 8fbd37e0f273d4fa1d4d50ef69bb98000b92b845 Mon Sep 17 00:00:00 2001 From: Patrick Cozzi Date: Fri, 28 Feb 2014 18:14:02 -0500 Subject: [PATCH 4/4] Tweak CHANGES.md --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 91528a63d39c..1809beabe1fa 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -118,9 +118,9 @@ Beta Releases * `RequestErrorEvent` now includes the headers that were returned with the error response. * Added `AssociativeArray`, which is a helper class for maintaining a hash of objects that also needs to be iterated often. * Added `TimeIntervalCollection.getChangedEvent` which returns an event that will be raised whenever intervals are updated. +* Added a second parameter to `Material.fromType` to override default uniforms. [#1522](https://github.com/AnalyticalGraphicsInc/cesium/pull/1522) * Added `Intersections2D` class containing operations on 2D triangles. * Added `czm_inverseViewProjection` and `czm_inverseModelViewProjection` automatic GLSL uniform. -* Added a second parameter to `Material.fromType` to override default uniforms. [#1522](https://github.com/AnalyticalGraphicsInc/cesium/pull/1522) ### b25 - 2014-02-03