瀏覽代碼

Examples: Improve refraction demo. (#21938)

Michael Herzog 4 年之前
父節點
當前提交
96a481eb37

+ 1 - 1
examples/js/shaders/WaterRefractionShader.js

@@ -62,7 +62,7 @@
 
 
 		void main() {
 		void main() {
 
 
-		 float waveStrength = 0.1;
+		 float waveStrength = 0.5;
 		 float waveSpeed = 0.03;
 		 float waveSpeed = 0.03;
 
 
 			// simple distortion (ripple) via dudv map (see https://www.youtube.com/watch?v=6B7IF6GOu7s)
 			// simple distortion (ripple) via dudv map (see https://www.youtube.com/watch?v=6B7IF6GOu7s)

+ 1 - 1
examples/jsm/shaders/WaterRefractionShader.js

@@ -65,7 +65,7 @@ const WaterRefractionShader = {
 
 
 		void main() {
 		void main() {
 
 
-		 float waveStrength = 0.1;
+		 float waveStrength = 0.5;
 		 float waveSpeed = 0.03;
 		 float waveSpeed = 0.03;
 
 
 			// simple distortion (ripple) via dudv map (see https://www.youtube.com/watch?v=6B7IF6GOu7s)
 			// simple distortion (ripple) via dudv map (see https://www.youtube.com/watch?v=6B7IF6GOu7s)

二進制
examples/screenshots/webgl_refraction.jpg


+ 91 - 47
examples/webgl_refraction.html

@@ -5,6 +5,14 @@
 		<meta charset="utf-8">
 		<meta charset="utf-8">
 		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
 		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
 		<link type="text/css" rel="stylesheet" href="main.css">
 		<link type="text/css" rel="stylesheet" href="main.css">
+		<style>
+			body {
+				color: #444;
+			}
+			a {
+				color: #08f;
+			}
+		</style>
 	</head>
 	</head>
 	<body>
 	<body>
 
 
@@ -21,37 +29,40 @@
 			import { Refractor } from './jsm/objects/Refractor.js';
 			import { Refractor } from './jsm/objects/Refractor.js';
 			import { WaterRefractionShader } from './jsm/shaders/WaterRefractionShader.js';
 			import { WaterRefractionShader } from './jsm/shaders/WaterRefractionShader.js';
 
 
-			let scene, camera, clock, renderer, refractor;
+			let camera, scene, renderer, clock;
+
+			let refractor, smallSphere;
 
 
 			init();
 			init();
 
 
 			function init() {
 			function init() {
 
 
-				// scene
-
-				scene = new THREE.Scene();
-
-				// camera
-
-				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
-				camera.position.set( - 10, 0, 15 );
-				camera.lookAt( scene.position );
-
-				// clock
+				const container = document.getElementById( 'container' );
 
 
 				clock = new THREE.Clock();
 				clock = new THREE.Clock();
 
 
-				// mesh
+				// renderer
+				renderer = new THREE.WebGLRenderer( { antialias: true } );
+				renderer.setPixelRatio( window.devicePixelRatio );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+				container.appendChild( renderer.domElement );
 
 
-				const geometry = new THREE.TorusKnotGeometry( 3, 1, 256, 32 );
-				const material = new THREE.MeshStandardMaterial( { color: 0x6083c2 } );
+				// scene
+				scene = new THREE.Scene();
+
+				// camera
+				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
+				camera.position.set( 0, 75, 160 );
 
 
-				const mesh = new THREE.Mesh( geometry, material );
-				scene.add( mesh );
+				const controls = new OrbitControls( camera, renderer.domElement );
+				controls.target.set( 0, 40, 0 );
+				controls.maxDistance = 400;
+				controls.minDistance = 10;
+				controls.update();
 
 
 				// refractor
 				// refractor
 
 
-				const refractorGeometry = new THREE.PlaneGeometry( 10, 10 );
+				const refractorGeometry = new THREE.PlaneGeometry( 90, 90 );
 
 
 				refractor = new Refractor( refractorGeometry, {
 				refractor = new Refractor( refractorGeometry, {
 					color: 0x999999,
 					color: 0x999999,
@@ -60,7 +71,7 @@
 					shader: WaterRefractionShader
 					shader: WaterRefractionShader
 				} );
 				} );
 
 
-				refractor.position.set( 0, 0, 5 );
+				refractor.position.set( 0, 50, 0 );
 
 
 				scene.add( refractor );
 				scene.add( refractor );
 
 
@@ -73,32 +84,60 @@
 				} );
 				} );
 
 
 				dudvMap.wrapS = dudvMap.wrapT = THREE.RepeatWrapping;
 				dudvMap.wrapS = dudvMap.wrapT = THREE.RepeatWrapping;
-				refractor.material.uniforms[ "tDudv" ].value = dudvMap;
-
-				// light
-
-				const ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
-				scene.add( ambientLight );
-
-				const pointLight = new THREE.PointLight( 0xffffff, 0.8 );
-				camera.add( pointLight );
-				scene.add( camera );
-
-				// renderer
-
-				renderer = new THREE.WebGLRenderer( { antialias: true } );
-				renderer.setSize( window.innerWidth, window.innerHeight );
-				renderer.setClearColor( 0x20252f );
-				renderer.setPixelRatio( window.devicePixelRatio );
-				document.body.appendChild( renderer.domElement );
+				refractor.material.uniforms.tDudv.value = dudvMap;
 
 
 				//
 				//
 
 
-				const controls = new OrbitControls( camera, renderer.domElement );
-				controls.minDistance = 10;
-				controls.maxDistance = 100;
-
-				//
+				const geometry = new THREE.IcosahedronGeometry( 5, 0 );
+				const material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x333333, flatShading: true } );
+				smallSphere = new THREE.Mesh( geometry, material );
+				scene.add( smallSphere );
+
+				// walls
+				const planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );
+
+				const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
+				planeTop.position.y = 100;
+				planeTop.rotateX( Math.PI / 2 );
+				scene.add( planeTop );
+
+				const planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
+				planeBottom.rotateX( - Math.PI / 2 );
+				scene.add( planeBottom );
+
+				const planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
+				planeBack.position.z = - 50;
+				planeBack.position.y = 50;
+				scene.add( planeBack );
+
+				const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
+				planeRight.position.x = 50;
+				planeRight.position.y = 50;
+				planeRight.rotateY( - Math.PI / 2 );
+				scene.add( planeRight );
+
+				const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
+				planeLeft.position.x = - 50;
+				planeLeft.position.y = 50;
+				planeLeft.rotateY( Math.PI / 2 );
+				scene.add( planeLeft );
+
+				// lights
+				const mainLight = new THREE.PointLight( 0xcccccc, 1.5, 250 );
+				mainLight.position.y = 60;
+				scene.add( mainLight );
+
+				const greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
+				greenLight.position.set( 550, 50, 0 );
+				scene.add( greenLight );
+
+				const redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
+				redLight.position.set( - 550, 50, 0 );
+				scene.add( redLight );
+
+				const blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 1000 );
+				blueLight.position.set( 0, 50, 550 );
+				scene.add( blueLight );
 
 
 				window.addEventListener( 'resize', onWindowResize );
 				window.addEventListener( 'resize', onWindowResize );
 
 
@@ -108,6 +147,7 @@
 
 
 				camera.aspect = window.innerWidth / window.innerHeight;
 				camera.aspect = window.innerWidth / window.innerHeight;
 				camera.updateProjectionMatrix();
 				camera.updateProjectionMatrix();
+
 				renderer.setSize( window.innerWidth, window.innerHeight );
 				renderer.setSize( window.innerWidth, window.innerHeight );
 
 
 			}
 			}
@@ -116,13 +156,17 @@
 
 
 				requestAnimationFrame( animate );
 				requestAnimationFrame( animate );
 
 
-				render();
-
-			}
+				const time = clock.getElapsedTime();
 
 
-			function render() {
+				refractor.material.uniforms.time.value = time;
 
 
-				refractor.material.uniforms[ "time" ].value += clock.getDelta();
+				smallSphere.position.set(
+					Math.cos( time ) * 30,
+					Math.abs( Math.cos( time * 2 ) ) * 20 + 5,
+					Math.sin( time ) * 30
+				);
+				smallSphere.rotation.y = ( Math.PI / 2 ) - time;
+				smallSphere.rotation.z = time * 8;
 
 
 				renderer.render( scene, camera );
 				renderer.render( scene, camera );