是否可以使用 Cg 修改片段(像素)着色器中的像素坐标?我确信此类功能在第二代/第三代着色器中可用,但我不知道确切的配置文件或如何做到这一点。
我正在学习 GLSL 并尝试实现一些照明和映射技巧。我正在使用 ShaderDesigner 工具。编码法线贴图后,我意识到我的模型照明看起来不真实。这是我的代码和一些图片。如果可能,请告诉我我的问题是什么。
顶点着色器
#define MAX_LIGHTS 1
struct LightProps
{
vec3 direction[MAX_LIGHTS];
};
attribute vec3 tangent;
attribute vec3 bitangent;
varying LightProps lights;
void main()
{
vec3 N = normalize(gl_NormalMatrix*gl_Normal);
vec3 T = normalize(gl_NormalMatrix*tangent);
vec3 B = normalize(gl_NormalMatrix*bitangent);
mat3 TBNMatrix = mat3(T,B,N);
vec4 vertex = gl_ModelViewMatrix*gl_Vertex;
for(int i = 0; i < MAX_LIGHTS; i++)
{
vec4 lightPos = gl_LightSource[i].position;
lights.direction[i] = vec3(lightPos.w > 0 ? lightPos-vertex : lightPos);
lights.direction[i] *= TBNMatrix;
}
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
} …Run Code Online (Sandbox Code Playgroud) 我有一个场景,其中的对象使用兰伯特材质相交,就像在这个jsFiddle中一样。
现在我需要/想要将该平面的材质切换为着色器材质,并且该平面变成背景物体,就像这里一样。
问题是,我可以在对象中使用不同的材质并仍然保留相交效果吗?这是 Three.js 的限制还是着色器的工作原理?或者我在渲染器/材质中缺少参数?
目前,无法将我的所有工作切换到着色器材质以利用着色器。
这就是我设置材料的方式:
var material1 = new THREE.ShaderMaterial( {
uniforms: {
color: { type: "c", value: new THREE.Color( 0x22A8E7 ) }
},
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
opacity: 0.5,
transparent: true,
} );
Run Code Online (Sandbox Code Playgroud)
谢谢!
我试图通过本教程使高斯模糊着色器工作: https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson5
但是当我将它转换为统一的着色器语言时,除了透明度(并且没有模糊以太)之外,我的纹理中除了白色什么也没有。
这是我的 Unity 模糊着色器代码:
Shader "Unlit/UnlitShaderTest"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
uniform float resolution = 800;
uniform float radius = 400;
uniform float2 dir = float2(0,1); …Run Code Online (Sandbox Code Playgroud) 在 OpenGL(不是 ES)中,是否有一种通用的方法可以在基于另一个纹理或变量的值进行绘制时基于纹理进行混合?在 OpenGLES 上,我知道我可以通过 GL_EXT_shader_framebuffer_fetch 等扩展在某些平台上进行自定义混合。我问的原因是我有一个特殊的纹理,其中第四个通道不是 alpha,我需要能够将它混合到不同地图上可用的单独 alpha 上。
我正在学习 WebGL 图形编程,最近出现了这段代码。
void main() { gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); }
Run Code Online (Sandbox Code Playgroud)
这是什么意思?我听说它与渲染光有关,但我完全不知道。
在iOS上使用OpenGL ES 3.0时,我想使用一个片段着色器使用一个帧缓冲区对象 (FBO) 绘制 2 个不同的颜色附件(不同时)。但是,我在调用时收到GL_INVALID_OPERATION错误:
const GLenum attachments[] = {GL_COLOR_ATTACHMENT1};
glDrawBuffers(1, attachments);
Run Code Online (Sandbox Code Playgroud)
我检查了 GL 状态并确保当前绑定了正确的 FBO,并且在此调用之前没有 GL_ERROR,并且 GL_MAX_COLOR_ATTACHMENTS 为 4。在 Xcode 中拍摄 GPU 快照会给出以下错误描述:
指定的操作对于当前 OpenGL 状态无效
我创建一个带有 2 个颜色附件的帧缓冲区对象,如下所示:
// Assuming the FBO and the 2 requried textures were correctly generated
glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _fbo_tex[0], 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, _fbo_tex[1], 0);
// glCheckFramebufferStatus(GL_FRAMEBUFFER) returns GL_FRAMEBUFFER_COMPLETE
Run Code Online (Sandbox Code Playgroud)
顶点着色器:
#version 300 es
uniform mat4 modelviewProjectionMatrix;
in vec4 position;
void main() {
gl_Position …Run Code Online (Sandbox Code Playgroud) 我一直在尝试使用texture2d_array来应用金属中的实时滤镜。但我没有得到正确的结果。
我像这样创建纹理数组,
代码:Class MetalTextureArray。
class MetalTextureArray {
private(set) var arrayTexture: MTLTexture
private var width: Int
private var height: Int
init(_ width: Int, _ height: Int, _ arrayLength: Int, _ device: MTLDevice) {
self.width = width
self.height = height
let textureDescriptor = MTLTextureDescriptor()
textureDescriptor.textureType = .type2DArray
textureDescriptor.pixelFormat = .bgra8Unorm
textureDescriptor.width = width
textureDescriptor.height = height
textureDescriptor.arrayLength = arrayLength
arrayTexture = device.makeTexture(descriptor: textureDescriptor)
}
func append(_ texture: MTLTexture) -> Bool {
if let bytes = texture.buffer?.contents() {
let region = …Run Code Online (Sandbox Code Playgroud) 我从 .obj 文件读取数据来绘制模型。我使用纹理和顶点正确绘制模型。但是,当我想使用光照进行绘制时,出现错误。\n此错误是 WebGL:
\n\n\n\n\nINVALID_VALUE:vertexAttribPointer:索引超出范围,INVALID_VALUE:enableVertexAttribArray:索引超出范围。
\n
这段代码是我的顶点着色器
\n\nvar VertexShaderCode = "precision mediump float;\\n"+\n "attribute vec3 vertexPos;\\n" +\n "attribute vec3 vertexNormal;\\n"+\n "uniform mat4 modelViewMatrix;\\n" +\n "uniform mat4 projectionMatrix;\\n" +\n "attribute vec2 aTextureCoord;\\n" +\n "varying vec2 vTextureCoord;\\n" +\n " void main(void) {\\n" +\n " gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPos, 1.0); \\n" +\n " vTextureCoord = aTextureCoord;\\n"+\n "}\\n";\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的片段着色器代码
\n\nvar FragmentShaderCode = "precision lowp float;"+\n "uniform vec4 color;"+\n "varying vec2 vTextureCoord;"+\n "uniform vec3 u_Ambient_color;\\n" +\n …Run Code Online (Sandbox Code Playgroud) 顶点着色器对对象多边形的每个顶点执行一次,返回其对应的v2f. 因此,这意味着顶点着色器不会为每个像素运行\xe2\x80\x99。
由于片段着色器将v2f对象作为输入,如果v2f,如果仅针对与模型顶点的屏幕位置相对应的几个像素生成 s