Sample code to get started with GLSL shaders in Processing (either Java or Python)

Introduction

You can run fragment shaders with Processing, a tool that even beginners can easily get started with. You can write in Java or Python.

Execution screen

The Java code is on the left and the Python code is on the right. processing_shader2.jpg

When you want to write in Java

Implementation

First, put the program that wrote the contents of the shader in the data folder.

data/FragmentShader.glsl



uniform vec2 resolution;
uniform float time;

void main() {
    vec2 uv = -1. + 2. * gl_FragCoord.xy / resolution.xy;
    gl_FragColor = vec4(
                        abs( sin( cos( time + 3. * uv.y ) * 2. * uv.x + time)),
                        abs( cos( sin( time + 2. * uv.x ) * 3. * uv.y + time)),
                        1.0,
                        1.0);
}

Write as follows in the Processing main program. Just specify FragmentShader.glsl and the file name and it will search in the data folder.

PShader sd;
 
void setup() {
  size(600, 600, P2D);
  sd = loadShader("FragmentShader.glsl");
}
 
void draw() {
  sd.set("time", millis() / 1000.0);
  sd.set("resolution", (float)width, (float)height);  
  shader(sd);
  rect(0, 0, width, height);
}

When you want to write in Python

Python mode installation

By default, only Java can be selected as the language setting in the upper right, You can easily add Python mode.

(This link is easy to understand with an image) https://pycarnival.com/processingpy1/

Implementation

In the same way, put the exact same shader file as in Java in the data folder

data/FragmentShader.glsl



uniform vec2 resolution;
uniform float time;

void main() {
    vec2 uv = -1. + 2. * gl_FragCoord.xy / resolution.xy;
    gl_FragColor = vec4(
                        abs( sin( cos( time + 3. * uv.y ) * 2. * uv.x + time)),
                        abs( cos( sin( time + 2. * uv.x ) * 3. * uv.y + time)),
                        1.0,
                        1.0);
}

Write as follows in the Processing main program. By declaring a variable in global sd, the same shader object can be referenced by both the setup function and the draw function.

def setup():
    global sd
    size(600, 600, P2D)
    sd = loadShader("FragmentShader.glsl")
 
def draw():
    global sd
    sd.set("time", millis() / 1000.0)
    sd.set("resolution", float(width), float(height))  
    shader(sd)
    rect(0, 0, width, height)

Now, let's enjoy GLSL in both Java and Python with Processing!

Recommended Posts

Sample code to get started with GLSL shaders in Processing (either Java or Python)
How to get started with Python
Get started with Python in Blender
Get Started with TopCoder in Python (2020 Edition)
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
A layman wants to get started with Python
5 Reasons Processing is Useful for Those Who Want to Get Started with Python
I tried to get started with blender python script_Part 01
I tried to get started with blender python script_Part 02
Super Primer to python-Getting started with python3.5 in 3 minutes
Get in touch with functional programming in JavaScript or Python 3
Get country code with python
Get started with Python! ~ ② Grammar ~
Minimum knowledge to get started with the Python logging module
Send push notifications to iOS apps with Python2 (with sample code)
Get started with Python! ~ ① Environment construction ~
[Python] A memo that I tried to get started with asyncio
How to get started with Scrapy
How to get started with Django
Python parallel / parallel processing sample code summary
[Python] To get started with Python, you must first make sure you can use Python.
How to get the date and time difference in seconds with python
Get started with Python in 30 minutes! Development environment construction & learn basic grammar
Get additional data in LDAP with python
Step notes to get started with django
Try logging in to qiita with Python
Express Python yield in JavaScript or Java
I tried to get started with Hy
Sample script to trap signals in Python
How to work with BigQuery in Python
How to get a stacktrace in python
Easy image processing in Python with Pillow
To work with timestamp stations in Python
Get the EDINET code list in Python
How Python beginners get started with Python with Progete
How to get started with laravel (Linux)
Sample to convert image to Wavelet with Python
[Blender x Python] Let's get started with Blender Python !!
How to generate a QR code and barcode in Python and read it normally or in real time with OpenCV
How to get started with the 2020 Python project (windows wsl and mac standardization)
For those who want to learn Excel VBA and get started with Python
How to get a list of files in the same directory with python
Getting started with Python with 100 knocks on language processing
Python hand play (let's get started with AtCoder?)
MessagePack-Try to link Java and Python with RPC
The easiest way to get started with Django
How to do multi-core parallel processing with python
Strategy on how to monetize with Python Java
MongoDB Basics: Getting Started with CRUD in JAVA
Introduction to Tornado (1): Python web framework started with Tornado
Personal notes to doc Python code in Sphinx
I tried to get CloudWatch data with Python
A clever way to time processing in Python
Get pointers to struct members in Python ctypes
Sample to send slack notification with python lambda
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
Get started with Python on macOS Big Sur
Use Python in Anaconda environment with VS Code
Getting started with AWS IoT easily in Python
Parallel processing with no deep meaning in Python