three.js学习笔记

 1. 点材质的使用

import * as THREE from "three"
// 导入轨道控制器
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'


// console.log(THREE)
// 1.创建场景
const scene = new THREE.Scene()

// 2.创建相机 --> 透视相机
const camera = new THREE.PerspectiveCamera(
	75, 
	window.innerWidth / window.innerHeight,
	0.1,
	100
)

// 设置相机位置
camera.position.set(0, 0, 10)
// 将相机添加到场景中
scene.add(camera)

// 创建球几何体
const sphereGeometry = new THREE.SphereBufferGeometry(3, 30, 30)
// const material = new THREE.MeshBasicMaterial({ 
//   color: 0xff0000, 
//   wireframe: true 
// })
// const mesh = new THREE.Mesh(sphereGeometry, material)
// scene.add(mesh)

// 设置点材质
const pointsMaterial = new THREE.PointsMaterial()
pointsMaterial.size = 0.1
pointsMaterial.color.set(0xfff000)
// 相机深度而衰减
pointsMaterial.sizeAttenuation = true

// 载入纹理
const textureLoader = new THREE.TextureLoader()
// const texture = textureLoader.load('./textures/particles/1.png')
const texture = textureLoader.load('./textures/particles/2.png')
// 设置点材质纹理
pointsMaterial.map = texture
pointsMaterial.alphaMap = texture
pointsMaterial.transparent  =true
// 渲染此材质是否对深度缓冲区有任何影响。默认为true。
pointsMaterial. depthWrite = false
pointsMaterial.blending = THREE.AdditiveBlending
 
const points = new THREE.Points(sphereGeometry, pointsMaterial)
scene.add(points)

// 初始化渲染器
const renderer = new THREE.WebGLRenderer();
// 设置渲染的尺寸大小
renderer.setSize(window.innerWidth, window.innerHeight);
// 开启场景中的阴影贴图
renderer.shadowMap.enabled = true
renderer.physicallyCorrectLights = true

// console.log(renderer);
// 将webgl渲染的canvas内容添加到body
document.body.appendChild(renderer.domElement);

// // 使用渲染器,通过相机将场景渲染进来
// renderer.render(scene, camera);

// 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement);
// 设置控制器阻尼,让控制器更有真实效果,必须在动画循环里调用.update()。
controls.enableDamping = true;

// 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
// 设置时钟
const clock = new THREE.Clock();

function render() {
  let time = clock.getElapsedTime()
  controls.update()
  renderer.render(scene, camera)
  //   渲染下一帧的时候就会调用render函数
  requestAnimationFrame(render)
}

render();

// 监听画面变化,更新渲染画面
window.addEventListener("resize", () => {
  //   console.log("画面变化了");
  // 更新摄像头
  camera.aspect = window.innerWidth / window.innerHeight;
  //   更新摄像机的投影矩阵
  camera.updateProjectionMatrix();

  //   更新渲染器
  renderer.setSize(window.innerWidth, window.innerHeight);
  //   设置渲染器的像素比
  renderer.setPixelRatio(window.devicePixelRatio);
});

2.使用pointes打造绚丽多彩的星空

import * as THREE from "three"
// 导入轨道控制器
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'

// 使用pointes打造绚丽多彩的星空

// console.log(THREE)
// 1.创建场景
const scene = new THREE.Scene()

// 2.创建相机 --> 透视相机
const camera = new THREE.PerspectiveCamera(
	75, 
	window.innerWidth / window.innerHeight,
	0.1,
	100
)

// 设置相机位置
camera.position.set(0, 0, 10)
// 将相机添加到场景中
scene.add(camera)

// 创建球几何体
const sphereGeometry = new THREE.SphereBufferGeometry(3, 30, 30)

const particlesGeometry = new THREE.BufferGeometry()
const count = 5000

// 设置缓冲区数组
const positions = new Float32Array(count * 3)
// 设置顶点的颜色
const colors = new Float32Array(count * 3)
// 设置顶点
for(let i = 0; i < count*3; i++) {
  // 点的位置是随机的
  positions[i] = Math.random() * 100 - 50
  // 点的颜色随机
  colors[i] = Math.random()
}
particlesGeometry.setAttribute(
  'position', 
  new THREE.BufferAttribute(positions, 3)
)
particlesGeometry.setAttribute(
  'color',
  new THREE.BufferAttribute(colors, 3)
)

// 设置点材质
const pointsMaterial = new THREE.PointsMaterial()
pointsMaterial.size = 0.5
pointsMaterial.color.set(0xfff000)
// 相机深度而衰减
pointsMaterial.sizeAttenuation = true

