在three.js中創建一個小球並且小球在外部添加輝光


本文參考 : https://blog.csdn.net/srk19960903/article/details/78734238

我在這里,非常感謝大佬的分享!!!!!

 

邏輯步驟:

        1.創建兩個球體,一個作為原始物體,一個略大一些作為它的輝光。

        2.作為輝光的球體從內到外片元透明度逐漸減小(線性減小或是指數減小都可以)

        3.將覆蓋原始物體的部分丟棄掉

 

let vertexShader    = [
'varying vec3 vVertexWorldPosition;',
'varying vec3 vVertexNormal;',
'varying vec4 vFragColor;',
'void main(){',
' vVertexNormal = normalize(normalMatrix * normal);',//將法線轉換到視圖坐標系中
' vVertexWorldPosition = (modelMatrix * vec4(position, 1.0)).xyz;',//將頂點轉換到世界坐標系中
' // set gl_Position',
' gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);',
'}'
].join('\n');
let fragmentShader1 = [
'uniform vec3 glowColor;',
'uniform float coeficient;',
'uniform float power;',
'varying vec3 vVertexNormal;',
'varying vec3 vVertexWorldPosition;',
'varying vec4 vFragColor;',
'void main(){',
' vec3 worldCameraToVertex= vVertexWorldPosition - cameraPosition;', //世界坐標系中從相機位置到頂點位置的距離
' vec3 viewCameraToVertex = (viewMatrix * vec4(worldCameraToVertex, 0.0)).xyz;',//視圖坐標系中從相機位置到頂點位置的距離
' viewCameraToVertex = normalize(viewCameraToVertex);',//規一化
' float intensity = pow(coeficient + dot(vVertexNormal, viewCameraToVertex), power);',
' gl_FragColor = vec4(glowColor, intensity);',
'}'//vVertexNormal視圖坐標系中點的法向量
//viewCameraToVertex視圖坐標系中點到攝像機的距離向量
//dot點乘得到它們的夾角的cos值
//從中心向外面角度越來越小(從鈍角到銳角)從cos函數也可以知道這個值由負變正,不透明度最終從低到高
].join('\n');
// let fragmentShader2 = [
// 'uniform vec3 glowColor;',
// 'uniform float coeficient;',
// 'uniform float power;',
// 'varying vec3 vVertexNormal;',
// 'varying vec3 vVertexWorldPosition;',
// 'varying vec4 vFragColor;',
// 'void main(){',
// ' vec3 worldVertexToCamera= cameraPosition - vVertexWorldPosition;', //世界坐標系中頂點位置到相機位置到的距離
// ' vec3 viewCameraToVertex = (viewMatrix * vec4(worldVertexToCamera, 0.0)).xyz;',//視圖坐標系中從相機位置到頂點位置的距離
// ' viewCameraToVertex = normalize(viewCameraToVertex);',//規一化
// ' float intensity = coeficient + dot(vVertexNormal, viewCameraToVertex);',
// ' if(intensity > 0.55){ intensity = 0.0;}',
// ' gl_FragColor = vec4(glowColor, intensity);',
// '}'//vVertexNormal視圖坐標系中點的法向量
// //viewCameraToVertex視圖坐標系中點到攝像機的距離向量
// //dot點乘得到它們的夾角的cos值
// //從中心向外面角度越來越大(從銳角到鈍角)從cos函數也可以知道這個值由負變正,不透明度最終從高到低
// ].join('\n');

//本質透明度遞減
let sphere = new THREE.SphereBufferGeometry( 16, 32, 32 );
let material = new THREE.ShaderMaterial({
uniforms: {
coeficient : {
type : "f",
value : 1.0
},
power : {
type : "f",
value : 2
},
glowColor : {
type : "c",
value : new THREE.Color('#01d8d2')
}
},
vertexShader : vertexShader,
fragmentShader : fragmentShader1,
blending : THREE.NormalBlending,
transparent : true
});
// let material_glow = new THREE.ShaderMaterial({
// uniforms: {
// coeficient : {
// type : "f",
// value : 0.0
// },
// power : {
// type : "f",
// value : 2
// },
// glowColor : {
// type : "c",
// value : new THREE.Color('#01d8d2')
// }
// },
// vertexShader : vertexShader,
// fragmentShader : fragmentShader2,
// blending : THREE.NormalBlending,
// transparent : true
// });
let group = new THREE.Group()
let mesh = new THREE.Mesh(sphere, material);
let sphere2 = new THREE.SphereBufferGeometry( 8, 32, 32 );
let material2 = new THREE.MeshPhongMaterial({color:new THREE.Color('#01d8d2')
});
let mesh2 = new THREE.Mesh(sphere2, material2);
group.add(mesh);
group.add(mesh2);
group.position.set(550,300,-2)
this.scene.add(group);

效果圖如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM