【threejs】每天一個小案例講解:光照

代碼倉

GitHub - TiffanyHoo/three_practices: Learning three.js together!

可自行clone,無需安裝依賴,直接liver-server運行/直接打開chapter01中的html文件

運行效果圖

知識要點

常見光照類型及其特點如下:

1. 環境光(AmbientLight)

? 特點:均勻照亮場景所有物體,不考慮光源位置,營造基礎明暗氛圍。

? 應用:常用于模擬間接光照(如室內墻壁反射光),需配合其他光源使用,避免場景過平。

? 示例:new THREE.AmbientLight(0x404040, 0.6)(顏色和強度可調整)。

2. 平行光(DirectionalLight)

? 特點:類似太陽光照,光線平行照射,方向固定,不受距離影響。

? 應用:模擬太陽光、大型場景主光源,可通過旋轉調整光照方向。

? 示例:new THREE.DirectionalLight(0xffffff, 0.8),需設置direction屬性(如指向地面)。

3. 點光源(PointLight)

? 特點:從單一位置向四周發散光線,光照強度隨距離衰減。

? 應用:模擬燈泡、蠟燭等點光源效果,可通過distance和decay控制衰減范圍。

? 示例:new THREE.PointLight(0xff0000, 1, 100)(紅色光,強度1,有效距離100)。

4. 聚光燈(SpotLight)

? 特點:光線呈錐形發散,可控制照射角度、邊緣柔和度等,類似手電筒。

? 應用:舞臺燈光、局部重點照明,可通過angle(錐角)和penumbra(邊緣模糊)調整效果。

? 示例:new THREE.SpotLight(0xffffff, 1, 100, Math.PI/4)(白光,強度1,錐角45度)。

5. 半球光(HemisphereLight)

? 特點:模擬天空和地面的光照,頂部為天空色,底部為地面色,光線柔和。

? 應用:戶外場景自然光照(如藍天與地面反射),參數包括天空色、地面色和強度。

? 示例:new THREE.HemisphereLight(0xaaaaaa, 0x444444, 1)(天空淺灰,地面深灰)。

關鍵參數對比

? 位置影響:平行光(方向)、點/聚光燈(位置),環境光/半球光無位置概念。

? 衰減特性:點/聚光燈支持距離衰減,平行光/環境光無衰減。

? 陰影支持:平行光和聚光燈可投射陰影(需開啟castShadow),其他光源通常不支持。

通過組合不同光照類型,可實現復雜場景的光影效果,例如“平行光(主光源)+ 環境光(基礎照明)+ 點光源(局部補光)”的搭配。

核心運行代碼

