# How to make crystal like shapes with Processing?

**URL:** <https://discourse.processing.org/t/how-to-make-crystal-like-shapes-with-processing/10392>\
**Category:** Project Guidance\
**Created:** [April 17, 2019, 1:23pm UTC](https://discourse.processing.org/t/how-to-make-crystal-like-shapes-with-processing/10392 "2019-04-17T13:23:44Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [April 17, 2019, 9:55pm UTC](https://discourse.processing.org/t/how-to-make-crystal-like-shapes-with-processing/10392/3 "2019-04-17T21:55:10Z")

</div>

Hi @liukov, welcome to the forum.

There are probably many ways to produce that kind of shape but I think your approach (applying modifications to a PShape object) is quite relevant in this case.

My first thought was to suggest playing with [HemeshGui](https://github.com/struct78/hemeshgui). It’s a visual tool built on the Hemesh library that facilitate the creation and manipulation of polygonal meshes. For instance you could start with a box, elongate it with the `skew` ‘modifier’ and then `chamfer` the corners.

 ![Hemesh%20GUI](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/78927a3b24461cbf1f452ee2f8e2d552e60debb5.png)

I then realized you could get more realistic results with a simpler approach I tried some time go and which is based on [this](https://flashyprogramming.wordpress.com/2015/05/21/sphere-morphing/) blogpost. Basically all you would have to do is **noising the vertices of an icosahedron**. The key part is to _divide_ the normalized vertices locations (PVectors) by the noise value instead of multiplying them.

The simplest way to do this is to use the [Hemesh](https://github.com/wblut/HE_Mesh) library I mentionned above.

- create an icosahedron with the `creator` class
- normalize the vertices
- compute a 3D noise value based their locations (x, y and z)
- divide the (normalized) vertices by their corresponding noise value

Normally this should result in long, sharp spikes in the mesh:

 ![Spikes](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8b14c8b1cd148791f12ca1cce6cb71a615db6b5e.png)

Short example sketch with Python mode:

```auto
add_library('peasycam')
add_library('hemesh')

factor = 1.5

def setup():
    size(1000, 600, P3D)
    smooth(8)
           
    creator = HEC_Geodesic()
    creator.setType(WB_Geodesic.Type.OCTAHEDRON).setRadius(200).setB(12).setC(12)

    global mesh, render
    mesh = HE_Mesh(creator)
    render = WB_Render(this)
    cam = PeasyCam(this, 800)
    
    for i, v in enumerate(mesh.getVertices()):
        p = PVector(v.xf(), v.yf(), v.zf()).normalize().mult(1.1) ### --> Converting to PVector() for clarity but could use normalizeSelf(), scaleSelf() or mul() instead
        n = map(noise(p.x * factor + 1, p.y * factor + 2, p.z * factor + 3), 0, 1, -.004, .03)
        p.div(n)
        mesh.getVertex(i).set(WB_Point(p.x, p.y, p.z))

def draw():
    background('#DCDCDC')
    lights()
    
    render.drawFaces(mesh)

```

Because of the pseudo-random nature of Perlin noise you’ll never end-up with the same result so feel free to run sketch as many times as you like until you find an output you feel satisfied with.

If realism is important to you, you can go a step further and render your mesh with a third party software. Here’s an example of a mesh generated with the same sketch and then rendered with Keyshot:

 ![10](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/7677e8a5419a6b1e4fef8c32be5149587cb6aecc.jpeg)

(Note that If you have a windows/linux machine you could also try @kosowski’s [simplePBR](https://discourse.processing.org/t/physically-based-rendering-library-for-processing-simplepbr/8152) library within your Processing sketch)

---

_[View the full topic](https://discourse.processing.org/t/how-to-make-crystal-like-shapes-with-processing/10392)._
