— Pixels Commander

[ In English, На русском ]

Can computer pray for the benefit of all living beings? Praying with JavaScript and WebGL

You probably noticed that last 2 years I was not that active writing on the blog. Sorry, I was busy with this study. Let`s make an agreement straight away – this is not your ordinary tech article demonstrating technical solutions (although you will find some interesting implementation details here). This is a study to prove that new technologies don’t tear the fabric of time and things that seem incompatible, located in unimaginably distant cultural coordinates can still be touched and the touch is beautiful. For me personally, this study is especially significant. The story began ten years ago when a Buddhist friend proposed making a mobile prayer application. This raised a huge number of questions to be answered before the main one could be: “Can a computer pray for the benefit of all living beings?”. Or at least help a prayer.

Prelude

Prayer… What could be more distanced from the technology? According to most of the religions: Christianity, Islam, Hinduism, prayer is an execution of customs in order to communicate to deities and request something from them. However, Buddhism sets getting rid of suffering and reaching enlightenment as a primary goal and what is very interesting a praying one (not deity) produces benefits for all living beings around. Compassion, assistance, care, good wishes – these are what Buddhist customs are designed to produce. And if other religions could be compared to client/server architecture then Buddhism assumes decentralized medium where benefits produced by every praying human being and not only…

Machines for praying now and then

Prayer of a computer? “A strange, cyberpunk idea,” you think. However, in Tibetan Buddhism for many centuries there are several types of prayer machines. The most famous of them is a prayer wheel, which is widely distributed in modern China, Nepal, India, Bhutan. Their time and place of occurrence is not known for certain. Legend has it that the prayer wheels were brought to the world of people by the master Nagarjuna from his journey into the underwater world of dragon-like creatures – the naga.

Picture of a naga on the wall of Chusan village, Upper Mustang, Nepal

Some researchers consider the drums to be a legacy of the ancient pre-Buddhist Bon religion that existed in Tibet since the beginning of our era. Others refer to the first written records of prayer wheels found in the scriptures of monasteries on the border of India and Nepal and dating back to the 11th century. AD, when these machines were already an established tradition. Professor Gregory Schopin translated one of these texts written by a monk under the name Vipulashrimitra who lived in the 11th century at the Buddhist University of Nalanda (according to legend, Nagarjuna studied there)

From continuous effort the Perfection of Wisdom Sutra constantly revolves in the great temple of the Avalokiteshvara, by means of the book case constructed by Vipulashrimitra, and he installed four images in the alms-houses on the holidays.

 

A typical prayer wheel is a metal or wooden hollow cylinder mounted on an axis. On the outside of the wheel, there is an engraved 6-syllable mantra of the compassion Buddha: “Om ma ni pad me hum”, and inside there is a roll with the same mantra printed or handwritten. It could be a hundred of mantras inside of a little handheld wheel and many millions in stationary ones built in monasteries.

Prayer Wheel schema

Using a prayer wheel is considered to be a powerful purifying practice and one revolution of a wheel to produce the same benefits for all living beings as would the oral utterance of the number of mantras that are located inside the drum. Here what Buddhist leaders and lamas say about the wheel:

Ancient Tibetan Buddhist text entitled “Mani Kabum.” 17 c. – The prayer wheel is like a precious jewel: whatever you wish for, it will accomplish all the supreme and ordinary attainments.

Lama Zopa Rinpoche (Dalai Lama student, the head of Mahayana traditions preservation) – installing a prayer wheel has the capacity to completely transform a place, which becomes peaceful, pleasant, and conducive to the mind.

His Holiness Jigdal Dagchen (head of Tibetan monastery of Seattle) – Spinning a prayer wheel purifies our body, spirit, and mind.

Gautama Buddha (in the sutra entitled the Great River) – The benefit of turning the Dharma wheel is that negative karma and disturbing thought obscurations accumulated over beginning-less rebirths are purified without effort. Even other mantras are without doubt completed.

There are a few kinds of Prayer Wheels: spin by a human or propelled by the power of wind, water, and even fire.


An old man makes use of a prayer wheel at the main square of Lomanthang, the capital of Upper Mustang


Stationery prayer wheel in the wall of Kagbeni monastery, Upper Mustang


Water propelled prayer wheel in the Muktinath temple, Nepal

Prayer wheels – a practice used and believed by hundreds of millions, an inanimate object, machine which is able to convert the mechanical energy of spinning into transcendental energy of a prayer which brings a good wish and benefits to all living beings. Could it be done the same way with a digital machine, with a computer? Can computers transform energy into a spiritual message transforming the world?
Prayer wheel was the only hook I had in the research so it was important to understand how the practice works. I started by reading thematic literature. Surprisingly but such an interesting phenomenon is described poorly by Western authors. First book got to me was written in 1896 by Englishman William Simpson based on his experiences traveling to Tibet and Nepal.

This is a voluminous work about circular movements in religion and rites, which is not easy to read because of Victorian English, but it is interesting to observe the author’s amazement from the cultural phenomena of Buddhism just discovered by Western researchers. This is exactly Simpson who coined the term «praying machine» apparently being influenced by the industrial revolution just ended at his motherland and the abundance of machines that appeared there. The book depicts all kinds of prayer wheels in detail: propelled by human muscles, wind, water, fire. Unfortunately, Simpson’s work has little to do with the spiritual aspect of using wheels and there are no translations of the original Tibetan texts at all. Another work, written by our contemporary Lorne Ladner, who lives in Washington, came to the rescue.

Wheel of great compassion book cover

The goal of his work is to popularise practice and describe the spiritual component. It is actually interesting how, using the example of these two books, the path traversed by Buddhism in Western culture over the past 100 years is traced… From an exotic curiosity to a religion that is fully accepted by the society which invests in Buddhism popularisation. Lorne`s book became a great find as it describes how the practice works and contains texts of ancient lamas allowing to make some conclusions on the topic. By the time of reading, I already had a little experience using the wheel, and the method I intuitively found was pretty much similar to that described in Tibetan texts. From them, an important aspect became clear – if water or wind-propelled prayer wheels work on their own and are considered artifacts that purify space – then the wheels rotated by man are a spiritual practice in which the prayer spins the drum, asking for mercy and compassion for all living beings, desiring for happiness, freedom, and deliverance from suffering for everyone. The prayer wheel is the ritual, spiritual activity aiming to transform the world for better and here the wheel itself is a visualization and a focal point.