<!DOCTYPE html><html><head><title>Example 03.02 - point Light</title><script type="text/javascript" src="../libs/three.js"></script><script type="text/javascript" src="../libs/stats.js"></script><script type="text/javascript" src="../libs/dat.gui.js"></script><style>body {/* set margin to 0 and overflow to hidden, to go fullscreen */margin: 0;overflow: hidden;}</style>
</head><body><div id="Stats-output"></div><!-- Div which will hold the Output --><div id="WebGL-output"></div><!-- Javascript code that runs our Three.js examples --><script type="text/javascript">// once everything is loaded, we run our Three.js stuff.function init() {// create a scene, that will hold all our elements such as objects, cameras and lights.var scene = new THREE.Scene();// create a camera, which defines where we're looking at.var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);// create a render and set the sizevar renderer = new THREE.WebGLRenderer();// var renderer = new THREE.WebGLRenderer({alpha: true});/*** alpha: true 啟用透明度支持* !!!透明度沒有生效* 1.因為沒有啟用渲染器透明度支持* 2.沒有正確使用setClearColor方法兩個參數*   renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));*   透明度 (0-1) 0 完全透明 1完全不透明**/renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));renderer.setSize(window.innerWidth, window.innerHeight);// renderer.shadowMapEnabled = true;// 啟用陰影貼圖:用于在3D場景中生成和渲染物體的陰影// create the ground planevar planeGeometry = new THREE.PlaneGeometry(60, 20, 20, 20);var planeMaterial = new THREE.MeshPhongMaterial({ color: 0xffffff });var plane = new THREE.Mesh(planeGeometry, planeMaterial);plane.receiveShadow = true; // 接收陰影// rotate and position the planeplane.rotation.x = -0.5 * Math.PI;plane.position.x = 15;plane.position.y = 0;plane.position.z = 0;// add the plane to the scenescene.add(plane);// create a cubevar cubeGeometry = new THREE.BoxGeometry(4, 4, 4);var cubeMaterial = new THREE.MeshLambertMaterial({ color: 0xff7777 });// MeshLambertMaterial 基于Lambert反射模型的材質,適用于不發光且沒有高光的物體(表面粗糙、沒有明顯反光)。它支持顏色、貼圖、透明度等屬性。var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);cube.castShadow = true; // 投射陰影// position the cubecube.position.x = -4;cube.position.y = 3;cube.position.z = 0;// add the cube to the scenescene.add(cube);var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);var sphereMaterial = new THREE.MeshLambertMaterial({ color: 0x7777ff });var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);// position the spheresphere.position.x = 20;sphere.position.y = 0;sphere.position.z = 2;sphere.castShadow = true;// add the sphere to the scenescene.add(sphere);// position and point the camera to the center of the scenecamera.position.x = -25;camera.position.y = 30;camera.position.z = 25;camera.lookAt(new THREE.Vector3(10, 0, 0));// 添加光源// add subtle ambient lightingvar ambiColor = "#0c0c0c";var ambientLight = new THREE.AmbientLight(ambiColor);scene.add(ambientLight);// add spotlight for the shadowsvar spotLight = new THREE.SpotLight(0xffffff);console.log(spotLight.position);spotLight.position.set(-40, 60, -10);spotLight.castShadow = true;console.log(spotLight);console.log(spotLight.target); // 默認(0,0,0)console.log(spotLight.shadow); // undefined shadow屬性是在r128之后引入的// scene.add( spotLight );var pointColor = "#ccffcc";var pointLight = new THREE.PointLight(pointColor); // 無陰影pointLight.distance = 100; // 0 無限遠 光線強度不隨距離增加而減弱scene.add(pointLight);// add a small sphere simulating the pointlightvar sphereLight = new THREE.SphereGeometry(0.2);var sphereLightMaterial = new THREE.MeshBasicMaterial({ color: 0xac6c25 });var sphereLightMesh = new THREE.Mesh(sphereLight, sphereLightMaterial);sphereLightMesh.castShadow = true;sphereLightMesh.position = new THREE.Vector3(3, 0, 3);scene.add(sphereLightMesh);// const directionalLight = new THREE.DirectionalLight(0xffffff, 1);// directionalLight.position.set(-40, 60, -10);// directionalLight.castShadow = true;// scene.add(directionalLight);// add the output of the renderer to the html elementdocument.getElementById("WebGL-output").appendChild(renderer.domElement);// call the render functionvar step = 0;// used to determine the switch point for the light animationvar invert = 1;var phase = 0;var controls = new function () {this.rotationSpeed = 0.03;this.bouncingSpeed = 0.03;this.ambientColor = ambiColor;this.pointColor = pointColor;this.intensity = 1;this.distance = 100;};var gui = new dat.GUI();gui.addColor(controls, 'ambientColor').onChange(function (e) {ambientLight.color = new THREE.Color(e);});gui.addColor(controls, 'pointColor').onChange(function (e) {pointLight.color = new THREE.Color(e);});gui.add(controls, 'intensity', 0, 3).onChange(function (e) {pointLight.intensity = e;});gui.add(controls, 'distance', 0, 100).onChange(function (e) {pointLight.distance = e;});var stats = initStats();render();function render() {stats.update();// rotate the cube around its axescube.rotation.x += controls.rotationSpeed;cube.rotation.y += controls.rotationSpeed;cube.rotation.z += controls.rotationSpeed;// bounce the sphere up and downstep += controls.bouncingSpeed;sphere.position.x = 20 + (10 * (Math.cos(step)));sphere.position.y = 2 + (10 * Math.abs(Math.sin(step)));// move the light simulationif (phase > 2 * Math.PI) {invert = invert * -1;phase -= 2 * Math.PI;} else {phase += controls.rotationSpeed;}sphereLightMesh.position.z = +(7 * (Math.sin(phase)));sphereLightMesh.position.x = +(14 * (Math.cos(phase)));sphereLightMesh.position.y = 5;if (invert < 0) {var pivot = 14;sphereLightMesh.position.x = (invert * (sphereLightMesh.position.x - pivot)) + pivot;}pointLight.position.copy(sphereLightMesh.position);// render using requestAnimationFramerequestAnimationFrame(render);renderer.render(scene, camera);}function initStats() {var stats = new Stats();stats.setMode(0); // 0: fps, 1: ms// Align top-leftstats.domElement.style.position = 'absolute';stats.domElement.style.left = '0px';stats.domElement.style.top = '0px';document.getElementById("Stats-output").appendChild(stats.domElement);return stats;}}window.onload = init</script>
</body></html>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/84555.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/84555.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/84555.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

