# Rounded cube with checkerboard texture

**URL:** <https://discourse.threejs.org/t/rounded-cube-with-checkerboard-texture/30397>\
**Category:** Questions\
**Tags:** geometry\
**Created:** [September 30, 2021, 12:14pm UTC](https://discourse.threejs.org/t/rounded-cube-with-checkerboard-texture/30397 "2021-09-30T12:14:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![blackInk](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@blackInk](https://discourse.threejs.org/u/blackInk)\
**Post date:** [September 30, 2021, 12:14pm UTC](https://discourse.threejs.org/t/rounded-cube-with-checkerboard-texture/30397/1 "2021-09-30T12:14:52Z")

</div>

Hey,

I just found this codepen which maps a checkerboard coloring on cells of a plane-geometry.  
https://codepen.io/jesserap/embed/preview/EXveVa?default-tabs=js%2Cresult&height=300&host=https%3A%2F%2Fcodepen.io&slug-hash=EXveVa

If I want to do same same but to an rounded-cube geometry… ?

Kinda goldfish here  
B

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [September 30, 2021, 12:24pm UTC](https://discourse.threejs.org/t/rounded-cube-with-checkerboard-texture/30397/2 "2021-09-30T12:24:45Z")

</div>

Hi!  
And how the desired result with a rounded cube has to look like?

---

<div class="post-metadata">

**Author:** ![blackInk](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@blackInk](https://discourse.threejs.org/u/blackInk)\
**Post date:** [September 30, 2021, 2:15pm UTC](https://discourse.threejs.org/t/rounded-cube-with-checkerboard-texture/30397/3 "2021-09-30T14:15:45Z")

</div>

@prisoner849

 ![e62882949731baf18619e29e4d7b4924](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/a/0/a09dbce8b1e51e1a7a7b67abee893fced96249f7.jpeg)

Just the texture → not the stretching of the cube geometry

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [September 30, 2021, 2:31pm UTC](https://discourse.threejs.org/t/rounded-cube-with-checkerboard-texture/30397/4 "2021-09-30T14:31:05Z")

</div>

So what’s the question? 🙂

Three.js has RoundedBoxGeometry `https://cdn.skypack.dev/three@0.132.2/examples/jsm/geometries/RoundedBoxGeometry.js`, that supports UVs.

Example: [Edit fiddle - JSFiddle - Code Playground](https://jsfiddle.net/prisoner849/089f7s2x/)

![изображение](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/0/c/0c8a8cdf8fd32c0314e33517a93609d462168e8a.jpeg)

---

<div class="post-metadata">

**Author:** ![blackInk](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@blackInk](https://discourse.threejs.org/u/blackInk)\
**Post date:** [October 1, 2021, 11:48am UTC](https://discourse.threejs.org/t/rounded-cube-with-checkerboard-texture/30397/5 "2021-10-01T11:48:23Z")

</div>

@prisoner849 almost yes, but still, at some sides it is not alternating the chequer´s

 ![checkerboard](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/f/4/f4cffcca2f805e01c4037af6e1013b850ac14778.jpeg)

I´d love to have it alternating it´s chequers on each side?

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [October 1, 2021, 1:48pm UTC](https://discourse.threejs.org/t/rounded-cube-with-checkerboard-texture/30397/6 "2021-10-01T13:48:07Z")

</div>

This is the best I could imagine for the UV based checkered pattern of a rounded box: [Edit fiddle - JSFiddle - Code Playground](https://jsfiddle.net/prisoner849/xykdrsq6/)

 ![изображение](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/e/2/e26fe2cb692c1a9098c3bd887607676d79af617a.png)

```javascript
let g = new RoundedBoxGeometry(5, 5, 5, 10, 1);
let m = new THREE.MeshBasicMaterial({
  onBeforeCompile: shader => {
  	shader.fragmentShader = `${shader.fragmentShader}`
    .replace(
    	`vec4 diffuseColor = vec4( diffuse, opacity );`,
      `
      	float chCount = 7.;
        float checker = (1. / chCount);
        float actualCheckers = 1. - checker;
        float halfChecker = checker * 0.5;
      	vec2 bUv = (vUv * actualCheckers) - halfChecker;
      	vec2 cUv = fract((bUv) * (chCount * 0.5)) - 0.5;
        float checkerVal = clamp(step(cUv.x * cUv.y, 0.), 0.5, 1.);
      	vec3 col = vec3(checkerVal);
      	vec4 diffuseColor = vec4( col, opacity );
      `
    );
  	console.log(shader.fragmentShader)
  }
});
m.defines = {"USE_UV":""};
let o = new THREE.Mesh(g, m);
scene.add(o);

```

`chCount` must be an odd number.

---

<div class="post-metadata">

**Author:** ![blackInk](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@blackInk](https://discourse.threejs.org/u/blackInk)\
**Post date:** [October 4, 2021, 11:23am UTC](https://discourse.threejs.org/t/rounded-cube-with-checkerboard-texture/30397/7 "2021-10-04T11:23:59Z")

</div>

Really cool @prisoner849 - would this approach be able to use a custom texture as well?

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [October 4, 2021, 1:27pm UTC](https://discourse.threejs.org/t/rounded-cube-with-checkerboard-texture/30397/8 "2021-10-04T13:27:17Z")

</div>

If segmentation is odd, then yes 🤔
