Created
December 7, 2013 22:37
-
-
Save xoppa/7850316 to your computer and use it in GitHub Desktop.
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
package com.badlogic.gdx.tests.g3d; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.assets.AssetManager; | |
import com.badlogic.gdx.graphics.Color; | |
import com.badlogic.gdx.graphics.GL10; | |
import com.badlogic.gdx.graphics.PerspectiveCamera; | |
import com.badlogic.gdx.graphics.Texture; | |
import com.badlogic.gdx.graphics.VertexAttributes.Usage; | |
import com.badlogic.gdx.graphics.g3d.Environment; | |
import com.badlogic.gdx.graphics.g3d.Material; | |
import com.badlogic.gdx.graphics.g3d.Model; | |
import com.badlogic.gdx.graphics.g3d.ModelBatch; | |
import com.badlogic.gdx.graphics.g3d.ModelInstance; | |
import com.badlogic.gdx.graphics.g3d.Renderable; | |
import com.badlogic.gdx.graphics.g3d.Shader; | |
import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute; | |
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; | |
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute; | |
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight; | |
import com.badlogic.gdx.graphics.g3d.environment.DirectionalShadowLight; | |
import com.badlogic.gdx.graphics.g3d.environment.PointLight; | |
import com.badlogic.gdx.graphics.g3d.model.Node; | |
import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader; | |
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController; | |
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; | |
import com.badlogic.gdx.math.MathUtils; | |
import com.badlogic.gdx.math.Vector3; | |
import com.badlogic.gdx.tests.utils.GdxTest; | |
import com.badlogic.gdx.utils.Array; | |
public class Fire3DTest extends GdxTest { | |
// Based on: http://www.clicktorelease.com/blog/vertex-displacement-noise-3d-webgl-glsl-three-js | |
public PerspectiveCamera cam; | |
public CameraInputController camController; | |
public ModelBatch modelBatch; | |
public Model explosionModel; | |
public Shader explosionShader; | |
public ModelInstance explosion; | |
public PointLight explodeLight; | |
public AssetManager assets; | |
public Array<ModelInstance> instances = new Array<ModelInstance>(); | |
public Environment lights; | |
public boolean loading; | |
public Array<ModelInstance> blocks = new Array<ModelInstance>(); | |
public Array<ModelInstance> invaders = new Array<ModelInstance>(); | |
public ModelInstance ship; | |
public ModelInstance space; | |
@Override | |
public void create () { | |
lights = new Environment(); | |
lights.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f)); | |
lights.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f)); | |
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); | |
cam.position.set(0f, 7f, 10f); | |
cam.lookAt(0,0,0); | |
cam.near = 0.1f; | |
cam.far = 300f; | |
cam.update(); | |
modelBatch = new ModelBatch(); | |
Texture texture = new Texture(Gdx.files.internal("data/g3d/fireball.png")); | |
ModelBuilder modelBuilder = new ModelBuilder(); | |
explosionModel = modelBuilder.createSphere(2f, 2f, 2f, 20, 20, | |
new Material(TextureAttribute.createDiffuse(texture), new BlendingAttribute()), | |
Usage.Position | Usage.Normal | Usage.TextureCoordinates); | |
explosionModel.manageDisposable(texture); | |
Renderable renderable = new Renderable(); | |
explosionModel.nodes.get(0).parts.get(0).setRenderable(renderable); | |
DefaultShader.Config config = new DefaultShader.Config( | |
Gdx.files.internal("data/g3d/fireball.vertex.glsl").readString(), | |
Gdx.files.internal("data/g3d/fireball.fragment.glsl").readString()); | |
config.numDirectionalLights = 0; | |
config.numPointLights = 0; | |
config.numSpotLights = 0; | |
explosionShader = new DefaultShader(renderable, config); | |
explosionShader.init(); | |
explodeLight = new PointLight().set(Color.ORANGE, Vector3.Zero, 30); | |
camController = new CameraInputController(cam); | |
Gdx.input.setInputProcessor(camController); | |
assets = new AssetManager(); | |
assets.load("data/g3d/invaders.g3dj", Model.class); | |
loading = true; | |
} | |
private void doneLoading() { | |
Model model = assets.get("data/g3d/invaders.g3dj", Model.class); | |
for (Node node : model.nodes) { | |
ModelInstance instance = new ModelInstance(model, node.id, true); | |
if (node.id.equals("space")) { | |
space = instance; | |
continue; | |
} | |
instances.add(instance); | |
if (node.id.equals("ship")) | |
ship = instance; | |
else if (node.id.startsWith("block")) | |
blocks.add(instance); | |
else if (node.id.startsWith("invader")) | |
invaders.add(instance); | |
} | |
explosion = new ModelInstance(explosionModel); | |
explode(); | |
//explosion.transform.set(invaders.get(7).transform); | |
//instances.removeValue(invaders.get(7), true); | |
//invaders.removeIndex(7); | |
loading = false; | |
} | |
float time; | |
@Override | |
public void render () { | |
if (loading && assets.update()) | |
doneLoading(); | |
camController.update(); | |
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); | |
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); | |
modelBatch.begin(cam); | |
for (ModelInstance instance : instances) | |
modelBatch.render(instance, lights); | |
if (space != null) | |
modelBatch.render(space); | |
if (explosion != null) { | |
time += Gdx.graphics.getDeltaTime(); | |
float t = MathUtils.clamp(time, 0f, 1f); | |
((BlendingAttribute)explosion.materials.get(0).get(BlendingAttribute.Type)).opacity = MathUtils.cos(0.5f * MathUtils.PI * t); | |
explodeLight.intensity = 30f * ((BlendingAttribute)explosion.materials.get(0).get(BlendingAttribute.Type)).opacity; | |
float g = 0.5f + 0.5f * MathUtils.sin(2f * MathUtils.PI * t); | |
float r = g + 0.5f * MathUtils.cos(0.5f * MathUtils.PI * t); | |
explodeLight.color.set(r, g, 0, 1); | |
modelBatch.render(explosion, explosionShader); | |
if (t >= 0.9f && lights.pointLights.size > 0) | |
lights.pointLights.removeValue(explodeLight, true); | |
if (time >= 5.f) { | |
time = 0.f; | |
if (invaders.size == 0) | |
explosion = null; | |
else | |
explode(); | |
} | |
} | |
modelBatch.end(); | |
} | |
private final Vector3 tmpV = new Vector3(); | |
void explode() { | |
int idx = (int)(Math.random() * invaders.size); | |
invaders.get(idx).transform.getTranslation(tmpV); | |
explosion.transform.set(invaders.get(idx).transform); | |
instances.removeValue(invaders.get(idx), true); | |
invaders.removeIndex(idx); | |
explodeLight.position.set(tmpV); | |
lights.pointLights.add(explodeLight); | |
} | |
@Override | |
public void dispose () { | |
modelBatch.dispose(); | |
explosionModel.dispose(); | |
explosionShader.dispose(); | |
instances.clear(); | |
assets.dispose(); | |
} | |
public boolean needsGL20 () { | |
return true; | |
} | |
} |
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
#ifdef GL_ES | |
precision mediump float; | |
#endif | |
uniform sampler2D u_diffuseTexture; | |
uniform float u_opacity; | |
varying float v_noise; | |
float random(vec3 scale) { | |
return fract(sin(dot(gl_FragCoord.xyz, scale) ) * 43758.5453); | |
} | |
void main() { | |
float r = 0.006 * random(vec3(12.9898, 78.233, 151.7182)); | |
vec2 tPos = vec2(0, 0.85 + r + 0.8 * v_noise); | |
gl_FragColor = vec4(texture2D(u_diffuseTexture, tPos).rgb, u_opacity); | |
} |
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
attribute vec3 a_position; | |
attribute vec3 a_normal; | |
attribute vec2 a_texCoord0; | |
uniform mat4 u_projTrans; | |
uniform mat4 u_worldTrans; | |
uniform float u_time = 0.0; | |
uniform float u_radius = 1.0; | |
varying float v_noise; | |
float pnoise(vec3 P, vec3 rep); | |
float turbulence(in vec3 p) { | |
float t = -0.5; | |
for (float power = 2.0; power <= 8.0; power *= 2.0) | |
t += abs(pnoise(vec3(power * p), vec3(10.0))/power); | |
return t; | |
} | |
void main() { | |
float noise = turbulence(0.5 * (a_normal + u_time)); | |
vec3 position = a_position + a_normal * (0.75 * noise * u_radius); | |
v_noise = noise; | |
gl_Position = u_projTrans * u_worldTrans * vec4(position, 1.0); | |
} | |
// | |
// GLSL textureless classic 3D noise "cnoise", | |
// with an RSL-style periodic variant "pnoise". | |
// Author: Stefan Gustavson ([email protected]) | |
// Version: 2011-10-11 | |
// | |
// Many thanks to Ian McEwan of Ashima Arts for the | |
// ideas for permutation and gradient selection. | |
// | |
// Copyright (c) 2011 Stefan Gustavson. All rights reserved. | |
// Distributed under the MIT license. See LICENSE file. | |
// https://github.com/ashima/webgl-noise | |
// | |
vec3 mod289(vec3 x) | |
{ | |
return x - floor(x * (1.0 / 289.0)) * 289.0; | |
} | |
vec4 mod289(vec4 x) | |
{ | |
return x - floor(x * (1.0 / 289.0)) * 289.0; | |
} | |
vec4 permute(vec4 x) | |
{ | |
return mod289(((x*34.0)+1.0)*x); | |
} | |
vec4 taylorInvSqrt(vec4 r) | |
{ | |
return 1.79284291400159 - 0.85373472095314 * r; | |
} | |
vec3 fade(vec3 t) { | |
return t*t*t*(t*(t*6.0-15.0)+10.0); | |
} | |
// Classic Perlin noise, periodic variant | |
float pnoise(vec3 P, vec3 rep) | |
{ | |
vec3 Pi0 = mod(floor(P), rep); // Integer part, modulo period | |
vec3 Pi1 = mod(Pi0 + vec3(1.0), rep); // Integer part + 1, mod period | |
Pi0 = mod289(Pi0); | |
Pi1 = mod289(Pi1); | |
vec3 Pf0 = fract(P); // Fractional part for interpolation | |
vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0 | |
vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); | |
vec4 iy = vec4(Pi0.yy, Pi1.yy); | |
vec4 iz0 = Pi0.zzzz; | |
vec4 iz1 = Pi1.zzzz; | |
vec4 ixy = permute(permute(ix) + iy); | |
vec4 ixy0 = permute(ixy + iz0); | |
vec4 ixy1 = permute(ixy + iz1); | |
vec4 gx0 = ixy0 * (1.0 / 7.0); | |
vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5; | |
gx0 = fract(gx0); | |
vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0); | |
vec4 sz0 = step(gz0, vec4(0.0)); | |
gx0 -= sz0 * (step(0.0, gx0) - 0.5); | |
gy0 -= sz0 * (step(0.0, gy0) - 0.5); | |
vec4 gx1 = ixy1 * (1.0 / 7.0); | |
vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5; | |
gx1 = fract(gx1); | |
vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1); | |
vec4 sz1 = step(gz1, vec4(0.0)); | |
gx1 -= sz1 * (step(0.0, gx1) - 0.5); | |
gy1 -= sz1 * (step(0.0, gy1) - 0.5); | |
vec3 g000 = vec3(gx0.x,gy0.x,gz0.x); | |
vec3 g100 = vec3(gx0.y,gy0.y,gz0.y); | |
vec3 g010 = vec3(gx0.z,gy0.z,gz0.z); | |
vec3 g110 = vec3(gx0.w,gy0.w,gz0.w); | |
vec3 g001 = vec3(gx1.x,gy1.x,gz1.x); | |
vec3 g101 = vec3(gx1.y,gy1.y,gz1.y); | |
vec3 g011 = vec3(gx1.z,gy1.z,gz1.z); | |
vec3 g111 = vec3(gx1.w,gy1.w,gz1.w); | |
vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); | |
g000 *= norm0.x; | |
g010 *= norm0.y; | |
g100 *= norm0.z; | |
g110 *= norm0.w; | |
vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); | |
g001 *= norm1.x; | |
g011 *= norm1.y; | |
g101 *= norm1.z; | |
g111 *= norm1.w; | |
float n000 = dot(g000, Pf0); | |
float n100 = dot(g100, vec3(Pf1.x, Pf0.yz)); | |
float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z)); | |
float n110 = dot(g110, vec3(Pf1.xy, Pf0.z)); | |
float n001 = dot(g001, vec3(Pf0.xy, Pf1.z)); | |
float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z)); | |
float n011 = dot(g011, vec3(Pf0.x, Pf1.yz)); | |
float n111 = dot(g111, Pf1); | |
vec3 fade_xyz = fade(Pf0); | |
vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z); | |
vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y); | |
float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x); | |
return 2.2 * n_xyz; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment