I am working on a game and this is what happens in the game scene.
I verified the shader on each piece dozens of times, but it is ok. In the picture you can see the debug messages with the color, emission and albedo on the materials, and they are ok.
Any ideas what the problem could be? Any suggestion is ok because the release day is coming.
EDIT:
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_Cube ("Cubemap", CUBE) = "" {}
_Emission ("Emission", Range (0.0, 1.0)) = 0.5
_Albedo("Albedo", Range (0.01, 1)) = 0.9
}
SubShader {
Tags {"RenderType" = "Opaque" }
//Blend Off
Cull Off
CGPROGRAM
#pragma surface surf Lambert approxview noforwardadd
fixed4 _Color;
struct Input
{
half3 worldRefl;
};
samplerCUBE _Cube;
float _Emission;
float _Albedo;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = _Color.rgb * _Albedo;
o.Emission = texCUBE (_Cube, IN.worldRefl).rgb * _Emission;
}
ENDCG
}
Fallback "Diffuse"
}
Try making "main color" brighter. That could potentially solve it.
Related
I'm trying to get a value property from a shader attached in an object at runtime, but it's never changes in material editor. Maybe I misunderstood anything of how shaders works?
I read about the GetFloat, GetColor, etc but don't figure out yet how it properly works to get an information of a shader in Update(). The real objective here is catch a specific value from shader (in realtime) and do something in C# script, if it's possible.
C# example:
public Color colorInfo;
Awake()
{
rend = GetComponent<Renderer>();// Get renderer
rend.material.shader = Shader.Find("Unlit/shadowCreatures");//shader
}
Update()// I want the info in a realtime
{
//get current float state of the shader
colorInfo = rend.material.GetColor("_Color");
//If I setup a white color in shader properties, the color in material editor is always white
}
Shader:
Shader "Unlit/shadowCreatures"
{
Properties {
_Color ("Color", Color) = (1,1,1,1)
[PerRendererData]_MainTex ("Sprite Texture", 2D) = "white" {}
_Cutoff("Shadow alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags
{
"Queue"="Geometry"
"RenderType"="TransparentCutout"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
LOD 200
Cull Off
CGPROGRAM
// Lambert lighting model, and enable shadows on all light types
#pragma surface surf Lambert addshadow fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
fixed4 _Color;
fixed _Cutoff;
struct Input
{
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
clip(o.Alpha - _Cutoff);
_Color = (0,0,0,0); //trying turn to black just for a test, and nothing happens
}
ENDCG
}
FallBack "Diffuse"
}
Thank you for your time
I think I figured out what you're trying to do and
That's not how things work, sorry
Piecing together your comments and question, this is what I think you're trying to fiddle with:
void surf (Input IN, inout SurfaceOutput o) {
_Color = (0,0,0,0); //trying turn to black just for a test, and nothing happens
}
That doesn't do what you think it does. That line sets this _Color:
#pragma target 3.0
sampler2D _MainTex;
fixed4 _Color;
fixed _Cutoff; //here
Not this one:
Properties {
_Color ("Color", Color) = (1,1,1,1) //here
[PerRendererData]_MainTex ("Sprite Texture", 2D) = "white" {}
_Cutoff("Shadow alpha cutoff", Range(0,1)) = 0.5
}
That second one is what is shown in the inspector panel, and its linkage with the CGPROGRAM block is effectively one-way because frag surf and geom are all called multiple times, in parallel, and rely on receiving the same data in, so the Properties value is read into the CGPROGRAM and the CGPROGRAM's values are discarded when it is done.
I don't think there's any way you can make the shader CGPROGRAM do anything that you can read from C# (because that code runs hundreds of times per frame, how would you know which one to read from?)
I know you can get at the properties (including changing a property for one instance or for all instances), but not the underlying CGPROGRAM data. The only way I can even think of getting around this would be to render to a texture and then read the texture data, and that would be slow (and again, you get into "which pixel has the value you want"?)
Yesterday I had implemented Fog of war for my RTS game. All was working fine last night before I went to bed, I checked it scene by scene and ran through loads of tests to make sure it was working and all was good.
But today, when I try and run the game, it loads the main menu, but when it tries to load the first level, unity crashes and doesn't give me a reason.
I tried going through code and game objects until I found that if I removed the custom shader attached to the terrain I was using to create fog over the map, it would stop crashing unity and continue running the game, however I reattached it and it ran fine again with the fog of war working again, but as soon as i closed unity and opened it again, the same problem occurred with crashing loading the first level.
I am unsure if it is the shader or the code for it thats causing the problem.
I have little experience with shaders but if anyone can have a look at the code for it and tell me if anything looks wrong with it I would be very greatful.
Shader "Fog Of War/Terrain" {
Properties {
_FOWTex ("Detail", 2D) = "gray" {}
// Splat Map Control Texture
[HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {}
[HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {}
[HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {}
[HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {}
[HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {}
// used in fallback on old cards & base map
// [HideInInspector] _MainTex ("BaseMap (RGB)", 2D) = "white" {}
// [HideInInspector] _Color ("Main Color", Color) = (1,1,1,1)
}
SubShader {
Tags { "RenderType"="Opaque"
"SplatCount" = "4"
}
CGPROGRAM
#pragma surface surf Lambert nolightmap
struct Input {
float2 uv_FOWTex;
float2 uv_Control : TEXCOORD0;
float2 uv_Splat0 : TEXCOORD1;
float2 uv_Splat1 : TEXCOORD2;
float2 uv_Splat2 : TEXCOORD3;
float2 uv_Splat3 : TEXCOORD4;
};
sampler2D _Control, _FOWTex;
sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
void surf (Input IN, inout SurfaceOutput o) {
fixed4 splat_control = tex2D (_Control, IN.uv_Control);
fixed3 col;
col = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3).rgb;
o.Albedo = col;
fixed3 c = tex2D (_FOWTex, IN.uv_FOWTex).aaa;
c = 1-c;
o.Albedo *= c;
}
ENDCG
}
FallBack off
}
It is very unlikely that a shader is causing the application to crash. If there is an issue with a shader, then Unity will fail to compile the shader and anything which used it will be rendered solid bright pink.
I don't see anything terribly wrong in the shader, however this line:
float2 uv_FOWTex;
in your input struct I think is probably unnecessary.
To debug it, you could strip down your Fog of War shader to as basic and simple as you can get it. Then see if it keeps crashing. It it doesn't, then gradually add back in pieces of the shader until it does.
But when it comes to crashes usually it's from you doing something bad with calls to unmanaged code.
I'm working on my volume rendering application (C# + OpenTK).
The volume is being rendered using raycasting, i found a lot of inspiration on this site:
http://graphicsrunner.blogspot.sk/2009/01/volume-rendering-101.html, and even though my applications works with OpenGL, the main idea of using 3D texture and other stuff is the same.
Application works fine, but after I "flow into the volume" (means inside the bounding box), everything dissapears, and I want to prevent this. So is there some easy way to do this? --> I will be able to flow through the volume or move in the volume.
Here is the code of fragment shader:
#version 330
in vec3 EntryPoint;
in vec4 ExitPointCoord;
uniform sampler2D ExitPoints;
uniform sampler3D VolumeTex;
uniform sampler1D TransferFunc;
uniform float StepSize;
uniform float AlphaReduce;
uniform vec2 ScreenSize;
layout (location = 0) out vec4 FragColor;
void main()
{
//gl_FragCoord --> http://www.txutxi.com/?p=182
vec3 exitPoint = texture(ExitPoints, gl_FragCoord.st/ScreenSize).xyz;
//background need no raycasting
if (EntryPoint == exitPoint)
discard;
vec3 rayDirection = normalize(exitPoint - EntryPoint);
vec4 currentPosition = vec4(EntryPoint, 0.0f);
vec4 colorSum = vec4(.0f,.0f,.0f,.0f);
vec4 color = vec4(0.0f,0.0f,0.0f,0.0f);
vec4 value = vec4(0.0f);
vec3 Step = rayDirection * StepSize;
float stepLength= length(Step);
float LengthSum = 0.0f;
float Length = length(exitPoint - EntryPoint);
for(int i=0; i < 16000; i++)
{
currentPosition.w = 0.0f;
value = texture(VolumeTex, currentPosition.xyz);
color = texture(TransferFunc, value.a);
//reduce the alpha to have a more transparent result
color.a *= AlphaReduce;
//Front to back blending
color.rgb *= color.a;
colorSum = (1.0f - colorSum.a) * color + colorSum;
//accumulate length
LengthSum += stepLength;
//break from the loop when alpha gets high enough
if(colorSum.a >= .95f)
break;
//advance the current position
currentPosition.xyz += Step;
//break if the ray is outside of the bounding box
if(LengthSum >= Length)
break;
}
FragColor = colorSum;
}
The code below is based on https://github.com/toolchainX/Volume_Rendering_Using_GLSL
Display() function:
public void Display()
{
// the color of the vertex in the back face is also the location
// of the vertex
// save the back face to the user defined framebuffer bound
// with a 2D texture named `g_bfTexObj`
// draw the front face of the box
// in the rendering process, i.e. the ray marching process
// loading the volume `g_volTexObj` as well as the `g_bfTexObj`
// after vertex shader processing we got the color as well as the location of
// the vertex (in the object coordinates, before transformation).
// and the vertex assemblied into primitives before entering
// fragment shader processing stage.
// in fragment shader processing stage. we got `g_bfTexObj`
// (correspond to 'VolumeTex' in glsl)and `g_volTexObj`(correspond to 'ExitPoints')
// as well as the location of primitives.
// draw the back face of the box
GL.Enable(EnableCap.DepthTest);
//"vykreslim" front || back face objemu do framebuffru --> teda do 2D textury s ID bfTexID
//(pomocou backface.frag &.vert)
GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferID);
GL.Viewport(0, 0, width, height);
LinkShader(spMain.GetProgramHandle(), bfVertShader.GetShaderHandle(), bfFragShader.GetShaderHandle());
spMain.UseProgram();
//cull front face
Render(CullFaceMode.Front);
spMain.UseProgram(0);
//klasicky framebuffer --> "obrazovka"
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
GL.Viewport(0, 0, width, height);
LinkShader(spMain.GetProgramHandle(), rcVertShader.GetShaderHandle(), rcFragShader.GetShaderHandle());
spMain.UseProgram();
SetUniforms();
Render(CullFaceMode.Back);
spMain.UseProgram(0);
GL.Disable(EnableCap.DepthTest);
}
private void DrawBox(CullFaceMode mode)
{
// --> Face culling allows non-visible triangles of closed surfaces to be culled before expensive Rasterization and Fragment Shader operations.
GL.Enable(EnableCap.CullFace);
GL.CullFace(mode);
GL.BindVertexArray(VAO);
GL.DrawElements(PrimitiveType.Triangles, 36, DrawElementsType.UnsignedInt, 0);
GL.BindVertexArray(0);
GL.Disable(EnableCap.CullFace);
spMain.UseProgram(0);//zapnuty bol v Render() ktora DrawBox zavolala
}
private void Render(CullFaceMode mode)
{
GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
spMain.UseProgram();
spMain.SetUniform("modelViewMatrix", Current);
spMain.SetUniform("projectionMatrix", projectionMatrix);
DrawBox(mode);
}
The problem is (I think) that as I'm moving towards the volume (I don't move the camera, just scaling the volume), if the scale factor > 2.7something, I'm in the volume, it means "after the plane on which is the final picture being rendered", so a can't see anything.
The solution (maybe) that I can think of, is something like that:
If I reach the scale factor = 2.7something:
1.) -> don't scale the volume
2.) -> somehow told to fragment shader to move EntryPoint towards the
RayDirection for some length (probably based on the scale factor).
Now, I tried this "method" and it seems that it can work:
vec3 entryPoint = EntryPoint + some_value * rayDirection;
The some_value have to be clamped between [0,1[ interval (or [0,1]?)
, but maybe it doesn't matter thank's to that:
if (EntryPoint == exitPoint)
discard;
So now, maybe (if my solution isn't so bad), I can change my answer to this:
How to compute the some_value (based on scale factor which I send to fragment shader)?
if(scale_factor < 2.7something)
work like before;
else
{
compute some_value; //(I need help with this part)
change entry point;
work like before;
}
(I'm not native english speeker, so If there are some big mistakes in the text and you don't understand something, just let me know and I'll try to fix these bugs)
Thank's.
I solved my problem. It doesn't make "being surrounded by the volume" illusion, but now, I can flow through the volume and nothing disappears.
This is the code of my solution added to fragment shader:
vec3 entryPoint = vec3(0.0f);
if(scaleCoeff >= 2.7f)
{
float tmp = min((scaleCoeff - 2.7f) * 0.1f, 1.0f);
entryPoint = EntryPoint + tmp * (exitPoint - EntryPoint);
}
else
{
entryPoint = EntryPoint;
}
//
But if you know or can think about better solution that makes the "being surrounded by the volume" effect, I'll be glad if you let me know.
Thank you.
If understand correctly, I think you should use Plane Clipping to go through the volume. (I could give you a simple example based on your code if you attach this solution. Translate the whole C++ project to C# is too time-consuming.)
I'm creating simple google cardboard game in unity and i want to add score count. I know how to do it normally, but in VR it doesn't work for me. I tried to just put Text and display score in it, and it looked good(not exactly because it was displayed only for one eye) but after I tested it on my phone text was in completely different place. Do you know what's the proper way to do it?
Create a World Space canvas and have that canvas attached to the camera. Thats how I get my UI in VR games.
You're going to need a custom shader for your World Space UI objects that has it's Render Order to Overlay and has ZTest turned off.
This is a copy of the Default UI shader with the necessary changes. Should do the trick. Just make a material with this shader, and apply it to everything you want drawn over the top of geometry in your WorldSpace UI.
Shader "UI/Default_OverlayNoZTest"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
}
SubShader
{
Tags
{
"Queue"="Overlay"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest Off
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
#ifdef UNITY_HALF_TEXEL_OFFSET
OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
#endif
OUT.color = IN.color * _Color;
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
half4 color = tex2D(_MainTex, IN.texcoord) * IN.color;
clip (color.a - 0.01);
return color;
}
ENDCG
}
}
}
credit goes to http://answers.unity3d.com/questions/878667/world-space-canvas-on-top-of-everything.html
I'd say; setup a RenderTexture and make your/the UI the only thing drawn onto it, then stick the RenderTexture onto something else in your world
I've been experimenting a fog of war. I have been following this tutorial apparently this is unity 4 and I'm using unity 5, I'm currently getting this error:
Surface shader vertex function 'vert' not found
I read on the comment section on this youtube video, I followed them, but it gives me the error. Tried the original version(The one in the video) but it makes my plane a always underneath even though it's y axis is zero, and the main map's y axis is -10.
btw here is my code for my shader:
Shader "Custom/FogOWarShader" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Transparent" "LightMode"="ForwardBase" }
Blend SrcAlpha OneMinusSrcAlpha
Lighting Off
LOD 200
CGPROGRAM
//#pragma surface surf NoLighting Lambert alpha:blend --the one that makes the map always on top
#pragma surface surf Lambert vertex:vert alpha:blend
fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, float aten){
fixed4 color;
color.rgb = s.Albedo;
color.a = s.Alpha;
return color;
}
fixed4 _Color;
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 baseColor = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = _Color.rgb * baseColor.b;
o.Alpha = _Color.a - baseColor.g;
}
ENDCG
}
FallBack "Diffuse"
}
You have a line in that shader which says:
#pragma surface surf Lambert vertex:vert alpha:blend
So you say you have a vertex shader called vert, but then there is no such function in the rest of your code. That is what it's complaining about.
Having taken a look at the actual shader, all you need to do it set up the blending. You can do that by modifying the pragma that is in the original shader to
#pragma surface surf NoLighting noambient alpha:blend
That should do the trick.