Visualization? I can do it with JavaScript!

Visualization? I can do it in javascript! Moreover, His Holiness the Dalai Lama announced that the mantra on the computer screen works the same as the traditional one, and from a technical point of view, I was completely sure of success after several years of developing React + WebGL applications.

When traveling in Asia, I saw many prayer wheels, specifically noted the materials used and aesthetics required, so I knew exactly what I would need to create an open-source WebGL wheel that would be a worthy visualization of the practice.

Praying Wheel no textures

I needed physically based rendering and a good 3D artist who, as usual in developing 3d applications, is 70% responsible for great graphics. Fortunately for me, PBR was at hand – in Babylon.js there is an excellent implementation out of the box, and a few weeks later an experienced artist from Moscow was found, regularly wintering in Asia and not by hearsay familiar with prayer wheels. This was an important requirement – the artist should know the details of the design of the prayer wheels because authenticity is important as if nothing more than a spectacular look.

State management

To manage the state of the application, I decided not to use heavyweight Redux / MobX, but to use that “all over on the blogs” ContextAPI. This I had to regret at first because of the need to write a significant amount of useless low-level code, and then rejoice at developing small cool wrapper for the ContextAPI that allows you to change data in the application state as simple as changing properties ​​of JS object. And make this with nearly zero boilerplate added to an app. As a result, it is a concise and very simple solution for the managing state in small projects. Please, feel free to check out the easy way of managing ContextAPI state.

Interaction

An interesting and very important task was the absolutely realistic rotation of the wheel in response to user actions. The first problem I encountered was the difference in the rotation of the wheel while dragging by the central part and by the edge. After all, the center of the cylinder is closer to the axis and its extreme point from the axis is located at different angles to the projection of the screen. This means that dragging, say, 10 pixels in the center of the wheel, we will rotate it at a smaller angle than after dragging by the same 10 pixels closer to the edge. This may seem like an insignificant detail at first glance, but in practice, it is a critical nuance for having that full realism of a touch rotation.

