# Custom WebGL code

**URL:** <https://community.cesium.com/t/custom-webgl-code/7898>\
**Category:** CesiumJS\
**Created:** [January 16, 2019, 1:43am UTC](https://community.cesium.com/t/custom-webgl-code/7898 "2019-01-16T01:43:13Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/sam/32/30333_2.png) [@Sam](https://community.cesium.com/u/Sam)\
**Post date:** [January 16, 2019, 1:43am UTC](https://community.cesium.com/t/custom-webgl-code/7898/1 "2019-01-16T01:43:13Z")

</div>

Hi Cesium team,

**1. A concise explanation of the problem you’re experiencing.**

I am trying to pass custom vertex shader code to a material. I know you can pass in fragment shader code in the fabric json under the “source” field. But I also need pass in a vertex shader because I am creating an animation.

**2. A minimal code example. If you’ve found a bug, this helps us reproduce and repair it.**

I attached an image of what I have. I am passing in the fragment shader using fabrics like the ones from here: [https://github.com/AnalyticalGraphicsInc/cesium-materials-pack](https://github.com/AnalyticalGraphicsInc/cesium-materials-pack).

I am also using tweens to update a time parameter to create the animation (Like the erosion example from the link).

**3. Context. Why do you need to do this? We might know a better way to accomplish your goal.**

I am trying to visualize wind data similar to what they have here: [https://earth.nullschool.net/](https://earth.nullschool.net/) . I need to update the location of the wind particles from frame to frame in order to run the animation. I can’t update them using a fragment shader so I am trying a find a way to make the particles move.

The work around I have now is that I am using tweens to update a time parameter (a uniform in the fragment shader). The fragment shader uses this to calculate which pixels should be shaded to simulate the wind movements. However, this method is inefficient because I essentially have to backtrack through 100 frames to calculate the new position of the wind particles at each frame.

**4. The Cesium version you’re using, your operating system and browser.**

Windows 10, Cesium 1.50, Chrome

Thanks for your help

![Capture1.PNG](https://global.discourse-cdn.com/cesium/original/1X/acc70b55862d5c4ca68a21d4d7148ceb92fb9851.png)

![Capture2.PNG](https://global.discourse-cdn.com/cesium/original/1X/ec187bc7db15dcd04875d05322c77d0860123f3a.png)

---

<div class="post-metadata">

**Author:** ![omar](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/omar/32/1999_2.png) [@omar](https://community.cesium.com/u/omar)\
**Post date:** [January 16, 2019, 4:07pm UTC](https://community.cesium.com/t/custom-webgl-code/7898/2 "2019-01-16T16:07:41Z")

</div>

You can create a custom frag/vertex shader by using a custom Appearance:

[https://cesiumjs.org/Cesium/Build/Documentation/Appearance.html?classFilter=Appear](https://cesiumjs.org/Cesium/Build/Documentation/Appearance.html?classFilter=Appear)

This is a good reference on appearances in general:

> **[Documentation | cesium.com](https://cesium.com/docs/)**
>
> Cesium is geospatial. Use Cesium ion's simple workflow to create 3D maps of your geospatial data for visualization, analysis, and sharing. For developers, use the open-source CesiumJS library to create custom 3D mapping apps.

I think the good news is that the WebGL wind simulation you linked to is open source. It might be worth looking at it to see how they set up the simulation:

> **[cambecc/earth](https://github.com/cambecc/earth)**
>
> a project to visualize global weather conditions. Contribute to cambecc/earth development by creating an account on GitHub.

I imagine the simulation logic is done in the shader itself, and it renders onto a frame buffer that is passed back as input in the next frame. If so, this can be done in Cesium but rendering to a texture isn’t very well documented right now since it’s a less common use case.

If you make progress on this it’d be great to post a demo or your tips here! I know this is something that’s often requested.

---

<div class="post-metadata">

**Author:** ![Sam](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/sam/32/30333_2.png) [@Sam](https://community.cesium.com/u/Sam)\
**Post date:** [January 16, 2019, 9:26pm UTC](https://community.cesium.com/t/custom-webgl-code/7898/3 "2019-01-16T21:26:08Z")

</div>

Thank you so much for your help Omar. I’ll definitely post a demo if I make good progress.

The problem I have now is that I can’t pass the wind data to the vertex shader if I use Appearances. Before, when I only used a fragment shader, I could pass an image that had the wind data encoded in it through the fabric json of the Material. So I just specified the variable name and the image location under the uniforms field (like in the screenshot of my code). Then, I had “uniform sampler2D WindSpeed;” in the fragment shader which stored the texture.

Do you know how I can pass the data to the vertex shader if I use a custom Appearance? I can’t find any examples so I haven’t been able to figure it out.

I think zwcloud had a similar issue here:

[https://groups.google.com/forum/#!topic/cesium-dev/t1huyGykzO4](https://groups.google.com/forum/#!topic/cesium-dev/t1huyGykzO4)

Thanks

---

<div class="post-metadata">

**Author:** ![Sam](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/sam/32/30333_2.png) [@Sam](https://community.cesium.com/u/Sam)\
**Post date:** [January 16, 2019, 9:36pm UTC](https://community.cesium.com/t/custom-webgl-code/7898/4 "2019-01-16T21:36:11Z")

</div>

Also, the data that I am passing in is the wind direction at each coordinate. As of now, I have the data encoded as an image where the red and green pixels depict the wind direction. But the data doesn’t have to be formatted as an image as long as I can somehow pass it in.

---

<div class="post-metadata">

**Author:** ![omar](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/omar/32/1999_2.png) [@omar](https://community.cesium.com/u/omar)\
**Post date:** [January 17, 2019, 4:00pm UTC](https://community.cesium.com/t/custom-webgl-code/7898/5 "2019-01-17T16:00:54Z")

</div>

An Appearance does take a vertexShaderSource in the same way that it takes a fragmentShaderSource. This is the most recent forum post I can think of that has an example of using both a custom vertex shader and a fragment shader:

[https://groups.google.com/d/msg/cesium-dev/PWMu8pehSPw/z5rrN0jTCAAJ](https://groups.google.com/d/msg/cesium-dev/PWMu8pehSPw/z5rrN0jTCAAJ)

You should be able to specify the uniforms and pass in the wind data as an image as well, which I think is the most efficient way to do it (it would be too much to send a giant array of numbers instead).

---

<div class="post-metadata">

**Author:** ![Sam](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/sam/32/30333_2.png) [@Sam](https://community.cesium.com/u/Sam)\
**Post date:** [January 17, 2019, 5:23pm UTC](https://community.cesium.com/t/custom-webgl-code/7898/6 "2019-01-17T17:23:50Z")

</div>

I was under the assumption that you can only pass in uniforms using fabrics if you use a Material (when the fragment shader code is passed through the “source” parameter of the fabrics json). But not if you use an Appearance.

I’ll look into it more carefully today. I’ll post what I find.

Thanks Omar.

---

<div class="post-metadata">

**Author:** ![Sam](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/sam/32/30333_2.png) [@Sam](https://community.cesium.com/u/Sam)\
**Post date:** [January 23, 2019, 4:18pm UTC](https://community.cesium.com/t/custom-webgl-code/7898/7 "2019-01-23T16:18:55Z")

</div>

Hi Omar. I wasn’t able to pass in Uniforms to the fragment/vertex shaders using Appearances. I can only do that with Materials (like the example from my code).

Do you know of any other examples using custom shaders for Appearances that I can refer to? Thanks.

---

<div class="post-metadata">

**Author:** ![omar](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/omar/32/1999_2.png) [@omar](https://community.cesium.com/u/omar)\
**Post date:** [January 25, 2019, 3:46pm UTC](https://community.cesium.com/t/custom-webgl-code/7898/8 "2019-01-25T15:46:07Z")

</div>

Can you post a Sandcastle example of the attempt(s) you’ve made? I’d be happy to take a look.

---

<div class="post-metadata">

**Author:** ![Sam](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/sam/32/30333_2.png) [@Sam](https://community.cesium.com/u/Sam)\
**Post date:** [January 25, 2019, 10:01pm UTC](https://community.cesium.com/t/custom-webgl-code/7898/9 "2019-01-25T22:01:14Z")

</div>

[Here](https://cesiumjs.org/Cesium/Build/Apps/Sandcastle/#c=dVRra9swFP0rIl/iQKqkG4OtebCSpqWw0pGGbjCPVpGvEzFZMnq4S0f++yTbcuyG6ot9dc85uvfo2gVRqGDwAgrNkIAXtADNbIYfy72oT8twIYUhTIDqDyaxiEXhWHpHkpL1HAsrWCpVhlIuiUGaZDmHheRSlWj6mj1lxIBihCMfbMHc1XHUTt6K3BrUiQYoFv9igdzqyDQvs6B4BSmx/CjclZmgSqQA+gFRKVXimB0I1qas1qNCAicsTa0Gh3XEj9EYj4ft9obI7XhLOiTC8x1xlDH+VKcUGKtEg3C7h+fysNrtUDT+wUSy3uf+wP4atOlPTjGNCQtCd4BJkjQ9vyc3RLWHKdkoRtFFiP0y/rwL9C73iKyvWXf5frU8cUnX+DF9aAloaRX1h1XDUyUOzo3jWCmghogth1uh3QuF7lzegMzAqH3IRlFdybZOOPEWfBXUAi9qFd4cdey+weNUyewKtgpAR2fnn8ubP/tSPurIB4NWcwUoA3+vnUHuG2gUl5yzXEuWPFiVEgqXeQ5E+dLx43K1Xv58ur5f3V2uay8G3ozKDedH+RFiTUEAzhXLmGEFaH/lUavJ7yHz1otgkr+wXyfG/q5rJ01FXe/er7xtItnIAm6UtCJx9JRw3Z6YMKqn4+X99eMVVXM+CP0PKwMQ6g17U232HOZV6ivLcqkMsopHGI8MuIlzYnq0sfQPGEy19r5NR4E0TViBWDKLe29+YnEPUU60dpnUcv7AXiHuzacjh+/Q3M8sYWJ77y6Wk72H7M7n36pNjPF05MJTlpGSb4hqKf4H)is an example of what I have when I only use the shader supplied to the material in the fabric json. I am just passing a float (sampleColor) as a uniform and it is used by the shader to set the value of the green pixels. This method works fine but I also need a vertex shader.

[Here](https://cesiumjs.org/Cesium/Build/Apps/Sandcastle/#c=xVZtU+M2EP4rO9wHHA4cOK6dgwBTJiRXZqDHkBzXmboDir1O1NqSR1ISQof/3pUsx85bvzYfwNLuPvv2aKUZUzDjOEcFlyBwDl3UfJqHT24v2I/dsiuFYVyg2m91IhGJ9kEkurLgmEAvy3ihJU8GU5WyGK+LApliIkYYTFiCSkOqZO5x93UkBnKqSBrLBEO4ThICmQqeSpWDZnmRYVdmUoGRYFAb4CncQszIq0KWADchDCcIo+kYuMfOKDT4+QzmExQwRzBqYc2nGpuIkTho2+BnlPLTgNJ9iQQzRvHR1CDMMD6FQmpuuBSnN7/y8aTzH/I7OV8XfwJtVvbSTDIDI2biyW3S8a4XXIxLsNlzBXff7eyU9dZkn0jm/ESiqlrpp5Fp6Yt6Ajl1LWhF4p9IAP3I/jMUlHr8lj/HMi8ozAfvKChb69QakZHyalXg41oZoP6120sZcAE5tTijRkuVcKoL6nX4noUPbDBO1ZLuETNm+AyHsrdAOICiFb4u3jrb4HGB28CJM5fgS2R3xtlzlaPPfOnsQcm/MLaSDbdk/h6Jl4owfU+Y/7uDLnzKVnGW3QrqH6ys6hbaGARhs8wVufzkb+iqXa5ObyhRuxwjFcTw2B/h35w0aGZ36ACD4/C45T9P6LNlKfOBpwmm0L/u9p773x5/XD/elCE0vFtUSnTOVBJU2zXkIbg/Jw78qJI7bBQJT6ucVjINNeHaQoa6s1VsvHyH1M47F4DVcRHU/Cee0cSboTJgmBijoN4UlMISxOdmh4wloRNuc7PRAMeFTT3vxZHvntH8ePVMRaYNtcNMvhdO2K3pvtaf7Y635mTnvc3dlFN2LlWWNHNYGXXOrcvgaI3Sm2ls2qztLKNp0riu6qVno7n3O8EKfp1Mxbm762G54c95X7FxeXu45n5e2odEo9TeBx+X3kLMudYUW127kGXFhLV8bh8w07gT3kZaTKQYB/XRWsu2VSPXmE1Cb4vYHYXGJPCH4zj8yWK8v7ga0O1bXtNVocIfXCTDRYEEsz+ka3O/s6mzrHiXxRMMWZIs67wL7hD83EnZSPEYzqu1/Rnr7xx22taafs7pVXv7a9745zbLWvxeftK/91Z1e4Ii2tJhyfBWaONeGSvPlq8oc6Trv5IGgfc39gJy0lB/rNAqu6AR3tJVneNSP7TvjhscK6SDeHTyxfXo6KycY+XqzA3LGs4ePXztW7KYGnH38yl86j0Oe7/bmXpf0fy9ZYvR8hwo322hjlFgWCiec3uDadvYoJHkQyVZr0VVJNuWPzYK+6ePndUPupXa7Y68WUQ2kjP8quRUJGSeMjpSjZpUhNwkka2vJVFQsnmzkOXj0r8lz+lB19BI6VDlNE/XdPqDBrksoi0lQFnMvcO9C20WGV6VSr/wvKDBC1OVBWHYNkg8tWO3PZrGf6MJY61tHy7aldFFwmfAk8tob+3NHO1BnDGtSZJOs2xAkyLau7pok/6KGb0BaLSPv1F+GVtYlcnJ1V25GYbhRZuWm1ZGymzEVAPxXw)is what I am trying to do using an Appearance. For this example, I took the vertex and fragment shaders from EllipsoidSurfaceAppearance. I then try to pass in the same uniform like I did with the first example but I get an error. I don’t get the error if I don’t read the uniform in the shaders.

My goal is to get cesium to render the vertex shader and fragment shader while passing in uniforms.

Thank you so much Omar.

---

<div class="post-metadata">

**Author:** ![omar](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/omar/32/1999_2.png) [@omar](https://community.cesium.com/u/omar)\
**Post date:** [January 28, 2019, 3:27pm UTC](https://community.cesium.com/t/custom-webgl-code/7898/10 "2019-01-28T15:27:40Z")

</div>

Thanks for the code examples! So here’s what’s going on. When you create a uniform, CesiumJS does a lot of setup for you, including figuring out the uniform’s type and assigning a name. In this case, it’s appending “\_0” to the name of the uniform:

[https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Scene/Material.js#L937](https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Scene/Material.js#L937)

So in order to use your uniform in your shader, you just have to remove your own declaration of it (since Cesium injects it for you) and then just use it as sampleColor\_0. [Here’s](https://cesiumjs.org/Cesium/Build/Apps/Sandcastle/#c=rVZtU+M2EP4rO9wHHA4cOK6dgwBTJiRXZqDHkBzXmboDir1O1NqSR1ISQof/3pUsx85bPzUfwNKunt1n36QZUzDjOEcFlyBwDl3UfJqHT24v2I/dsiuFYVyg2m91IhGJ9kEkurLgmEAvy3ihJU8GU5WyGK+LApliIkYYTFiCSkOqZO5x93UkBnKqSBrLBEO4ThICmQqeSpWDZnmRYVdmUoGRYFAb4CncQszIqkKWADchDCcIo+kYuMfOyDX4+QzmExQwRzBqYY9PNTYRI3HQts7PiPLTgOi+RIIZo/hoahBmGJ9CITU3XIrTm1/5eNL5D/mdnK+LP4E2K3tpJpmBETPx5DbpeNMLLsYl2Oy5grvvdnbKemuyTyRzdhwexR1yykzQisQ/kQD6kc5nKIhe/JY/xzIvyJUHDxaU6XNqDeukvMocPq5RhfrXbi9lwAXklMaMkilVwok76nX4noUPrDNO1RbWI2bM8BkOZW+BcABFK3xdvHW2weMCt4FTXVyCD4PdGWfPFUfPfGnsQcm/MLaSDbN0/D0SL1VR9H1R/L9Z2pYj5yDxUZxlt4IyBCurOknWiqDOYJkLY/nJ39DFs1yd3hAVuxwjUTY89o34m5MGTf8PHWBwHB63/OcJfbZsUXzgaYIp9K+7vef+t8cf1483pQsN6xaV2nTOVBJU2zXkIbg/Jw78qJI7bBQJTytOK0xDTbg2VKHubBUbL98htVPLOWB1nAd1hVMl0dyaoTJgmBijMKALorAE8dzsqLBl5oTbzGwkwGV7U89bceV1z2gKvPpaRKYNpcNMvhdO2K0Lei0/2w1v5WSntuVuylk5lypLmhxWBpYz6xgcrRXtJo3NM2s7S2+aZVxH9dJXo7n3O8EKfk2mqrm762G54Tu5r9i4vANccj8vz4dURqmd6h+X1kLMudbkWx27kGXFhLU8tw+YadwJbz0tJlKMg7q11ti2auQas1nQ2zx2rdC4e56r9jgOf7Io7y9+gvvrtgpV+IOLZLgokID2h3T97Xc2dZYx77J4giFLkmWkd8Edgp88KRspHsN5tbY/Y+2dw86ztaa/p/Xqeftr3tznlmUtfi8/6d97q7oFQVHhUrtkeCu0ca+FlefHV5Q50jVeSYPA2xt7ARlpqD9WaNW5oOHe0lTNcakf2vfDDY4VUisenXxxOTo6KydZuTpz47KGs82Hr31bLqZG3P0MCp96j8Pe73aq3leF/t6ywWhVt7h7a4U6RoFhoXjO7S2lbWKDBsmHSrIeiypINi1/bAT2T+87qx9mK7Hb7XkziGwkZ/hVyalI6HjKqKkaMakKcrOIbHxtEQVlNW8Gsnwk+jfhOT3MGhoptVVOE3VNpz9oFJdFtKEEKIO5d7h3oc0iw6tS6ReeFzR6YaqyIAzbBqlO7eBtj6bx32jCWGubh4t2degi4TPgyWW0t/b2jfYgzpjWJEmnWTagWRHtXV20SX/lGL36aLiPvxG/jC2syuTk6q7cDMPwok3LzVNGymzEVAPxXw) a working example. This is an area of Cesium that isn’t very well documented, so I appreciate you asking this question and providing your code samples. I’m sure others in the community will find this very helpful.

---

<div class="post-metadata">

**Author:** ![Sam](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/sam/32/30333_2.png) [@Sam](https://community.cesium.com/u/Sam)\
**Post date:** [January 29, 2019, 10:17pm UTC](https://community.cesium.com/t/custom-webgl-code/7898/11 "2019-01-29T22:17:51Z")

</div>

Thank you so much Omar. I wouldn’t have been able to figure it out without your help.

Now I am trying to update the position of the wind particles from frame to frame. My plan is to to render onto a frame buffer and pass it back as input to the next frame like you mentioned last week. Do you know of any examples or documentation that I can refer to? Thanks Omar.

---

<div class="post-metadata">

**Author:** ![omar](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/omar/32/1999_2.png) [@omar](https://community.cesium.com/u/omar)\
**Post date:** [January 29, 2019, 11:20pm UTC](https://community.cesium.com/t/custom-webgl-code/7898/12 "2019-01-29T23:20:56Z")

</div>

I’ve been trying to put together an example of that for a while now. Doing this is rather tricky at the time of writing. I think you’d have to either create your own DrawCommand ([https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Renderer/DrawCommand.js](https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Renderer/DrawCommand.js)) or pass in a flag to make the Primitive modify the draw command it sends to draw to a custom framebuffer. There’s not a lot of documentation on this, but this blog post is a helpful reference:

> **[Graphics Tech in Cesium - Rendering a Frame | cesium.com](https://cesium.com/blog/2015/05/14/graphics-tech-in-cesium/)**
>
> This article traces through Cesium’s Scene.render to explain how Cesium 1.9 renders a frame using its WebGL renderer. Put a breakpoint in Scene.render, run ...

And possibly this one too:

> **[Graphics Tech in Cesium - Renderer Architecture | cesium.com](https://cesium.com/blog/2015/05/15/graphics-tech-in-cesium-architecture/)**
>
> Cesium is built on a custom WebGL engine - the Renderer - that we started developing in March 2011 when the WebGL 1.0 spec was released. I consider this a 4...

---

<div class="post-metadata">

**Author:** ![Sam](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/sam/32/30333_2.png) [@Sam](https://community.cesium.com/u/Sam)\
**Post date:** [January 30, 2019, 1:33am UTC](https://community.cesium.com/t/custom-webgl-code/7898/13 "2019-01-30T01:33:50Z")

</div>

Thanks Omar. I really appreciate you taking the time trying to create an example. If I end up getting it to work, I’ll put together a Sandcastle example and post it here.

Just out of curiosity, do you think I would have to modify Cesium’s source code to do what you described?

---

<div class="post-metadata">

**Author:** ![omar](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/omar/32/1999_2.png) [@omar](https://community.cesium.com/u/omar)\
**Post date:** [January 30, 2019, 1:51pm UTC](https://community.cesium.com/t/custom-webgl-code/7898/14 "2019-01-30T13:51:47Z")

</div>

You’d have to modify the source code in order to pass in this new flag to a Primitive to make it modify its draw command, but I do think it’s the easiest way to do this (and it might be a great contribution to open a PR back into CesiumJS!)

That way I think relieves the burden from having to figure out how to construct the DrawCommand and send it to the renderer yourself, and leverages the existing pipeline.
