Quantcast
Channel: Questions in topic: "shader programming"
Viewing all articles
Browse latest Browse all 169

Why is shader/canvas background black?,When I run my shader code in unity the canvas has a black background?

$
0
0
I am sorry if this doesn't make sense but i am new in Unity/C#. I have created this with the help of a tutorial but whenever I hit play and the "Game" starts, my canvas has a black background. I am not sure if this is due to the shader or the canvas but any help would be appreciated. I am looking into bringing this canvas into another project and I don't want it to have a background since it will hide everything else. Also, another question, could i possibly multiply this object in realtime? Thanks! Shader "Custom/Mandelbulb" { Properties { baseColour ("Base Colour", Color) = (1,1,1,1) } SubShader { Pass{ Blend One OneMinusSrcAlpha, One One BlendOp Add CGPROGRAM #include "UnityCG.cginc" #pragma target 4.0 #pragma vertex vert_img #pragma fragment frag #define black float4(0,0,0,0) #define green float4(1,1,1,1) #define white float4(1,1,1,1) #define blue float4(0,0,0.7,1) #define red float4(1,0,0,1) #define orange float4(1, 0.64, 0,1) #define yellow float4(1,1,0,1) #define seethrough float4(0,0,0,0) sampler2D _MainTex; float3 _LightDir; float _Exponent; int _NumIterations; int _NumRayMarchSteps; float _Fov; float4 baseColour; inline float2 complex_mult(float2 c1, float2 c2) { return float2(c1.x * c2.x - c1.y * c2.y, c1.x * c2.y + c1.y * c2.x); } float2 recurse_complex_mult(float2 cin, int n) { int ii; float2 cout = cin; for(ii = 0; ii < n; ii++) { cout = complex_mult(cout, cin); } return cout; } inline void cartesian_to_polar(float3 p, out float r, out float theta, out float psi) { r = length(p); float r1 = p.x*p.x + p.y*p.y; theta = atan(p.z / r1); psi = atan(p.y / p.x); } inline void polar_to_cartesian(float r, float theta, float psi, out float3 p) { p.x = r * cos(theta) * cos(psi); p.y = r * cos(theta) * sin(psi); p.z = r * sin(theta); } float norm_to_unorm(float i) { return (i + 1) * 0.5; } float de_sphere_surface(float3 p, float3 c, float r) { return abs(length(p - c) - r); } float de_sphere_instances(float3 p) { p.xy = fmod( (p.xy), 1.0 ) - 0.5; return length(p) - 0.2; } // distance estimator for the mandelbulb float de_mandelbulb(float3 c) { const float delta = 2; bool converges = true; // unused float divergenceIter = 0; // unused float3 p = c; float dr = 2.0, r = 1.0; int ii; for(ii = 0; ii < _NumIterations; ii++) { float theta, psi; cartesian_to_polar(p, r, theta, psi); dr = _Exponent * pow(r, _Exponent - 1) *dr + 1.0; r = pow(r,_Exponent); theta *= _Exponent; psi *= _Exponent; // convert to cartesian coordinates polar_to_cartesian(r, theta, psi, p); // add c p += c; // check for divergence if (length(p) > delta) { divergenceIter = ii; converges = false; break; } } return log(r) * r / dr; // Greens formula } float de_scene(float3 p) { float ds2 = de_mandelbulb(p); return ds2; } float4 raymarch(float3 rayo, float3 rayd) { const float minimumDistance = 0.0001; float3 p = rayo; bool hit = false; float distanceStepped = 0.0; int steps; for(steps = 0; steps < _NumRayMarchSteps; steps++) { float d = de_scene(p); distanceStepped += d; if (d < minimumDistance) { hit = true; break; } p += d * rayd; } float greyscale = 1 - (steps/(float)_NumRayMarchSteps); return float4(greyscale, greyscale, greyscale, 1); } float3 get_eye_ray_through_pixel(float2 svpos) { float2 pixelNormPos = -1 + (2 * svpos.xy) / _ScreenParams; // account for aspect ratio pixelNormPos.x *= _ScreenParams.x / _ScreenParams.y; pixelNormPos.y *= -1; float zNearNorm = rcp( tan(_Fov/2) ); // tan(fov/2) = 1 / zn_norm return normalize(float3(pixelNormPos, zNearNorm)); } float4 frag(v2f_img i) : COLOR { float3 csRay = get_eye_ray_through_pixel(i.pos); float3 wsRay = mul(unity_CameraToWorld, float4(csRay, 0)); return raymarch(_WorldSpaceCameraPos, wsRay) *baseColour; } ENDCG } } FallBack "Diffuse" } ,Sorry if this doesn't make sense, I am new to Unity/C#, after following a tutorial I have created a shader and it runs great. However, I would like it to not have a black background is this possible? The reason I want this is because I want to create a package of the project and include it in another one but when I do that and I run the two scenes together, the shader scene has a black background and hides everything else. Also, not as important, but could i possible multiply it in realtime? Below is my shader code if any help? Shader "Custom/Mandelbulb" { Properties { baseColour ("Base Colour", Color) = (1,1,1,1) } SubShader { Pass{ Blend One OneMinusSrcAlpha, One One BlendOp Add CGPROGRAM #include "UnityCG.cginc" #pragma target 4.0 #pragma vertex vert_img #pragma fragment frag #define black float4(0,0,0,0) #define green float4(1,1,1,1) #define white float4(1,1,1,1) #define blue float4(0,0,0.7,1) #define red float4(1,0,0,1) #define orange float4(1, 0.64, 0,1) #define yellow float4(1,1,0,1) #define seethrough float4(0,0,0,0) sampler2D _MainTex; float3 _LightDir; float _Exponent; int _NumIterations; int _NumRayMarchSteps; float _Fov; float4 baseColour; inline float2 complex_mult(float2 c1, float2 c2) { return float2(c1.x * c2.x - c1.y * c2.y, c1.x * c2.y + c1.y * c2.x); } float2 recurse_complex_mult(float2 cin, int n) { int ii; float2 cout = cin; for(ii = 0; ii < n; ii++) { cout = complex_mult(cout, cin); } return cout; } inline void cartesian_to_polar(float3 p, out float r, out float theta, out float psi) { r = length(p); float r1 = p.x*p.x + p.y*p.y; theta = atan(p.z / r1); psi = atan(p.y / p.x); } inline void polar_to_cartesian(float r, float theta, float psi, out float3 p) { p.x = r * cos(theta) * cos(psi); p.y = r * cos(theta) * sin(psi); p.z = r * sin(theta); } float norm_to_unorm(float i) { return (i + 1) * 0.5; } float de_sphere_surface(float3 p, float3 c, float r) { return abs(length(p - c) - r); } float de_sphere_instances(float3 p) { p.xy = fmod( (p.xy), 1.0 ) - 0.5; return length(p) - 0.2; } // distance estimator for the mandelbulb float de_mandelbulb(float3 c) { const float delta = 2; bool converges = true; // unused float divergenceIter = 0; // unused float3 p = c; float dr = 2.0, r = 1.0; int ii; for(ii = 0; ii < _NumIterations; ii++) { float theta, psi; cartesian_to_polar(p, r, theta, psi); dr = _Exponent * pow(r, _Exponent - 1) *dr + 1.0; r = pow(r,_Exponent); theta *= _Exponent; psi *= _Exponent; // convert to cartesian coordinates polar_to_cartesian(r, theta, psi, p); // add c p += c; // check for divergence if (length(p) > delta) { divergenceIter = ii; converges = false; break; } } return log(r) * r / dr; // Greens formula } float de_scene(float3 p) { float ds2 = de_mandelbulb(p); return ds2; } float4 raymarch(float3 rayo, float3 rayd) { const float minimumDistance = 0.0001; float3 p = rayo; bool hit = false; float distanceStepped = 0.0; int steps; for(steps = 0; steps < _NumRayMarchSteps; steps++) { float d = de_scene(p); distanceStepped += d; if (d < minimumDistance) { hit = true; break; } p += d * rayd; } float greyscale = 1 - (steps/(float)_NumRayMarchSteps); return float4(greyscale, greyscale, greyscale, 1); } float3 get_eye_ray_through_pixel(float2 svpos) { float2 pixelNormPos = -1 + (2 * svpos.xy) / _ScreenParams; // account for aspect ratio pixelNormPos.x *= _ScreenParams.x / _ScreenParams.y; pixelNormPos.y *= -1; float zNearNorm = rcp( tan(_Fov/2) ); // tan(fov/2) = 1 / zn_norm return normalize(float3(pixelNormPos, zNearNorm)); } float4 frag(v2f_img i) : COLOR { float3 csRay = get_eye_ray_through_pixel(i.pos); float3 wsRay = mul(unity_CameraToWorld, float4(csRay, 0)); return raymarch(_WorldSpaceCameraPos, wsRay) *baseColour; } ENDCG } } FallBack "Diffuse" }

Viewing all articles
Browse latest Browse all 169

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>