大模型在輸尿管下段積水預測及臨床應用的研究

目錄 一、引言 1.1 研究背景與意義 1.2 研究目的 1.3 研究范圍與限制 1.4 文獻綜述 1.5 研究方法和框架 二、相關理論與概念 2.1 大模型技術原理 2.2 輸尿管下段積水病理機制 2.3 大模型在醫學預測領域的應用 三、大模型預測輸尿管下段積水的方法 3.1 數據收集 3.…

gitlab相關操作

2025.06.11今天我學習了如何在終端使用git相關操作&#xff1a; 一、需要修改新的倉庫git地址的時候&#xff1a; &#xff08;1&#xff09;檢查當前遠程倉庫 git remote -v 輸出示例&#xff1a; origin https://github.com/old-repo.git (fetch) origin https://github.c…

51c自動駕駛~合集58

我自己的原文哦~ https://blog.51cto.com/whaosoft/13967107 #CCA-Attention 全局池化局部保留&#xff0c;CCA-Attention為LLM長文本建模帶來突破性進展 琶洲實驗室、華南理工大學聯合推出關鍵上下文感知注意力機制&#xff08;CCA-Attention&#xff09;&#xff0c;…

通過共享內存在多程序之間實現數據通信

注&#xff1a;以下內容為與 GPT-4O 共同創作完成 以共享內存的方式實現多程序之間的數據通信&#xff0c;尤其適合在一臺機器上的多程序之間進行高頻數據交換。 以下示例展示了 sender.py 向 receiver.py 發送數據并接收經 receiver.py 處理后的數據&#xff0c;以及如何通過…

[論文閱讀] 人工智能+軟件工程 | 理解GitGoodBench:評估AI代理在Git中表現的新基準

理解GitGoodBench&#xff1a;評估AI代理在Git中表現的新基準 論文信息 GitGoodBench: A Novel Benchmark For Evaluating Agentic Performance On Git Tobias Lindenbauer, Egor Bogomolov, Yaroslav Zharov Cite as: arXiv:2505.22583 [cs.SE] 研究背景&#xff1a;當AI走進…

開源 java android app 開發(十二)封庫.aar

文章的目的為了記錄使用java 進行android app 開發學習的經歷。本職為嵌入式軟件開發&#xff0c;公司安排開發app&#xff0c;臨時學習&#xff0c;完成app的開發。開發流程和要點有些記憶模糊&#xff0c;趕緊記錄&#xff0c;防止忘記。 相關鏈接&#xff1a; 開源 java an…

ubuntu + nginx 1.26 + php7.4 + mysql8.0 調優

服務器配置 8核 16G 查看內存 free -h nginx配置 worker_processes auto; # 自動檢測CPU核心數 worker_rlimit_nofile 65535; # 提高文件描述符限制 ? events {worker_connections 8192; # 每個worker的最大連接數multi_accept on; # 一次性接受…

[未驗證]abaqus2022 更改內置python

如何在 Abaqus 2022 中更改內置 Python 在 Abaqus 中&#xff0c;Python 是常用的腳本語言&#xff0c;它使得用戶能夠自動化模型的創建、分析和后處理。可能有時候你需要更改默認的 Python 版本&#xff0c;比如使用特定庫或者功能。本文將為您詳細說明如何在 Abaqus 2022 中更…

RAG文檔解析難點2:excel數據“大海撈針”,超大Excel解析與精準行列查詢指南

寫在前面 在構建檢索增強生成(RAG)應用時,Excel文件是不可或缺的數據源。它們通常包含了企業運營、市場分析、科學研究等各個領域的寶貴數據。然而,當這些Excel文件變得“超大”——可能包含數十萬甚至數百萬行數據時,傳統的解析方法和RAG數據處理流程將面臨嚴峻的內存、…

深度掌控,智啟未來 —— 基于 STM32F103RBT6 的控制板

在科技浪潮奔涌向前的時代&#xff0c;電子領域的創新發展從未停歇。對于電子工程師、科研工作者以及電子技術愛好者&#xff0c;在校電子專業學生而言&#xff0c;一款性能卓越、功能全面且穩定可靠的開發板&#xff0c;是探索電子世界奧秘、實現創意構想的關鍵基石。今天&…

