实现轮胎印

2024-06-13 03:58
文章标签 实现 轮胎

本文主要是介绍实现轮胎印,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

接到一个任务,实现一个轮胎印记。特效用粒子做的效果, 感觉不是很好。主程大哥说,那程序来实现吧。

关键词

  • 主程大哥,给了一些关键词。
  • TrailRender
  • Decal
  • 在项目使用的版本上,纹理贴图是被拉伸的,实际是要的纹理复制使用,看网格的效果如下,在SomeTips的skidmarksTest.sence

  • 看到Unity5.6.2.f上,使用TrailRender可以实现想要的效果,demo 的SkidmarksTest.unity

网格控制-网络

  • 看到一个解决方案,是网格动画,参考A skidmark effect for vehicles in Unity3D
  • 从文章的中github看到案例。是在初始化一定数目的网格,然后根据位置来使用。
  • 代码分析
using UnityEngine;
using UnityEngine.Rendering;// Skidmarks controller. Put one of these in a scene somewhere. Call AddSkidMark.
// Copyright 2017 Nition, BSD licence (see LICENCE file). http://nition.co
public class Skidmarks : MonoBehaviour {// INSPECTOR SETTINGS[SerializeField]Material skidmarksMaterial; // Material for the skidmarks to use// END INSPECTOR SETTINGSconst int MAX_MARKS = 2048; // Max number of marks total for everyone togetherconst float MARK_WIDTH = 0.35f; // Width of the skidmarks. Should match the width of the wheelsconst float GROUND_OFFSET = 0.02f;  // Distance above surface in metresconst float MIN_DISTANCE = 0.25f; // Distance between skid texture sections in metres. Bigger = better performance, less smoothconst float MIN_SQR_DISTANCE = MIN_DISTANCE * MIN_DISTANCE;// Info for each mark created. Needed to generate the correct meshclass MarkSection {public Vector3 Pos = Vector3.zero;public Vector3 Normal = Vector3.zero;public Vector4 Tangent = Vector4.zero;public Vector3 Posl = Vector3.zero;public Vector3 Posr = Vector3.zero;public byte Intensity;public int LastIndex;};int markIndex;MarkSection[] skidmarks;Mesh marksMesh;MeshRenderer mr;MeshFilter mf;Vector3[] vertices;Vector3[] normals;Vector4[] tangents;Color32[] colors;Vector2[] uvs;int[] triangles;bool meshUpdated;bool haveSetBounds;// #### UNITY INTERNAL METHODS ####protected void Start() {// Generate a fixed array of skidmarksskidmarks = new MarkSection[MAX_MARKS];for (int i = 0; i < MAX_MARKS; i++) {skidmarks[i] = new MarkSection();}mf = GetComponent<MeshFilter>();mr = GetComponent<MeshRenderer>();if (mr == null) {mr = gameObject.AddComponent<MeshRenderer>();}marksMesh = new Mesh();marksMesh.MarkDynamic();if (mf == null) {mf = gameObject.AddComponent<MeshFilter>();}mf.sharedMesh = marksMesh;vertices = new Vector3[MAX_MARKS * 4];normals = new Vector3[MAX_MARKS * 4];tangents = new Vector4[MAX_MARKS * 4];colors = new Color32[MAX_MARKS * 4];uvs = new Vector2[MAX_MARKS * 4];triangles = new int[MAX_MARKS * 6];mr.shadowCastingMode = ShadowCastingMode.Off;mr.receiveShadows = false;mr.material = skidmarksMaterial;mr.lightProbeUsage = LightProbeUsage.Off;}protected void LateUpdate() {if (!meshUpdated) return;meshUpdated = false;// Reassign the mesh if it's changed this framemarksMesh.vertices = vertices;marksMesh.normals = normals;marksMesh.tangents = tangents;marksMesh.triangles = triangles;marksMesh.colors32 = colors;marksMesh.uv = uvs;if (!haveSetBounds) {// Could use RecalculateBounds here each frame instead, but it uses about 0.1-0.2ms each time// Save time by just making the mesh bounds huge, so the skidmarks will always draw// Not sure why I only need to do this once, yet can't do it in Start (it resets to zero)marksMesh.bounds = new Bounds(new Vector3(0, 0, 0), new Vector3(10000, 10000, 10000));haveSetBounds = true;}mf.sharedMesh = marksMesh;}// #### PUBLIC METHODS ####// Function called by the wheel that's skidding. Sets the intensity of the skidmark section// by setting the alpha of the vertex colorpublic int AddSkidMark(Vector3 pos, Vector3 normal, float intensity, int lastIndex) {if (intensity > 1) intensity = 1.0f;else if (intensity < 0) return -1;if (lastIndex > 0) {float sqrDistance = (pos - skidmarks[lastIndex].Pos).sqrMagnitude;if (sqrDistance < MIN_SQR_DISTANCE) return lastIndex;}MarkSection curSection = skidmarks[markIndex];curSection.Pos = pos + normal * GROUND_OFFSET;curSection.Normal = normal;curSection.Intensity = (byte)(intensity * 255f);curSection.LastIndex = lastIndex;if (lastIndex != -1) {MarkSection lastSection = skidmarks[lastIndex];Vector3 dir = (curSection.Pos - lastSection.Pos);Vector3 xDir = Vector3.Cross(dir, normal).normalized;curSection.Posl = curSection.Pos + xDir * MARK_WIDTH * 0.5f;curSection.Posr = curSection.Pos - xDir * MARK_WIDTH * 0.5f;curSection.Tangent = new Vector4(xDir.x, xDir.y, xDir.z, 1);if (lastSection.LastIndex == -1) {lastSection.Tangent = curSection.Tangent;lastSection.Posl = curSection.Pos + xDir * MARK_WIDTH * 0.5f;lastSection.Posr = curSection.Pos - xDir * MARK_WIDTH * 0.5f;}}UpdateSkidmarksMesh();int curIndex = markIndex;// Update circular indexmarkIndex = ++markIndex % MAX_MARKS;return curIndex;}// #### PROTECTED/PRIVATE METHODS ####// Update part of the mesh for the current markIndexvoid UpdateSkidmarksMesh() {MarkSection curr = skidmarks[markIndex];// Nothing to connect to yetif (curr.LastIndex == -1) return;MarkSection last = skidmarks[curr.LastIndex];vertices[markIndex * 4 + 0] = last.Posl;vertices[markIndex * 4 + 1] = last.Posr;vertices[markIndex * 4 + 2] = curr.Posl;vertices[markIndex * 4 + 3] = curr.Posr;normals[markIndex * 4 + 0] = last.Normal;normals[markIndex * 4 + 1] = last.Normal;normals[markIndex * 4 + 2] = curr.Normal;normals[markIndex * 4 + 3] = curr.Normal;tangents[markIndex * 4 + 0] = last.Tangent;tangents[markIndex * 4 + 1] = last.Tangent;tangents[markIndex * 4 + 2] = curr.Tangent;tangents[markIndex * 4 + 3] = curr.Tangent;colors[markIndex * 4 + 0] = new Color32(0, 0, 0, last.Intensity);colors[markIndex * 4 + 1] = new Color32(0, 0, 0, last.Intensity);colors[markIndex * 4 + 2] = new Color32(0, 0, 0, curr.Intensity);colors[markIndex * 4 + 3] = new Color32(0, 0, 0, curr.Intensity);uvs[markIndex * 4 + 0] = new Vector2(0, 0);uvs[markIndex * 4 + 1] = new Vector2(1, 0);uvs[markIndex * 4 + 2] = new Vector2(0, 1);uvs[markIndex * 4 + 3] = new Vector2(1, 1);triangles[markIndex * 6 + 0] = markIndex * 4 + 0;triangles[markIndex * 6 + 2] = markIndex * 4 + 1;triangles[markIndex * 6 + 1] = markIndex * 4 + 2;triangles[markIndex * 6 + 3] = markIndex * 4 + 2;triangles[markIndex * 6 + 5] = markIndex * 4 + 1;triangles[markIndex * 6 + 4] = markIndex * 4 + 3;meshUpdated = true;}
}
  • 在LateUpdate()对位置做处理。
  • 在理解了demo中用使用WheelCollider,rigidbody。其实是针对速度有每帧计算,速度大于0.5f有了颜色深浅的变化。还有在Y方向上,给予了(0,1,0)* 0.02f偏移值,和地面有一点间距。
  • 针对项目中具体使用,加速度部分是可以不用的,但是从效率的角度考虑,mesh的数量不要太多,可以复用。同时可以随着时间渐渐隐藏。
  • 不断使用回收,像对象池一样的。在原来的基础上,对代码做了二次开发。
  • 在渲染角度上,只增加了一个DrawCall。
  • 网络长度上和渐隐时间可以在预制件上修改,让美术调整出最好的效果。
  • 当然发现自己计算的把运动物体本身的位移忽视了。

