1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| import * as THREE from "three"; import { Lensflare, LensflareElement, } from "three/examples/jsm/objects/Lensflare.js"; export default class SphereSky { constructor(radius, uTime, envMap) { let geometry = new THREE.SphereGeometry(radius, 32, 32); let material = new THREE.MeshBasicMaterial({ map: envMap, side: THREE.BackSide, }); this.mesh = new THREE.Mesh(geometry, material); this.mesh.rotation.y = -Math.PI / 2;
material.onBeforeCompile = (shader) => { console.log(shader.fragmentShader); shader.uniforms.uTime = uTime; shader.fragmentShader = shader.fragmentShader.replace( "#include <common>", ` #include <common> uniform float uTime; ` ); shader.fragmentShader = shader.fragmentShader.replace( "#include <dithering_fragment>", ` #include <dithering_fragment> float dayStrength = 0.0; if( abs(uTime - 12.0) < 4.0 ){ dayStrength = 1.0; } if( abs(uTime - 12.0) > 6.0 ){ dayStrength = 0.15; } if( abs(uTime - 12.0) >= 4.0 && abs(uTime - 12.0) <= 6.0 ){ dayStrength = 1.0 - ( abs(uTime - 12.0) - 4.0 ) / 2.0; dayStrength = clamp( dayStrength, 0.15, 1.0 ); } gl_FragColor= mix(vec4(0.0,0.0,0.0,1.0),gl_FragColor,dayStrength); ` ); };
let sunGeometry = new THREE.SphereGeometry(100, 32, 32); let sunMaterial = new THREE.MeshStandardMaterial({ emissive: 0xffffcc, }); this.sun = new THREE.Mesh(sunGeometry, sunMaterial); this.sun.position.set(500, 500, 4000); this.sun.visible = false;
let sunLight = new THREE.DirectionalLight(0xffffcc, 2); sunLight.castShadow = true; sunLight.shadow.camera.near = 0.1; sunLight.shadow.camera.far = 10000; sunLight.shadow.camera.left = -1000; sunLight.shadow.camera.right = 1000; sunLight.shadow.camera.top = 1000; sunLight.shadow.camera.bottom = -1000; sunLight.shadow.mapSize.width = 20480; sunLight.shadow.mapSize.height = 20480; sunLight.shadow.radius = 3; this.sun.add(sunLight);
const textureLoader = new THREE.TextureLoader(); const textureFlare0 = textureLoader.load( "./textures/lensflare/lensflare0.png" ); const textureFlare3 = textureLoader.load( "./textures/lensflare/lensflare3.png" ); const lensflare = new Lensflare(); lensflare.addElement(new LensflareElement(textureFlare0, 700, 0)); lensflare.addElement(new LensflareElement(textureFlare3, 300, 0.6)); lensflare.addElement(new LensflareElement(textureFlare3, 200, 0.75)); lensflare.addElement(new LensflareElement(textureFlare3, 150, 0.9)); lensflare.addElement(new LensflareElement(textureFlare3, 100, 1)); sunLight.add(lensflare); } updateSun(time) { this.sun.position.z = Math.cos(((time - 6) * 2 * Math.PI) / 24) * 4000; this.sun.position.y = Math.sin(((time - 6) * 2 * Math.PI) / 24) * 4000; } }
|