什么樣的登錄方式才是最安全的?

目錄 一、基礎協議&#xff1a;HTTP與HTTPS HTTP協議 HTTPS協議 二、常見Web攻擊與防御 2.1 XSS 常見攻擊手段 針對XSS 攻擊竊取 Cookie 2.2 CSRF CSRF攻擊的核心特點 與XSS的區別 常見防御措施 三、疑問解答 四、登錄方式演變 4.1 方案一&#x1f436;狗都不用 …

android studio底部導航欄

實現底部導航欄切換 將java文件return的xml文件賦值給頁面FrameLayout控件 java文件BottomNavigationView&#xff0c;監聽器setOnNavigationItemSelectedListener MainActivity.java代碼 package com.example.myapplication;import android.os.Bundle;import androidx.appc…

vue-router相關理解

一、前言 隨著 Vue.js 在前端開發中的廣泛應用&#xff0c;Vue Router 成為了 Vue 官方推薦的路由管理器。它不僅支持單頁面應用&#xff08;SPA&#xff09;中常見的路由跳轉、嵌套路由、懶加載等功能&#xff0c;還提供了導航守衛、動態路由等高級特性。 本文將帶你深入了解…

uni-app 自定義路由封裝模塊詳解(附源碼逐行解讀)

&#x1f680;uni-app 自定義路由封裝模塊詳解&#xff08;附源碼逐行解讀&#xff09; &#x1f4cc; 請收藏 點贊 關注&#xff0c;獲取更多 uni-app 項目實用技巧&#xff01; 在實際 uni-app 項目中&#xff0c;我們常常需要對 uni.navigateTo、uni.switchTab 等 API 做…

QML顯示圖片問題解決辦法

以前用qtwediget的時候&#xff0c;好像是放在qlabel或者什么組件上面&#xff0c;把圖片的路徑放上去就可以直接加載&#xff0c;但我用QML創建界面的時候就遇到了問題&#xff0c;哦對&#xff0c;qtwedget用qpixmap組件顯示圖片&#xff0c;也有image。話說回來&#xff0c;…

Vue中使用jsx

1. jsx的babel配置 1.1 在項目中使用jsx&#xff0c;需要添加對jsx的支持&#xff1a; jsx通常會通過Babel來進行轉換(React編寫的jsx就是通過babel轉換的)Vue中&#xff0c;只需要在Babel中配置對應的插件即可以下列舉需要支持轉換的案例&#xff1a; template -> vue-l…

Spring Cache+Redis緩存方案 vs 傳統redis緩存直接使用RedisTemplate 方案對比

結合 Spring Cache 和 Redis 的緩存方案&#xff08;即 Spring Cache Redis&#xff09;相較于普通的 Redis 緩存使用&#xff08;如直接通過 RedisTemplate 操作&#xff09;&#xff0c;具有以下顯著優勢&#xff1a; 具體實現方案請參考&#xff1a;Spring CacheRedis緩存…

Web應用安全漏洞掃描:原理、常用方法及潛在風險解析?

Web應用安全的關鍵環節在于進行漏洞掃描&#xff0c;這種掃描通過自動化或半自動化的方式&#xff0c;對應用進行安全測試。它能揭示出配置錯誤、代碼缺陷等眾多安全風險。接下來&#xff0c;我將詳細闡述這些情況。 掃描原理 它主要模擬攻擊者的行為&#xff0c;以探測和攻擊…

Spring中@Value注解:原理、加載順序與實戰指南

文章目錄 前言一、Value注解的核心原理1.1 容器啟動階段&#xff1a;環境準備1.2 Bean實例化階段&#xff1a;后置處理器介入1.3 值解析階段&#xff1a;雙引擎處理1. 占位符解析&#xff08;${...}&#xff09;2. SpEL表達式解析&#xff08;#{...}&#xff09; 1.4 類型轉換與…

MySQL 8配置文件詳解

MySQL 8 配置文件詳解 MySQL 8 的配置文件(my.cnf或my.ini)是MySQL服務器啟動時讀取的主要配置文件&#xff0c;它包含了服務器運行所需的各種參數設置。以下是MySQL 8配置文件的詳細解析&#xff1a; 配置文件位置 MySQL 8 會按照以下順序查找配置文件&#xff1a; /etc/m…