diff --git a/examples/dminer/src/dminer/core/blocks.d b/examples/dminer/src/dminer/core/blocks.d index c659e55c..306670e4 100644 --- a/examples/dminer/src/dminer/core/blocks.d +++ b/examples/dminer/src/dminer/core/blocks.d @@ -94,12 +94,16 @@ X<-----x----- 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 = [ - 0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, - -0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, - -0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - 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, TC0, + -CCC, CCC, -CCC, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, TC1, TC0, + -CCC, -CCC, -CCC, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, TC1, TC1, + CCC, -CCC, -CCC, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, TC0, TC1, ]; /* 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 = [ - -0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, - 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, - 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - -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, TC0, + CCC, CCC, CCC, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, TC1, TC0, + CCC, -CCC, CCC, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, TC1, TC1, + -CCC, -CCC, CCC, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, TC0, TC1, ]; /* 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 = [ - -0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, - -0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, - -0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - -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, TC0, + -CCC, CCC, CCC, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC0, + -CCC, -CCC, CCC, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC1, + -CCC, -CCC, -CCC, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC0, TC1 ]; /* East, x=1 @@ -141,10 +145,10 @@ Z<-----x----- __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, - 0.5, 0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, - 0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - 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, TC0, + CCC, CCC, -CCC, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC0, + CCC, -CCC, -CCC, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC1, + CCC, -CCC, CCC, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC0, TC1, ]; /* 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 = [ - -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, - 0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, - 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - -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, TC0, + CCC, CCC, -CCC, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC0, + CCC, CCC, CCC, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC1, + -CCC, CCC, CCC, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC0, TC1, ]; /* Down, y=-1 @@ -172,10 +176,10 @@ X<-----x----- __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, - -0.5, -0.5,-0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, - -0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - 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, TC0, + -CCC, -CCC,-CCC, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC0, + -CCC, -CCC, CCC, 0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, TC1, TC1, + 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 = diff --git a/examples/dminer/src/dminer/core/chunk.d b/examples/dminer/src/dminer/core/chunk.d index 2aed7442..9a503187 100644 --- a/examples/dminer/src/dminer/core/chunk.d +++ b/examples/dminer/src/dminer/core/chunk.d @@ -299,8 +299,6 @@ struct SmallChunk { if (!_minerMesh) { _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]]; @@ -312,17 +310,9 @@ struct SmallChunk { } dirtyMesh = false; } - if (_minerMesh) { - if (_minerMesh.vertexFormat.length > 10) { - import dlangui.core.logger; - Log.d("Corrupted mesh: ", oldMesh); - } - if (oldMesh && (oldMesh !is _minerMesh)) { - import dlangui.core.logger; - Log.d("Corrupted mesh: ", oldMesh); - } - if (!_minerMesh.vertexCount) - return null; + if (_minerMesh && !_minerMesh.vertexCount) { + destroy(_minerMesh); + _minerMesh = null; } return _minerMesh; } diff --git a/examples/dminer/src/minermain.d b/examples/dminer/src/minermain.d index 641b2b1d..693d7d74 100644 --- a/examples/dminer/src/minermain.d +++ b/examples/dminer/src/minermain.d @@ -156,7 +156,7 @@ class UiWidget : VerticalLayout { //, CellVisitor 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.createDirectional(vec3(1, 0.5, 0.5)); - dirLightNode.light.enabled = true; + dirLightNode.light.enabled = false; _scene.addChild(dirLightNode); @@ -209,6 +209,7 @@ class UiWidget : VerticalLayout { //, CellVisitor Material minerMaterial = new Material(EffectId("textured.vert", "textured.frag", null), "blocks"); minerMaterial.ambientColor = vec3(0.1,0.1,0.1); minerMaterial.textureLinear = false; + minerMaterial.fogParams = new FogParams(vec4(0.1, 0.1, 0.1, 1), 12, 60); //minerMaterial.specular = 10; _minerDrawable = new MinerDrawable(_world, minerMaterial, _cam); //Model minerDrawable = new Model(minerMaterial, _minerMesh); diff --git a/src/dlangui/graphics/scene/material.d b/src/dlangui/graphics/scene/material.d index 2f3c08eb..3a01a543 100644 --- a/src/dlangui/graphics/scene/material.d +++ b/src/dlangui/graphics/scene/material.d @@ -135,12 +135,14 @@ class Material : RefCountedObject { return this; } - + FogParams _fogParams; + @property FogParams fogParams() { return _fogParams; } + @property Material fogParams(FogParams fogParams) { _fogParams = fogParams; return this; } private AutoParams _lastParams; private string _lastDefs; 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) { _lastParams = newParams; _lastDefs = _lastParams.defs; @@ -182,6 +184,16 @@ class Material : RefCountedObject { if (_effect.hasUniform(DefaultUniform.u_specularExponent)) _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 if (lights && !lights.empty) { if (lights.u_directionalLightDirection.length) { @@ -239,7 +251,8 @@ struct AutoParams { bool vertexColor = false; bool specular = 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) vertexColor = mesh.hasElement(VertexElementType.COLOR); if (lights) { @@ -249,11 +262,17 @@ struct AutoParams { } this.specular = specular > 0.01; this.bumpMapping = bumpMapping; + this.fogParams = fogParams; } string defs() { import std.conv : to; char[] buf; + if (fogParams) { + buf ~= "FOG"; + } if (directionalLightCount) { + if (buf.length) + buf ~= ";"; buf ~= "DIRECTIONAL_LIGHT_COUNT "; buf ~= directionalLightCount.to!string; } @@ -287,3 +306,14 @@ struct AutoParams { 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; + } +} diff --git a/src/dlangui/graphics/scene/mesh.d b/src/dlangui/graphics/scene/mesh.d index c848a45e..acafd74c 100644 --- a/src/dlangui/graphics/scene/mesh.d +++ b/src/dlangui/graphics/scene/mesh.d @@ -92,6 +92,11 @@ enum DefaultUniform : int { u_modulateColor, //uniform vec4 u_modulateColor; 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 u_worldViewProjectionMatrix, //uniform mat4 u_worldViewProjectionMatrix; u_inverseTransposeWorldViewMatrix, //uniform mat4 u_inverseTransposeWorldViewMatrix; diff --git a/views/res/shaders/textured.frag b/views/res/shaders/textured.frag index cd137922..9a12f44b 100644 --- a/views/res/shaders/textured.frag +++ b/views/res/shaders/textured.frag @@ -72,6 +72,12 @@ uniform vec4 u_modulateColor; uniform float u_modulateAlpha; #endif +#if defined(FOG) +uniform vec4 u_fogColor; +uniform float u_fogMinDistance; +uniform float u_fogMaxDistance; +#endif + /////////////////////////////////////////////////////////// // Variables vec4 _baseColor; @@ -117,14 +123,28 @@ varying vec3 v_cameraDirection; varying float v_clipDistance; #endif +#if defined(FOG) +varying vec4 viewSpace; +#endif void main() { #if defined(CLIP_PLANE) if(v_clipDistance < 0.0) discard; #endif - + _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; diff --git a/views/res/shaders/textured.vert b/views/res/shaders/textured.vert index 00d72c9c..5ced5cdd 100644 --- a/views/res/shaders/textured.vert +++ b/views/res/shaders/textured.vert @@ -46,7 +46,7 @@ uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3]; #if defined(LIGHTING) 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; #endif @@ -69,6 +69,12 @@ uniform vec3 u_spotLightDirection[SPOT_LIGHT_COUNT]; uniform vec3 u_cameraPosition; #endif +#else + +#if defined(FOG) +uniform mat4 u_worldViewMatrix; +#endif + #endif #if defined(TEXTURE_REPEAT) @@ -131,9 +137,16 @@ varying vec3 v_cameraDirection; varying float v_clipDistance; #endif +#if defined(FOG) +varying vec4 viewSpace; +#endif + void main() { vec4 position = getPosition(); +#if defined(FOG) + viewSpace = u_worldViewMatrix * position; +#endif gl_Position = u_worldViewProjectionMatrix * position; #if defined(LIGHTING)