【发布时间】:2016-03-08 15:18:50
【问题描述】:
在这里,我有一个有四个顶点和四个面的几何(金字塔) -
var geom = new THREE.Geometry();
geom.vertices.push(new THREE.Vector3(0,100,0), new THREE.Vector3(-100,-100,100), new THREE.Vector3(0,-100,-100), new THREE.Vector3(100,-100,100));
geom.faces.push( new THREE.Face3( 0, 2, 1), new THREE.Face3( 0, 1, 3), new THREE.Face3( 0, 3, 2), new THREE.Face3( 1, 2, 3) );
geom.computeFaceNormals();
这是我的 RawShaderMaterial -
var geomMaterial = new THREE.RawShaderMaterial({
vertexShader: [
'precision highp float;',
'precision highp int;',
'uniform mat4 modelViewMatrix;',
'uniform mat4 projectionMatrix;',
'attribute vec3 position;',
'attribute vec2 uv;',
'varying vec2 interpolatedUV;',
'void main() {',
'interpolatedUV = uv;',
'gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);',
'}'
].join('\n'),
fragmentShader: [
'precision highp float;',
'precision highp int;',
'uniform sampler2D texSampler;',
'varying vec2 interpolatedUV;',
'void main() {',
'gl_FragColor = texture2D(texSampler, interpolatedUV);',
'}'
].join('\n'),
uniforms: {
texSampler: {
type: 't',
value: new THREE.ImageUtils.loadTexture("images/test.png")
}
}
});
"images/test.png" 可以从脚本中访问。这对我来说似乎微不足道,但纹理根本没有出现。我只能看到一个白色的金字塔。
你能告诉我它到底出了什么问题吗?
更新:
在四处挖掘之后,我发现我必须为我创建的自定义几何体提供 UV 贴图。所以我以这种方式添加了它-
var uvs = [];
uvs.push(new THREE.Vector2(0.5, 1.0));
uvs.push(new THREE.Vector2(0.5, 0.0));
uvs.push(new THREE.Vector2(0.0, 0.0));
uvs.push(new THREE.Vector2(1.0, 0.0));
geom.faces.push( new THREE.Face3( 0, 2, 1));
geom.faceVertexUvs[0].push(uvs[0], uvs[2], uvs[1]);
geom.faces.push( new THREE.Face3( 0, 1, 3));
geom.faceVertexUvs[0].push(uvs[0], uvs[1], uvs[3]);
geom.faces.push( new THREE.Face3( 0, 3, 2));
geom.faceVertexUvs[0].push(uvs[0], uvs[3], uvs[2]);
geom.faces.push( new THREE.Face3( 1, 2, 3));
geom.faceVertexUvs[0].push(uvs[1], uvs[2], uvs[3]);
但它仍然显示白色金字塔。而且我现在也收到此错误 -
THREE.BufferAttribute.copyVector2sArray(): 向量未定义 0 三.BufferAttribute.copyVector2sArray():向量未定义 1 三.BufferAttribute.copyVector2sArray():向量未定义 2 三.BufferAttribute.copyVector2sArray():向量未定义 3 三.BufferAttribute.copyVector2sArray():向量未定义 4 三.BufferAttribute.copyVector2sArray():向量未定义 5 三.BufferAttribute.copyVector2sArray():向量未定义 6 三.BufferAttribute.copyVector2sArray():向量未定义 7 三.BufferAttribute.copyVector2sArray():向量未定义 8 三.BufferAttribute.copyVector2sArray():向量未定义 9 三.BufferAttribute.copyVector2sArray():向量未定义 10 三.BufferAttribute.copyVector2sArray():向量未定义 11
有什么想法吗?
【问题讨论】:
-
没有错误?我认为原始着色器材料不会给你 projectionMatrix MVMatrix 等。
-
如果我删除我的自定义几何并提供一个 sphereGeometry,它工作正常。所以我认为问题不存在。
-
做 gl_FragColor = vec4( abs( interpolatedUV ) , 0. , 1.);看看它是否给出任何颜色
-
hmm,interpolatedUV 包含每个片段的 (0, 0)。这意味着属性“uv”没有被传递给顶点着色器。知道为什么会这样吗?
-
最后一次制作这样的几何图形是在大约 60 个版本 :) 不完全确定,但它是一个开始。
标签: javascript three.js textures