以下代码在1.117环境中测试通过,可使用sandcastle直接运行:
const viewer = new Cesium.Viewer("cesiumContainer");
const inverseModelLocal = new Cesium.Matrix4();
const modelLocal = new Cesium.Matrix4();
try {
viewer.entities.add({
name: "Red line on terrain",
polyline: {
positions: [
Cesium.Cartesian3.fromDegrees(-74.01677147382591, 40.70346066721817,10),
Cesium.Cartesian3.fromDegrees(-74.01677147382591, 40.70346066721817,100000),
],
width: 5,
material: Cesium.Color.RED,
},
});
const tileset = await Cesium.Cesium3DTileset.fromIonAssetId(75343, {
customShader: new Cesium.CustomShader({
lightingModel: Cesium.LightingModel.UNLIT,
uniforms: {
u_inverseLocalModel: {
type: Cesium.UniformType.MAT4,
value: inverseModelLocal
},
u_localModel: {
type: Cesium.UniformType.MAT4,
value: modelLocal
},
u_planeHeight: {
type: Cesium.UniformType.FLOAT,
value: 1250
}
},
vertexShaderText: `
float random(vec2 seed) {
const vec2 random_number_generator_constants = vec2(12.9898, 78.233);
vec2 uv = fract(seed * random_number_generator_constants);
return uv.x * uv.y;
}
void vertexMain(VertexInput vsInput, inout czm_modelVertexOutput vsOutput) {
vec4 positionWC = u_inverseLocalModel * czm_model * vec4(vsInput.attributes.positionMC, 1.0);
positionWC.z += czm_frameNumber * 1.0;
vsOutput.positionMC = (czm_inverseModel * u_localModel * positionWC).xyz;
}
`,
fragmentShaderText: `
// Color tiles by distance to the camera
void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material)
{
material.diffuse = vec3(0.0, 0.0, 1.0);
material.diffuse.g = -fsInput.attributes.positionEC.z / 1.0e4;
}
`,
}),
});
const center = tileset.boundingSphere.center;
tileset.localView = Cesium.Transforms.eastNorthUpToFixedFrame(center, viewer.scene.globe.ellipsoid, modelLocal);
tileset.localViewInverse = Cesium.Matrix4.inverse(modelLocal, inverseModelLocal);
viewer.scene.primitives.add(tileset);
} catch (error) {
console.log(`Error loading tileset: ${error}`);
}
const initialPosition = Cesium.Cartesian3.fromDegrees(
-74.01881302800248,
40.69114333714821,
753
);
const initialOrientation = new Cesium.HeadingPitchRoll.fromDegrees(
21.27879878293835,
-21.34390550872461,
0.0716951918898415
);
viewer.scene.camera.setView({
destination: initialPosition,
orientation: initialOrientation,
endTransform: Cesium.Matrix4.IDENTITY,
});