### Main Scene Setup for Volumetric Cloud Rendering Source: https://context7.com/spiri0/volumetric-clouds/llms.txt Sets up the main Three.js scene, including the WebGL2 renderer, camera, orbit controls, and render target for volumetric clouds. It configures the scene background, camera perspective, and depth texture for accurate cloud rendering. ```javascript import * as THREE from "../lib/three/build/three.module.js"; import { OrbitControls } from '../lib/three/examples/jsm/controls/OrbitControls.js'; import { EffectComposer } from '../lib/three/examples/jsm/postprocessing/EffectComposer.js'; import { RenderPass } from '../lib/three/examples/jsm/postprocessing/RenderPass.js'; import { ShaderPass } from '../lib/three/examples/jsm/postprocessing/ShaderPass.js'; class Main { constructor() { // Setup WebGL2 renderer const canvas = document.createElement('canvas'); const context = canvas.getContext('webgl2'); this.renderer = new THREE.WebGLRenderer({ canvas: canvas, context: context, antialias: true }); this.renderer.setPixelRatio(window.devicePixelRatio); // Create scene and camera this.scene = new THREE.Scene(); this.scene.background = new THREE.Color(0x5cccfc); const fov = 50; const near = 0.1; const far = 1e6; this.camera = new THREE.PerspectiveCamera(fov, aspect, near, far); this.camera.position.set(10000, 15000, 10000); // Setup orbit controls this.controls = new OrbitControls(this.camera, this.renderer.domElement); this.controls.enableZoom = true; this.controls.target.set(0, 0, 0); // Setup depth render target for cloud rendering this.target = new THREE.WebGLRenderTarget(width, height); this.target.depthTexture = new THREE.DepthTexture(); this.target.depthTexture.format = THREE.DepthFormat; this.target.depthTexture.type = THREE.FloatType; // Attach to container const container = document.getElementById('container'); container.appendChild(this.renderer.domElement); } } ``` -------------------------------- ### Configure Volumetric Cloud Shader Material Source: https://context7.com/spiri0/volumetric-clouds/llms.txt Sets up the `THREE.RawShaderMaterial` for rendering volumetric clouds. It defines uniforms such as scene color, depth, sun direction and color, camera information, and various noise textures required by the shaders. The material uses `cloudsVS.js` for the vertex shader and `cloudsFS.js` for the fragment shader. This configuration is typically part of a post-processing setup. ```javascript import { cloudsVS } from "../resources/shader/cloudsVS.js"; import { cloudsFS } from "../resources/shader/cloudsFS.js"; import { cloud_model } from './cloud-model.js'; // Generate cloud textures const cloudmodel = new cloud_model.CloudModel({ renderer: renderer }); // Define shader uniforms const uniform = { tDiffuse: { value: null }, // Scene color texture tDepth: { value: null }, // Scene depth texture sunDir: { value: null }, // Sun direction vector sunColor: { value: null }, // Sun color cameraForward: { value: null }, // Camera forward vector inverseProjection: { value: null }, // Inverse projection matrix inverseView: { value: null }, // Inverse view matrix cameraPos: { value: null }, // Camera position planetPos: { value: null }, // Planet center position near: { value: 0.1 }, // Near plane far: { value: 1e6 }, // Far plane cloud: { value: cloudmodel.cloud }, // 3D cloud noise texture worley: { value: cloudmodel.worley }, // 3D worley noise texture weather: { value: cloudmodel.weather }, // 2D weather map }; // Create shader material const cloudsMaterial = new THREE.RawShaderMaterial({ glslVersion: THREE.GLSL3, uniforms: uniform, vertexShader: cloudsVS, fragmentShader: cloudsFS, }); // Add to post-processing pipeline const composer = new EffectComposer(renderer); composer.addPass(new RenderPass(scene, camera)); composer.addPass(new ShaderPass(cloudsMaterial)); ``` -------------------------------- ### Initialize CloudModel for Volumetric Clouds Source: https://context7.com/spiri0/volumetric-clouds/llms.txt Initializes the CloudModel class to generate procedural 3D noise textures used in volumetric cloud rendering. It requires a WebGL2 renderer and produces Perlin-Worley noise, Worley FBM, and weather map textures. ```javascript import { cloud_model } from './cloud-model.js'; // Initialize renderer first const renderer = new THREE.WebGLRenderer({ canvas: document.createElement('canvas'), context: canvas.getContext('webgl2'), antialias: true }); // Create cloud model with required parameters const params = { renderer: renderer }; const cloudModel = new cloud_model.CloudModel(params); // Access generated textures const cloudTexture = cloudModel.cloud; // 128x128x128 3D texture (Perlin-Worley noise) const worleyTexture = cloudModel.worley; // 64x64x64 3D texture (Worley FBM) const weatherTexture = cloudModel.weather; // 2D weather map texture ``` -------------------------------- ### Volumetric Cloud Raymarching with Light Scattering in GLSL Source: https://context7.com/spiri0/volumetric-clouds/llms.txt Implements the core raymarching algorithm for volumetric clouds, calculating light scattering and ambient light based on sun direction and cloud density. It requires uniforms for sun direction and color, and helper functions like sampleDensity, getHeightFraction, noiseKernel, and dithering. ```glsl uniform vec3 sunDir; uniform vec3 sunColor; #define CLOUDS_AMBIENT_COLOR_TOP vec3(179., 179., 179.) * (1.5/255.) #define CLOUDS_AMBIENT_COLOR_BOTTOM vec3(65., 70., 80.) * (1.5/255.) float HG(float sundotrd, float g) { float gg = g * g; return (1. - gg) / pow(1. + gg - 2. * g * sundotrd, 1.5) * 1./(4.*PI); } float powder(float d) { return (1. - exp(-2.*d)); } float raymarchToLight(vec3 startPos, float stepSize) { vec3 pos = startPos; float totalDensity = 0.0; float sigma = 0.00035; // March towards sun with cone sampling for(int i = 0; i < 6; i++) { pos = startPos + noiseKernel[i] * float(i); float density = sampleDensity(pos); if(density > 0.0) { totalDensity += density * stepSize; } startPos += stepSize * normalize(sunDir); } return exp(-totalDensity * sigma); // Light transmission } vec4 traceClouds(vec3 rayDir, vec3 startPos, vec3 endPos) { vec3 path = endPos - startPos; float len = length(path); int nSteps = 128; float stepSize = len / float(nSteps); vec4 col = vec4(0.); float lightDotEye = dot(normalize(sunDir), normalize(rayDir)); float T = 1.0; // Transmittance float sigma = 0.02; // Apply dithering to starting position startPos += rayDir * stepSize * (dithering(gl_FragCoord.xy) * 2. - 1.); vec3 pos = startPos; for(int i = 0; i < nSteps; ++i) { float heightFraction = getHeightFraction(pos, CLOUDS_BOTTOM_HEIGHT, CLOUDS_TOP_HEIGHT); float density = sampleDensity(pos); if(density > 0.0) { // Calculate lighting float light_density = raymarchToLight(pos, stepSize); float scattering = mix(HG(lightDotEye, -0.08), HG(lightDotEye, 0.08), clamp(lightDotEye*0.5 + 0.5, 0.0, 1.0)); scattering = max(scattering, 1.0); float powderTerm = powder(density); // Ambient light based on height vec3 ambientLight = mix(CLOUDS_AMBIENT_COLOR_BOTTOM, CLOUDS_AMBIENT_COLOR_TOP, heightFraction); // Combine lighting vec3 S = mix(ambientLight, vec3(1.,1.,1.) * scattering, powderTerm * light_density) * density; // Accumulate color and transmittance float dTrans = exp(-density * stepSize * sigma); vec3 Sint = (S - S * dTrans) * (1.0/density); col.rgb += T * Sint; T *= dTrans; } if(T <= 0.01) break; // Early exit pos += rayDir * stepSize; } col.a = 1.0 - T; // Alpha from transmittance return col; } ``` -------------------------------- ### Sample Volumetric Cloud Density using GLSL Source: https://context7.com/spiri0/volumetric-clouds/llms.txt A GLSL function to sample volumetric cloud density at a given world position. It considers cloud height, type gradients, a low-frequency base shape, a weather map for coverage, and high-frequency Worley noise for detail. Requires uniform samplers for cloud, worley, and weather textures. ```glsl // Cloud height constants #define CLOUDS_BOTTOM_HEIGHT 4000.0 #define CLOUDS_TOP_HEIGHT 8000.0 // Cloud type gradients #define STRATUS_GRADIENT vec4(0.0, 0.1, 0.2, 0.3) #define STRATOCUMULUS_GRADIENT vec4(0.02, 0.2, 0.48, 0.625) #define CUMULUS_GRADIENT vec4(0.00, 0.1625, 0.88, 0.98) uniform sampler3D cloud; uniform sampler3D worley; uniform sampler2D weather; float getHeightFraction(vec3 pos, float low, float height) { return (length(pos.y - 0.) - low) / (height - low); } float getDensityForCloud(float heightFraction, float cloudType) { float stratusFactor = 1.0 - clamp(cloudType * 2.0, 0.0, 1.0); float stratoCumulusFactor = 1.0 - abs(cloudType - 0.5) * 2.0; float cumulusFactor = clamp(cloudType - 0.5, 0.0, 1.0) * 2.0; vec4 baseGradient = stratusFactor * STRATUS_GRADIENT + stratoCumulusFactor * STRATOCUMULUS_GRADIENT + cumulusFactor * CUMULUS_GRADIENT; return smoothstep(baseGradient.x, baseGradient.y, heightFraction) - smoothstep(baseGradient.z, baseGradient.w, heightFraction); } float sampleDensity(vec3 pos) { float heightFraction = getHeightFraction(pos, CLOUDS_BOTTOM_HEIGHT, CLOUDS_TOP_HEIGHT); if (heightFraction < 0. || heightFraction > 1.) { return 0.; } // Normalize position to texture space vec3 newpos = pos * (1.0 / 20000.0) + vec3(0.5); // Sample low-frequency base shape vec3 lowpos = scaledPos(newpos, 3.); vec4 lowFreqNoise = textureLod(cloud, lowpos, 0.0); float lowFreqFBM = dot(lowFreqNoise.gba, vec3(0.625, 0.25, 0.125)); float base_cloud = remap(lowFreqNoise.r, -(1.0 - lowFreqFBM), 1., 0.0, 1.0); // Apply height-based density gradient float density = getDensityForCloud(heightFraction, 1.0); base_cloud *= (density / heightFraction); // Apply weather map for coverage vec3 wpos = scaledPos(newpos, 1.); float weather = max(0.0, sampleWeather(wpos).g - 0.65); float baseCloudWithCoverage = remap(base_cloud, weather, 1.0, 0.0, 1.0); baseCloudWithCoverage *= weather; // Add high-frequency detail vec3 highpos = scaledPos(pos, 5.); vec3 highFreqNoise = textureLod(worley, highpos * heightFraction, 0.0).rgb; float highFreqFBM = dot(highFreqNoise, vec3(0.625, 0.25, 0.125)); float highFreqNoiseModifier = mix(highFreqFBM, 1.0 - highFreqFBM, clamp(heightFraction * 10.0, 0.0, 1.0)); baseCloudWithCoverage = baseCloudWithCoverage - highFreqNoiseModifier * (1.0 - baseCloudWithCoverage); baseCloudWithCoverage = remap(baseCloudWithCoverage * 2.0, highFreqNoiseModifier * 0.2, 1.0, 0.0, 1.0); return clamp(baseCloudWithCoverage, 0.0, 1.0); } ``` -------------------------------- ### Implement Render Loop for Volumetric Clouds Source: https://context7.com/spiri0/volumetric-clouds/llms.txt Implements the main render loop for a scene that includes volumetric clouds. The `update` method is responsible for updating all necessary uniforms for the cloud shader each frame, including depth texture, camera matrices, and lighting information. The `render` method orchestrates the rendering process, first rendering the scene to a depth buffer, then updating cloud uniforms, and finally rendering the scene with clouds using a composer. The `animate` method drives the loop using `requestAnimationFrame`. ```javascript class Main { update() { // Update cloud shader uniforms each frame this.clouds.uniforms.tDepth.value = this.target.depthTexture; this.clouds.uniforms.planetPos.value = new THREE.Vector3(0, 0, 0); this.clouds.uniforms.inverseProjection.value = this.camera.projectionMatrixInverse; this.clouds.uniforms.inverseView.value = this.camera.matrixWorld; this.clouds.uniforms.cameraPos.value = this.camera.position; this.clouds.uniforms.sunDir.value = new THREE.Vector3(1, 1, 1); this.clouds.uniforms.sunColor.value = new THREE.Vector3(1, 1, 1); this.clouds.uniformsNeedUpdate = true; } render() { // First pass: render scene to depth buffer this.renderer.setRenderTarget(this.target); this.renderer.render(this.scene, this.camera); this.renderer.setRenderTarget(null); // Update cloud uniforms this.update(); // Second pass: render scene with clouds this.renderer.render(this.scene, this.camera); this.composer.render(); } animate() { requestAnimationFrame(this.animate.bind(this)); this.render(); } } // Start animation loop const main = new Main(); main.animate(); ``` -------------------------------- ### Generate 3D Noise Texture for Volumetric Clouds Source: https://context7.com/spiri0/volumetric-clouds/llms.txt Generates a 3D texture using compute shaders for volumetric cloud shapes. It utilizes `THREE.RawShaderMaterial` with `commonVS` and `basecloudFS` to perform noise generation. A `THREE.WebGL3DRenderTarget` is used to store the generated 3D texture. The process involves rendering each slice of the 3D texture sequentially by updating a `uZCoord` uniform in the compute material. ```javascript import { commonVS } from "../resources/shader/common.js"; import { basecloudFS } from "../resources/shader/basecloud.js"; Cloud(params) { const SIDE = 128; // 128x128x128 volume // Create compute material for noise generation const computeMaterial = new THREE.RawShaderMaterial({ glslVersion: THREE.GLSL3, vertexShader: commonVS, fragmentShader: basecloudFS, uniforms: { uZCoord: { value: 0 }, size: { value: SIDE }, }, depthTest: false, }); const computeMesh = new THREE.Mesh( new THREE.PlaneBufferGeometry(2, 2), computeMaterial ); // Create 3D render target const target3d = new THREE.WebGL3DRenderTarget(SIDE, SIDE, SIDE); target3d.texture.format = THREE.RGBAFormat; target3d.texture.minFilter = THREE.LinearFilter; target3d.texture.magFilter = THREE.LinearFilter; target3d.texture.type = THREE.UnsignedByteType; // Render each slice of the 3D texture const textureCamera = new THREE.Camera(); for (let i = 0; i < SIDE; i++) { computeMesh.material.uniforms.uZCoord.value = i; params.renderer.setRenderTarget(target3d, i); params.renderer.render(computeMesh, textureCamera); } params.renderer.setRenderTarget(null); return target3d.texture; } ``` -------------------------------- ### Generate 3D Worley Noise Texture using JavaScript Source: https://context7.com/spiri0/volumetric-clouds/llms.txt Generates a 3D Worley noise texture using Three.js for cloud detail and turbulence. It utilizes a compute shader to render noise slices into a 3D render target. Requires Three.js and a renderer instance. ```javascript import { worleyFS } from "../resources/shader/worley.js"; Worley(params) { const SIDE = 64; // 64x64x64 volume for detail noise const worleyCamera = new THREE.Camera(); // Create Worley noise compute shader const computeWorley = new THREE.RawShaderMaterial({ glslVersion: THREE.GLSL3, vertexShader: commonVS, fragmentShader: worleyFS, uniforms: { uZCoord: { value: 0 }, size: { value: SIDE }, }, depthTest: false, }); const worleyMesh = new THREE.Mesh( new THREE.PlaneBufferGeometry(2, 2), computeWorley ); // Create 3D render target const worley3d = new THREE.WebGL3DRenderTarget(SIDE, SIDE, SIDE); worley3d.texture.format = THREE.RGBAFormat; worley3d.texture.minFilter = THREE.LinearFilter; worley3d.texture.magFilter = THREE.LinearFilter; // Generate all slices for (let i = 0; i < SIDE; i++) { worleyMesh.material.uniforms.uZCoord.value = i; params.renderer.setRenderTarget(worley3d, i); params.renderer.render(worleyMesh, worleyCamera); } params.renderer.setRenderTarget(null); return worley3d.texture; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.