- 可以使用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 })