Threejs-11、材质文理颜色

主要如下代码

//texture.colorSpace = THREE.SRGBColorSpace;
//texture.colorSpace = THREE.LinearSRGBColorSpace;
texture.colorSpace = THREE.NoColorSpace;
gui.add(texture,'colorSpace',{
  sRGB:THREE.SRGBColorSpace,
  Linear:THREE.LinearSRGBColorSpace,
}).onChange(()=>{
  texture.needsUpdate = true;
})

完整代码

<script setup>
// 导入threejs
import * as THREE from "three"
// 引入轨道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
// 引入lil.gui
import { GUI } from "three/examples/jsm/libs/lil-gui.module.min.js";
//导入hdr加载器

import{ RGBELoader} from "three/examples/jsm/loaders/RGBELoader.js"

// 创建场景
const scene = new THREE.Scene();
//创建相机
const camera = new THREE.PerspectiveCamera(
    800,// 视角
    window.innerWidth / window.innerHeight,
    0.1, // 近平面
    1000  // 远平面
);
// 创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth,window.innerHeight);
document.body.appendChild(renderer.domElement);
// 创建文理加载器
let textureLoader = new THREE.TextureLoader();
// 加载文理
let texture = textureLoader.load("./img/image.png")

//texture.colorSpace = THREE.SRGBColorSpace;
//texture.colorSpace = THREE.LinearSRGBColorSpace;
texture.colorSpace = THREE.NoColorSpace;

//  加载ao贴图
let aoMap = textureLoader.load("./img/image.png");

//透明度贴图
let alphaMap = textureLoader.load("./img/1718266068070.png")

// 光照贴图
let lightMap = textureLoader.load("./img/光照贴图.png")
// 高光贴图
let specularMap = textureLoader.load("./img/高光贴图.png")
// 创建rgbeLoader对象
let rgbeLoader = new RGBELoader();

rgbeLoader.load("./img/幼儿园1.hdr",(enMap)=>{
  // 设置球形映射
  enMap.mapping = THREE.EquirectangularReflectionMapping;
  scene.background=enMap;
  //场景设置环境贴图
  scene.environment = enMap;
  //设置plane 环境贴图
  planeMaterial.envMap= enMap;
})
// 创建一个平面
let planeGeometry = new THREE.PlaneGeometry(1,1);
// 创建平面材质
let planeMaterial = new THREE.MeshBasicMaterial({

  color:0xffffff,
  map:texture,
  // 允许透明度
  transparent:true,
  // 设置ao贴图
  aoMap:aoMap,
  aoMapIntensity:0,
  //透明度贴图
  //alphaMap:alphaMap,
  //反射强度设置  默认为11
  reflectivity:0.5,
  //光照贴图
  //lightMap:lightMap,
  //设置高光贴图
  specularMap:specularMap
})
// 设置文理第二种方式
//planeMaterial.map=texture;
//planeMaterial.aoMap=aoMap
let plane = new THREE.Mesh(planeGeometry,planeMaterial);
scene.add(plane);
//设置相机位置
camera.position.x=5;
camera.position.y=5;
camera.position.z=5;
//相机默认看向远点
camera.lookAt(0,0,0);
// 添加世界坐标辅助器
const axesHelper =  new THREE.AxesHelper(10);
scene.add(axesHelper);
// 添加轨道控制器
const controls = new OrbitControls(camera,renderer.domElement);
// 设置带阻尼的惯性
controls.enableDamping=true;
// 设置阻尼系数
controls.dampingFactor = 0.05;
// 设置自动旋转
//controls.autoRotate = true;
//渲染函数
function animate(){
    controls.update();
    requestAnimationFrame(animate);
    renderer.render(scene,camera);
}
animate();
// 监听窗口变化
window.addEventListener("resize",()=>{
  camera.aspect = window.innerWidth / window.innerHeight;
  //   更新摄像机的投影矩阵
  camera.updateProjectionMatrix();
  //   更新渲染器
  renderer.setSize(window.innerWidth, window.innerHeight);
  //   设置渲染器的像素比
  renderer.setPixelRatio(window.devicePixelRatio);
})
// 创建GUI
const gui =  new GUI();
gui.add(planeMaterial,"aoMapIntensity").min(0).max(1).name("ao强度")
gui.add(texture,'colorSpace',{
  sRGB:THREE.SRGBColorSpace,
  Linear:THREE.LinearSRGBColorSpace,
}).onChange(()=>{
  texture.needsUpdate = true;
})


</script>
<template>
  <div></div>
</template>
<style >
*{
  margin: 0;
  padding: 0;
}
canvas{
  display: block;
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
}
</style>

相关推荐

  1. Threejs-11材质文理颜色

    2024-06-14 12:52:04       33 阅读
  2. Three.js的材质Material信息

    2024-06-14 12:52:04       27 阅读
  3. three.js捋文档的记录笔记(五):threejs引入

    2024-06-14 12:52:04       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-14 12:52:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-14 12:52:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-14 12:52:04       87 阅读
  4. Python语言-面向对象

    2024-06-14 12:52:04       96 阅读

热门阅读

  1. Flutter知识点

    2024-06-14 12:52:04       33 阅读
  2. 软件设计模式概述

    2024-06-14 12:52:04       28 阅读
  3. Linux之逻辑控制符&&

    2024-06-14 12:52:04       31 阅读
  4. 带你学习Mybatis之执行器Executor

    2024-06-14 12:52:04       28 阅读
  5. 聊聊C/S模式架构的优缺点

    2024-06-14 12:52:04       33 阅读
  6. 面试题(常见)

    2024-06-14 12:52:04       35 阅读
  7. Webrtc支持FFMPEG硬解码之NVIDA(二)

    2024-06-14 12:52:04       35 阅读
  8. 字节一面(年前)测开—飞书

    2024-06-14 12:52:04       30 阅读
  9. 代码详解工厂设计模式【2】

    2024-06-14 12:52:04       30 阅读
  10. NumPy 字节交换

    2024-06-14 12:52:04       27 阅读
  11. 未来展望:超越现实的边界

    2024-06-14 12:52:04       21 阅读
  12. 2024-03 GESP C++ 六级试题及答案

    2024-06-14 12:52:04       24 阅读