Hot answers tagged

35

coderanger is right about HLSL targeting DirectX, GLSL targeting OpenGL and CG being available with both interfaces. However there are other things to consider (learned on the OGRE forum) : CG will not allow you to use the latest features of GLSL (I'm not sure about HLSL). It's a middle ground so you'll not be able to fully exploit the GLSL features, only ...


14

I can only talk about CG vs HLSL because those are the 2 I have used so far. Cg is not the same as HLSL. In Cg, NVIDIA did an excellent job in creating a very clean shader syntax. Its very similar to HLSL. But, tie-together with D3D9/D3D11 (init code, shader compilation code) is much cleaner on HLSL than Cg. -1 Cg. Cg has a nasty bit of start up code ...


9

My very basic understanding is that HLSL is only for DirectX and GLSL is only for OpenGL. Cg is basically the same language as HLSL, but can be used with either DirectX or OpenGL (though via different runtime code).


8

Another crucial difference between HLSL and GLSL (I don't know CG so I can't speak for it) is that with HLSL Microsoft provide the shader compiler as part of the D3D runtime whereas with GLSL your hardware vendor provides it as part of their driver. This has advantages and disadvantages on both sides. With the GLSL method the vendor can tune the compiler ...


6

They are very different: smoothstep(x, y, z); basically does saturate((z-x)/(y-x)) and then smoothes the result to start and end smoothly. The result always lies between 0 and 1. lerp(x, y, z); returns z*y+(1-z)*x As long as x lies between 0 and 1 the result lies between x and y. What you want is saturate((z-x)/(y-x)) which gives you the same range as ...


6

If nothing else, you can manually calculate shadows using techniques similar to low-resolution ray tracing, then somehow put them on screen. I think this would be fairly easy, since unity supports hit detection with custom rays and all scene objects that have a collider, but of course this is highly inefficient. If you can't afford unity pro (not even the ...


6

Firstly you can do texture fetching inside conditional blocks in HLSL. tex2Dlod() and tex2Dgrad() will work fine inside one. It's just tex2D() that won't compile, and you can work round that by computing ddx() and ddy() outside the conditional and using tex2Dgrad(). To reliably stop the texture fetch (or any other block of code) being executed in HLSL, use ...


5

It appears to be a slightly misleading book. Offset does not change drawing order or even the 3D position of pixels. It simply adds some numbers to the Z value (the one that's going to the Z tests and Z-buffer) of a pixel (hence the term "offset"), making it appear above or below other triangles. This is generally used to prioritize visibility of triangles ...


4

I don't see any special shaders there, just some vertex colors that are generated with an ambient occlusion (or similar) calculation algorithm and perhaps some differently calculated vertex normals. The results remind me of a demo I've seen some time ago which simulated global illumination by "bending" vertex normals and generating occlusion values for ...


4

CG (as your tags indicate) supports the WPOS semantic in some profiles, for example this one. An input parameter bound to the WPOS semantic will get the window position (with the origin in the lower-left) of the fragment. Other languages, like GLSL, have similar predefined uniforms or inputs (gl_FragCoord for example), although the specifics of their value ...


4

Set the filterMode of your textures to FilterMode.Point. This will use nearest-neighbor interpolation, which will result in blocky rendering.


4

In my opinion the best way to generate a glow around an arbitrary object is by using the geometry shader to detect edges and extrude new faces, there are no doubt other ways that work as well or even better and you can google around for those, but I'll cover my way doing edges. Full disclosure: I don't know Unity or glsl so I can't give direct code but I'll ...


3

Why not do that with a surface shader? you could have Per Vertex coloring and on a same time keep multi-light shading (Which is "almost" default when using surface shaders) Surface shaders doc : Writing surface shaders Surface shader Examples Surface Shaders with DX11 Tessellation According to those docs over here, you should be able to create a basic ...


3

The answer is in the documentation, in "API Overview/Graphics Overview": Shader descriptions have the following restrictions in comparison with standard Cg language. Variables and functions with the same names as GLSL and Cg reserved words cannot be used. It turns that the emulator, the PSVita and Android devices all have very different ...


3

The error here is that you're trying to sample from a texture in a vertex shader the way you would in a fragment or pixel shader. Vertex texture fetches are a little bit special. They're only supported in Shader Model 3 and up, and they're unable to use the tex2d() function. That may sound odd, but tex2D() is really a shortcut that says "figure out the ...


2

The Cg toolkit supports its own version of effect files, CgFX. These support techniques, states, annotations... I don't have any experience with them personally, but they are supported by FX Composer, at least. CGeffect cgCreateEffectFromFile( CGcontext context, const char * filename, const ...


2

The reference manual is up to date in the nvidia site Cg 3.0 Reference Manual, which i believe is where you can get most of the information you want. It does contain very detailed data of everything you can do with Cg, I don't think there's something missing there, but it may be hard to read at first as it's just a reference for all the technical aspects of ...


2

To get the RGB values of your scene influenced by the minimum of several alpha values from your fog, the most straightforward way to use a render texture. This requires Unity Pro. [Edit: Unity 5 now allows the use of render textures in the free version as well. Yay! :D ] (Some alternatives are provided below if this isn't an option) Set up a second camera,...


2

One way of doing this is using min blending. With Unity this can be set up in ShaderLab using the BlendOp command. If your objects are all monochrome as seen here, you may simply be able to use min blending directly on the objects as they're rendered and avoid having an alpha channel at all. (White is transparent when using min blending.) Min blending ...


2

They are not equivalent. In the GLSL shader you use the same texture coordinate for your diffuse and normal map (gl_TexCoord[0]). In the CG shader you use separate ones (TEXCOORD0 and TEXCOORD1, which is presumably not set).


2

Hardware has not always supported floating-point texture formats. In fact, D3D has not always supported depth textures. When you see this sort of thing, it is usually because the application is trying to support an older API or older hardware. To store depth on these systems, they must be converted to fixed-point and packed into 4 channels of a traditional ...


2

You can control texture UV with external code. Define uniform float4 _Diffuse_ST; in your shader and then in your script call it every Update(): public float rollSpeed = 0.1f; void Update() { offset += Time.deltaTime * rollSpeed; renderer.material.SetTextureOffset("_Diffuse", new Vector2(offset, 0)); } Then your shader will look like this (it ...


1

It turns out it was the Mesh creation and the shader was working fine. In my above example vertices are shared between different tiles, below vertices are only shared between the 2 triangles that make each tile. void BuildMesh() { Vector3[] vertices = new Vector3[(int) mapSize.x * (int) mapSize.y * 4]; Vector3[] normals = new Vector3[...


1

If you're writing shaders, you should know a bit about the world, view, and projection matrices. I'm assuming you do, so if you don't, either read up on it or let me know. For every vertex, you can determine its position on the screen by creating a float4, with the first 3 parameters being the vertex position, and the last being 1. When you have this float4, ...


1

The shader was fine. But when I tried to use other shaders I realized what was wrong. It was the C++ code that was lacking lines (rrr!! This is the reason why I hate shaders! It's so complicated to make a link in your head between every line you write in one file and what must be written in the other for it to be recognized!). In the display function, I ...


1

I tried compiling it from the command-line as follows: cgc -profile glslf -entry FS_Main test.cg This gave the following error output: test.cg test.cg(18) : error C1066: invalid type in type constructor test.cg(18) : error C1010: expression left of ."rgb" is not a struct This immediately highlights the fact that you used texture2D on line 18 instead of ...


1

cgGetNamedParameter Example code: CGprogram cgprog; cgprog = cgCreateProgram(...); cgGLLoadProgram(cgprog); CGparameter modelviewproj, lightpos; modelviewproj = cgGetNamedParameter(cgprog, "modelviewproj"); lightpos = cgGetNamedParameter(cgprog, "lightpos"); Sample code paraphrased from the book Advanced Game Development with Programmable Graphics ...


1

I was thinking wrong. I do not more passes to receive every light in the scene. I took a shortcut and used a surface shader in the end. I post it here for reference Shader "Custom/Test01" { Properties { _MainTex ("Texture (RGBA)", 2D) = "white" {} } SubShader { Tags { "Queue" = "Transparent" "...


1

Um ... well ... it's a pretty basic example of triplanar texturing but it works ... Shader "My Shaders/TriPlanar" { Properties { Tex1 ("Texture 1", 2D) = "white" {} Tex2 ("Texture 2", 2D) = "white" {} Tex3 ("Texture 3", 2D) = "white" {} } SubShader { Pass { CGPROGRAM #pragma target 3....


1

Yes, the position sent to the vertex shader is generally the local coordinates of the object's vertex. Your multiplication by worldViewProjMatrix in the vertex shader is what handles the transformation from world space to camera space to projection space, all in one shot. The GPU will interpolate any out variables from your vertex shader across the face of ...



Only top voted, non community-wiki answers of a minimum length are eligible