73 lines
2.2 KiB
JavaScript
73 lines
2.2 KiB
JavaScript
/* global THREE */
|
|
|
|
THREE.Extras = THREE.Extras || {}
|
|
THREE.Extras.Planet = {}
|
|
|
|
const atmosShader = require('./builtin/atmosphere.glsl.js')
|
|
const cubePlanet = require('./cubeplanet.js')
|
|
|
|
class ScatterShader {
|
|
static _compose (iR, oR, vS, fS, p) {
|
|
const st = new THREE.ShaderMaterial({
|
|
uniforms: {},
|
|
vertexShader: vS,
|
|
fragmentShader: fS
|
|
})
|
|
ScatterShader.scatterUniforms(st, iR, oR, p)
|
|
return st
|
|
}
|
|
|
|
static newAtmosphere (innerRadius, outerRadius, params) {
|
|
const st = ScatterShader._compose(
|
|
innerRadius,
|
|
outerRadius,
|
|
atmosShader.vertexUniforms + atmosShader.vertexFunctions + atmosShader.vertexAtmosphere,
|
|
atmosShader.fragmentAtmosphere,
|
|
params
|
|
)
|
|
st.side = THREE.BackSide
|
|
st.transparent = true
|
|
return st
|
|
}
|
|
|
|
static newAttenuate (innerRadius, outerRadius, params) {
|
|
return ScatterShader._compose(
|
|
innerRadius,
|
|
outerRadius,
|
|
atmosShader.vertexUniforms + atmosShader.vertexFunctions + atmosShader.vertexGround,
|
|
atmosShader.fragmentGround,
|
|
params
|
|
)
|
|
}
|
|
|
|
static scatterUniforms (shader, innerRadius, outerRadius, custom = {}) {
|
|
const Kr = custom.Kr || 0.0025
|
|
const Km = custom.Km || 0.0015
|
|
const ESun = custom.ESun || 15.0
|
|
const Scale = custom.scale || (1 / (outerRadius - innerRadius))
|
|
const ScaleDepth = custom.scaleDepth || 0.25
|
|
const G = custom.g || -0.950
|
|
const Wavelength = custom.wavelength || [0.650, 0.570, 0.475]
|
|
|
|
shader.uniforms.invWavelength = {
|
|
value: [1 / Math.pow(Wavelength[0], 4), 1 / Math.pow(Wavelength[1], 4), 1 / Math.pow(Wavelength[2], 4)],
|
|
type: 'v3'
|
|
}
|
|
shader.uniforms.outerRadius = { value: outerRadius, type: 'f' }
|
|
shader.uniforms.innerRadius = { value: innerRadius, type: 'f' }
|
|
shader.uniforms.Kr = { value: Kr, type: 'f' }
|
|
shader.uniforms.Km = { value: Km, type: 'f' }
|
|
shader.uniforms.ESun = { value: ESun, type: 'f' }
|
|
shader.uniforms.scale = { value: Scale, type: 'f' }
|
|
shader.uniforms.scaleDepth = { value: ScaleDepth, type: 'f' }
|
|
shader.uniforms.g = { value: G, type: 'f' }
|
|
}
|
|
}
|
|
|
|
for (const o in cubePlanet) {
|
|
THREE.Extras.Planet[o] = cubePlanet[o]
|
|
}
|
|
|
|
THREE.Extras.Planet.ScatterShader = ScatterShader
|
|
module.exports = THREE.Extras.Planet
|