How computers can pray for the benefit of all living beings using ReactJS & WebGL?
You’ve probably noticed over the last year that I haven’t been 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 (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, to prove that 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 venture raised a huge number of questions to be resolved before the primary one could be addressed: “Can a computer pray for the benefit of all living beings?”
Want a spoiler? The result is here. After launching the computer starts praying for you in accordance with Tibetan Buddhism customs. How comes? Read the article.
Prelude
Prayer… What could be more distanced from modern technology? According to most religions (such as 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. What is very interesting is that when one prays, it produces benefits for all surrounding living beings. Compassion, assistance, care, good wishes – these are what Buddhist customs are designed to produce. And if other religions can be compared to client/server architecture, then Buddhism assumes a decentralized p2p medium where benefits may be produced by every being, not limited to living beings…
Machines for praying now and then
Prayer via computer? “A strange, cyberpunk idea,” you think. However, in Tibetan Buddhism, for many centuries there existed several types of prayer machines. The most famous of them is a prayer wheel, which is widely distributed in modern China, Nepal, India, and Bhutan. The time and place of their origination are uncertain. Legend has it that the prayer wheels were brought to the world of people by the master Nagarjuna, who obtained them on 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 has 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, 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 named 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. There could be hundreds of mantras inside of a little handheld wheel, and many millions in stationary wheels built in monasteries.
Using a prayer wheel is considered to be a powerful purifying practice, with one revolution of a wheel producing the same benefits for all living beings as would the oral utterance of the number of mantras that are located inside the drum. Here is 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, characterized by how they are powered: spun by a human, or propelled by the wind, water, or 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 – practiced and believed by hundreds of millions – are inanimate objects, machines which are able to convert the mechanical energy of spinning into the transcendental energy of a prayer, bringing 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, thereby 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, such an interesting phenomenon is described poorly by Western authors. The first book that spoke to me, Buddhist Prayer Wheel, was written in 1896 by Englishman William Simpson, based on his experiences traveling to Tibet and Nepal.
A voluminous work about circular movements in religion and rites, it’s not an easy read due to its Victorian English, but it is interesting to observe the author’s amazement at the cultural phenomena of Buddhism, which had been just discovered by Western researchers. In fact, Simpson coined the term “praying machine,” apparently having been influenced by the industrial revolution that had just ended in his motherland. 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.
The goal of his work is to popularise prayer wheel practice and describe the spiritual component. It is actually interesting how, using these two books, the path traversed by Buddhism in Western culture over the past 100 years can be traced: from an exotic curiosity to a religion that is fully accepted by the society which invests in the popularisation of Buddhism. Lorne’s book became a great find, as it describes how the practice works and contains texts of ancient lamas, enabling readers to draw some insightful conclusions on the topic. When I read it, I’d 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!
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 confident of success after several years of developing React + WebGL applications.
When traveling in Asia, observing many prayer wheels, I 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 an authentic visualization of the practice.
I needed a physically-based rendering and a good 3D artist who, as is standard practice 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. I had it on good authority that he was familiar with prayer wheels. This was an important requirement; the artist should know the details of the design because authenticity is important in creating a spectacular visual impression.
State management
To manage the state of the application, I decided not to use heavyweight Redux / MobX, but React`s ContextAPI. Though regretting my choice initially because of the need to write a significant amount of useless low-level code, I then rejoiced at the ability to develop a small cool wrapper for the ContextAPI that allows you to change data in the application state, as simply as changing properties of a JS object. Feel free to check out the easy way of managing ContextAPI state.
Interaction
An imperative, crucial 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 versus 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 a sense of complete realism in touch rotation.
The 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.
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, plus friction and the adhesion of the finger on the prayer wheel is not perfect. Here linear interpolation (the LERP technique) comes to the rescue. Thus, the current wheel speed does not work out straight from the speed of the finger, and each frame changes, tending to be equal.
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 a visual aspect, the process was affected by the artist’s lack of experience in creating models for Babylon.js. Therefore, multiple details like shininess and reflectivity of materials had to be sorted out by myself. However, it was well worth the effort, as the result matched our expectations, especially regarding materials and reflections.
Meet the world’s first Web-technology based prayer wheel
Moreover, 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, calming and tuning the mind in a constructive manner. Try using it before an important meeting.
Expedition
Our venture could have been stopped at the creation of wheels, but curiosity pushed us to test the application on real users. And who can be a more relevant focus group than the inhabitants of the Tibetan Highlands, who have been 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.
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 those who reach the peak of Everest in a year! And it is the place where the pristine Tibetan way of life has been preserved, with Buddhist monasteries founded in the 8th-10th centuries still 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/her 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. What is important is that a computer displaying a spinning wheel purifies the place, sending goodwill for the benefit of all living beings and purifying their karma, which could be considered 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, subtlety 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 has felt at least once that he was engaged in a project which did not bring joy to work on, and the written code did not add any good to the world. And, as developers, this is our 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.