Scene3d engine - fog support; miner-d example fixes for fog

This commit is contained in:
Vadim Lopatin 2016-08-17 12:29:20 +03:00
parent 5b60f6a011
commit 737ddfbfbc
7 changed files with 106 additions and 43 deletions

View File

@ -94,12 +94,16 @@ X<-----x-----
3 | 2 3 | 2
*/ */
private immutable float CCC = 0.5; // cell cube coordinates
private immutable float TC0 = 0.03;
private immutable float TC1 = 0.97;
__gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_north = __gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_north =
[ [
0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, CCC, CCC, -CCC, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, TC0, TC0,
-0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, -CCC, CCC, -CCC, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, TC1, TC0,
-0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -CCC, -CCC, -CCC, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, TC1, TC1,
0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, CCC, -CCC, -CCC, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, TC0, TC1,
]; ];
/* South, z=1 /* South, z=1
@ -111,10 +115,10 @@ __gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_north =
__gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_south = __gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_south =
[ [
-0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, -CCC, CCC, CCC, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, TC0, TC0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, CCC, CCC, CCC, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, TC1, TC0,
0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, CCC, -CCC, CCC, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, TC1, TC1,
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, -CCC, -CCC, CCC, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, TC0, TC1,
]; ];
/* West, x=-1 /* West, x=-1
@ -126,10 +130,10 @@ __gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_south =
__gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_west = __gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_west =
[ [
-0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, -CCC, CCC, -CCC, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC0, TC0,
-0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, -CCC, CCC, CCC, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC0,
-0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -CCC, -CCC, CCC, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC1,
-0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0 -CCC, -CCC, -CCC, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC0, TC1
]; ];
/* East, x=1 /* East, x=1
@ -141,10 +145,10 @@ Z<-----x-----
__gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_east = __gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_east =
[ [
0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, CCC, CCC, CCC, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC0, TC0,
0.5, 0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, CCC, CCC, -CCC, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC0,
0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, CCC, -CCC, -CCC, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC1,
0.5, -0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, CCC, -CCC, CCC, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC0, TC1,
]; ];
/* Up, y=1 /* Up, y=1
@ -157,10 +161,10 @@ __gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_east =
__gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_up = __gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_up =
[ [
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, -CCC, CCC, -CCC, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC0, TC0,
0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, CCC, CCC, -CCC, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, CCC, CCC, CCC, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC1,
-0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, -CCC, CCC, CCC, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC0, TC1,
]; ];
/* Down, y=-1 /* Down, y=-1
@ -172,10 +176,10 @@ X<-----x-----
__gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_down = __gshared static const float[VERTEX_COMPONENTS * 4] face_vertices_down =
[ [
0.5, -0.5,-0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, CCC, -CCC,-CCC, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC0, TC0,
-0.5, -0.5,-0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, -CCC, -CCC,-CCC, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC0,
-0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -CCC, -CCC, CCC, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC1,
0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, CCC, -CCC, CCC, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC0, TC1,
]; ];
__gshared static const ushort[6] face_indexes = __gshared static const ushort[6] face_indexes =

View File

@ -299,8 +299,6 @@ struct SmallChunk {
if (!_minerMesh) { if (!_minerMesh) {
_minerMesh = new Mesh(VertexFormat(VertexElementType.POSITION, VertexElementType.NORMAL, VertexElementType.COLOR, VertexElementType.TEXCOORD0)); _minerMesh = new Mesh(VertexFormat(VertexElementType.POSITION, VertexElementType.NORMAL, VertexElementType.COLOR, VertexElementType.TEXCOORD0));
import dlangui.core.logger;
//Log.d("Created mesh: ", cast(void*)_minerMesh);
} }
BlockDef def = BLOCK_DEFS[cells[index]]; BlockDef def = BLOCK_DEFS[cells[index]];
@ -312,17 +310,9 @@ struct SmallChunk {
} }
dirtyMesh = false; dirtyMesh = false;
} }
if (_minerMesh) { if (_minerMesh && !_minerMesh.vertexCount) {
if (_minerMesh.vertexFormat.length > 10) { destroy(_minerMesh);
import dlangui.core.logger; _minerMesh = null;
Log.d("Corrupted mesh: ", oldMesh);
}
if (oldMesh && (oldMesh !is _minerMesh)) {
import dlangui.core.logger;
Log.d("Corrupted mesh: ", oldMesh);
}
if (!_minerMesh.vertexCount)
return null;
} }
return _minerMesh; return _minerMesh;
} }

View File

@ -156,7 +156,7 @@ class UiWidget : VerticalLayout { //, CellVisitor
dirLightNode.translateZ(0); dirLightNode.translateZ(0);
dirLightNode.light = Light.createPoint(vec3(1.0, 1.0, 1.0), 35); //Light.createDirectional(vec3(1, 0.5, 0.5)); dirLightNode.light = Light.createPoint(vec3(1.0, 1.0, 1.0), 35); //Light.createDirectional(vec3(1, 0.5, 0.5));
//dirLightNode.light = Light.createDirectional(vec3(1, 0.5, 0.5)); //dirLightNode.light = Light.createDirectional(vec3(1, 0.5, 0.5));
dirLightNode.light.enabled = true; dirLightNode.light.enabled = false;
_scene.addChild(dirLightNode); _scene.addChild(dirLightNode);
@ -209,6 +209,7 @@ class UiWidget : VerticalLayout { //, CellVisitor
Material minerMaterial = new Material(EffectId("textured.vert", "textured.frag", null), "blocks"); Material minerMaterial = new Material(EffectId("textured.vert", "textured.frag", null), "blocks");
minerMaterial.ambientColor = vec3(0.1,0.1,0.1); minerMaterial.ambientColor = vec3(0.1,0.1,0.1);
minerMaterial.textureLinear = false; minerMaterial.textureLinear = false;
minerMaterial.fogParams = new FogParams(vec4(0.1, 0.1, 0.1, 1), 12, 60);
//minerMaterial.specular = 10; //minerMaterial.specular = 10;
_minerDrawable = new MinerDrawable(_world, minerMaterial, _cam); _minerDrawable = new MinerDrawable(_world, minerMaterial, _cam);
//Model minerDrawable = new Model(minerMaterial, _minerMesh); //Model minerDrawable = new Model(minerMaterial, _minerMesh);

View File

@ -135,12 +135,14 @@ class Material : RefCountedObject {
return this; return this;
} }
FogParams _fogParams;
@property FogParams fogParams() { return _fogParams; }
@property Material fogParams(FogParams fogParams) { _fogParams = fogParams; return this; }
private AutoParams _lastParams; private AutoParams _lastParams;
private string _lastDefs; private string _lastDefs;
string calcAutoEffectParams(Mesh mesh, LightParams * lights) { string calcAutoEffectParams(Mesh mesh, LightParams * lights) {
AutoParams newParams = AutoParams(mesh, lights, _specular, !bumpTexture.isNull); AutoParams newParams = AutoParams(mesh, lights, _specular, !bumpTexture.isNull, _fogParams);
if (newParams != _lastParams) { if (newParams != _lastParams) {
_lastParams = newParams; _lastParams = newParams;
_lastDefs = _lastParams.defs; _lastDefs = _lastParams.defs;
@ -182,6 +184,16 @@ class Material : RefCountedObject {
if (_effect.hasUniform(DefaultUniform.u_specularExponent)) if (_effect.hasUniform(DefaultUniform.u_specularExponent))
_effect.setUniform(DefaultUniform.u_specularExponent, _specular); _effect.setUniform(DefaultUniform.u_specularExponent, _specular);
// fog uniforms
if (_fogParams) {
if (_effect.hasUniform(DefaultUniform.u_fogColor))
_effect.setUniform(DefaultUniform.u_fogColor, _fogParams.fogColor);
if (_effect.hasUniform(DefaultUniform.u_fogMinDistance))
_effect.setUniform(DefaultUniform.u_fogMinDistance, _fogParams.fogMinDistance);
if (_effect.hasUniform(DefaultUniform.u_fogMaxDistance))
_effect.setUniform(DefaultUniform.u_fogMaxDistance, _fogParams.fogMaxDistance);
}
// lighting uniforms // lighting uniforms
if (lights && !lights.empty) { if (lights && !lights.empty) {
if (lights.u_directionalLightDirection.length) { if (lights.u_directionalLightDirection.length) {
@ -239,7 +251,8 @@ struct AutoParams {
bool vertexColor = false; bool vertexColor = false;
bool specular = false; bool specular = false;
bool bumpMapping = false; bool bumpMapping = false;
this(Mesh mesh, LightParams * lights, float specular, bool bumpMapping) { FogParams fogParams;
this(Mesh mesh, LightParams * lights, float specular, bool bumpMapping, FogParams fogParams) {
if (mesh) if (mesh)
vertexColor = mesh.hasElement(VertexElementType.COLOR); vertexColor = mesh.hasElement(VertexElementType.COLOR);
if (lights) { if (lights) {
@ -249,11 +262,17 @@ struct AutoParams {
} }
this.specular = specular > 0.01; this.specular = specular > 0.01;
this.bumpMapping = bumpMapping; this.bumpMapping = bumpMapping;
this.fogParams = fogParams;
} }
string defs() { string defs() {
import std.conv : to; import std.conv : to;
char[] buf; char[] buf;
if (fogParams) {
buf ~= "FOG";
}
if (directionalLightCount) { if (directionalLightCount) {
if (buf.length)
buf ~= ";";
buf ~= "DIRECTIONAL_LIGHT_COUNT "; buf ~= "DIRECTIONAL_LIGHT_COUNT ";
buf ~= directionalLightCount.to!string; buf ~= directionalLightCount.to!string;
} }
@ -287,3 +306,14 @@ struct AutoParams {
return buf.dup; return buf.dup;
} }
} }
class FogParams {
immutable vec4 fogColor;
immutable float fogMinDistance;
immutable float fogMaxDistance;
this(vec4 fogColor, float fogMinDistance, float fogMaxDistance) {
this.fogColor = fogColor;
this.fogMinDistance = fogMinDistance;
this.fogMaxDistance = fogMaxDistance;
}
}

View File

@ -92,6 +92,11 @@ enum DefaultUniform : int {
u_modulateColor, //uniform vec4 u_modulateColor; u_modulateColor, //uniform vec4 u_modulateColor;
u_modulateAlpha, //uniform float u_modulateAlpha; u_modulateAlpha, //uniform float u_modulateAlpha;
// fog
u_fogColor, // uniform vec4 u_fogColor
u_fogMinDistance, // uniform float u_fogMinDistance
u_fogMaxDistance, // uniform float u_fogMaxDistance
// matrix // matrix
u_worldViewProjectionMatrix, //uniform mat4 u_worldViewProjectionMatrix; u_worldViewProjectionMatrix, //uniform mat4 u_worldViewProjectionMatrix;
u_inverseTransposeWorldViewMatrix, //uniform mat4 u_inverseTransposeWorldViewMatrix; u_inverseTransposeWorldViewMatrix, //uniform mat4 u_inverseTransposeWorldViewMatrix;

View File

@ -72,6 +72,12 @@ uniform vec4 u_modulateColor;
uniform float u_modulateAlpha; uniform float u_modulateAlpha;
#endif #endif
#if defined(FOG)
uniform vec4 u_fogColor;
uniform float u_fogMinDistance;
uniform float u_fogMaxDistance;
#endif
/////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////
// Variables // Variables
vec4 _baseColor; vec4 _baseColor;
@ -117,6 +123,9 @@ varying vec3 v_cameraDirection;
varying float v_clipDistance; varying float v_clipDistance;
#endif #endif
#if defined(FOG)
varying vec4 viewSpace;
#endif
void main() void main()
{ {
@ -126,6 +135,17 @@ void main()
_baseColor = texture2D(u_diffuseTexture, v_texCoord); _baseColor = texture2D(u_diffuseTexture, v_texCoord);
#if defined(FOG)
float dist = 0;
float fogFactor = 0;
//range based
dist = length(viewSpace);
// linear fog: u_fogMinDistance .. u_fogMaxDistance
fogFactor = (u_fogMaxDistance - dist) / (u_fogMaxDistance - u_fogMinDistance);
fogFactor = clamp( fogFactor, 0.0, 1.0 );
_baseColor = mix(u_fogColor, _baseColor, fogFactor);
#endif
gl_FragColor.a = _baseColor.a; gl_FragColor.a = _baseColor.a;
#if defined(TEXTURE_DISCARD_ALPHA) #if defined(TEXTURE_DISCARD_ALPHA)

View File

@ -46,7 +46,7 @@ uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
#if defined(LIGHTING) #if defined(LIGHTING)
uniform mat4 u_inverseTransposeWorldViewMatrix; uniform mat4 u_inverseTransposeWorldViewMatrix;
#if defined(SPECULAR) || (POINT_LIGHT_COUNT > 0) || (SPOT_LIGHT_COUNT > 0) #if defined(FOG) || defined(SPECULAR) || (POINT_LIGHT_COUNT > 0) || (SPOT_LIGHT_COUNT > 0)
uniform mat4 u_worldViewMatrix; uniform mat4 u_worldViewMatrix;
#endif #endif
@ -69,6 +69,12 @@ uniform vec3 u_spotLightDirection[SPOT_LIGHT_COUNT];
uniform vec3 u_cameraPosition; uniform vec3 u_cameraPosition;
#endif #endif
#else
#if defined(FOG)
uniform mat4 u_worldViewMatrix;
#endif
#endif #endif
#if defined(TEXTURE_REPEAT) #if defined(TEXTURE_REPEAT)
@ -131,9 +137,16 @@ varying vec3 v_cameraDirection;
varying float v_clipDistance; varying float v_clipDistance;
#endif #endif
#if defined(FOG)
varying vec4 viewSpace;
#endif
void main() void main()
{ {
vec4 position = getPosition(); vec4 position = getPosition();
#if defined(FOG)
viewSpace = u_worldViewMatrix * position;
#endif
gl_Position = u_worldViewProjectionMatrix * position; gl_Position = u_worldViewProjectionMatrix * position;
#if defined(LIGHTING) #if defined(LIGHTING)