Permalink
Browse files

Merge pull request #601 from AnalyticalGraphicsInc/grid-material

Grid material and Ellipsoid primitive
  • Loading branch information...
pjcozzi committed Apr 3, 2013
2 parents b6345d5 + 8f804fa commit 1c3c8aad073ab803f790a2b96f514950d14e6ad0
@@ -310,6 +310,11 @@
primitive.material = new Cesium.Material.fromType(scene.getContext(), 'Grass');
}
+ function applyGridMaterial(primitive, scene) {
+ Sandcastle.declare(applyGridMaterial); // For highlighting in Sandcastle.
+ primitive.material = new Cesium.Material.fromType(scene.getContext(), 'Grid');
+ }
+
function applyImageMaterial(primitive, scene) {
Sandcastle.declare(applyImageMaterial); // For highlighting in Sandcastle.
primitive.material = new Cesium.Material({
@@ -618,6 +623,15 @@
}
}));
+ proceduralTextureMenu.addChild(new MenuItem({
+ label: 'Grid',
+ onClick: function() {
+ togglePolygonVisibility(polygon, worldPolygon);
+ applyGridMaterial(polygon, scene);
+ Sandcastle.highlight(applyGridMaterial);
+ }
+ }));
+
commonMaterialMenu.addChild(new MenuItem({
label: 'Image',
onClick: function() {
View
@@ -7,6 +7,8 @@ Beta Releases
* Breaking changes:
*
* Added `AnimationViewModel.snapToTicks`, which when set to true, causes the shuttle ring on the Animation widget to snap to the defined tick values, rather than interpolate between them.
+* Added new `Grid` material.
+* Made `EllipsoidPrimitive` double-sided.
### b15 - 2013-04-01
View
@@ -25,6 +25,7 @@ define([
'../Shaders/Materials/FacetMaterial',
'../Shaders/Materials/FresnelMaterial',
'../Shaders/Materials/GrassMaterial',
+ '../Shaders/Materials/GridMaterial',
'../Shaders/Materials/NormalMapMaterial',
'../Shaders/Materials/ReflectionMaterial',
'../Shaders/Materials/RefractionMaterial',
@@ -60,6 +61,7 @@ define([
FacetMaterial,
FresnelMaterial,
GrassMaterial,
+ GridMaterial,
NormalMapMaterial,
ReflectionMaterial,
RefractionMaterial,
@@ -202,6 +204,13 @@ define([
* <li><code>dirtColor</code>: rgba color object for the dirt's color. </li>
* <li><code>patchiness</code>: Number that controls the size of the color patches in the grass.</li>
* </ul>
+ * <li>Grid</li>
+ * <ul>
+ * <li><code>color</code>: rgba color object for the whole material.</li>
+ * <li><code>cellAlpha</code>: Alpha value for the cells between grid lines. This will be combined with color.alpha.</li>
+ * <li><code>lineCount</code>: Object with x and y values specifying the number of columns and rows respectively.</li>
+ * <li><code>lineThickness</code>: Object with x and y values specifying the thickness of grid lines (in pixels where available).</li>
+ * </ul>
* <li>Stripe</li>
* <ul>
* <li><code>horizontal</code>: Boolean that determines if the stripes are horizontal or vertical.</li>
@@ -1055,6 +1064,18 @@ define([
source : GrassMaterial
});
+ Material.GridType = 'Grid';
+ Material._materialCache.addMaterial(Material.GridType, {
+ type : Material.GridType,
+ uniforms : {
+ color : new Color(0.0, 1.0, 0.0, 1.0),
+ cellAlpha : 0.1,
+ lineCount : new Cartesian2(8.0, 8.0),
+ lineThickness : new Cartesian2(1.0, 1.0)
+ },
+ source : GridMaterial
+ });
+
Material.StripeType = 'Stripe';
Material._materialCache.addMaterial(Material.StripeType, {
type : Material.StripeType,
@@ -3,29 +3,18 @@ uniform vec3 u_oneOverEllipsoidRadiiSquared;
varying vec3 v_positionEC;
-void main()
+vec4 computeEllipsoidColor(czm_ray ray, float intersection, float side)
{
- czm_ellipsoid ellipsoid = czm_ellipsoidNew(czm_modelView[3].xyz, u_radii);
- vec3 direction = normalize(v_positionEC);
- czm_ray ray = czm_ray(vec3(0.0), direction);
- czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid);
-
- if (czm_isEmpty(intersection))
- {
- discard;
- }
-
- // Pick the intersection point based on if the viewer is outside or inside the ellipsoid
- bool hitFrontFace = (intersection.start != 0.0);
- vec3 positionEC = czm_pointAlongRay(ray, hitFrontFace ? intersection.start : intersection.stop);
+ vec3 positionEC = czm_pointAlongRay(ray, intersection);
vec3 positionMC = (czm_inverseModelView * vec4(positionEC, 1.0)).xyz;
vec3 geodeticNormal = normalize(czm_geodeticSurfaceNormal(positionMC, vec3(0.0), u_oneOverEllipsoidRadiiSquared));
- vec3 normalMC = hitFrontFace ? geodeticNormal : -geodeticNormal; // normalized surface normal (always facing the viewer) in model coordinates
- vec3 normalEC = normalize(czm_normal * normalMC); // normalized surface normal in eye coordiantes
-
- vec2 st = czm_ellipsoidWgs84TextureCoordinates(geodeticNormal);
- vec3 positionToEyeEC = -positionEC;
-
+ vec3 sphericalNormal = normalize(positionMC / u_radii);
+ vec3 normalMC = geodeticNormal * side; // normalized surface normal (always facing the viewer) in model coordinates
+ vec3 normalEC = normalize(czm_normal * normalMC); // normalized surface normal in eye coordiantes
+
+ vec2 st = czm_ellipsoidWgs84TextureCoordinates(sphericalNormal);
+ vec3 positionToEyeEC = -positionEC;
+
czm_materialInput materialInput;
materialInput.s = st.s;
materialInput.st = st;
@@ -36,5 +25,27 @@ void main()
materialInput.positionMC = positionMC;
czm_material material = czm_getMaterial(materialInput);
- gl_FragColor = czm_phong(normalize(positionToEyeEC), material);
+ return czm_phong(normalize(positionToEyeEC), material);
+}
+
+void main()
+{
+ czm_ellipsoid ellipsoid = czm_ellipsoidNew(czm_modelView[3].xyz, u_radii);
+ vec3 direction = normalize(v_positionEC);
+ czm_ray ray = czm_ray(vec3(0.0), direction);
+ czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid);
+
+ if (czm_isEmpty(intersection))
+ {
+ discard;
+ }
+
+ // If the viewer is outside, compute outsideFaceColor, with normals facing outward.
+ vec4 outsideFaceColor = (intersection.start != 0.0) ? computeEllipsoidColor(ray, intersection.start, 1.0) : vec4(0.0);
+
+ // If the viewer either is inside or can see inside, compute insideFaceColor, with normals facing inward.
+ vec4 insideFaceColor = (outsideFaceColor.a < 1.0) ? computeEllipsoidColor(ray, intersection.stop, -1.0) : vec4(0.0);
+
+ gl_FragColor = mix(insideFaceColor, outsideFaceColor, outsideFaceColor.a);
+ gl_FragColor.a = 1.0 - (1.0 - insideFaceColor.a) * (1.0 - outsideFaceColor.a);
}
@@ -11,10 +11,10 @@ void main()
// but doing it here allows us to change the radii without rewriting the vertex data, and
// allows all ellipsoids to reuse the same vertex data.
vec4 p = vec4(u_radii * position, 1.0);
-
+
v_positionEC = (czm_modelView * p).xyz; // position in eye coordinates
gl_Position = czm_modelViewProjection * p; // position in clip coordinates
-
+
// With multi-frustum, when the ellipsoid primitive is positioned on the intersection of two frustums
// and close to terrain, the terrain (writes depth) in the closest frustum can overwrite part of the
// ellipsoid (does not write depth) that was rendered in the farther frustum.
@@ -0,0 +1,48 @@
+#ifdef GL_OES_standard_derivatives
+ #extension GL_OES_standard_derivatives : enable
+#endif
+
+uniform vec4 color;
+uniform float cellAlpha;
+uniform vec2 lineCount;
+uniform vec2 lineThickness;
+
+czm_material czm_getMaterial(czm_materialInput materialInput)
+{
+ czm_material material = czm_getDefaultMaterial(materialInput);
+
+ vec2 st = materialInput.st;
+
+ float scaledWidth = fract(lineCount.s * st.s);
+ scaledWidth = abs(scaledWidth - floor(scaledWidth + 0.5));
+ float scaledHeight = fract(lineCount.t * st.t);
+ scaledHeight = abs(scaledHeight - floor(scaledHeight + 0.5));
+
+ float value;
+#ifdef GL_OES_standard_derivatives
+ // Fuzz Factor - Controls blurriness of lines
+ const float fuzz = 1.2;
+ vec2 thickness = lineThickness - 1.0;
+
+ // From "3D Engine Design for Virtual Globes" by Cozzi and Ring, Listing 4.13.
+ vec2 dx = abs(dFdx(st));
+ vec2 dy = abs(dFdy(st));
+ vec2 dF = vec2(max(dx.s, dy.s), max(dx.t, dy.t)) * lineCount;
+ value = min(
+ smoothstep(dF.s * thickness.s, dF.s * (fuzz + thickness.s), scaledWidth),
+ smoothstep(dF.t * thickness.t, dF.t * (fuzz + thickness.t), scaledHeight));
+#else
+ // Fuzz Factor - Controls blurriness of lines
+ const float fuzz = 0.05;
+
+ vec2 range = 0.5 - (lineThickness * 0.05);
+ value = min(
+ 1.0 - smoothstep(range.s, range.s + fuzz, scaledWidth),
+ 1.0 - smoothstep(range.t, range.t + fuzz, scaledHeight));
+#endif
+
+ material.diffuse = color.rgb;
+ material.alpha = color.a * (1.0 - ((1.0 - cellAlpha) * value));
+
+ return material;
+}
@@ -153,6 +153,10 @@ defineSuite([
verifyMaterial('Grass');
});
+ it('draws Grid built-in material', function() {
+ verifyMaterial('Grid');
+ });
+
it('draws Stripe built-in material', function() {
verifyMaterial('Stripe');
});

0 comments on commit 1c3c8aa

Please sign in to comment.