Skip to content

Random textures #2

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions 5 Textures/Test_RandomTexture_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import graphics.core.*;
import graphics.math.*;
import graphics.geometry.*;
import graphics.material.*;
import graphics.extras.*;
import graphics.light.*;
import graphics.font.*;

public class Test_RandomTexture_1 extends Base {
public Renderer renderer;
public Scene scene;
public Camera camera;
public Mesh mesh;
public MovementRig rig;
public Material waterMat;

public void initialize() {
renderer = new Renderer();
scene = new Scene();
camera = new Camera();
camera.setPosition( new Vector(0, 0, 4) );

Geometry rect = new RectangleGeometry(1.5, 1.5);
waterMat = new RandomTexture();

waterMat.addUniform("bool", "useSmooth", 1);
waterMat.locateUniforms();

mesh = new Mesh(rect, waterMat);
scene.add(mesh);
}

public void update() {
waterMat.uniforms.get("time").data = time;
renderer.render(scene, camera);
}

// driver method
public static void main(String[] args) {
new Test_RandomTexture_1().run();
}

}

82 changes: 82 additions & 0 deletions 5 Textures/Test_RandomTexture_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import graphics.core.*;
import graphics.math.*;
import graphics.geometry.*;
import graphics.material.*;
import graphics.extras.*;
import graphics.light.*;
import graphics.font.*;

import java.awt.*;

public class Test_RandomTexture_2 extends Base
{
public Renderer renderer;
public Scene scene;
public Camera camera;
public Mesh skyBox;
public MovementRig rig;
public Material liquidMat, skyMat;

public void initialize()
{
renderer = new Renderer();
scene = new Scene();
camera = new Camera();

rig = new MovementRig();
rig.attach( camera );
rig.setPosition( new Vector(0.5, 2, 4) );
scene.add( rig );

// SkyBox
Geometry skySphere = new SphereGeometry(50);
skyMat = new RandomTexture();

skyMat.addUniform("bool", "cloud", 1);
skyMat.locateUniforms();

skyBox = new Mesh(skySphere, skyMat);
scene.add(skyBox);

// Water
Geometry liquidSurface = new EllipsoidGeometry(110, 0.1, 110, 128, 128);
liquidMat = new RandomTexture();

// Make liquid water
liquidMat.addUniform("bool", "water", 1);
liquidMat.addUniform("bool", "waves", 1);
liquidMat.locateUniforms();

Mesh liquid = new Mesh(liquidSurface, liquidMat);
liquid.setPosition(new Vector(0, 0.4 , 0));
scene.add(liquid);

// Island
Geometry islandSphere = new EllipsoidGeometry(20, 2, 17, 32, 32);
Material islandMat = new TextureMaterial(new Texture("images/grass.jpg"));
Mesh island = new Mesh(islandSphere, islandMat);
island.setPosition(new Vector(0, 0.25, 0));
scene.add(island);
}

public void update()
{
// Rotate skyBox
//skyBox.rotateY(0.000337, true);

// Animate liquid and sky
skyMat.uniforms.get("time").data = 0.1f * time;
liquidMat.uniforms.get("time").data = 1.4f * time;

rig.update(input, deltaTime);
renderer.render(scene, camera);
}

// driver method
public static void main(String[] args)
{
new Test_RandomTexture_2().run();
}

}

111 changes: 111 additions & 0 deletions 5 Textures/graphics/material/RandomTexture.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Return a random value in [0, 1]
float random(vec2 UV, float time) {
return fract(235711.0 * sin(14.337*UV.x + 42.418*UV.y) + 0.2*time );
}//end random

float boxRandom(vec2 UV, float scale, float time) {
vec2 iScaleUV = floor(scale * UV);

if(iScaleUV.x == (scale - 1))
iScaleUV.x = 0;

return random(iScaleUV, time);
}//end boxRandom

