Skip to content

Commit

Permalink
Fixed dirtiness logic in WebGLRenderer and the automatically detected…
Browse files Browse the repository at this point in the history
… errors

Also improved the example and added a thumbnail for it.
  • Loading branch information
EliasHasle committed Mar 28, 2023
1 parent 2075f87 commit 1dd9f2a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"webgl_geometry_spline_editor",
"webgl_geometry_teapot",
"webgl_geometry_terrain",
"webgl_geometry_terrain_fog_types",
"webgl_geometry_terrain_raycast",
"webgl_geometry_text",
"webgl_geometry_text_shapes",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 17 additions & 6 deletions examples/webgl_geometry_terrain_fog_types.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
background-color: #efd1b5;
color: #61443e;
}
body, #container {
overflow: hidden;
}
a {
color: #a06851;
}
Expand Down Expand Up @@ -40,6 +43,7 @@
import Stats from 'three/addons/libs/stats.module.js';
import { GUI } from './jsm/libs/lil-gui.module.min.js';

import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js';
import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js';

let container, stats;
Expand All @@ -53,7 +57,7 @@

var params = {
"Fog type": "DensityFog",
"Fog color": "#efd1b5",
"Fog color": "#b7dc50",
}

init();
Expand All @@ -63,21 +67,21 @@

container = document.getElementById( 'container' );

camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );

scene = new THREE.Scene();
scene.background = new THREE.Color();

fogs = {
"RangeFog": new THREE.RangeFog(),
"DensityFog": new THREE.DensityFog(),
}
"DensityFog": new THREE.DensityFog(undefined, 0.001, false),
};

setFogColor( params[ "Fog color" ] );

var data = generateHeight( worldWidth, worldDepth );

camera.position.y = data[ 0.5 * worldWidth + 0.5 * worldDepth * worldWidth ] * 10 + 500;
camera.position.y = data[ 0.5 * worldWidth + 0.5 * worldDepth * worldWidth ] * 10 + 300;

var geometry = new THREE.PlaneGeometry( 7500, 7500, worldWidth - 1, worldDepth - 1 );
geometry.rotateX( - Math.PI / 2 );
Expand All @@ -102,6 +106,10 @@
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );

controls = new FirstPersonControls( camera, renderer.domElement );
controls.movementSpeed = 100;
controls.lookSpeed = 0.03;

stats = new Stats();
container.appendChild( stats.dom );

Expand All @@ -121,7 +129,7 @@
fogGuis = {
"RangeFog": gui.addFolder( "RangeFog" ),
"DensityFog": gui.addFolder( "DensityFog" )
}
};

setFogType( params[ "Fog type" ] );

Expand Down Expand Up @@ -167,6 +175,8 @@

renderer.setSize( window.innerWidth, window.innerHeight );

controls.handleResize();

}

function generateHeight( width, height ) {
Expand Down Expand Up @@ -271,6 +281,7 @@

function render() {

controls.update( clock.getDelta() );
renderer.render( scene, camera );

}
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,7 @@ class WebGLRenderer {

materialProperties.environment = material.isMeshStandardMaterial ? scene.environment : null;
materialProperties.fog = scene.fog;
materialProperties.fogSquared = scene.fog.squared || null;
materialProperties.fogSquared = ( scene.fog && scene.fog.squared ) || null;
materialProperties.envMap = ( material.isMeshStandardMaterial ? cubeuvmaps : cubemaps ).get( material.envMap || materialProperties.environment );

if ( programs === undefined ) {
Expand Down Expand Up @@ -1583,7 +1583,7 @@ class WebGLRenderer {

needsProgramChange = true;

} else if ( material.fog === true && ( materialProperties.fog !== fog || materialProperties.fogSquared !== fog.squared ) ) {
} else if ( material.fog === true && ( materialProperties.fog !== fog || ( fog && ( materialProperties.fogSquared !== fog.squared ) ) ) ) {

needsProgramChange = true;

Expand Down
6 changes: 3 additions & 3 deletions test/unit/src/scenes/DensityFog.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export default QUnit.module( 'Scenes', () => {
assert.ok( object_color, 'Can instantiate a DensityFog with color.' );

// color, density
const object_all = new DensityFog( 0xffffff, 0.00030 );
assert.ok( object_all, 'Can instantiate a DensityFog with color, density.' );
const object_color_density = new DensityFog( 0xffffff, 0.00030 );
assert.ok( object_color_density, 'Can instantiate a DensityFog with color, density.' );

// color, density
// color, density, squared
const object_all = new DensityFog( 0xffffff, 0.00030, false );
assert.ok( object_all, 'Can instantiate a DensityFog with color, density, squared == false.' );

Expand Down

0 comments on commit 1dd9f2a

Please sign in to comment.