Skip to content

Commit

Permalink
Merge pull request #10 from FarazzShaikh/dev
Browse files Browse the repository at this point in the history
1.4.0
  • Loading branch information
FarazzShaikh authored Jul 18, 2021
2 parents ac71086 + f1bcbb6 commit 9946924
Show file tree
Hide file tree
Showing 31 changed files with 609 additions and 65 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ jobs:
run: |
npm install
npm run build
chmod +x scripts/patchModule.sh
scripts/patchModule.sh
- name: Test
run: |
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/sub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ jobs:
run: |
npm install
npm run build
chmod +x scripts/patchModule.sh
scripts/patchModule.sh
npm run test
- name: Push build to branch
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Version 1.4.0

### New

- [Curl Noise](https://farazzshaikh.github.io/glNoise/examples/index.html?src=Curl/index.html)
- Introduced dependency system
- This will allow for more complex, compound noise functions.

## Version 1.3.1

### Breaking
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ float n = gln_normalize(p);

The concept is pretty simple. You can fork it and write your GLSL functions in a file with the `.glsl` extension in the `src` directory. **The function must be shader independent so no use of `gl_FragCoord` or any shader-specific variables.**

Each new file must begin with the `name` directive like so:

```
// #name: <File Name without extension>
```

If your function requires functions from other files in this library, include the `deps` directive like so:
```
// #name: <File Name without extension>
// #deps: <Dependancy Name 1> <Dependancy Name 2> ... <Dependancy Name n>
```

You can document your code using [JSDoc](https://jsdoc.app/about-getting-started.html) style comment blocks. The documentation is auto-generated so you **MUST** include a `@name` with the name of your function. See the preexisting functions for reference. This is because

Include your file in index.ts by importing it and exporting it like all the preexisting files. Make sure to include your new file in the `_all` array.
Expand All @@ -294,6 +306,7 @@ I have not come up with these noise functions. Here's attribution to the creator
| Perlin Noise | Hugh Kennedy | [GitHub](https://github.com/hughsk/glsl-noise/blob/master/periodic/2d.glsl) | MIT |
| Simplex Noise | Ian McEwan | [GitHub](https://github.com/ashima/webgl-noise/blob/master/src/noise3D.glsl) | MIT |
| Worley Noise | Max Bittker | [GitHub](https://github.com/MaxBittker/glsl-voronoi-noise) | MIT |
| Curl Noise | Isaac Cohen | [GitHub](https://github.com/cabbibo/glsl-curl-noise) | ??? |

Every other function is by yours truly.

Expand Down
57 changes: 48 additions & 9 deletions build/glNoise.js

Large diffs are not rendered by default.

57 changes: 47 additions & 10 deletions build/glNoise.m.js

Large diffs are not rendered by default.

57 changes: 47 additions & 10 deletions build/glNoise.m.node.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions examples/Curl/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>gl-Noise • Curl</title>
<link href="../main.css" rel="stylesheet" />
<style>
body {
background-color: #3a4042;
}
.Info {
color: white;
}
</style>
</head>
<body type="0">
<div class="Info">Particles via Curl Noise.</div>
<script type="module" src="./main.js" defer></script>
</body>
</html>
144 changes: 144 additions & 0 deletions examples/Curl/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import * as THREE from "https://cdn.skypack.dev/three";
import { OrbitControls } from "https://cdn.skypack.dev/three/examples/jsm/controls/OrbitControls.js";

import { CustomShaderMaterial, TYPES } from "../lib/three-csm.module.js";
import { loadShadersCSM, Common, Simplex } from "../../build/glNoise.m.js";

import waves from "./waves.js";

function lights(scene) {
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5, 100);
const light = new THREE.HemisphereLight(0xffffff, 0xf7d9aa, 0.5);

scene.add(light);
scene.add(directionalLight);

directionalLight.position.set(0, 1, 0); //default; light shining from top
directionalLight.castShadow = true;

directionalLight.shadow.mapSize.width = 512; // default
directionalLight.shadow.mapSize.height = 512; // default
directionalLight.shadow.camera.near = 0.5; // default
directionalLight.shadow.camera.far = 500;
}

function map(x, in_min, in_max, out_min, out_max) {
return ((x - in_min) * (out_max - out_min)) / (in_max - in_min) + out_min;
}

function rand(min, max) {
return map(Math.random(), 0, 1, min, max);
}

const v = {
defines: "./shaders/particle_defines.glsl",
header: "./shaders/particle_header.glsl",
main: "./shaders/particle_main.glsl",
};

const f = {
defines: "./shaders/frag/defines.glsl",
header: "./shaders/frag/header.glsl",
main: "./shaders/frag/main.glsl",
};

(async () => {
const { defines: vdefines, header: vheader, main: vmain } = await loadShadersCSM(v);
const { defines: fdefines, header: fheader, main: fmain } = await loadShadersCSM(f);
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap
document.body.appendChild(renderer.domElement);

const fov = 60;
const aspect = window.innerWidth / window.innerHeight;
const near = 0.1;
const far = 200;
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);

const controls = new OrbitControls(camera, renderer.domElement);
camera.position.set(7, 7, 7);

const scene = new THREE.Scene();

lights(scene);

const loader = new THREE.TextureLoader();
const disk = loader.load("./textures/circle-sprite.png");

const geometry = new THREE.IcosahedronGeometry(4, 64);
console.log(geometry.attributes.position.count);
const material = new CustomShaderMaterial({
baseMaterial: TYPES.POINTS,
vShader: {
defines: vdefines,
header: vheader,
main: vmain,
},
fShader: {
defines: fdefines,
header: fheader,
main: fmain,
},
uniforms: {
uShift: {
value: 0,
},
uShape: {
value: disk,
},
uScale: {
value: window.innerHeight / 2,
},
uTime: {
value: 0,
},
uTargetPos: {
value: new THREE.Vector3(0),
},
},
passthrough: {
size: 0.05,
},
});

const points = new THREE.Points(geometry, material);

scene.add(points);

const targetPos = new THREE.Vector3();

renderer.domElement.addEventListener("pointermove", (event) => {
var vec = new THREE.Vector3(); // create once and reuse
var pos = new THREE.Vector3(); // create once and reuse

vec.set((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1, 0.5);

vec.unproject(camera);

vec.sub(camera.position).normalize();

var distance = -camera.position.z / vec.z;

pos.copy(camera.position).add(vec.multiplyScalar(distance));
targetPos.x = pos.x;
targetPos.y = pos.y;
targetPos.z = pos.z;
});

const render = (time) => {
if (material && material.uniforms) {
material.uniforms.uTime.value = time * 0.001;
// const p = new THREE.Vector3(targetPos).sub(material.uniforms.uTargetPos.value).multiplyScalar(0.05);
material.uniforms.uTargetPos.value = targetPos;
}

requestAnimationFrame(render);
renderer.render(scene, camera);
controls.update();
};

render();
})();
Empty file.
6 changes: 6 additions & 0 deletions examples/Curl/shaders/frag/header.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

uniform float uShift;
uniform sampler2D uShape;

varying vec2 vUv;
varying vec3 vPosition;
10 changes: 10 additions & 0 deletions examples/Curl/shaders/frag/main.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

vec2 uv = vUv;
uv.x += uShift;

vec4 shapeData = texture2D(uShape, gl_PointCoord);
if (shapeData.a < 0.0625)
discard;

vec4 newColor = vec4(vPosition, 1.0);
newColor = newColor * shapeData;
Empty file.
6 changes: 6 additions & 0 deletions examples/Curl/shaders/particle_header.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
uniform float uScale;
uniform float uTime;
uniform vec3 uTargetPos;

varying vec2 vUv;
varying vec3 vPosition;
21 changes: 21 additions & 0 deletions examples/Curl/shaders/particle_main.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@


vec3 p = position;
// p += uTargetPos * 0.1;

vec3 f = gln_curl((p * 0.2) + uTime * 0.05);

vUv = uv;

vec3 newPos = position + f;

vec3 seg = newPos - uTargetPos;
vec3 dir = normalize(seg);
float dist = length(seg);
if (dist < 3.) {
float force = clamp(1. / (dist * dist), 0., 1.);
newPos += dir * force;
}

vPosition = newPos;
vec3 newNormal = normal;
Binary file added examples/Curl/textures/circle-sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions examples/Curl/waves.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as THREE from "https://cdn.skypack.dev/three";
import { Common } from "../../build/glNoise.m.js";
import { CustomShaderMaterial, TYPES } from "../lib/three-csm.module.js";

export default function waves(scene, mobile) {
let material;
material = new CustomShaderMaterial({
baseMaterial: TYPES.TOON,
vShader: {
defines: ` `,
header: `
varying vec3 vPosition;
`,
main: `
vec3 newPos = position;
vPosition = position;
vec3 newNormal = normal;
`,
},
fShader: {
defines: ` `,
header: `
${Common}
varying vec3 vPosition;
vec4 calcColor() {
vec4 diffuseColor;
float mask;
mask = mix(0.0, 1.0, gln_map(vPosition.z, -5.0, 5.0, 0.0, 1.0));
mask = pow(mask, 8.0);
diffuseColor = vec4(1.,0.929,0.851,1.);
diffuseColor.rgb *= mask * 0.65;
diffuseColor.a *= mask;
return diffuseColor;
}
`,
main: `
vec4 newColor = calcColor();
`,
},
uniforms: {},
passthrough: {},
});

const geometry = new THREE.BoxGeometry(7, 7, 10, 1, 1, 1);
const plane = new THREE.Mesh(geometry, material);
plane.rotateX(-Math.PI / 2);
plane.position.y = -7;
plane.receiveShadow = true;

if (!mobile) scene.add(plane);

function displace(dt) {}

return displace;
}
6 changes: 4 additions & 2 deletions examples/Map/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Renderer, Program, Color, Mesh, Triangle, Vec3 } from "https://cdn.skypack.dev/ogl";
import * as dat from "../lib/dat.gui.module.js";
import { loadShaders, Common } from "../../build/glNoise.m.js";
import { loadShaders, Common, All } from "../../build/glNoise.m.js";

const paths = ["./shader_f.glsl", "./shader_v.glsl"];

Expand All @@ -9,7 +9,9 @@ precision highp float;
${Common}
`;

loadShaders(paths, null, [head, head]).then(([fragment, vertex]) => {
const chunks = [All, All];

loadShaders(paths, chunks, [head, head]).then(([fragment, vertex]) => {
const renderer = new Renderer();
const gl = renderer.gl;
document.body.appendChild(gl.canvas);
Expand Down
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<a class="Example" sec="misc" selected="true" type="0" href="./Planet/3D.html">Planet</a>
<a class="Example" sec="misc" selected="false" type="0" href="./3D/3D.html">Cube</a>
<a class="Example" sec="misc" selected="false" type="0" href="./Waves/3D.html">Waves</a>
<a class="Example" sec="misc" selected="false" type="0" href="./Curl/index.html">Curl</a>

<div class="Section">Misc</div>
<a class="Example" sec="misc" selected="false" type="0" href="./BlendModes/blend.html">Blend Modes</a>
Expand Down
Loading

0 comments on commit 9946924

Please sign in to comment.