LoginSignup
23
17

More than 5 years have passed since last update.

TouchDesigner - GLSL Image Displacement

Last updated at Posted at 2017-12-20

Requirements

TouchDesigner 099 2017.15400+
今回の完成ソースはここ。

Description

Open GLSL_Displacement.toe

Image Displacement can be easily setup in TouchDesigner using TOPs, SOPs and CHOPs.
But the SOPs and CHOPs calculate on the CPU, performance will slow down as more points are added to geometry.
Example /base_top2sop_displacement
Try increasing Grid SOP Rows/Columns = 250.
TOP -> CHOP - can be slow
CHOP -> SOP displacement - can be slow

GLSL Materials can be used displace geometry on the GPU for better performance so you can use many more points.
Example /container_glsl_displacement
TOP -> GLSL displacement - fast!

Prepare Geometry

Create Grid SOP -> Geometry COMP.
Grid SOP settings
Rows = 250
Columns = 250

Add Attribute Create SOP to calculate tangents for GLSL shader later.
Attribute Create SOP settings
Compute Tangents = On
Screen Shot 2017-12-19 at 4.32.49 PM.png

Basic geometry setup
Screen Shot 2017-12-19 at 4.33.20 PM.png

Create GLSL Material

Easy way to start, output shader from Phong MAT

Click Output Shader in Phong MAT parameters
Untitled-1.png

Click OK on Output GLSL Code... dialog
GLSLoutput.png

Result is GLSL MAT with all the same settings as Phong MAT
OutputGLSLPhongMAT.png

Assign phong1GLSL to Geometry COMP
Screen Shot 2017-12-19 at 7.03.01 PM.png

Add Image Sampler to GLSL Shader

Create Noise TOP -> Null TOP
On phong1GLSL Samplers page, assign Null TOP and give it a sampler name dispTexture
Screen Shot 2017-12-19 at 4.54.58 PM.png

Declare sampler2D dispTexture in vertex shader
Screen Shot 2017-12-19 at 5.05.26 PM.png

Displace Point Positions Using Sampler

  1. Use texture() function to sample red channel of dispTexture
  2. Create newPos which adds offset to original position
  3. Use newPos in TDDeform()

vertex1.png

Displacement! :grinning:
But bad lighting... :cry:

Calculating Normals For Lighting

For correct lighting, we must re-calculate the normals in vertex shader and pass the data to pixel shader for final lighting.

Get normal information for input image using Normal Map TOP
Normal Map TOP settings
Heightmap in Alpha Channel = On

Screen Shot 2017-12-19 at 5.28.49 PM.png

Now sample alpha channel in vertex shader to get height map
Screen Shot 2017-12-19 at 5.31.39 PM.png

Update Vertex Data Structure

From vertex shader, pass tbn matrix and texture coordinates to pixel shader
Vertex Shader
Screen Shot 2017-12-19 at 5.43.45 PM.png
Pixel Shader
Screen Shot 2017-12-19 at 5.45.27 PM.png

Vertex Shader - Calculate tbn and texCoord0

In the vertex shader
Declare tangents T
Screen Shot 2017-12-19 at 5.51.41 PM.png

Calculate tbn matrix (tangent, bitangent, normal matrix) and set texCoord0 texture coordinates
Screen Shot 2017-12-19 at 5.48.16 PM.png
TDCreateTBNMatrix() is new in TouchDesigner (Build 2017.15400+)

Pixel Shader - Normal Recalculation

In the pixel shader
Declare sampler2D dispTexture
Screen Shot 2017-12-19 at 6.00.38 PM.png

Do normal recalculation and comment old normal code
Screen Shot 2017-12-19 at 6.01.01 PM.png

GLSL Displacement should be working!
Try more points in Grid SOP (Rows/Columns = 500 or 1000 depending on your computer)

Screen Shot 2017-12-19 at 6.16.07 PM.png

Extras in GLSL_Displacement.toe

Here are some extras to make it more interesting...
1. In TOP to input any image or movie for displacement
2. Fit TOP so that TOP pixels = Grid SOP points
3. dispScale uniform in shader controls amount of displacement
4. Particle render mode from Convert SOP
5. Custom parameters on component for easier control
6. Use /container_glsl_displacement viewer to move camera (arcBall camera component from Palette)
7. Pretty lighting :smile:

Summary

Using this method you can displace many more points and higher density geometry than with CHOPs and SOPs.

I taught this example in our TouchDesigner 202 Intro to GLSL workshop this fall, but the math is easier now because we added TDCreateTBNMatrix() to TouchDesigner.

Original inspiration for this displacement example came from Matthew Ragan and his Make_Some_Noise workshop earlier this year. Thank you again Matthew!

ごめん、日本語のカレンダーなのに、日本語で教えられない!はは!

23
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
17