Kinematic 0.5.0 is out. Modeling and sculpting in the viewport, a timeline on every graph, photo and sound editing. What's new
Kinematic / Learn / Develop / Recolor a twisting torus in its shader
Develop tutorial

Recolor a twisting torus in its shader

Tutorial10 minGetting going7 stepsStarts from the Twisted Torus demo
What you will make

The Twisted Torus demo repainted in teal and violet stripes, with more and thinner stripes, by changing three lines of its material shader while it runs.

The Twisted Torus after three changed lines: teal and violet, with three times as many stripes.
The Twisted Torus after three changed lines: teal and violet, with three times as many stripes.

The Twisted Torus gets its metal look from a shader material: a short program that works out the color of every point on the surface. Change a number in it and the torus changes while it spins.

Steps

  1. Choose Help > Browse Demos..., type twisted in the search box and open Twisted Torus. A copper and gold torus twists and turns in the viewport.

    The demo running. The Objects panel lists mat under Shaders and render under Renders.
    The demo running. The Objects panel lists mat under Shaders and render under Renders.
  2. In the Objects panel, under Shaders, click mat. Hover over the viewport and press E, or click Open Shader Editor (E) in Properties. The Shader editor opens on the shader that colors the surface.

    The color shader. Line 6 sets the stripes; lines 7 and 8 hold the two stripe colors.
    The color shader. Line 6 sets the stripes; lines 7 and 8 hold the two stripe colors.
  3. In the Objects panel, under Renders, click render, so the viewport shows the torus again while you edit. Keep the editor open beside it.

  4. Change the two colors. Replace line 7 and line 8 with:

    wgsl
    let gold = vec3f(0.2, 0.9, 0.85);
    let copper = vec3f(0.45, 0.15, 0.85);

    Each color is red, green and blue, from 0 to 1. Press F5. The stripes turn teal and violet while the torus keeps turning.

    Teal and violet stripes, still following the twist.
    Teal and violet stripes, still following the twist.
  5. Add more stripes. On line 6, change 2.0 at the end of sin(in_.vertex_color.g * 2.0) to 6.0 and press F5. The stripes get thinner and there are three times as many.

    Thinner stripes from one changed number.
    Thinner stripes from one changed number.
  6. Try a softer shine: on line 14, change 80.0 to 20.0 and press F5. The bright highlight spreads wider across the surface.

  7. Choose File > Save As... and save your version under a new name, so the demo stays as it was.

On the canvas

The demo's 3DGeoTorus is turned by a 3DGeoTransform, then a 3DGeoSetMaterial puts the ShaderMaterial called mat on it. A 3DGeoSimpleRenderToImage named render draws the picture, and your changes are saved in mat's settings.

Make it yours

  • Pick your own pair of colors. Two that are far apart, like vec3f(1.0, 0.3, 0.2) and vec3f(0.1, 0.2, 0.9), show the twist best.
  • Change 4.0 on line 16 to 1.5 for a strong glow around the edges.
  • Turn the stripe count into a setting: add a struct with a stripes field above the shader, bind it as uKINCustomUser at group 2, binding 12, and use uKINCustomUser.stripes in place of 6.0. A stripes value then shows up under Uniforms in Properties.
  • Open Help > Shaders > API Reference to see every helper the shader can call.

If it goes wrong

  • The torus does not change after F5: look at the editor's Info tab. If the code has a mistake, the torus keeps its last working look until you fix it.
  • The viewport shows a small preview instead of the torus: click render under Renders in the Objects panel.
Keep going

Free and open. Yours, forever.

No account, no license key. macOS, Windows, Linux and Raspberry Pi.

Download for macOS 0.5.0