unity shader 鼠标传入世界坐标到shader的练习

embedded/2024/10/8 19:02:39/

练习贴

c#代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;public class TestInputPosShader : MonoBehaviour
{public Material material;const int arrayCount= 2000;Vector4[] list = new Vector4[arrayCount];private int tempIndex;private void Update(){if (Input.GetMouseButton(0)){RaycastHit hit;Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);if (Physics.Raycast(ray, out hit, 1000)){// Debug.Log("Raycast hit: " + hit.point);list[tempIndex]= hit.point;material.SetVectorArray("_InputPoints", list);material.SetInt("_InputPointCount", tempIndex+1);tempIndex++;if (tempIndex>= arrayCount){tempIndex = 0;}}}}}

shader代码

Shader "Custom/MousePointSetColor"
{Properties{_MainTex ("Texture", 2D) = "white" {} //主纹理_Radius("Radius",float) = 1 //影响半径_Intensity("Intensity",float) = 1 //影响的强度_PointColor ("PointColor", Color) = (1,1,1,1) //选中颜色}SubShader{Tags{"Queue"="Transparent"}Blend SrcAlpha OneMinusSrcAlphaLOD 100Pass{CGPROGRAM#pragma vertex vert#pragma fragment frag// make fog work#pragma multi_compile_fog#include "UnityCG.cginc"half _Alpha;float _Radius;//影响半径float _Intensity;//影响的强度uniform int _InputPointCount;uniform float4 _InputPoints[2000];float4 _PointColor;struct appdata{float4 vertex : POSITION;float2 uv : TEXCOORD0;};struct v2f{float2 uv : TEXCOORD0;fixed3 worldPos : TEXCOORD1;UNITY_FOG_COORDS(1)float4 vertex : SV_POSITION;};sampler2D _MainTex;float4 _MainTex_ST;half calculateIntensity(fixed3 worldPos,float4 inputPos,float radius,float intensity){half dis = distance(worldPos,inputPos.xyz);//计算输入点和每个顶点的距离half ratio = 1-saturate(dis/radius);//saturate 返0~1,计算强度百分比,距离目标点越远影响强度越小half heat = intensity*ratio;return heat;}v2f vert (appdata v){v2f o;o.vertex = UnityObjectToClipPos(v.vertex);o.uv = TRANSFORM_TEX(v.uv, _MainTex);UNITY_TRANSFER_FOG(o,o.vertex);float3 worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;//获取每一个顶点信息o.worldPos = worldPos;return o;}fixed4 frag (v2f i) : SV_Target{// sample the texturefixed4 col = tex2D(_MainTex, i.uv);// apply fogUNITY_APPLY_FOG(i.fogCoord, col);half value = 0;for( int j = 0 ; j < _InputPointCount;j++ ){//计算顶点世界坐标和传入的世界坐标点的影响范围返回值value+=calculateIntensity(i.worldPos,_InputPoints[j],_Radius,_Intensity);value=clamp(value,0,1);}float rate =1- step(0.01,value);col= (col*rate)+ (_PointColor*float4(value,value,value,_PointColor.w));return col;}ENDCG}}
}

效果图
在这里插入图片描述


http://www.ppmy.cn/embedded/103322.html

相关文章

P1509 找啊找啊找GF

人才出题人&#xff0c;挺会整活的。。。 一眼二维费用背包。 题目要求不单单是求最小时间&#xff0c;还要保证MM数量最多。 需要开两个数组f,dp,分别代表给定人品&#xff0c;钱能get到的最多MM数量&#xff0c;确保最多数量下的最少时间花费。 将人品&#xff0c;钱作为…

应用案例|亚克力板CNC加工自动化上下料

在现代化制造领域&#xff0c;自动化和智能化已成为提升生产效率、降低成本的关键。针对当前CNC加工过程中亚克力板上下料环节的人工操作问题&#xff0c;富唯智能提出了基于AMR&#xff08;自主移动机器人&#xff09;的复合机器人解决方案。传统的人工取料、放置以及加工完成…

disk manager操作教程 如何使用Disk Manager组件 Mac如何打开ntfs格式文件

macOS系统有一个特别明显的弱点&#xff0c;即不能对NTFS格式磁盘写入数据。想要适合Mac系统使用来回转换磁盘格式又十分麻烦&#xff0c;这该怎么办呢&#xff1f;Tuxera ntfs for mac作为一款Mac完全读写软件&#xff0c;大家在安装该软件后&#xff0c;能充分使用它的磁盘管…

fpga图像处理实战-图像浮雕

图像浮雕 图像浮雕(Embossing)是一种图像处理技术,通过模仿浮雕效果,将二维图像转换为具有三维质感的图像。浮雕效果通常会使图像看起来像是雕刻在某种材质上的图案,具有突出的边缘和阴影,增强了图像的立体感。 图像浮雕特效实现的基本原理 实现图像浮雕特效的算法很多,…

union 的正确食用方法

0.前情提要 &#xff08;很久&#xff09;之前上编译原理时&#xff0c;一次实验课需要补充完善一个用 c 写的词法分析器&#xff1b;而这个分析器在定义语法树结点时使用了 union 存储语言中不同表达式的类型标签或值本身。因为当时刚好学完了 cpp&#xff0c;拿着锤子看啥都…

第4章-08-用Python Requests库模拟浏览器访问接口

🏆作者简介,黑夜开发者,CSDN领军人物,全栈领域优质创作者✌,CSDN博客专家,阿里云社区专家博主,2023年CSDN全站百大博主。 🏆数年电商行业从业经验,历任核心研发工程师,项目技术负责人。 🏆本文已收录于专栏:Web爬虫入门与实战精讲,后续完整更新内容如下。 文章…

Docker私有镜像仓库Harbor安装并推拉镜像

1.环境信息 前置要求&#xff1a; linux&#xff1a;Oracle Linux Server release 7.9 docker&#xff1a;26.1.4 安装&#xff1a; docker-compose: Docker Compose version v2.29.2 harbor&#xff1a;v2.11.1 2.下载安装说明 docker-compose下载&#xff1a; https://githu…

【React】useEffect的使用场景与作用

useEffect 是 React 中的一个 Hook&#xff0c;用于在函数组件中执行副作用。副作用是指那些与渲染过程无关的操作&#xff0c;比如数据获取、订阅事件、手动操作 DOM、日志记录等。useEffect 允许你在函数组件中引入这些副作用&#xff0c;而不必依赖于类组件的生命周期方法&a…