- 可以使用Cesium.PostProcessStage来添加全屏幕的后处理shader,PostProcessStage只支持fs,没有vs;这个shader里的uniform可以设为一个函数,每帧处理前自动调用该函数以更新uniform的值;
- PostProcessStage里可以直接将当前时间通过uniform变量设为回调函数的方式传入,以实现动画效果;普通shader里,可以使用czm_frameNumber获取每帧的编号,以实现动画效果
- fs中的v_textureCoordinates是当前处理的纹理坐标,坐标原点在屏幕左下角,x轴向右为正,y轴向上为正,范围都是[0.0, 1.0].
- shader中的for循环,初始值、判断条件和步进量都必须是常量,如果shader中不确定,可以在外部传入shader字符串时写入。例如:
var fs = "for (float y = 1.0; y> 0.0; y-=
" + (1.0 / viewer.canvas.height) + "
) {}";
自定义Geometry中shader定义
Appearance类构造中可以自定义shader的vs和fs,vertexShaderSource定义vs,fragmentShaderSource定义fs;例如:
1 new Cesium.EllipsoidSurfaceAppearance({ 2 vertexShaderSource: 3 'attribute vec3 position3DHigh;\n\ 4 attribute vec3 position3DLow;\n\ 5 attribute vec2 st;\n\ 6 attribute float batchId;\n//batchId是appearance的shader必需的\ 7 varying vec3 v_positionMC; \n //顶点模型坐标传递到fs \ 8 varying vec3 v_positionEC;\n //顶点视域坐标传递到fs \ 9 varying vec3 v_positionWC;\n //顶点世界坐标传递到fs \ 10 varying vec2 v_st;\n //顶点纹理坐标传递到fs\ 11 void main()\n\ 12 {\n\ 13 vec4 p = czm_computePosition();\n\ 14 v_positionMC = position3DHigh + position3DLow; //模型坐标\n\ 15 v_positionWC = czm_model * v_positionMC; //计算世界坐标\n\ 16 v_positionPC = czm_modelView * v_positionMC; //投影坐标 17 v_positionEC = (czm_modelViewRelativeToEye * p).xyz; //position in eye coordinates\n\ 18 v_st = st;\n\ 19 gl_Position = czm_modelViewProjectionRelativeToEye * p;\n\ 20 }', 21 fragmentShaderSource: 'precision highp float;\n\ 22 varying vec3 v_positionMC; \n \ 23 varying vec3 v_positionEC;\n \ 24 varying vec3 v_positionWC;\n \ 25 varying vec2 v_st;\n\ 26 void main(void) {\n \ 27 czm_materialInput materialInput;\n \ 28 vec3 normalEC = normalize(czm_normal3D * czm_geodeticSurfaceNormal(v_positionMC, vec3(0.0), vec3(1.0)));\n \ 29 materialInput.s = v_st.s;\n \ 30 materialInput.st = v_st;\n \ 31 materialInput.str = vec3(v_st, 0.0);\n \ 32 // Convert tangent space material normal to eye space\n \ 33 materialInput.normalEC = normalEC;\n \ 34 materialInput.tangentToEyeMatrix = czm_eastNorthUpToEyeCoordinates(v_positionMC, materialInput.normalEC);\n \ 35 // Convert view vector to world space\n \ 36 vec3 positionToEyeEC = -v_positionEC;\n \ 37 materialInput.positionToEyeEC = positionToEyeEC;\n \ 38 czm_material material = czm_getMaterial(materialInput);\n \ 39 gl_FragColor = czm_phong(normalize(positionToEyeEC), material);\n \ 40 }' 41 })