float smoothRandom(vec2 UV, float scale, float time) {
vec2 iScaleUV = floor(scale * UV);
vec2 fScaleUV = fract(scale * UV);

float a = random(round(iScaleUV + vec2(0,0)), time);
float b = random(round(iScaleUV + vec2(1,0)), time);
float c = random(round(iScaleUV + vec2(0,1)), time);
float d = random(round(iScaleUV + vec2(1,1)), time);

if(iScaleUV.x == (scale - 1)) {
b = random(round(iScaleUV - vec2(scale - 1, 0) ), time);
d = random(round(iScaleUV - vec2(iScaleUV.x - scale, 1 ) ), time);
}

return mix(mix(a, b, fScaleUV.x),
mix(c, d, fScaleUV.x),
fScaleUV.y);
}//end smoothRandom

/*
* Add smooth random values at different scales
* Weighted (amplitudes) so the sum is approx. 1.0
*/
float fractalRandom(vec2 UV, float scale, float time) {
float value = 0.0;
float amplitude = 0.5;

for(int i = 0; i < 6; i++) {
value += amplitude * smoothRandom(UV, scale, time);
scale *= 2.0;
amplitude *= 0.5;
}

return value;
}//end fractalRandom


uniform bool useBox;
uniform bool useSmooth;
uniform bool useFractal;
uniform bool lava;
uniform bool cloud;
uniform bool water;

in vec2 UV;
uniform float time;
out vec4 fragColor;

void main() {

// Default settings
float r = random(UV, time);

if (useBox)
r = boxRandom(UV, 6, time);

else if (useSmooth)
r = smoothRandom(UV, 4, time);

else if (useFractal)
r = fractalRandom(UV, 4, time);

// Send out basic function
fragColor = vec4(r, r, r, 1);

// Pre-designed randomized textures are below
if (lava) {
r = fractalRandom(UV, 40, time);
vec4 color1 = vec4(1, 0.8, 0, 1);
vec4 color2 = vec4(0.8, 0, 0, 1);

fragColor = mix(color1, color2, r);
}

else if (water) {
r = fractalRandom(UV, 40, time);
vec4 color1 = vec4(0.0, 0.0, 0.8, 1); // Dark Blue
vec4 color2 = vec4(0.3, 0.4, 0.9, 1); // Lighter blue

fragColor = mix(color1, color2, r);
}

else if (cloud) {
r = fractalRandom(UV, 25, time);
vec4 color1 = vec4(0.1, 0.2, 1, 1); // Blue
vec4 color2 = vec4(1, 1, 1, 1); // White

vec4 sky = mix(color1, color2, r);

vec4 horizon = vec4(0.4, 0.6, 1, 1);// Light blue
float amount = -2 * UV.y + 2;
amount = pow(amount, 7);

fragColor = mix(sky, horizon, amount);
}

}//end main
21 changes: 21 additions & 0 deletions 5 Textures/graphics/material/RandomTexture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import graphics.core.Texture;
import graphics.math.Vector;

import static org.lwjgl.opengl.GL40.*;

public class RandomTexture extends Material {
public RandomTexture() {
super(
"graphics/material/RandomTexture.vert",
"graphics/material/RandomTexture.frag");

addRenderSetting( "doubleSide", true );
addRenderSetting( "wireframe", false );
addRenderSetting( "lineWidth", 1 );

// Defaults
addUniform("float", "time", (float)0);

locateUniforms();
}
}
24 changes: 24 additions & 0 deletions 5 Textures/graphics/material/RandomTexture.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

uniform bool waves;

uniform float time;
in vec3 vertexPosition;
in vec2 vertexUV;
out vec2 UV;

void main() {

vec4 pos = vec4(vertexPosition, 1.0);
gl_Position = projectionMatrix * viewMatrix * modelMatrix * pos;
UV = vertexUV;

if (waves) {
float offset = 0.1 * sin(0.5 * vertexPosition.x + time);
vec3 pos = vertexPosition + vec3(0.0, offset, 0.0);
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(pos, 1);
}

}//end main
Loading