three.js shader 实现天空中白云

ops/2024/9/22 23:48:23/

three.js shader 实现天空中白云

预览: https://threehub.cn/#/codeMirror?navigation=ThreeJS&classify=shader&id=whiteCloud

更多案例 可见 预览: https://threehub.cn
在这里插入图片描述

import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";const { innerWidth, innerHeight } = window;
const aspect = innerWidth / innerHeight;class Base {constructor () {this.init();this.main();}main() {this.deltaTime = {value: 0}var fragmentSrc = ["precision mediump float;","uniform vec2 iResolution;","uniform float iGlobalTime;","uniform vec2 iMouse;","float hash( float n ) {","return fract(sin(n)*43758.5453);","}","float noise( in vec3 x ) {","vec3 p = floor(x);","vec3 f = fract(x);","f = f*f*(3.0-2.0*f);","float n = p.x + p.y*57.0 + 113.0*p.z;","return mix(mix(mix( hash(n+  0.0), hash(n+  1.0),f.x),","mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y),","mix(mix( hash(n+113.0), hash(n+114.0),f.x),","mix( hash(n+170.0), hash(n+171.0),f.x),f.y),f.z);","}","vec4 map( in vec3 p ) {","float d = 0.2 - p.y;","vec3 q = p - vec3(1.0,0.1,0.0)*iGlobalTime;","float f;","f  = 0.5000*noise( q ); q = q*2.02;","f += 0.2500*noise( q ); q = q*2.03;","f += 0.1250*noise( q ); q = q*2.01;","f += 0.0625*noise( q );","d += 3.0 * f;","d = clamp( d, 0.0, 1.0 );","vec4 res = vec4( d );","res.xyz = mix( 1.15*vec3(1.0,0.95,0.8), vec3(0.7,0.7,0.7), res.x );","return res;","}","vec3 sundir = vec3(-1.0,0.0,0.0);","vec4 raymarch( in vec3 ro, in vec3 rd ) {","vec4 sum = vec4(0, 0, 0, 0);","float t = 0.0;","for(int i=0; i<64; i++) {","if( sum.a > 0.99 ) continue;","vec3 pos = ro + t*rd;","vec4 col = map( pos );","#if 1","float dif =  clamp((col.w - map(pos+0.3*sundir).w)/0.6, 0.0, 1.0 );","vec3 lin = vec3(0.65,0.68,0.7)*1.35 + 0.45*vec3(0.7, 0.5, 0.3)*dif;","col.xyz *= lin;","#endif","col.a *= 0.35;","col.rgb *= col.a;","sum = sum + col*(1.0 - sum.a);	","#if 0","t += 0.1;","#else","t += max(0.1,0.025*t);","#endif","}","sum.xyz /= (0.001+sum.w);","return clamp( sum, 0.0, 1.0 );","}","void main(void) {","vec2 q = gl_FragCoord.xy / iResolution.xy;","vec2 p = -1.0 + 2.0*q;","p.x *= iResolution.x/ iResolution.y;","vec2 mo = -1.0 + 2.0*iMouse.xy / iResolution.xy;",// camera"vec3 ro = 4.0*normalize(vec3(cos(2.75-3.0*mo.x), 0.7+(mo.y+1.0), sin(2.75-3.0*mo.x)));","vec3 ta = vec3(0.0, 1.0, 0.0);","vec3 ww = normalize( ta - ro);","vec3 uu = normalize(cross( vec3(0.0,1.0,0.0), ww ));","vec3 vv = normalize(cross(ww,uu));","vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );","vec4 res = raymarch( ro, rd );","float sun = clamp( dot(sundir,rd), 0.0, 1.0 );","vec3 col = vec3(0.6,0.71,0.75) - rd.y*0.2*vec3(1.0,0.5,1.0) + 0.15*0.5;","col += 0.2*vec3(1.0,.6,0.1)*pow( sun, 8.0 );","col *= 0.95;","col = mix( col, res.xyz, res.w );","col += 0.1*vec3(1.0,0.4,0.2)*pow( sun, 3.0 );","gl_FragColor = vec4( col, 1.0 );","}"];const geometry = new THREE.SphereGeometry(5000, 50);const material = new THREE.ShaderMaterial({transparent: true,side: THREE.BackSide,uniforms: {iGlobalTime: this.deltaTime,iResolution: {value: {x: window.innerWidth,y: window.innerHeight},},iMouse: {value: {x: 0,y: 0}}},vertexShader: `varying vec2 vUv;void main() {vUv = uv;gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);}`,fragmentShader: fragmentSrc.join("\n"),});const mesh = new THREE.Mesh(geometry, material);this.scene.add(mesh);const scope = thisfunction animate() {scope.controls.update();scope.renderer.render(scope.scene, scope.camera);scope.deltaTime.value = scope.clock.getElapsedTime()requestAnimationFrame(animate);}animate()}init() {this.clock = new THREE.Clock();this.loader = new GLTFLoader();this.renderer = new THREE.WebGLRenderer({antialias: true,logarithmicDepthBuffer: true,});this.renderer.setPixelRatio(window.devicePixelRatio);this.renderer.setSize(innerWidth, innerHeight);document.body.appendChild(this.renderer.domElement);this.camera = new THREE.PerspectiveCamera(60, aspect, 0.01, 10000);this.camera.position.set(5, 5, 5);this.scene = new THREE.Scene();this.controls = new OrbitControls(this.camera, this.renderer.domElement);const light = new THREE.AmbientLight(0xffffff, 0.5);this.scene.add(light);}}
new Base();

http://www.ppmy.cn/ops/114476.html

相关文章

基于协同过滤+python+django+vue的音乐推荐系统

作者&#xff1a;计算机学姐 开发技术&#xff1a;SpringBoot、SSM、Vue、MySQL、JSP、ElementUI、Python、小程序等&#xff0c;“文末源码”。 专栏推荐&#xff1a;前后端分离项目源码、SpringBoot项目源码、SSM项目源码 系统展示 【2025最新】基于协同过滤pythondjangovue…

Facebook运营:账号类型有哪些?有必要用静态住宅IP吗?

Facebook作为月活跃用户数高达几十亿的社交媒体平台&#xff0c;一直不断有新用户选择加入。从个人用户的生活分享到企业用户的商务宣传推广&#xff0c;Facebook提供各大功能和模块来满足用户需求。相应的&#xff0c;用户也需要了解平台特点来进行相应的操作。本文从账号类型…

windows安装docker 本地打包代码

参考文章1&#xff1a;https://gitcode.csdn.net/65ea814b1a836825ed792f4a.html 参考文章2&#xff1a; Windows 安装docker&#xff08;详细图解&#xff09;-CSDN博客 一 下载 Docker Desktop 在官网上下载 Docker Desktop&#xff0c;可以从以下链接下载最新版本&#x…

Linux文件IO(五)-三种进程退出方法及空洞文件

1.三种进程退出方法 return 当程序在执行某个函数出错的时候&#xff0c;如果此函数执行失败会导致后面的步骤不能在进行下去时&#xff0c;应该在出错时终止程序运行&#xff0c;不应该让程序继续运行下去&#xff0c;那么如何退出程序、终止程序运行呢&#xff1f;有过编程…

2024年华为杯中国研究生数学建模竞赛B题(WLAN组网中网络吞吐量建模)思路

问题 1:信道容量模型的建立 背景 WLAN(无线局域网)的吞吐量与其信道容量密切相关,而信道容量是指在给定的信号与噪声比 (SNR) 下,信道能够传输的最大数据速率。香农公式为信道容量提供了理论上限: C = B log ⁡ 2 ( 1 + S N ) C &

纯前端表格导出Excel

先写好两个js文件 直接复制粘贴 文件目录是这样的 Bolb.js /* eslint-disable */ /* Blob.js* A Blob implementation.* 2014-05-27** By Eli Grey, http://eligrey.com* By Devin Samarin, https://github.com/eboyjr* License: X11/MIT* See LICENSE.md*//*global self, …

【Leetcode:1184. 公交站间的距离 + 模拟】

&#x1f680; 算法题 &#x1f680; &#x1f332; 算法刷题专栏 | 面试必备算法 | 面试高频算法 &#x1f340; &#x1f332; 越难的东西,越要努力坚持&#xff0c;因为它具有很高的价值&#xff0c;算法就是这样✨ &#x1f332; 作者简介&#xff1a;硕风和炜&#xff0c;…

有关elementui form验证问题,有值却仍然显示不通过

参考链接 有关elementui form验证问题&#xff0c;有值却仍然显示不通过 - 一棵写代码的柳树 - 博客园 需要保证表单上的 :model" "和prop" "对应的属性相同 el-form 绑定数据:model 和 规则:rules input 绑定 数据表单里的数据 其父组件提供校验所绑定的…