From 4c0b2fff9f28409c90ac290bca8803bcbf245c93 Mon Sep 17 00:00:00 2001 From: luxiaotao1123 <t1341870251@63.com> Date: 星期五, 17 十二月 2021 16:26:28 +0800 Subject: [PATCH] # --- static/js/app.js | 47 +++++++- static/js/object/Sky.js | 219 +++++++++++++++++++++++++++++++++++++++++++ static/js/object/StoreShelf.js | 4 3 files changed, 260 insertions(+), 10 deletions(-) diff --git a/static/js/app.js b/static/js/app.js index af80b15..1763d19 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -4,6 +4,7 @@ import {OBJLoader} from './lib/OBJLoader.js'; import {StoreShelf} from './object/StoreShelf.js'; import {StoreCrn} from './object/StoreCrn.js'; +import { Sky } from './object/Sky.js'; var APP = { @@ -72,6 +73,10 @@ this.renderer = new THREE.WebGLRenderer(); // this.renderer.outputEncoding = THREE.sRGBEncoding; + // this.renderer.toneMapping = THREE.ACESFilmicToneMapping; + // this.renderer.toneMappingExposure = 0.5; + + // this.renderer.outputEncoding = THREE.sRGBEncoding; // this.renderer.shadowMap.enabled = true; // this.renderer.toneMapping = THREE.ReinhardToneMapping; // @@ -79,6 +84,8 @@ this.renderer.setSize( window.innerWidth, window.innerHeight ); this.dom = document.getElementById("container"); this.dom.appendChild( this.renderer.domElement ); + + } this.initLight = function () { @@ -130,14 +137,38 @@ // this.dom.appendChild( this.stats.dom ); } this.initBackground = function () { - const cubeTextureLoader = new THREE.CubeTextureLoader(); - cubeTextureLoader.setPath( '../static/img/skybox/' ); - this.scene.background = cubeTextureLoader.load([ - "px.jpg", "nx.jpg", - "py.jpg", "ny.jpg", - "pz.jpg", "nz.jpg" - ]); - // this.scene.background = new THREE.Color( 0xf0f0f0 ); + // const cubeTextureLoader = new THREE.CubeTextureLoader(); + // cubeTextureLoader.setPath( '../static/img/skybox/' ); + // this.scene.background = cubeTextureLoader.load([ + // "px.jpg", "nx.jpg", + // "py.jpg", "ny.jpg", + // "pz.jpg", "nz.jpg" + // ]); + this.scene.background = new THREE.Color( 0xf0f0f0 ); + + // 澶槼 + // let sky = new Sky(); + // sky.scale.setScalar( 450000 ); + // this.addObject( sky ); + // let sun = new THREE.Vector3(); + // const effectController = { + // turbidity: 10, + // rayleigh: 3, + // mieCoefficient: 0.005, + // mieDirectionalG: 0.7, + // elevation: 2, + // azimuth: 180, + // exposure: this.renderer.toneMappingExposure + // }; + // const uniforms = sky.material.uniforms; + // uniforms[ 'turbidity' ].value = effectController.turbidity; + // uniforms[ 'rayleigh' ].value = effectController.rayleigh; + // uniforms[ 'mieCoefficient' ].value = effectController.mieCoefficient; + // uniforms[ 'mieDirectionalG' ].value = effectController.mieDirectionalG; + // const phi = THREE.MathUtils.degToRad( 90 - effectController.elevation ); + // const theta = THREE.MathUtils.degToRad( effectController.azimuth ); + // sun.setFromSphericalCoords( 1, phi, theta ); + // uniforms[ 'sunPosition' ].value.copy( sun ); } this.initReSize = function(object){ window.addEventListener('resize', function () { diff --git a/static/js/object/Sky.js b/static/js/object/Sky.js new file mode 100644 index 0000000..650b32f --- /dev/null +++ b/static/js/object/Sky.js @@ -0,0 +1,219 @@ +import { + BackSide, + BoxGeometry, + Mesh, + ShaderMaterial, + UniformsUtils, + Vector3 +} from '../three.module.js'; + +/** + * Based on "A Practical Analytic Model for Daylight" + * aka The Preetham Model, the de facto standard analytic skydome model + * https://www.researchgate.net/publication/220720443_A_Practical_Analytic_Model_for_Daylight + * + * First implemented by Simon Wallner + * http://www.simonwallner.at/projects/atmospheric-scattering + * + * Improved by Martin Upitis + * http://blenderartists.org/forum/showthread.php?245954-preethams-sky-impementation-HDR + * + * Three.js integration by zz85 http://twitter.com/blurspline +*/ + +class Sky extends Mesh { + + constructor() { + + const shader = Sky.SkyShader; + + const material = new ShaderMaterial( { + name: 'SkyShader', + fragmentShader: shader.fragmentShader, + vertexShader: shader.vertexShader, + uniforms: UniformsUtils.clone( shader.uniforms ), + side: BackSide, + depthWrite: false + } ); + + super( new BoxGeometry( 1, 1, 1 ), material ); + + } + +} + +Sky.prototype.isSky = true; + +Sky.SkyShader = { + + uniforms: { + 'turbidity': { value: 2 }, + 'rayleigh': { value: 1 }, + 'mieCoefficient': { value: 0.005 }, + 'mieDirectionalG': { value: 0.8 }, + 'sunPosition': { value: new Vector3() }, + 'up': { value: new Vector3( 0, 1, 0 ) } + }, + + vertexShader: /* glsl */` + uniform vec3 sunPosition; + uniform float rayleigh; + uniform float turbidity; + uniform float mieCoefficient; + uniform vec3 up; + + varying vec3 vWorldPosition; + varying vec3 vSunDirection; + varying float vSunfade; + varying vec3 vBetaR; + varying vec3 vBetaM; + varying float vSunE; + + // constants for atmospheric scattering + const float e = 2.71828182845904523536028747135266249775724709369995957; + const float pi = 3.141592653589793238462643383279502884197169; + + // wavelength of used primaries, according to preetham + const vec3 lambda = vec3( 680E-9, 550E-9, 450E-9 ); + // this pre-calcuation replaces older TotalRayleigh(vec3 lambda) function: + // (8.0 * pow(pi, 3.0) * pow(pow(n, 2.0) - 1.0, 2.0) * (6.0 + 3.0 * pn)) / (3.0 * N * pow(lambda, vec3(4.0)) * (6.0 - 7.0 * pn)) + const vec3 totalRayleigh = vec3( 5.804542996261093E-6, 1.3562911419845635E-5, 3.0265902468824876E-5 ); + + // mie stuff + // K coefficient for the primaries + const float v = 4.0; + const vec3 K = vec3( 0.686, 0.678, 0.666 ); + // MieConst = pi * pow( ( 2.0 * pi ) / lambda, vec3( v - 2.0 ) ) * K + const vec3 MieConst = vec3( 1.8399918514433978E14, 2.7798023919660528E14, 4.0790479543861094E14 ); + + // earth shadow hack + // cutoffAngle = pi / 1.95; + const float cutoffAngle = 1.6110731556870734; + const float steepness = 1.5; + const float EE = 1000.0; + + float sunIntensity( float zenithAngleCos ) { + zenithAngleCos = clamp( zenithAngleCos, -1.0, 1.0 ); + return EE * max( 0.0, 1.0 - pow( e, -( ( cutoffAngle - acos( zenithAngleCos ) ) / steepness ) ) ); + } + + vec3 totalMie( float T ) { + float c = ( 0.2 * T ) * 10E-18; + return 0.434 * c * MieConst; + } + + void main() { + + vec4 worldPosition = modelMatrix * vec4( position, 1.0 ); + vWorldPosition = worldPosition.xyz; + + gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); + gl_Position.z = gl_Position.w; // set z to camera.far + + vSunDirection = normalize( sunPosition ); + + vSunE = sunIntensity( dot( vSunDirection, up ) ); + + vSunfade = 1.0 - clamp( 1.0 - exp( ( sunPosition.y / 450000.0 ) ), 0.0, 1.0 ); + + float rayleighCoefficient = rayleigh - ( 1.0 * ( 1.0 - vSunfade ) ); + + // extinction (absorbtion + out scattering) + // rayleigh coefficients + vBetaR = totalRayleigh * rayleighCoefficient; + + // mie coefficients + vBetaM = totalMie( turbidity ) * mieCoefficient; + + }`, + + fragmentShader: /* glsl */` + varying vec3 vWorldPosition; + varying vec3 vSunDirection; + varying float vSunfade; + varying vec3 vBetaR; + varying vec3 vBetaM; + varying float vSunE; + + uniform float mieDirectionalG; + uniform vec3 up; + + const vec3 cameraPos = vec3( 0.0, 0.0, 0.0 ); + + // constants for atmospheric scattering + const float pi = 3.141592653589793238462643383279502884197169; + + const float n = 1.0003; // refractive index of air + const float N = 2.545E25; // number of molecules per unit volume for air at 288.15K and 1013mb (sea level -45 celsius) + + // optical length at zenith for molecules + const float rayleighZenithLength = 8.4E3; + const float mieZenithLength = 1.25E3; + // 66 arc seconds -> degrees, and the cosine of that + const float sunAngularDiameterCos = 0.999956676946448443553574619906976478926848692873900859324; + + // 3.0 / ( 16.0 * pi ) + const float THREE_OVER_SIXTEENPI = 0.05968310365946075; + // 1.0 / ( 4.0 * pi ) + const float ONE_OVER_FOURPI = 0.07957747154594767; + + float rayleighPhase( float cosTheta ) { + return THREE_OVER_SIXTEENPI * ( 1.0 + pow( cosTheta, 2.0 ) ); + } + + float hgPhase( float cosTheta, float g ) { + float g2 = pow( g, 2.0 ); + float inverse = 1.0 / pow( 1.0 - 2.0 * g * cosTheta + g2, 1.5 ); + return ONE_OVER_FOURPI * ( ( 1.0 - g2 ) * inverse ); + } + + void main() { + + vec3 direction = normalize( vWorldPosition - cameraPos ); + + // optical length + // cutoff angle at 90 to avoid singularity in next formula. + float zenithAngle = acos( max( 0.0, dot( up, direction ) ) ); + float inverse = 1.0 / ( cos( zenithAngle ) + 0.15 * pow( 93.885 - ( ( zenithAngle * 180.0 ) / pi ), -1.253 ) ); + float sR = rayleighZenithLength * inverse; + float sM = mieZenithLength * inverse; + + // combined extinction factor + vec3 Fex = exp( -( vBetaR * sR + vBetaM * sM ) ); + + // in scattering + float cosTheta = dot( direction, vSunDirection ); + + float rPhase = rayleighPhase( cosTheta * 0.5 + 0.5 ); + vec3 betaRTheta = vBetaR * rPhase; + + float mPhase = hgPhase( cosTheta, mieDirectionalG ); + vec3 betaMTheta = vBetaM * mPhase; + + vec3 Lin = pow( vSunE * ( ( betaRTheta + betaMTheta ) / ( vBetaR + vBetaM ) ) * ( 1.0 - Fex ), vec3( 1.5 ) ); + Lin *= mix( vec3( 1.0 ), pow( vSunE * ( ( betaRTheta + betaMTheta ) / ( vBetaR + vBetaM ) ) * Fex, vec3( 1.0 / 2.0 ) ), clamp( pow( 1.0 - dot( up, vSunDirection ), 5.0 ), 0.0, 1.0 ) ); + + // nightsky + float theta = acos( direction.y ); // elevation --> y-axis, [-pi/2, pi/2] + float phi = atan( direction.z, direction.x ); // azimuth --> x-axis [-pi/2, pi/2] + vec2 uv = vec2( phi, theta ) / vec2( 2.0 * pi, pi ) + vec2( 0.5, 0.0 ); + vec3 L0 = vec3( 0.1 ) * Fex; + + // composition + solar disc + float sundisk = smoothstep( sunAngularDiameterCos, sunAngularDiameterCos + 0.00002, cosTheta ); + L0 += ( vSunE * 19000.0 * Fex ) * sundisk; + + vec3 texColor = ( Lin + L0 ) * 0.04 + vec3( 0.0, 0.0003, 0.00075 ); + + vec3 retColor = pow( texColor, vec3( 1.0 / ( 1.2 + ( 1.2 * vSunfade ) ) ) ); + + gl_FragColor = vec4( retColor, 1.0 ); + + #include <tonemapping_fragment> + #include <encodings_fragment> + + }` + +}; + +export { Sky }; diff --git a/static/js/object/StoreShelf.js b/static/js/object/StoreShelf.js index fcf42b0..c583ea0 100644 --- a/static/js/object/StoreShelf.js +++ b/static/js/object/StoreShelf.js @@ -15,10 +15,10 @@ this.rackWidth = 3;//鏀灦鐨勫搴︼紝榛樿璁惧畾涓�3 // 鏉愯川 - let shelfMat = new THREE.MeshLambertMaterial({ + let shelfMat = new THREE.MeshPhysicalMaterial({ color: 0x175EC0, transparent: true, - opacity: 0.6 + opacity: 0.7 }); //瀹氫箟涓�涓粍鍚堜綋 let group = new THREE.Group(); -- Gitblit v1.9.1