shadertoy.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>and Shadertoy</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – and Shadertoy">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="/files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="/files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="/manual/resources/lesson.css">
  12. <link rel="stylesheet" href="/manual/resources/lang.css">
  13. </head>
  14. <body>
  15. <div class="container">
  16. <div class="lesson-title">
  17. <h1>and Shadertoy</h1>
  18. </div>
  19. <div class="lesson">
  20. <div class="lesson-main">
  21. <p><a href="https://shadertoy.com">Shadertoy</a> is a famous website hosting amazing shader
  22. experiments. People often ask how they can use those shaders with Three.js.</p>
  23. <p>It's important to recognize it's called Shader<strong>TOY</strong> for a reason. In general
  24. shadertoy shaders are not about best practices. Rather they are a fun challenge
  25. similar to say <a href="https://dwitter.net">dwitter</a> (write code in 140 characters) or
  26. <a href="https://js13kgames.com">js13kGames</a> (make a game in 13k or less).</p>
  27. <p>In the case of Shadertoy the puzzle is, <em>write a function that for a given pixel
  28. location outputs a color that draws something interesting</em>. It's a fun challenge
  29. and many of the result are amazing. But, it is not best practice.</p>
  30. <p>Compare <a href="https://www.shadertoy.com/view/XtsSWs">this amazing shadertoy shader that draws an entire city</a></p>
  31. <div class="threejs_center"><img src="../resources/images/shadertoy-skyline.png"></div>
  32. <p>Fullscreen on my GPU it runs at about 5 frames a second. Contrast that to
  33. <a href="https://store.steampowered.com/app/255710/Cities_Skylines/">a game like Cities: Skylines</a></p>
  34. <div class="threejs_center"><img src="../resources/images/cities-skylines.jpg" style="width: 600px;"></div>
  35. <p>This game runs 30-60 frames a second on the same machine because it uses more
  36. traditional techniques, drawing buildings made from triangles with textures on
  37. them, etc...</p>
  38. <p>Still, let's go over using a Shadertoy shader with three.js.</p>
  39. <p>This is the default shadertoy shader if you <a href="https://www.shadertoy.com/new">pick "New" on shadertoy.com</a>, at least as of January 2019.</p>
  40. <pre class="prettyprint showlinemods notranslate lang-glsl" translate="no">// By iq: https://www.shadertoy.com/user/iq
  41. // license: Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  42. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  43. {
  44. // Normalized pixel coordinates (from 0 to 1)
  45. vec2 uv = fragCoord/iResolution.xy;
  46. // Time varying pixel color
  47. vec3 col = 0.5 + 0.5*cos(iTime+uv.xyx+vec3(0,2,4));
  48. // Output to screen
  49. fragColor = vec4(col,1.0);
  50. }
  51. </pre>
  52. <p>One thing important to understand about shaders is they are written in a
  53. language called GLSL (Graphics Library Shading Language) designed for 3D math
  54. which includes special types. Above we see <code class="notranslate" translate="no">vec4</code>, <code class="notranslate" translate="no">vec2</code>, <code class="notranslate" translate="no">vec3</code> as 3 such
  55. special types. A <code class="notranslate" translate="no">vec2</code> has 2 values, a <code class="notranslate" translate="no">vec3</code> 3, a <code class="notranslate" translate="no">vec4</code> 4 values. They can be
  56. addressed in a bunch of ways. The most common ways are with <code class="notranslate" translate="no">x</code>, <code class="notranslate" translate="no">y</code>, <code class="notranslate" translate="no">z</code>, and
  57. <code class="notranslate" translate="no">w</code> as in</p>
  58. <pre class="prettyprint showlinemods notranslate lang-glsl" translate="no">vec4 v1 = vec4(1.0, 2.0, 3.0, 4.0);
  59. float v2 = v1.x + v1.y; // adds 1.0 + 2.0
  60. </pre>
  61. <p>Unlike JavaScript, GLSL is more like C/C++ where variables have to have their
  62. type declared so instead of <code class="notranslate" translate="no">var v = 1.2;</code> it's <code class="notranslate" translate="no">float v = 1.2;</code> declaring <code class="notranslate" translate="no">v</code>
  63. to be a floating point number.</p>
  64. <p>Explaining GLSL in detail is more than we can do in this article. For a quick
  65. overview see <a href="https://webglfundamentals.org/webgl/lessons/webgl-shaders-and-glsl.html">this article</a>
  66. and maybe follow that up with <a href="https://thebookofshaders.com/">this series</a>.</p>
  67. <p>It should be noted that, at least as of January 2019,
  68. <a href="https://shadertoy.com">shadertoy.com</a> only concerns itself with <em>fragment
  69. shaders</em>. A fragment shader's responsibility is, given a pixel location output
  70. a color for that pixel.</p>
  71. <p>Looking at the function above we can see the shader has an <code class="notranslate" translate="no">out</code> parameter
  72. called <code class="notranslate" translate="no">fragColor</code>. <code class="notranslate" translate="no">out</code> stands for <code class="notranslate" translate="no">output</code>. It's a parameter the function is
  73. expected to provide a value for. We need to set this to some color.</p>
  74. <p>It also has an <code class="notranslate" translate="no">in</code> (for input) parameter called <code class="notranslate" translate="no">fragCoord</code>. This is the pixel
  75. coordinate that is about to be drawn. We can use that coordinate to decide on a
  76. color. If the canvas we're drawing to is 400x300 pixels then the function will
  77. be called 400x300 times or 120,000 times. Each time <code class="notranslate" translate="no">fragCoord</code> will be a
  78. different pixel coordinate.</p>
  79. <p>There are 2 more variables being used that are not defined in the code. One is
  80. <code class="notranslate" translate="no">iResolution</code>. This is set to the resolution of the canvas. If the canvas is
  81. 400x300 then <code class="notranslate" translate="no">iResolution</code> would be 400,300 so as the pixel coordinates change
  82. that makes <code class="notranslate" translate="no">uv</code> go from 0.0 to 1.0 across and up the texture. Working with
  83. <em>normalized</em> values often makes things easier and so the majority of shadertoy
  84. shaders start with something like this.</p>
  85. <p>The other undefined variable in the shader is <code class="notranslate" translate="no">iTime</code>. This is the time since
  86. the page loaded in seconds.</p>
  87. <p>In shader jargon these global variables are called <em>uniform</em> variables. They are
  88. called <em>uniform</em> because they don't change, they stay uniform from one iteration
  89. of the shader to the next. It's important to note all of them are specific to
  90. shadertoy. They not <em>official</em> GLSL variables. They are variables the makers of
  91. shadertoy made up.</p>
  92. <p>The <a href="https://www.shadertoy.com/howto">Shadertoy docs define several more</a>. For
  93. now let's write something that handles the two being used in the shader above.</p>
  94. <p>The first thing to do is let's make a single plane that fills the canvas. If you
  95. haven't read it yet we did this in <a href="backgrounds.html">the article on backgrounds</a>
  96. so let's grab that example but remove the cubes. It's pretty short so here's the
  97. entire thing</p>
  98. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function main() {
  99. const canvas = document.querySelector('#c');
  100. const renderer = new THREE.WebGLRenderer({canvas});
  101. renderer.autoClearColor = false;
  102. const camera = new THREE.OrthographicCamera(
  103. -1, // left
  104. 1, // right
  105. 1, // top
  106. -1, // bottom
  107. -1, // near,
  108. 1, // far
  109. );
  110. const scene = new THREE.Scene();
  111. const plane = new THREE.PlaneGeometry(2, 2);
  112. const material = new THREE.MeshBasicMaterial({
  113. color: 'red',
  114. });
  115. scene.add(new THREE.Mesh(plane, material));
  116. function resizeRendererToDisplaySize(renderer) {
  117. const canvas = renderer.domElement;
  118. const width = canvas.clientWidth;
  119. const height = canvas.clientHeight;
  120. const needResize = canvas.width !== width || canvas.height !== height;
  121. if (needResize) {
  122. renderer.setSize(width, height, false);
  123. }
  124. return needResize;
  125. }
  126. function render() {
  127. resizeRendererToDisplaySize(renderer);
  128. renderer.render(scene, camera);
  129. requestAnimationFrame(render);
  130. }
  131. requestAnimationFrame(render);
  132. }
  133. main();
  134. </pre>
  135. <p>As <a href="backgrounds.html">explained in the backgrounds article</a> an
  136. <a href="/docs/#api/en/cameras/OrthographicCamera"><code class="notranslate" translate="no">OrthographicCamera</code></a> with these parameters and a 2 unit plane will fill the
  137. canvas. For now all we'll get is a red canvas as our plane is using a red
  138. <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a>.</p>
  139. <p></p><div translate="no" class="threejs_example_container notranslate">
  140. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadertoy-prep.html"></iframe></div>
  141. <a class="threejs_center" href="/manual/examples/shadertoy-prep.html" target="_blank">click here to open in a separate window</a>
  142. </div>
  143. <p></p>
  144. <p>Now that we have something working let's add the shadertoy shader. </p>
  145. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const fragmentShader = `
  146. #include &lt;common&gt;
  147. uniform vec3 iResolution;
  148. uniform float iTime;
  149. // By iq: https://www.shadertoy.com/user/iq
  150. // license: Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  151. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  152. {
  153. // Normalized pixel coordinates (from 0 to 1)
  154. vec2 uv = fragCoord/iResolution.xy;
  155. // Time varying pixel color
  156. vec3 col = 0.5 + 0.5*cos(iTime+uv.xyx+vec3(0,2,4));
  157. // Output to screen
  158. fragColor = vec4(col,1.0);
  159. }
  160. void main() {
  161. mainImage(gl_FragColor, gl_FragCoord.xy);
  162. }
  163. `;
  164. </pre>
  165. <p>Above we declared the 2 uniform variables we talked about. Then we inserted the
  166. shader GLSL code from shadertoy. Finally we called <code class="notranslate" translate="no">mainImage</code> passing it
  167. <code class="notranslate" translate="no">gl_FragColor</code> and <code class="notranslate" translate="no">gl_FragCoord.xy</code>. <code class="notranslate" translate="no">gl_FragColor</code> is an official WebGL
  168. global variable the shader is responsible for setting to whatever color it wants
  169. the current pixel to be. <code class="notranslate" translate="no">gl_FragCoord</code> is another official WebGL global
  170. variable that tells us the coordinate of the pixel we're currently choosing a
  171. color for.</p>
  172. <p>We then need to setup three.js uniforms so we can supply values to the shader.</p>
  173. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const uniforms = {
  174. iTime: { value: 0 },
  175. iResolution: { value: new THREE.Vector3() },
  176. };
  177. </pre>
  178. <p>Each uniform in THREE.js has <code class="notranslate" translate="no">value</code> parameter. That value has to match the type
  179. of the uniform.</p>
  180. <p>Then we pass both the fragment shader and uniforms to a <a href="/docs/#api/en/materials/ShaderMaterial"><code class="notranslate" translate="no">ShaderMaterial</code></a>.</p>
  181. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const material = new THREE.MeshBasicMaterial({
  182. - color: 'red',
  183. -});
  184. +const material = new THREE.ShaderMaterial({
  185. + fragmentShader,
  186. + uniforms,
  187. +});
  188. </pre>
  189. <p>and before rendering we need to set the values of the uniforms</p>
  190. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-function render() {
  191. +function render(time) {
  192. + time *= 0.001; // convert to seconds
  193. resizeRendererToDisplaySize(renderer);
  194. + const canvas = renderer.domElement;
  195. + uniforms.iResolution.value.set(canvas.width, canvas.height, 1);
  196. + uniforms.iTime.value = time;
  197. renderer.render(scene, camera);
  198. requestAnimationFrame(render);
  199. }
  200. </pre>
  201. <blockquote>
  202. <p>Note: I have no idea why <code class="notranslate" translate="no">iResolution</code> is a <code class="notranslate" translate="no">vec3</code> and what's in the 3rd value
  203. <a href="https://www.shadertoy.com/howto">is not documented on shadertoy.com</a>. It's
  204. not used above so just setting it to 1 for now. ¯\_(ツ)_/¯</p>
  205. </blockquote>
  206. <p></p><div translate="no" class="threejs_example_container notranslate">
  207. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadertoy-basic.html"></iframe></div>
  208. <a class="threejs_center" href="/manual/examples/shadertoy-basic.html" target="_blank">click here to open in a separate window</a>
  209. </div>
  210. <p></p>
  211. <p>This <a href="https://www.shadertoy.com/new">matches what we see on Shadertoy for a new shader</a>,
  212. at least as of January 2019 😉. What's the shader above doing? </p>
  213. <ul>
  214. <li><code class="notranslate" translate="no">uv</code> goes from 0 to 1. </li>
  215. <li><code class="notranslate" translate="no">cos(uv.xyx)</code> gives us 3 cosine values as a <code class="notranslate" translate="no">vec3</code>. One for <code class="notranslate" translate="no">uv.x</code>, another for <code class="notranslate" translate="no">uv.y</code> and another for <code class="notranslate" translate="no">uv.x</code> again.</li>
  216. <li>Adding in the time, <code class="notranslate" translate="no">cos(iTime+uv.xyx)</code> makes them animate.</li>
  217. <li>Adding in <code class="notranslate" translate="no">vec3(0,2,4)</code> as in <code class="notranslate" translate="no">cos(iTime+uv.xyx+vec3(0,2,4))</code> offsets the cosine waves</li>
  218. <li><code class="notranslate" translate="no">cos</code> goes from -1 to 1 so the <code class="notranslate" translate="no">0.5 * 0.5 + cos(...)</code> converts from -1 &lt;-&gt; 1 to 0.0 &lt;-&gt; 1.0</li>
  219. <li>the results are then used as the RGB color for the current pixel</li>
  220. </ul>
  221. <p>A minor change will make it easier to see the cosine waves. Right now <code class="notranslate" translate="no">uv</code> only
  222. goes from 0 to 1. A cosine repeats at 2π so let's make it go from 0 to 40 by
  223. multiplying by 40.0. That should make it repeat about 6.3 times.</p>
  224. <pre class="prettyprint showlinemods notranslate lang-glsl" translate="no">-vec3 col = 0.5 + 0.5*cos(iTime+uv.xyx+vec3(0,2,4));
  225. +vec3 col = 0.5 + 0.5*cos(iTime+uv.xyx*40.0+vec3(0,2,4));
  226. </pre>
  227. <p>Counting below I see about 6.3 repeats. We can see the blue between the red
  228. since it's offset by 4 via the <code class="notranslate" translate="no">+vec3(0,2,4)</code>. Without that the blue and red
  229. would overlap perfectly making purple.</p>
  230. <p></p><div translate="no" class="threejs_example_container notranslate">
  231. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadertoy-basic-x40.html"></iframe></div>
  232. <a class="threejs_center" href="/manual/examples/shadertoy-basic-x40.html" target="_blank">click here to open in a separate window</a>
  233. </div>
  234. <p></p>
  235. <p>Knowing how simple the inputs are and then seeing results like
  236. <a href="https://www.shadertoy.com/view/MdXGW2">a city canal</a>,
  237. <a href="https://www.shadertoy.com/view/4ttSWf">a forest</a>,
  238. <a href="https://www.shadertoy.com/view/ld3Gz2">a snail</a>,
  239. <a href="https://www.shadertoy.com/view/4tBXR1">a mushroom</a>
  240. make the challenge all that much more impressive. Hopefully they also make it
  241. clear why it's not generally the right approach vs the more traditional ways of
  242. making scenes from triangles. The fact that so much math has to be put into
  243. computing the color of every pixel means those examples run very slow.</p>
  244. <p>Some shadertoy shaders take textures as inputs like
  245. <a href="https://www.shadertoy.com/view/MsXSzM">this one</a>. </p>
  246. <pre class="prettyprint showlinemods notranslate lang-glsl" translate="no">// By Daedelus: https://www.shadertoy.com/user/Daedelus
  247. // license: Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  248. #define TIMESCALE 0.25
  249. #define TILES 8
  250. #define COLOR 0.7, 1.6, 2.8
  251. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  252. {
  253. vec2 uv = fragCoord.xy / iResolution.xy;
  254. uv.x *= iResolution.x / iResolution.y;
  255. vec4 noise = texture2D(iChannel0, floor(uv * float(TILES)) / float(TILES));
  256. float p = 1.0 - mod(noise.r + noise.g + noise.b + iTime * float(TIMESCALE), 1.0);
  257. p = min(max(p * 3.0 - 1.8, 0.1), 2.0);
  258. vec2 r = mod(uv * float(TILES), 1.0);
  259. r = vec2(pow(r.x - 0.5, 2.0), pow(r.y - 0.5, 2.0));
  260. p *= 1.0 - pow(min(1.0, 12.0 * dot(r, r)), 2.0);
  261. fragColor = vec4(COLOR, 1.0) * p;
  262. }
  263. </pre>
  264. <p>Passing a texture into a shader is similar to
  265. <a href="textures.html">passing one into a normal material</a> but we need to set
  266. up the texture on the uniforms.</p>
  267. <p>First we'll add the uniform for the texture to the shader. They're referred to
  268. as <code class="notranslate" translate="no">sampler2D</code> in GLSL.</p>
  269. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const fragmentShader = `
  270. #include &lt;common&gt;
  271. uniform vec3 iResolution;
  272. uniform float iTime;
  273. +uniform sampler2D iChannel0;
  274. ...
  275. </pre>
  276. <p>Then we can load a texture like we covered <a href="textures.html">here</a> and assign the uniform's value.</p>
  277. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">+const loader = new THREE.TextureLoader();
  278. +const texture = loader.load('resources/images/bayer.png');
  279. +texture.minFilter = THREE.NearestFilter;
  280. +texture.magFilter = THREE.NearestFilter;
  281. +texture.wrapS = THREE.RepeatWrapping;
  282. +texture.wrapT = THREE.RepeatWrapping;
  283. const uniforms = {
  284. iTime: { value: 0 },
  285. iResolution: { value: new THREE.Vector3() },
  286. + iChannel0: { value: texture },
  287. };
  288. </pre>
  289. <p></p><div translate="no" class="threejs_example_container notranslate">
  290. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadertoy-bleepy-blocks.html"></iframe></div>
  291. <a class="threejs_center" href="/manual/examples/shadertoy-bleepy-blocks.html" target="_blank">click here to open in a separate window</a>
  292. </div>
  293. <p></p>
  294. <p>So far we've been using Shadertoy shaders as they are used on
  295. <a href="https://shadertoy.com">Shadertoy.com</a>, namely drawing to cover the canvas.
  296. There's no reason we need to limit it to just that use case though. The
  297. important part to remember is the functions people write on shadertoy generally
  298. just take a <code class="notranslate" translate="no">fragCoord</code> input and a <code class="notranslate" translate="no">iResolution</code>. <code class="notranslate" translate="no">fragCoord</code> does not have to
  299. come from pixel coordinates, we could use something else like texture
  300. coordinates instead and could then use them kind of like other textures. This
  301. technique of using a function to generate textures is often called a
  302. <a href="https://www.google.com/search?q=procedural+texture"><em>procedural texture</em></a>.</p>
  303. <p>Let's change the shader above to do this. The simplest thing to do might be to
  304. take the texture coordinates that three.js normally supplies, multiply them by
  305. <code class="notranslate" translate="no">iResolution</code> and pass that in for <code class="notranslate" translate="no">fragCoords</code>. </p>
  306. <p>To do that we add in a <em>varying</em>. A varying is a value passed from the vertex
  307. shader to the fragment shader that gets interpolated (or varied) between
  308. vertices. To use it in our fragment shader we declare it. Three.js refers to its
  309. texture coordinates as <code class="notranslate" translate="no">uv</code> with the <code class="notranslate" translate="no">v</code> in front meaning <em>varying</em>.</p>
  310. <pre class="prettyprint showlinemods notranslate lang-glsl" translate="no">...
  311. +varying vec2 vUv;
  312. void main() {
  313. - mainImage(gl_FragColor, gl_FragCoord.xy);
  314. + mainImage(gl_FragColor, vUv * iResolution.xy);
  315. }
  316. </pre>
  317. <p>Then we need to also provide our own vertex shader. Here is a fairly common
  318. minimal three.js vertex shader. Three.js declares and will provide values for
  319. <code class="notranslate" translate="no">uv</code>, <code class="notranslate" translate="no">projectionMatrix</code>, <code class="notranslate" translate="no">modelViewMatrix</code>, and <code class="notranslate" translate="no">position</code>.</p>
  320. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const vertexShader = `
  321. varying vec2 vUv;
  322. void main() {
  323. vUv = uv;
  324. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  325. }
  326. `;
  327. </pre>
  328. <p>We need to pass the vertex shader to the <a href="/docs/#api/en/materials/ShaderMaterial"><code class="notranslate" translate="no">ShaderMaterial</code></a></p>
  329. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const material = new THREE.ShaderMaterial({
  330. vertexShader,
  331. fragmentShader,
  332. uniforms,
  333. });
  334. </pre>
  335. <p>We can set the <code class="notranslate" translate="no">iResolution</code> uniform value at init time since it will no longer change.</p>
  336. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const uniforms = {
  337. iTime: { value: 0 },
  338. - iResolution: { value: new THREE.Vector3() },
  339. + iResolution: { value: new THREE.Vector3(1, 1, 1) },
  340. iChannel0: { value: texture },
  341. };
  342. </pre>
  343. <p>and we no longer need to set it at render time</p>
  344. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const canvas = renderer.domElement;
  345. -uniforms.iResolution.value.set(canvas.width, canvas.height, 1);
  346. uniforms.iTime.value = time;
  347. </pre>
  348. <p>Otherwise I copied back in the original camera and code that sets up 3 rotating
  349. cubes from <a href="responsive.html">the article on responsiveness</a>. The result:</p>
  350. <p></p><div translate="no" class="threejs_example_container notranslate">
  351. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadertoy-as-texture.html"></iframe></div>
  352. <a class="threejs_center" href="/manual/examples/shadertoy-as-texture.html" target="_blank">click here to open in a separate window</a>
  353. </div>
  354. <p></p>
  355. <p>I hope this at least gets you started on how to use a shadertoy shader with
  356. three.js. Again, it's important to remember that most shadertoy shaders are an
  357. interesting challenge (draw everything with a single function) rather than the
  358. recommended way to actually display things in a performant way. Still, they are
  359. amazing, impressive, beautiful, and you can learn a ton by seeing how they work.</p>
  360. </div>
  361. </div>
  362. </div>
  363. <script src="/manual/resources/prettify.js"></script>
  364. <script src="/manual/resources/lesson.js"></script>
  365. </body></html>