Rotating next to edge of a wheel projection on the screen results in larger rotation angle for the same number of pixels passed by finger on the screenThe angle A is almost half the angle B with the same projection onto the screen plane.

To solve the problem, we use fairly simple trigonometry.

If when touching the center of the cylinder touchPosition = 0, and when touching the edge touchPosition = 1, then the angle from the center to the point of a touch is acos(touchPosition) , respectively, knowing the angle of contact on the previous frame, you can calculate the rotation speed oldTouchAngle – newTouchAngle. Then repeat it on every frame.

Touch angle calculation code

In addition to correctly calculating the angular velocity transmitted by passing a finger over the surface, realistic physics is also needed. The wheel has mass and friction, for the implementation of which current speed is multiplied by the magnitude of the friction on every frame. In addition, we cannot immediately transmit the speed of rotation obtained from the energy applied by the finger, because the wheel has mass, friction and the adhesion of the finger and the prayer wheel is not perfect. Here linear interpolation or the LERP technique comes to the rescue. So the current wheel speed does not work out straight from the speed of the finger, and each frame changes tending to be equal.

Where 0.2 determines the adhesion quality and when replacing 0.2 by 1, the speed will be transmitted from the finger to the prayer wheel instantly. After putting in place the correct calculation of the rotation speed with LERP and friction, we got a realistic and detailed physical model.

From the visual side, we got affected by the artist’s lack of experience creating models for Babylon.js. So multiple details as shiness and reflections of materials we had to sort out together. But it was worthy as the result matched the expectations. especially materials and reflections. Meet – the world’s first Web-technology based prayer wheel . Plus it is open source. Every accepted pull request is deployed to the app automatically. Want to add a side settings menu, a choice of background image or traditional Tibetan music? You are welcome!

You do not have to be a Buddhist to use the wheel, but there is a prerequisite – you may rotate it only with a pure thought for the benefit of loved ones and all living beings. The wheel perfectly helps in moments of anxiety, calms, and tunes in for a constructive manner. Try using it before an important meeting.

Expedition

This could be stopped with wheels, but curiosity pushed to test the application on real users. And who can be a more relevant focus group than the inhabitants of the Tibetan Highlands practicing prayer with the help of wheels for centuries? Who can be more authoritative in the matter of Buddhism than a Tibetan monk practicing for decades? I decided to go to the Himalayas. In the process of planning the expedition, the choice was made to go to the Lo kingdom – the protected area of ​​Nepal, bordering Tibet.

Lo Manthang, Nepal map

Due to the need to obtain permission from the government of Nepal and the King of the Upper Mustang, only 600-800 tourists visit the region annually, which is less than what reaches every year the peak of Everest! And it is the place where the pristine Tibetan way of life was preserved and Buddhist monasteries founded in the 8th-10th centuries still are in service. Let’s listen to what the monks of the Lomantang Monastery say about prayer wheels, and what they think about the prayer wheel developed in JavaScript.

Conclusion

Millions of people in the world use prayer machines – prayer wheels, both driven by the physical strength of a person and working without his participation. Can a computer be a prayer machine? Since His Holiness the Dalai Lama has declared the power of the mantra on the computer screen equivalent to the power of the traditional mantra, you can pray using an application written in JavaScript, and what is important – computer displaying a spinning wheel would purify the place sending a goodwill for the benefit of all living beings around purifying their karma which could be considered to be a prayer.

P.S.

While working on the study, I was many times visited by the idea that programming is very similar to spiritual practices in terms of focus, subtility of the outcome and the desire to transform the world. So if development is the practice that resonates with you, and if you do the practice correctly, then the process brings satisfaction and calm. But is it always like that? Of course, it is not! Perhaps everyone felt at least once that he was engaged in a project which was not resonating with and the written code does not add any good to the world. And this is our, developers, great concern and responsibility – to strive to carry out the practice correctly, with a pure heart, to work on projects for the benefit of all living beings.