// 载入纹理
const textureLoader = new THREE.TextureLoader()
// const texture = textureLoader.load('./textures/particles/1.png')
const texture = textureLoader.load('./textures/particles/1.png')
// 设置点材质纹理
pointsMaterial.map = texture
pointsMaterial.alphaMap = texture
pointsMaterial.transparent  =true
// 渲染此材质是否对深度缓冲区有任何影响。默认为true。
pointsMaterial. depthWrite = false
pointsMaterial.blending = THREE.AdditiveBlending
// 设置启动顶点的颜色
pointsMaterial.vertexColors = true
 
const points = new THREE.Points(particlesGeometry, pointsMaterial)
scene.add(points)

// 初始化渲染器
const renderer = new THREE.WebGLRenderer();
// 设置渲染的尺寸大小
renderer.setSize(window.innerWidth, window.innerHeight);
// 开启场景中的阴影贴图
renderer.shadowMap.enabled = true
renderer.physicallyCorrectLights = true

// console.log(renderer);
// 将webgl渲染的canvas内容添加到body
document.body.appendChild(renderer.domElement);

// // 使用渲染器,通过相机将场景渲染进来
// renderer.render(scene, camera);

// 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement);
// 设置控制器阻尼,让控制器更有真实效果,必须在动画循环里调用.update()。
controls.enableDamping = true;

// 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
// 设置时钟
const clock = new THREE.Clock();

function render() {
  let time = clock.getElapsedTime()
  controls.update()
  renderer.render(scene, camera)
  //   渲染下一帧的时候就会调用render函数
  requestAnimationFrame(render)
}

render();

// 监听画面变化,更新渲染画面
window.addEventListener("resize", () => {
  // 更新摄像头
  camera.aspect = window.innerWidth / window.innerHeight;
  //   更新摄像机的投影矩阵
  camera.updateProjectionMatrix();

  //   更新渲染器
  renderer.setSize(window.innerWidth, window.innerHeight);
  //   设置渲染器的像素比
  renderer.setPixelRatio(window.devicePixelRatio);
});

3.打造漫天飞舞的雪花

import * as THREE from "three"
// 导入轨道控制器
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'

// 打造漫天的雪花

// console.log(THREE)
// 1.创建场景
const scene = new THREE.Scene()

// 2.创建相机 --> 透视相机
const camera = new THREE.PerspectiveCamera(
	75, 
	window.innerWidth / window.innerHeight,
	0.1,
	30
)
 
// 设置相机位置
camera.position.set(0, 0, 40)
// 将相机添加到场景中
scene.add(camera)

function createPoints(url, size = 0.5) {
  // 创建球几何体
  const sphereGeometry = new THREE.SphereBufferGeometry(3, 30, 30)

  const particlesGeometry = new THREE.BufferGeometry()
  const count = 10000

  // 设置缓冲区数组
  const positions = new Float32Array(count * 3)
  // 设置顶点的颜色
  const colors = new Float32Array(count * 3)
  // 设置顶点
  for(let i = 0; i < count*3; i++) {
    // 点的位置是随机的
    positions[i] = (Math.random() - 0.5) * 100
    // 点的颜色随机
    colors[i] = Math.random()
  }
  particlesGeometry.setAttribute(
    'position', 
    new THREE.BufferAttribute(positions, 3)
  )
  particlesGeometry.setAttribute(
    'color',
    new THREE.BufferAttribute(colors, 3)
  )

  // 设置点材质
  const pointsMaterial = new THREE.PointsMaterial()
  pointsMaterial.size = size
  pointsMaterial.color.set(0xfff000)
  // 相机深度而衰减
  pointsMaterial.sizeAttenuation = true

  // 载入纹理
  const textureLoader = new THREE.TextureLoader()
  // const texture = textureLoader.load('./textures/particles/1.png')
  const texture = textureLoader.load(`./textures/particles/${url}.png`)
  // 设置点材质纹理
  pointsMaterial.map = texture
  pointsMaterial.alphaMap = texture
  pointsMaterial.transparent  =true
  // 渲染此材质是否对深度缓冲区有任何影响。默认为true。
  pointsMaterial. depthWrite = false
  pointsMaterial.blending = THREE.AdditiveBlending
  // 设置启动顶点的颜色
  pointsMaterial.vertexColors = true
  
  const points = new THREE.Points(particlesGeometry, pointsMaterial)
  // 添加到场景中
  scene.add(points)
  return points
}

