Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
example of custom shader, fireball!
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentfretin committed Oct 29, 2017
1 parent 35c8bb8 commit 9b3e6ff
Show file tree
Hide file tree
Showing 9 changed files with 568 additions and 30 deletions.
6 changes: 4 additions & 2 deletions config/webpack.config.dev.js
Expand Up @@ -113,7 +113,8 @@ module.exports = {
/\.(js|jsx)(\?.*)?$/,
/\.css$/,
/\.json$/,
/\.svg$/
/\.svg$/,
/\.glsl$/
],
loader: 'url',
query: {
Expand Down Expand Up @@ -156,7 +157,8 @@ module.exports = {
query: {
name: 'static/media/[name].[hash:8].[ext]'
}
}
},
{ test: /\.glsl$/, loader: 'raw!glslify', exclude: /node_modules/ },
// ** STOP ** Are you adding a new loader?
// Remember to add the new extension(s) to the "url" loader exclusion list.
]
Expand Down
6 changes: 4 additions & 2 deletions config/webpack.config.prod.js
Expand Up @@ -111,7 +111,8 @@ module.exports = {
/\.(js|jsx)$/,
/\.css$/,
/\.json$/,
/\.svg$/
/\.svg$/,
/\.glsl$/
],
loader: 'url',
query: {
Expand Down Expand Up @@ -160,7 +161,8 @@ module.exports = {
query: {
name: 'static/media/[name].[hash:8].[ext]'
}
}
},
{ test: /\.glsl$/, loader: 'raw!glslify', exclude: /node_modules/ },
// ** STOP ** Are you adding a new loader?
// Remember to add the new extension(s) to the "url" loader exclusion list.
]
Expand Down
6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "Boilerplate for building virtual reality experiences with A-Frame and React.",
"dependencies": {
"aframe": "vincentfretin/aframe",
"aframe": "aframevr/aframe",
"aframe-animation-component": "^3.2.5",
"aframe-ar": "chenzlabs/aframe-ar",
"aframe-environment-component": "vincentfretin/aframe-environment-component",
Expand All @@ -14,6 +14,7 @@
"aframe-react": "^4.0.x",
"aframe-teleport-controls": "^0.3.0",
"babel-polyfill": "^6.3.14",
"glsl-noise": "^0.0.0",
"react": "^15.4.1",
"react-dom": "^15.4.1",
"react-hot-loader": "^3.0.0-beta.7",
Expand Down Expand Up @@ -73,13 +74,16 @@
"file-loader": "0.10.0",
"fs-extra": "0.30.0",
"gh-pages": "^0.12.0",
"glslify": "^6.1.0",
"glslify-loader": "^1.0.2",
"html-webpack-plugin": "2.24.0",
"http-proxy-middleware": "0.17.3",
"jest": "18.1.0",
"json-loader": "0.5.4",
"object-assign": "4.1.1",
"postcss-loader": "1.2.2",
"promise": "7.1.1",
"raw-loader": "^0.5.1",
"react-dev-utils": "^0.5.2",
"style-loader": "0.13.1",
"url-loader": "0.5.7",
Expand Down
22 changes: 20 additions & 2 deletions src/App.js
Expand Up @@ -7,6 +7,7 @@ import "aframe-physics-system";
// import "aframe-extras";
import "aframe-event-set-component";
import "super-hands";
import "./material-displacement";
import { Entity, Scene } from "aframe-react";
import React from "react";

Expand Down Expand Up @@ -174,6 +175,20 @@ class App extends React.Component {
material="color: green"
/>

<a-sphere
material-displacement
position="0.8 1 -2"
radius="0.4"
segments-height="18"
segments-width="36"
// wireframe="true"
hoverable
grabbable
draggable
droppable
dynamic-body
/>

{/* <Entity primitive="a-camera">
<Entity
primitive="a-cursor"
Expand All @@ -186,7 +201,10 @@ class App extends React.Component {
}}
/>
</Entity> */}
<a-entity id="cameraRig" progressive-controls="objects: .cube">
<a-entity
id="cameraRig"
progressive-controls="objects: .cube,[grabbable]"
>
<a-entity
class="right-controller"
teleport-controls="cameraRig: #cameraRig; button: trigger; maxLength: 200; type: line; collisionEntities: .environmentGround, .environmentDressing, .cube"
Expand All @@ -203,7 +221,7 @@ class App extends React.Component {
teleport-controls="cameraRig: #cameraRig; button: trigger; maxLength: 200; type: line; collisionEntities: .environmentGround, .environmentDressing, .cube"
// gearvr-controls
laser-controls
raycaster="objects: .cube"
raycaster="objects: .cube,[grabbable]"
// line="color: red; opacity: 0.75"
super-hands={superHandsRaycasterConfig}
// super-hands="colliderEvent: raycaster-intersection; colliderEventProperty: els; colliderEndEvent: raycaster-intersection-cleared; colliderEndEventProperty: clearedEls; colliderState:"
Expand Down
44 changes: 44 additions & 0 deletions src/material-displacement.js
@@ -0,0 +1,44 @@
/* global AFRAME, THREE */

const vertexShader = require("./shaders/vertex.glsl");
const fragmentShader = require("./shaders/fragment.glsl");

AFRAME.registerComponent("material-displacement", {
/**
* Creates a new THREE.ShaderMaterial using the two shaders defined
* in vertex.glsl and fragment.glsl.
*/
init: function() {
this.material = new THREE.ShaderMaterial({
uniforms: {
tExplosion: {
type: "t",
value: THREE.ImageUtils.loadTexture(
require("./shaders/explosion.png")
)
},
time: { type: "f", value: 0.0 }
},
vertexShader,
fragmentShader
});
this.el.addEventListener("model-loaded", () => this.update());
},

/**
* Apply the material to the current entity.
*/
update: function() {
const mesh = this.el.getObject3D("mesh");
if (mesh) {
mesh.material = this.material;
}
},

/**
* On each frame, update the 'time' uniform in the shaders.
*/
tick: function(t) {
this.material.uniforms.time.value = t / 2000;
}
});
Binary file added src/shaders/explosion.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions src/shaders/fragment.glsl
@@ -0,0 +1,16 @@
varying float noise;
uniform sampler2D tExplosion;

float random( vec3 scale, float seed ){
return fract( sin( dot( gl_FragCoord.xyz + seed, scale ) ) * 43758.5453 + seed ) ;
}

void main() {
// get a random offset
float r = .01 * random( vec3( 12.9898, 78.233, 151.7182 ), 0.0 );
// lookup vertically in the texture, using noise and offset
// to get the right RGB colour
vec2 tPos = vec2( 0, 1.0 - 1.3 * noise + r );
vec4 color = texture2D( tExplosion, tPos );
gl_FragColor = vec4( color.rgb, 1.0 );
}
34 changes: 34 additions & 0 deletions src/shaders/vertex.glsl
@@ -0,0 +1,34 @@
#pragma glslify: pnoise3 = require(glsl-noise/periodic/3d)

//
// Based on @thespite's article:
//
// "Vertex displacement with a noise function using GLSL and three.js"
// Source: https://www.clicktorelease.com/blog/vertex-displacement-noise-3d-webgl-glsl-three-js/
//

varying float noise;
uniform float time;

float turbulence( vec3 p ) {

float w = 100.0;
float t = -.5;

for (float f = 1.0 ; f <= 10.0 ; f++ ){
float power = pow( 2.0, f );
t += abs( pnoise3( vec3( power * p ), vec3( 10.0, 10.0, 10.0 ) ) / power );
}

return t;

}

void main() {
noise = 10.0 * -.10 * turbulence( .5 * normal + time / 3.0 );
float b = 5.0 * pnoise3( 0.05 * position, vec3( 100.0 ) );
float displacement = (- 10. * noise + b) / 50.0;

vec3 newPosition = position + normal * displacement;
gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );
}

0 comments on commit 9b3e6ff

Please sign in to comment.