// 构造cesium viewer
var viewer = new Cesium.Viewer('cesiumContainer');
// 创建新的shaderProgram对象
var shaderProgram = new Cesium.ShaderProgram({
context: viewer.scene.context,
vertexShaderSource: `
attribute vec3 position;
attribute vec3 normal;
attribute vec2 textureCoord;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 normalMatrix;
varying vec3 v_normal;
varying vec2 v_textureCoord;
void main() {
gl_Position = modelViewProjectionMatrix * vec4(position, 1.0);
v_normal = normalize(vec3(normalMatrix * vec4(normal, 0.0)));
v_textureCoord = textureCoord;
}
`,
fragmentShaderSource: `
precision mediump float;
uniform vec4 color;
varying vec3 v_normal;
varying vec2 v_textureCoord;
void main(){
gl_FragColor = color * vec4(v_normal, 1.0);
}
`
});
// 创建一个简单的矩形实体
var rectangle = viewer.entities.add({
rectangle: {
coordinates: Cesium.Rectangle.fromDegrees(-100.0, 30.0, -90.0, 40.0), // 矩形范围
height: 0.0, // 高度
},
// 设置 appearance,使用新创建的shaderProgram
appearance: new Cesium.MaterialAppearance({
material: new Cesium.Material({
fabric: {
type: "Color",
uniforms: {
color: new Cesium.Color(1.0, 0.0, 0.0, 1.0) // 设置实体的基础颜色
}
}
}),
shaderProgram: shaderProgram,
translucent: true
})
});
// 设置相机位置
viewer.camera.setView({
destination : Cesium.Cartesian3.fromDegrees(-97.0, 36.0, 1000000.0)
});
<script>
// 构造cesium viewer
var viewer = new Cesium.Viewer('cesiumContainer');
// 创建新的shaderProgram对象
var shaderProgram = new Cesium.ShaderProgram({
context: viewer.scene.context,
vertexShaderSource: `
attribute vec3 position;
attribute vec3 normal;
attribute vec2 textureCoord;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 normalMatrix;
varying vec3 v_normal;
varying vec2 v_textureCoord;
void main() {
gl_Position = modelViewProjectionMatrix * vec4(position, 1.0);
v_normal = normalize(vec3(normalMatrix * vec4(normal, 0.0)));
v_textureCoord = textureCoord;
}
`,
fragmentShaderSource: `
precision mediump float;
uniform vec3 color; // 修改uniform属性值,设置为v_normal或其它属性值
varying vec3 v_normal;
varying vec2 v_textureCoord;
void main(){
gl_FragColor = vec4(normalize(v_normal)*color, 1.0); // 为gl_FragColor赋值
}
`
});
// 创建一个简单的矩形实体
var rectangle = viewer.entities.add({
rectangle: {
coordinates: Cesium.Rectangle.fromDegrees(-100.0, 30.0, -90.0, 40.0), // 矩形范围
height: 0.0, // 高度
},
// 设置 appearance,使用新创建的shaderProgram
appearance: new Cesium.MaterialAppearance({
material: new Cesium.Material({
fabric: {
type: "Color",
uniforms: {
color: new Cesium.Color(1.0, 0.0, 0.0, 1.0) // 设置实体的基础颜色
}
}
}),
shaderProgram: shaderProgram,
translucent: true
})
});
// 设置相机位置
viewer.camera.setView({
destination : Cesium.Cartesian3.fromDegrees(-97.0, 36.0, 1000000.0)
});
</script>