const points = createPoints('8')
// const points1 = createPoints('3', 1)
// const points2 = createPoints('10', 1)
const points3 = createPoints('14', 0.5)

// 初始化渲染器
const renderer = new THREE.WebGLRenderer();
// 设置渲染的尺寸大小
renderer.setSize(window.innerWidth, window.innerHeight);
// 开启场景中的阴影贴图
renderer.shadowMap.enabled = true
renderer.physicallyCorrectLights = true

// console.log(renderer);
// 将webgl渲染的canvas内容添加到body
document.body.appendChild(renderer.domElement);

// // 使用渲染器,通过相机将场景渲染进来
// renderer.render(scene, camera);

// 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement);
// 设置控制器阻尼,让控制器更有真实效果,必须在动画循环里调用.update()。
controls.enableDamping = true;

// 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
// 设置时钟
const clock = new THREE.Clock();

function render() {
  let time = clock.getElapsedTime()
  // 设置旋转
  points.rotation.x = time * 0.3
  // points1.rotation.x = time * 0.5
  // points2.rotation.x = time * 0.5
  // points1.rotation.y = time * 0.05
  points3.rotation.x = time * 0.3
  points3.rotation.y = time * 0.05
  controls.update()
  renderer.render(scene, camera)
  //   渲染下一帧的时候就会调用render函数
  requestAnimationFrame(render)
}

render();

// 监听画面变化,更新渲染画面
window.addEventListener("resize", () => {
  // 更新摄像头
  camera.aspect = window.innerWidth / window.innerHeight;
  //   更新摄像机的投影矩阵
  camera.updateProjectionMatrix();

  //   更新渲染器
  renderer.setSize(window.innerWidth, window.innerHeight);
  //   设置渲染器的像素比
  renderer.setPixelRatio(window.devicePixelRatio);
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我会为您解答关于WebGL three.js的阴影与实现物体动画的问题。首先,让我们来了解一下WebGL three.js是什么。 WebGL three.js是一款基于WebGL的JavaScript 3D库,可以帮助我们快速搭建3D场景和应用。接下来我们来讲解阴影和实现物体动画的方法。 一、阴影 阴影是模拟物体之间的阴影效果,让3D场景更加真实。在three.js中,我们可以通过设置Mesh的castShadow和receiveShadow属性来实现阴影效果。 1. 首先,我们需要在场景中添加光源,例如SpotLight或DirectionalLight。 2. 然后,在需要投射阴影的物体上设置castShadow为true。 3. 最后,在需要接收阴影的物体上设置receiveShadow为true。 代码示例: ```javascript // 添加光源 const light = new THREE.SpotLight(0xffffff); light.position.set(0, 100, 0); light.castShadow = true; scene.add(light); // 添加需要投射阴影的物体 const cube = new THREE.Mesh(new THREE.BoxGeometry(10, 10, 10), new THREE.MeshLambertMaterial({ color: 0xff0000 })); cube.castShadow = true; scene.add(cube); // 添加需要接收阴影的物体 const plane = new THREE.Mesh(new THREE.PlaneGeometry(200, 200, 1, 1), new THREE.MeshLambertMaterial({ color: 0xffffff })); plane.receiveShadow = true; plane.rotation.x = -Math.PI / 2; scene.add(plane); ``` 二、物体动画 在three.js中,我们可以通过Tween.js库来实现物体的动画效果。Tween.js是一款JavaScript动画库,可以帮助我们实现非常丰富的动画效果。 1. 首先,我们需要在HTML文件中引入Tween.js库文件。 2. 然后,在需要动画的物体上设置初始状态。 3. 最后,通过Tween.js库来设置物体的目标状态和动画效果,例如缓动动画(ease)或弹跳动画(bounce)。 代码示例: ```javascript // 引入Tween.js库文件 <script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/18.6.4/tween.min.js"></script> // 添加需要动画的物体 const cube = new THREE.Mesh(new THREE.BoxGeometry(10, 10, 10), new THREE.MeshLambertMaterial({ color: 0xff0000 })); cube.position.set(0, 0, 0); scene.add(cube); // 设置初始状态 const start = { x: 0, y: 0, z: 0 }; // 设置目标状态 const end = { x: 50, y: 50, z: 50 }; // 设置动画效果 const tween = new TWEEN.Tween(start) .to(end, 1000) .easing(TWEEN.Easing.Quadratic.InOut) .onUpdate(() => { cube.position.set(start.x, start.y, start.z); }) .start(); ``` 以上是关于WebGL three.js阴影与实现物体动画的方法,希望能够对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值