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

练习贴

c#代码

csharp 复制代码
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代码

csharp 复制代码
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 OneMinusSrcAlpha
        LOD 100

        Pass
        {
            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 texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_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
        }
    }
}

效果图

相关推荐
一步一个foot-print3 小时前
【Unity】Light Probe 替代点光源给环境动态物体加光照
unity·游戏引擎
@LYZY4 小时前
Unity 中隐藏文件规则
unity·游戏引擎·游戏程序·vr
霜绛6 小时前
C#知识补充(二)——命名空间、泛型、委托和事件
开发语言·学习·unity·c#
Sator19 小时前
使用Unity ASE插件设置数值不会生效的问题
unity·游戏引擎
AA陈超9 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P07-08 点击移动
c++·游戏·ue5·游戏引擎·虚幻
程序猿追11 小时前
轻量级云原生体验:在OpenEuler 25.09上快速部署单节点K3s
人工智能·科技·机器学习·unity·游戏引擎
B0URNE11 小时前
【Unity基础详解】(7)Unity核心:动画系统
unity·游戏引擎
我的golang之路果然有问题12 小时前
mac M系列芯片 unity 安装会遇到的错误以及解决
经验分享·学习·macos·unity·游戏引擎
Hody912 天前
【XR开发系列】2025 年 XR 开发入门,我该选择 Unity 还是 Unreal Engine?
unity·xr·虚幻
DvLee10242 天前
UnityGLTF 材质创建与赋值流程
unity·材质