因为公司原因,最终的代码不能分享。

这篇关于实现轮胎印的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1056220

相关文章

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoo WebFlux+MongoDB实现非阻塞API过程

《SpringBooWebFlux+MongoDB实现非阻塞API过程》本文介绍了如何使用SpringBootWebFlux和MongoDB实现非阻塞API,通过响应式编程提高系统的吞吐量和响应性能... 目录一、引言二、响应式编程基础2.1 响应式编程概念2.2 响应式编程的优势2.3 响应式编程相关技术

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC

Nginx更新SSL证书的实现步骤

《Nginx更新SSL证书的实现步骤》本文主要介绍了Nginx更新SSL证书的实现步骤,包括下载新证书、备份旧证书、配置新证书、验证配置及遇到问题时的解决方法,感兴趣的了解一下... 目录1 下载最新的SSL证书文件2 备份旧的SSL证书文件3 配置新证书4 验证配置5 遇到的http://www.cppc

Nginx之https证书配置实现

《Nginx之https证书配置实现》本文主要介绍了Nginx之https证书配置的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 目录背景介绍为什么不能部署在 IIS 或 NAT 设备上?具体实现证书获取nginx配置扩展结果验证

SpringBoot整合 Quartz实现定时推送实战指南

《SpringBoot整合Quartz实现定时推送实战指南》文章介绍了SpringBoot中使用Quartz动态定时任务和任务持久化实现多条不确定结束时间并提前N分钟推送的方案,本文结合实例代码给大... 目录前言一、Quartz 是什么?1、核心定位:解决什么问题?2、Quartz 核心组件二、使用步骤1