【Shader】ShaderToy搬运过程

这篇博客探讨了ShaderToy中使用GLSL与UnityLab中使用HLSL之间的差异,主要关注函数和内置变量的不同。文章提供了GLSL到HLSL的转换示例,并给出了一段Unity Shader模板,帮助理解如何在Unity中实现类似ShaderToy的效果。同时,还附带了一个C#脚本,用于更新iMouse参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ShaderToy使用GLSL代码,UnityLab使用HLSL和CG代码主要在于函数和内置变量的不同。

可参考:HLSL内置函数,及HLSL与GLSL的对应函数_eloudy的专栏-CSDN博客_glsl hlsl

Shadertoy搬运-基本方法_suixinger_lmh的随笔-CSDN博客  

Shader模板


// iChannelResolution[i]系列shadertoy参数需改为iChannelResolutioni形式表达,例如iChannelResolution[0]改为iChannelResolution0
Shader "ShaderToyTemplate"{
	Properties{
		iMouse("Mouse Pos", Vector) = (100, 100, 0, 0)
		iChannel0("iChannel0", 2D) = "white" {}
		iChannelResolution0("iChannelResolution0", Vector) = (100, 100, 0, 0)
	}

		CGINCLUDE
		#include "UnityCG.cginc"   
		#pragma target 3.0      

		#define vec2 float2
		#define vec3 float3
		#define vec4 float4
		#define mat2 float2x2
		#define mat3 float3x3
		#define mat4 float4x4
		#define iTime _Time.y//		
		#define iGlobalTime _Time.y
		#define mod fmod
		#define mix lerp
		#define fract frac
        #define atan atan2		
		#define texture tex2D//
		#define texture2D tex2D
		#define iResolution _ScreenParams
		#define gl_FragCoord ((_iParam.scrPos.xy/_iParam.scrPos.w) * _ScreenParams.xy)

		#define PI2 6.28318530718
		#define pi 3.14159265358979
		#define halfpi (pi * 0.5)
		#define oneoverpi (1.0 / pi)

		fixed4 iMouse;
		sampler2D iChannel0;
		fixed4 iChannelResolution0;

		struct v2f {
			float4 pos : SV_POSITION;
			float4 scrPos : TEXCOORD0;
		};

		v2f vert(appdata_base v) {
			v2f o;
			o.pos = UnityObjectToClipPos(v.vertex);
			o.scrPos = ComputeScreenPos(o.pos);
			return o;
		}

		vec4 main(vec2 fragCoord);

		fixed4 frag(v2f _iParam) : COLOR0{
			vec2 fragCoord = gl_FragCoord;
			return main(gl_FragCoord);
		}

		vec4 main(vec2 fragCoord) {
			return vec4(1, 1, 1, 1);
		}

		ENDCG

		SubShader{
			Pass {
				CGPROGRAM

				#pragma vertex vert    
				#pragma fragment frag    
				#pragma fragmentoption ARB_precision_hint_fastest     

				ENDCG
			}
		}
		FallBack Off
}

【可选】C#模板(iMouse参数的设置)挂在材质所在物体身上

using UnityEngine;
using System.Collections;

public class ShaderToyHelper : MonoBehaviour
{

    private Material _material = null;

    private bool _isDragging = false;

    // Use this for initialization
    void Start()
    {
        Renderer render = GetComponent<Renderer>();
        if (render != null)
        {
            _material = render.material;
        }

        _isDragging = false;
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 mousePosition = Vector3.zero;
        if (_isDragging)
        {
            mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f);
        }
        else
        {
            mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.0f);
        }

        if (_material != null)
        {
            _material.SetVector("iMouse", mousePosition);
        }
    }

    void OnMouseDown()
    {
        _isDragging = true;
    }

    void OnMouseUp()
    {
        _isDragging = false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值