-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't output derivative lines for constant nodes
- Loading branch information
1 parent
a284c15
commit a6037ca
Showing
6 changed files
with
206 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@davepagurek/glsl-autodiff", | ||
"version": "0.0.19", | ||
"version": "0.0.20", | ||
"main": "build/autodiff.js", | ||
"author": "Dave Pagurek <[email protected]>", | ||
"license": "MIT", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>glsl-autodiff test</title> | ||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min.js"></script> | ||
<script type="text/javascript" src="../../build/autodiff.js"></script> | ||
<script type="text/javascript" src="test.js"></script> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
const vert = ` | ||
attribute vec3 aPosition; | ||
attribute vec3 aNormal; | ||
attribute vec2 aTexCoord; | ||
uniform mat4 uModelViewMatrix; | ||
uniform mat4 uProjectionMatrix; | ||
uniform mat3 uNormalMatrix; | ||
uniform float time; | ||
varying vec2 vTexCoord; | ||
varying vec3 vNormal; | ||
varying vec3 vPosition; | ||
void main(void) { | ||
vec4 objSpacePosition = vec4(aPosition, 1.0); | ||
float origZ = objSpacePosition.z; | ||
${AutoDiff.gen((ad) => { | ||
const pos = ad.vec3Param('objSpacePosition') | ||
const y = pos.y() | ||
const time = ad.param('time') | ||
offset = ad.vec3(time.mult(0.005).add(y.mult(2)).sin().mult(0.5), 0, 0) | ||
offset.output('offset') | ||
offset.adjustNormal(ad.vec3Param('aNormal'), pos).output('normal') | ||
//offset.output('z') | ||
//offset.outputDeriv('dzdx', x) | ||
//offset.outputDeriv('dzdy', y) | ||
}, { debug: true, maxDepthPerVariable: 8 })} | ||
objSpacePosition.xyz += offset; | ||
//vec3 slopeX = vec3(1.0, 0.0, dzdx); | ||
//vec3 slopeY = vec3(0.0, 1.0, dzdy); | ||
vec4 worldSpacePosition = uModelViewMatrix * objSpacePosition; | ||
gl_Position = uProjectionMatrix * worldSpacePosition; | ||
vTexCoord = aTexCoord; | ||
vPosition = worldSpacePosition.xyz; | ||
//vNormal = uNormalMatrix * aNormal; | ||
//normal=cross(_glslad_v66,_glslad_v65); | ||
//normal=_glslad_v66; | ||
vNormal = uNormalMatrix * normal; | ||
} | ||
` | ||
console.log(vert) | ||
|
||
const frag = ` | ||
precision mediump float; | ||
const int MAX_LIGHTS = 3; | ||
varying vec2 vTexCoord; | ||
varying vec3 vNormal; | ||
varying vec3 vPosition; | ||
uniform sampler2D img; | ||
uniform int numLights; | ||
uniform vec3 lightPositions[MAX_LIGHTS]; | ||
uniform vec3 lightColors[MAX_LIGHTS]; | ||
uniform float lightStrengths[MAX_LIGHTS]; | ||
uniform vec3 ambientLight; | ||
uniform float materialShininess; | ||
void main(void) { | ||
vec3 materialColor = texture2D(img, vTexCoord).rgb; | ||
vec3 normal = normalize(vNormal); | ||
gl_FragColor = vec4(abs(normal), 1.); return; | ||
//gl_FragColor = length(vNormal) * vec4(1.); return; | ||
vec3 color = vec3(0.0, 0.0, 0.0); | ||
for (int i = 0; i < MAX_LIGHTS; i++) { | ||
if (i >= numLights) break; | ||
vec3 lightPosition = lightPositions[i]; | ||
float distanceSquared = 0.0; /*0.00015*dot( | ||
lightPosition - vPosition, | ||
lightPosition - vPosition);*/ | ||
vec3 lightDir = normalize(lightPosition - vPosition); | ||
float lambertian = max(dot(lightDir, normal), 0.0); | ||
color += lambertian * materialColor * lightColors[i] * | ||
lightStrengths[i] / (1.0 + distanceSquared); | ||
vec3 viewDir = normalize(-vPosition); | ||
float spec = pow( | ||
max(dot(viewDir, reflect(-lightDir, normal)), 0.0), | ||
materialShininess); | ||
color += spec * lightStrengths[i] * lightColors[i] / | ||
(1.0 + distanceSquared); | ||
} | ||
color += ambientLight * materialColor; | ||
gl_FragColor = vec4(color, 1.0); | ||
} | ||
` | ||
|
||
let distortShader | ||
let texture | ||
function setup() { | ||
createCanvas(800, 600, WEBGL) | ||
distortShader = createShader(vert, frag) | ||
texture = createGraphics(500, 500) | ||
} | ||
|
||
const lights = [{ | ||
position: [200, 50, -100], | ||
color: [1, 1, 1], | ||
strength: 0.5, | ||
}, | ||
{ | ||
position: [-200, -50, -100], | ||
color: [1, 1, 1], | ||
strength: 0.5, | ||
}, | ||
]; | ||
|
||
function draw() { | ||
texture.background(255, 0, 0) | ||
texture.fill(255) | ||
texture.noStroke() | ||
texture.textSize(70) | ||
texture.textAlign(CENTER, CENTER) | ||
texture.text('hello, world', texture.width / 2, texture.height / 2) | ||
|
||
background(0) | ||
|
||
const shininess = 1000 | ||
const ambient = [0.2, 0.2, 0.2] | ||
|
||
orbitControl() | ||
noStroke() | ||
shader(distortShader) | ||
distortShader.setUniform('img', texture) | ||
distortShader.setUniform('lightPositions', lights.map(l => l.position).flat()) | ||
distortShader.setUniform('lightColors', lights.map(l => l.color).flat()) | ||
distortShader.setUniform('lightStrengths', lights.map(l => l.strength).flat()) | ||
distortShader.setUniform('numLights', lights.length) | ||
distortShader.setUniform('ambientLight', ambient) | ||
distortShader.setUniform('materialShininess', shininess) | ||
distortShader.setUniform('time', millis()) | ||
push() | ||
sphere(200, 60, 30) | ||
pop() | ||
} |