Skip to content

Instantly share code, notes, and snippets.

@pjcozzi
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjcozzi/4bc8fbf5a2907de6c3a9 to your computer and use it in GitHub Desktop.
Save pjcozzi/4bc8fbf5a2907de6c3a9 to your computer and use it in GitHub Desktop.
Cesium Dynamic Geometry 2
var viewer = new Cesium.Viewer('cesiumContainer', {
sceneModePicker : false
});
////////////////////////////////////////////////////////////////////////
var Cartesian3InstanceAttribute = function(x, y, z) {
this.value = new Float32Array([
Cesium.defaultValue(x, 0.0),
Cesium.defaultValue(y, 0.0),
Cesium.defaultValue(z, 0.0)
]);
};
Cesium.defineProperties(Cartesian3InstanceAttribute.prototype, {
componentDatatype : {
get : function() {
return Cesium.ComponentDatatype.FLOAT;
}
},
componentsPerAttribute : {
get : function() {
return 3;
}
},
normalize : {
get : function() {
return false;
}
}
});
Cartesian3InstanceAttribute.fromCartesian3 = function(cartesian) {
//>>includeStart('debug', pragmas.debug);
if (!Cesium.defined(cartesian)) {
throw new Cesium.DeveloperError('cartesian is required.');
}
//>>includeEnd('debug');
return new Cartesian3InstanceAttribute(cartesian.x, cartesian.y, cartesian.z);
};
Cartesian3InstanceAttribute.toValue = function(cartesian, result) {
//>>includeStart('debug', pragmas.debug);
if (!Cesium.defined(cartesian)) {
throw new Cesium.DeveloperError('cartesian is required.');
}
//>>includeEnd('debug');
if (!Cesium.defined(result)) {
return new Float32Array([cartesian.x, cartesian.y, cartesian.z]);
}
result[0] = cartesian.x;
result[1] = cartesian.y;
result[2] = cartesian.z;
return result;
};
////////////////////////////////////////////////////////////////////////
var vs =
'attribute vec3 position3DHigh;' +
'attribute vec3 position3DLow;' +
'attribute vec3 normal;' +
'attribute vec4 color;' +
'attribute vec3 offset;' +
'varying vec3 v_positionEC;' +
'varying vec3 v_normalEC;' +
'varying vec4 v_color;' +
'void main()' +
'{' +
' vec4 p = czm_computePosition();' +
' vec4 positionEC = czm_modelViewRelativeToEye * p;' +
' v_positionEC = positionEC.xyz;' +
' v_normalEC = czm_normal * normal;' +
' v_color = color;' +
' vec4 positionMC = czm_inverseView * positionEC;' +
' positionMC.xyz += offset;' +
' gl_Position = czm_viewProjection * positionMC;' +
'}';
////////////////////////////////////////////////////////////////////////
var instances = [];
var updates = [];
var id = 0;
for (var i = -180; i < 180; i += 5) {
for (var j = -60; j < 60; j += 5) {
var maxHeight = 1200000.0;
var radius = 100000.0;
var cylinderGeometry = new Cesium.SphereGeometry({
radius : radius,
vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
stackPartitions : 12,
slicePartitions : 12
});
var center = Cesium.Cartesian3.fromDegrees(i, j, radius + maxHeight);
var direction = Cesium.Ellipsoid.WGS84.geodeticSurfaceNormal(center, direction);
instances.push(new Cesium.GeometryInstance({
geometry : cylinderGeometry,
modelMatrix : Cesium.Transforms.eastNorthUpToFixedFrame(center),
attributes : {
color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({
alpha : 1.0
})),
offset : new Cartesian3InstanceAttribute()
},
id : id++
}));
updates.push({
attributes : undefined,
direction : Cesium.Cartesian3.clone(direction),
maxHeight : maxHeight,
speed : Cesium.Math.nextRandomNumber() * 0.05,
offsetArray : new Float32Array(3)
});
}
}
var primitive = viewer.scene.primitives.add(new Cesium.Primitive({
geometryInstances : instances,
appearance : new Cesium.PerInstanceColorAppearance({
vertexShaderSource : vs
})
}));
// No need to keep the geometry in memory since the primitive
// pushed it to GPU memory.
instances = undefined;
var frameNumber = 0;
var scratch = new Cesium.Cartesian3();
var ready = false;
viewer.clock.onTick.addEventListener(function() {
var length = updates.length;
var k;
var u;
// This check will not be needed in the future, see
// https://github.com/AnalyticalGraphicsInc/cesium/issues/2174
if (!ready && primitive.ready) {
ready = true;
for (k = 0; k < length; ++k) {
u = updates[k];
u.attributes = primitive.getGeometryInstanceAttributes(k);
}
}
if (ready) {
for (k = 0; k < length; ++k) {
u = updates[k];
scratch = Cesium.Cartesian3.multiplyByScalar(u.direction, (Math.sin(frameNumber * u.speed) * u.maxHeight), scratch);
u.attributes.offset = Cartesian3InstanceAttribute.toValue(scratch, u.offsetArray);
}
}
++frameNumber;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment