1. A concise explanation of the problem you’re experiencing.
A custom geometry always float on terrain.
2. A minimal code example. If you’ve found a bug, this helps us reproduce and repair it.
var positions = Cesium.Cartesian3.fromDegreesArrayHeights([
104.317776, 31.59491, 0,
105.317776, 32.59491, 0,
102.317776, 33.59491, 0
]);
var numPositions = positions.length;
var pos = new Float64Array(numPositions * 3);
for (var i = 0; i < numPositions; ++i) {
pos[i * 3] = positions[i].x;
pos[i * 3 + 1] = positions[i].y;
pos[i * 3 + 2] = positions[i].z;
}
var geometry = new Cesium.Geometry({
attributes: {
position: new Cesium.GeometryAttribute({
componentDatatype: Cesium.ComponentDatatype.DOUBLE, // not FLOAT
componentsPerAttribute: 3,
values: pos
}),
normal: new Cesium.GeometryAttribute({
componentDatatype: Cesium.ComponentDatatype.FLOAT,
componentsPerAttribute: 3,
values: new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0])
})
},
indices: new Uint32Array([0, 1, 2]),
primitiveType: Cesium.PrimitiveType.TRIANGLES,
boundingSphere: Cesium.BoundingSphere.fromVertices(pos)
});
var geoInstance = new Cesium.GeometryInstance({
geometry: geometry,
attributes: {
color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.RED)
},
show: new Cesium.ShowGeometryInstanceAttribute(true)
});
var appearance = new Cesium.MaterialAppearance({
flat: true,
vertexShaderSource:"attribute vec3 position3DHigh;\n\
attribute vec3 position3DLow;\n\
attribute vec3 normal;\n\
attribute float batchId;\n\
varying vec3 v_positionEC;\n\
varying vec3 v_normalEC;\n\
void main(){\n\
vec4 p = czm_computePosition();\n\
v_positionEC = (czm_modelViewRelativeToEye * p).xyz;\n\
v_normalEC = czm_normal * normal;\n\
gl_Position = czm_modelViewProjectionRelativeToEye * p;\n\
}",
fragmentShaderSource:"varying vec3 v_positionEC;\n\
varying vec3 v_normalEC;\n\
void main(){\n\
gl_FragColor = vec4(1.0,0.0,0.0,1.0);\n\
}",
translucent: true
});
viewer.scene.primitives.add(new Cesium.Primitive({
geometryInstances: [geoInstance],
asynchronous: false,
appearance: appearance
}));
3. Context. Why do you need to do this? We might know a better way to accomplish your goal.
4. The Cesium version you’re using, your operating system and browser.
Cesium 1.53