Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by BulchyC · Jun 05, 2018 at 01:46 PM · shadershadersshader programmingshader writing

Converted ShaderToy shader showing up black

I'm attempting to convert this shader from ShaderToy into Unity: https://www.shadertoy.com/view/XsXSz2

I've reached a point where it doesn't throw any errors, but when placed on a quad it just appears solid black. I'm at a loss as to where to go from here. Are there any obvious mistakes I'm making?


Here is my code:
Shader "Custom/Warp Center" {

     Properties {
         
     }
 
     SubShader
     {
         Tags
         {
             "Queue" = "Transparent"
             "PreviewType" = "Plane"
         }
         Pass
         {
             Blend SrcAlpha OneMinusSrcAlpha
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 float4 vertex : SV_POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             v2f vert(appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = v.uv;
                 return o;
             }
 
             float PI = 3.14159265359;
 
             float RATIO_SPIN = .1;
             float RATIO_SPREAD = 23.3;
 
             float REPETITION_POWER = 35.0;
 
             float RIPPLE_POWER = 20.0;
             float RIPPLE_DISTANCE = 0.1;
 
 
             float getAngle(float2 uvCenter){
                 float angle = atan2(uvCenter.x, uvCenter.y)/(2.0*PI);
                 if(angle <0.0) angle +=1.0;
                 return angle;
             }
 
             float getDis(float2 v){
                 return sqrt(v.x* v.x + v.y*v.y);
             }
 
             float4 frag(v2f i) : SV_Target
             {
 
                 float2 uv = i.uv.xy / _ScreenParams.xy;
                 float2 uvCenter = uv - float2(0.5,0.5);
                 float angle = getAngle(uvCenter) + _Time.y*RATIO_SPIN;
                 float dis = getDis(uvCenter);
 
                 dis *= 1.0+ sin(angle*2.0*PI*RIPPLE_POWER) *RIPPLE_DISTANCE; // distorts distance to ripple effect
                 float color = sin(dis*PI*REPETITION_POWER -_Time.y*RATIO_SPREAD); //final color, use sin to create "repetition" pattern effect
 
                 float4 fragColor = float4(color * 0.1, color * 0.1, color, 1);
 
                 return fragColor;
             }
             ENDCG
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Sourav11 · Jul 06, 2018 at 10:54 AM

Hello @BulchyC

The reason of the shader is turned black is because the shader has some properties which need to assign to make it work.

You need to make a script which does this for you. Here is a C# script which i created :-

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [ExecuteInEditMode]
 public class ChronoTriggerCenter : MonoBehaviour {
 
     public Material material;
 
     // Use This For Initialization
     private void Start () {
 
         if (material == null) {
             material = new Material (Shader.Find ("ChronoTriggerCenter"));
         }
     }
 
     // Update Is Called Once Per Frame
     private void Update () {
 
         material.SetVector ("_MousePosition", new Vector4 (Input.mousePosition.x, Input.mousePosition.y, 0, 0));
     }
 
     private void OnRenderImage (RenderTexture source, RenderTexture destination) {
 
         if (material == null) {
             Graphics.Blit (source, destination);
             return;
         }
 
         material.SetFloat ("_RATIO_SPIN", 0.1f);
         material.SetFloat ("_RATIO_SPREAD", 23.3f);
         material.SetFloat ("_REPETITION_POWER", 35.5f);
         material.SetFloat ("_RIPPLE_POWER", 20.0f);
         material.SetFloat ("_RIPPLE_DISTANCE", 0.1f);
 
         Graphics.Blit (source, destination, material);
     }
 }

And here a shader script which goes with it :-

 // Original Shader By BlueBug.
 // Original Link Https://www.ShaderToy.Com/view/XsXSz2
 // Converted By K.S.
 
 Shader "ChronoTriggerCenter" {
 
     Properties {
     
         _MainTex ("Texture", 2D) = "white" {}
     }
 
     SubShader {
     
         Pass {
         
             CGPROGRAM
             
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             sampler2D _MainTex;
 
             uniform float _RATIO_SPIN;
             uniform float _RATIO_SPREAD;
             uniform float _REPETITION_POWER;
             uniform float _RIPPLE_POWER;
             uniform float _RIPPLE_DISTANCE;
 
             struct appData {
             
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD;
             };
 
             struct v2f {
             
                 float4 vertex : SV_POSITION;
                 float2 uv : TEXCOORD;
             };
 
             v2f vert (appData v) {
             
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = v.uv;
                 return o;
             }
 
             float getAngle (float2 uvCenter) {
                 
                 float angle = atan2(uvCenter.y, uvCenter.x) / (2.0 * UNITY_PI);
                 if (angle < 0.0) {
                     angle += 1.0;
                 }
                 return angle;
             }
 
             float getDistance (float2 v) {
             
                 return sqrt(v.x * v.x + v.y * v.y);
             }
 
             float4 frag (v2f i) : SV_Target {
 
                 float2 uvCenter = i.uv - float2(0.5, 0.5);
                 float angle = getAngle(uvCenter) + _Time.y * _RATIO_SPIN;
                 float distance = getDistance(uvCenter);
 
                 distance *= 1.0 + sin(angle * 2.0 * UNITY_PI * _RIPPLE_POWER) * _RIPPLE_DISTANCE;
                 float color = sin(distance * UNITY_PI * _REPETITION_POWER - _Time.y * _RATIO_SPREAD);
 
                 return float4(color * 0.1, color * 0.1, color, 1.0);
             }
 
             ENDCG
         }
     }
 }

I replace PI with UNITY_PI variable and cleaned up code a bit.

Hope this help.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BulchyC · Jul 06, 2018 at 01:22 PM 0
Share

Thank you so much for this. I'm new to shaders and this is a lot of help.

I'm afraid I'm messing this up somewhere still though: I have my editor script and my shader script in place, but a material is never generated, if I place one in manually then I just get the same black quad as before.

The c# script is definitely supposed to run in the editor and not be placed on a gameobject, right?

avatar image Sourav11 BulchyC · Jul 07, 2018 at 02:20 PM 0
Share

You do need to attach C# script to gameobject. [ExecuteInEdit$$anonymous$$ode] attribute simply execute any $$anonymous$$onoBehaviour callback even when not in playmode. it must not be mistaken with Custom Editors. https://docs.unity3d.com/ScriptReference/ExecuteInEdit$$anonymous$$ode.html Attach the C# script to your Camera.

avatar image BulchyC Sourav11 · Jul 09, 2018 at 08:23 AM 0
Share

Ah yep, it was me being dumb and putting the script in my Editor folder which was causing the issue. Thank you very much for helping, it works perfectly!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

197 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Operate the shader, rotate the material 90 degrees 0 Answers

How much operations does tex2d func take? 0 Answers

Reversed UV Light with 2D PointLight (shader graph) 0 Answers

How do I get the fragment position in world coordinates? 1 Answer

How to write a shader like this? [Screenshots] 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges