shadertoy.html 23 KB

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