2014-09-30 99 views
1

我有一個我加載到Scenekit中的Collada模型。 當我在模型上執行命中測試時,我能夠檢索命中的模型的紋理座標。IOS8 Scenekit在紋理座標上繪畫

有了這些紋理座標,我應該可以用顏色替換紋理座標。 所以這種方式,我應該可以借鑑模型

如果我錯了,糾正我到目前爲止。

我讀了很多文章到現在,但我只是沒有讓我的着色器正確。 (雖然我沒有得到一些時髦的效果;-)

我的頂點着色器:

precision highp float; 

attribute vec4 position; 
attribute vec2 textureCoordinate; 
attribute vec2 aTexureCoordForColor; //coordinates from the hittest 

uniform mat4 modelViewProjection; 

varying vec2 aTexureCoordForColorVarying; // passing to the fragment shader here 
varying vec2 texCoord; 

void main(void) { 
     // Pass along to the fragment shader 
     texCoord = textureCoordinate; 
     aTexureCoordForColorVarying = aTexureCoordForColor; //assigning here 
     // output the projected position 
     gl_Position = modelViewProjection * position; 
} 

我的片段着色器

precision highp float; 

uniform sampler2D yourTexture; 

uniform vec2 uResolution; 
uniform int uTexureCoordsCount; 

varying vec2 texCoord; 
varying vec2 aTexureCoordForColorVarying; 

void main(void) { 

     ??????????? no idea anymore what to do here 
     gl_FragColor = texture2D(yourTexture, texCoord); 
// 

如果您需要更多的代碼,請讓我知道。

回答

6

首先,着色器不是繪製物體材質的唯一方法。另一個可能適用於您的選項是使用SpriteKit場景作爲素材的內容 - 請參閱this answer以獲取幫助。

如果你堅持着色器路線,你不需要重寫整個着色器程序來繪製現有紋理。 (如果你這樣做了,那麼你會失去SceneKit程序爲你提供的東西,比如光照和凹凸貼圖,除非你真的想要重新設計這些輪子。)而是使用着色器修改器 - 插入到GLSL中的一小段代碼SceneKit着色器程序。 SCNShadable reference解釋如何使用這些。

第三,我不確定你是否以最好的方式爲着色器提供了紋理座標。您希望每個片段都能爲點擊點獲取相同的texcoord值,因此將它作爲屬性傳遞給GL並在頂點和片段之間插入它是沒有意義的。只需將其作爲統一標準傳遞,然後使用鍵值編碼將材料設置在材質上。 (見SCNShadable reference again的信息與KVC結合材質參數。)

最後,讓你的問題的重點... :)

要改變片段着色器的輸出顏色(或着色器修改器)在一組特定的紋理座標處或其附近,只需將您的傳入點擊座標與當前用於常規紋理查找的texcoords集相比較即可。下面是這樣做,會着色器修改路線的例子:

uniform vec2 clickTexcoord; 
// set this from ObjC/Swift code with setValue:forKey: 
// and an NSValue with CGPoint data 

uniform float radius = 0.01; 
// change this to determine how large an area to highlight 

uniform vec3 paintColor = vec4(0.0, 1.0, 0.0); 
// nice and green; you can change this with KVC, too 

#pragma body 

if (distance(_surface.diffuseTexcoord.x, clickTexcoord.x) < radius) { 
    _surface.diffuse.rgb = paintColor 
} 

用這個例子作爲SCNShaderModifierEntryPointSurface着色劑和照明/陰影仍將被應用於結果。如果您希望繪畫覆蓋照明,請改爲使用SCNShaderModifierEntryPointFragment着色器修改器,並使用GLSL片段集_output.color.rgb而不是_surface.color.rgb

+0

哇......這是迄今爲止最好的信息......謝謝。我將處理這些信息,並讓您知道哪些方法最有效。 – Vilois 2014-10-01 09:14:06

+0

是的..謝謝....事情現在有了很多更清楚的.....我添加了shaderModifier,稍微加大了上面的代碼,但總的來說它是一樣的.....非常感謝你 – Vilois 2014-10-01 11:43:45

+0

@rickster你的回答很清楚,我認爲這會幫助我解決我自己的問題。它沒有解決,所以我已經發布了一個問題http://stackoverflow.com/questions/38999782/how-to-use-a-shadermodifier-to-alter-the-color-of-specific-triangles希望像你這樣的人能夠幫助你。順便說一句,你提供的第一個鏈接「這個答案」實際上鍊接到相同的「SCNShadable參考」;那是故意的。另外,在你最後的筆記中你提到_surface.color,我認爲你的意思是_surface.diffuse。 – PKCLsoft 2016-08-19 03:34:37