cesium绘制动态墙体

Cesium如何插入片段着色器与顶点着色器的shader

此文主要讲解如何将自己写的webgl与cesium结合起来 在线示例
1.书写自己的shader,随便先写一个最简单的片段着色器,顶点着色器的添加方法还在研究

this.fshader = `
  void main (void)
  {
	vec2 uv = gl_FragCoord.xy/vec2(1024,100);
	vec3 color = hsb2rgb(vec3(uv.x,1.0,uv.y));
	gl_FragColor = vec4(color,0.5);
	// gl_FragColor.a = material.alpha; //=>0.5
  }
`;

2.在添加图形的时候一定要使用Primitive添加,Entity生成的实例无法修改其材质,但是可以也可以使用CallbackProperty对位置进行实时修改

//先生成材质,默认fabric的type(不写),就可通过source进行传值u_time
const material = new Cesium.Material({
			fabric: {
				uniforms: {
					u_time: 0.5,
				},
				source: `
				uniform float u_time;
				vec3 hsb2rgb( vec3 c ){
					vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0), 6.0)-3.0)-1.0, 0.0, 1.0 );
					rgb = rgb*rgb*(3.0-2.0*rgb);
					return c.z * mix(vec3(1.0), u_time*rgb, c.y);
				}
				`,
			},
		});
//使用primitives.add添加
this.wall = this.viewer.scene.primitives.add(
			new Cesium.Primitive({
				geometryInstances: new Cesium.GeometryInstance({
					geometry: wall,
				}),
				appearance: new Cesium.MaterialAppearance({
					// aboveGround: true,
					// closed: false,
					material: material,
					renderState: {
						blending: Cesium.BlendingState.DISABLED, //混合
						depthTest: { enabled: true }, //深度测试
						depthMask: true,
					},
					// vertexShaderSource: this.vshader, //顶点着色器写在这里
					fragmentShaderSource: this.fshader,
				}),
				show: true,
			})
		);

这样只要修改material 的 u_time就能实时修改了,设置定时器或者通过按钮绑定都可以

setInterval(() => {
			material.uniforms.u_time = Math.random();
		}, 1000);

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值