|
| 1 | +const vert = ` |
| 2 | +attribute vec3 aPosition; |
| 3 | +attribute vec3 aNormal; |
| 4 | +attribute vec2 aTexCoord; |
| 5 | +
|
| 6 | +uniform mat4 uModelViewMatrix; |
| 7 | +uniform mat4 uProjectionMatrix; |
| 8 | +uniform mat3 uNormalMatrix; |
| 9 | +
|
| 10 | +uniform float time; |
| 11 | +
|
| 12 | +varying vec2 vTexCoord; |
| 13 | +varying vec3 vNormal; |
| 14 | +varying vec3 vPosition; |
| 15 | +
|
| 16 | +void main(void) { |
| 17 | + vec4 objSpacePosition = vec4(aPosition, 1.0); |
| 18 | + float origZ = objSpacePosition.z; |
| 19 | + ${AutoDiff.gen((ad) => { |
| 20 | + const pos = ad.vec3Param('objSpacePosition') |
| 21 | + const y = pos.y() |
| 22 | + const time = ad.param('time') |
| 23 | +
|
| 24 | + offset = ad.vec3(time.mult(0.005).add(y.mult(2)).sin().mult(0.5), 0, 0) |
| 25 | + offset.output('offset') |
| 26 | + offset.adjustNormal(ad.vec3Param('aNormal'), pos).output('normal') |
| 27 | + //offset.output('z') |
| 28 | + //offset.outputDeriv('dzdx', x) |
| 29 | + //offset.outputDeriv('dzdy', y) |
| 30 | + }, { debug: true, maxDepthPerVariable: 8 })} |
| 31 | + objSpacePosition.xyz += offset; |
| 32 | + //vec3 slopeX = vec3(1.0, 0.0, dzdx); |
| 33 | + //vec3 slopeY = vec3(0.0, 1.0, dzdy); |
| 34 | + vec4 worldSpacePosition = uModelViewMatrix * objSpacePosition; |
| 35 | + gl_Position = uProjectionMatrix * worldSpacePosition; |
| 36 | + vTexCoord = aTexCoord; |
| 37 | + vPosition = worldSpacePosition.xyz; |
| 38 | + //vNormal = uNormalMatrix * aNormal; |
| 39 | + //normal=cross(_glslad_v66,_glslad_v65); |
| 40 | + //normal=_glslad_v66; |
| 41 | + vNormal = uNormalMatrix * normal; |
| 42 | +} |
| 43 | +` |
| 44 | +console.log(vert) |
| 45 | + |
| 46 | +const frag = ` |
| 47 | +precision mediump float; |
| 48 | +const int MAX_LIGHTS = 3; |
| 49 | +
|
| 50 | +varying vec2 vTexCoord; |
| 51 | +varying vec3 vNormal; |
| 52 | +varying vec3 vPosition; |
| 53 | +
|
| 54 | +uniform sampler2D img; |
| 55 | +uniform int numLights; |
| 56 | +uniform vec3 lightPositions[MAX_LIGHTS]; |
| 57 | +uniform vec3 lightColors[MAX_LIGHTS]; |
| 58 | +uniform float lightStrengths[MAX_LIGHTS]; |
| 59 | +uniform vec3 ambientLight; |
| 60 | +uniform float materialShininess; |
| 61 | +
|
| 62 | +void main(void) { |
| 63 | + vec3 materialColor = texture2D(img, vTexCoord).rgb; |
| 64 | + vec3 normal = normalize(vNormal); |
| 65 | + gl_FragColor = vec4(abs(normal), 1.); return; |
| 66 | + //gl_FragColor = length(vNormal) * vec4(1.); return; |
| 67 | + vec3 color = vec3(0.0, 0.0, 0.0); |
| 68 | + for (int i = 0; i < MAX_LIGHTS; i++) { |
| 69 | + if (i >= numLights) break; |
| 70 | + vec3 lightPosition = lightPositions[i]; |
| 71 | + float distanceSquared = 0.0; /*0.00015*dot( |
| 72 | + lightPosition - vPosition, |
| 73 | + lightPosition - vPosition);*/ |
| 74 | + vec3 lightDir = normalize(lightPosition - vPosition); |
| 75 | + float lambertian = max(dot(lightDir, normal), 0.0); |
| 76 | + color += lambertian * materialColor * lightColors[i] * |
| 77 | + lightStrengths[i] / (1.0 + distanceSquared); |
| 78 | + vec3 viewDir = normalize(-vPosition); |
| 79 | + float spec = pow( |
| 80 | + max(dot(viewDir, reflect(-lightDir, normal)), 0.0), |
| 81 | + materialShininess); |
| 82 | + color += spec * lightStrengths[i] * lightColors[i] / |
| 83 | + (1.0 + distanceSquared); |
| 84 | + } |
| 85 | + color += ambientLight * materialColor; |
| 86 | + gl_FragColor = vec4(color, 1.0); |
| 87 | +} |
| 88 | +` |
| 89 | + |
| 90 | +let distortShader |
| 91 | +let texture |
| 92 | +function setup() { |
| 93 | + createCanvas(800, 600, WEBGL) |
| 94 | + distortShader = createShader(vert, frag) |
| 95 | + texture = createGraphics(500, 500) |
| 96 | +} |
| 97 | + |
| 98 | +const lights = [{ |
| 99 | + position: [200, 50, -100], |
| 100 | + color: [1, 1, 1], |
| 101 | + strength: 0.5, |
| 102 | + }, |
| 103 | + { |
| 104 | + position: [-200, -50, -100], |
| 105 | + color: [1, 1, 1], |
| 106 | + strength: 0.5, |
| 107 | + }, |
| 108 | +]; |
| 109 | + |
| 110 | +function draw() { |
| 111 | + texture.background(255, 0, 0) |
| 112 | + texture.fill(255) |
| 113 | + texture.noStroke() |
| 114 | + texture.textSize(70) |
| 115 | + texture.textAlign(CENTER, CENTER) |
| 116 | + texture.text('hello, world', texture.width / 2, texture.height / 2) |
| 117 | + |
| 118 | + background(0) |
| 119 | + |
| 120 | + const shininess = 1000 |
| 121 | + const ambient = [0.2, 0.2, 0.2] |
| 122 | + |
| 123 | + orbitControl() |
| 124 | + noStroke() |
| 125 | + shader(distortShader) |
| 126 | + distortShader.setUniform('img', texture) |
| 127 | + distortShader.setUniform('lightPositions', lights.map(l => l.position).flat()) |
| 128 | + distortShader.setUniform('lightColors', lights.map(l => l.color).flat()) |
| 129 | + distortShader.setUniform('lightStrengths', lights.map(l => l.strength).flat()) |
| 130 | + distortShader.setUniform('numLights', lights.length) |
| 131 | + distortShader.setUniform('ambientLight', ambient) |
| 132 | + distortShader.setUniform('materialShininess', shininess) |
| 133 | + distortShader.setUniform('time', millis()) |
| 134 | + push() |
| 135 | + sphere(200, 60, 30) |
| 136 | + pop() |
| 137 | +} |
0 commit comments