我需要帮助循环回声定位着色器

问题描述 投票:0回答:0

我目前正在做一个重要的项目,我想创建一个回声位置着色器,让波浪在整个环境中波动。我已经设法从不同的来源创建了这样的着色器,但我想在每个动作中循环着色器波,而不是取消中间波并从一开始就重新投射波。

EchoLocation Preview

这里有 2 个脚本,第一个是用于 Waves 的 ShaderScript

Shader "EchoLocationShader"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _EchoOrigin("Echo Origin", Vector) = (0,0,0,0)
        _EchoDistance("Echo Distance", Float) = 0
        _EchoWidth("Echo Width", Float) = 0.1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
            float3 worldPos;
        };

        fixed4 _Color;
        fixed4 _EchoOrigin;
        float _EchoDistance;
        float _EchoWidth;
        //float numbers[] = {  };


        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            float distance = length(IN.worldPos.xyz - _EchoOrigin.xyz) - _EchoDistance * _EchoOrigin.w;
            float halfWidth = _EchoWidth * 0.5;
            float lowerDistance = distance - halfWidth;
            float upperDistance = distance + halfWidth;

            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            //fixed4 c = fixed4(pow(1 - (abs(distance) / halfWidth), 8), 0, 0, 1);
            float ringStrength = pow(1 - (abs(distance) / halfWidth), 8) * (lowerDistance < 0 && upperDistance > 0);
            o.Albedo = ringStrength * c.rgb * (1 - _EchoOrigin.w);
            o.Alpha = c.a;

            
        }
        ENDCG
    }
    FallBack "Diffuse"
}

这是用来随着玩家的每一步产生波浪的(出于测试目的,我还绑定了每次点击都会产生波浪

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class EchoSpawner : MonoBehaviour
{
    public EchoState state;
    public int mouseClick;

    // define an array/list of click points/raycast results
    // a time limit to despawn these clicks

    private List<EchoData> data = new List<EchoData>();

    private void Update()
    {
        data.ForEach(d => d.Decay(Time.deltaTime));

        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("Click");
            mouseClick++;
            EchoSpawn();

            Vector3[] activePoints = data
                .Where(d => d.IsAlive) // find points that are still alive
                .Select(d => d.origin) // get only the origin points
                .ToArray(); // convert to array
            
            // state.SetEchoOrigins(activePoints);
        }
    }
    
    void EchoSpawn()
    {
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            data.Add(new EchoData(1, hit.point));
            state.EchoOrigin = hit.point;
            
            state.SetEchoOrigins(data
                    .Where(d => d.IsAlive) // find points that are still alive
                    .Select(d => d.origin) // get only the origin points
                    .Take(10) // Select up to 10
                    .ToArray() // convert to array
                );
        }
    }
}
c# unity3d shader echo ripple
© www.soinside.com 2019 - 2024. All rights reserved.