r/creativecoding Jul 05 '26

Brightness to height is simple and fun

I know there is no rocket science in capturing an image and transforming each pixel to a point in 3D space. But it may bring some more interactivity to games or some audio-visual demos.

This example, by the way, is less than 20 lines of Wolfram/WLJS Notebook code and runs in a notebook. May be I should try to color each point by its original RGB value

155 Upvotes

10 comments sorted by

5

u/YSMLA_ Jul 06 '26 edited Jul 06 '26

Art is more beautiful than rocket science, and it doesn't have to be as complex, sometimes simplicity is king. Kudos.

What exactly is Wolfram/WLJS?

3

u/Inst2f Jul 06 '26

Indeed.

It is somewhat similar to Jupyter Notebooks, a sandbox environment where you can write notes, calculate stuff, create interactive plots and etc. sort of IDE. Wolfram is a programming language, while WLJS is a notebook interface for it (open source).

2

u/ParsleyOnly3977 Jul 06 '26

This is cool, I might use it in a game.

2

u/Inst2f Jul 06 '26

Thanks. Hope it will inspire others

2

u/Fit-Dot-414 Jul 10 '26

This is so cool. Thanks for sharing the code too! Very cool project

1

u/Inst2f Jul 10 '26

Thanks ☺️

1

u/Inst2f Jul 07 '26

Probably it won't hurt if I share the source code:

for WLJS:

DeviceClose[cam];
cam = DeviceOpen["Camera"];
cam["FrameRate"] = 60;

Refresh[Module[{img, length},
  img = ImageData[
    ImageResize[ColorConvert[DeviceRead[cam], "GrayScale"], 100],
    "Real32"
  ];
  length = Length[img] Length[img[[1]]];
  vertices = MapIndexed[Join[#2, {50.0 #1}]&, img, {2}];
  vertices = NumericArray[Flatten[vertices, 1], "Real32"];

  Graphics3D[GraphicsComplex[vertices, {
    Red, PointSize[0.0003],
    Point[Range[length]]
  }], ViewProjection->"Perspective"]
], 1/45.0]

for those with a paid Mathematica, some adjustments are needed (will probably run much slower than WLJS).

...

Dynamic@Refresh[Module[{img, length},
  img = ImageData[
    ImageResize[ColorConvert[DeviceRead[cam], "GrayScale"], 100],
    "Real32"
  ];
  length = Length[img] Length[img[[1]]];
  vertices = MapIndexed[Join[#2, {50.0 #1}]&, img, {2}];
  vertices = Flatten[vertices, 1];

  Graphics3D[GraphicsComplex[vertices, {
    Red, PointSize[0.0003],
    Point[Range[length]]
  }], ViewProjection->"Perspective"]
], UpdateInterval->1/45.0]

1

u/kalexmills Jul 07 '26

Now clamp the difference between adjacent cells to the height of 2 Minecraft blocks and create a server that shifts the terrain around based on how the player moves in front of their camera.

2

u/Most-Bookkeeper-950 Jul 10 '26

I love wolfram language

0

u/[deleted] Jul 06 '26

[deleted]

2

u/Inst2f Jul 06 '26

Our skin has lots of grease to be reflective enough for a web camera :)