Skip to content

Commit e2a6e30

Browse files
committed
fixed BatchedPrimitive add a PlanetMaterial
1 parent 5f15e32 commit e2a6e30

6 files changed

Lines changed: 92 additions & 27 deletions

File tree

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11

22
export * from './src/engine/primitives/primitive.js';
3+
export * from './src/engine/primitives/batched.js';
4+
export * from './src/engine/primitives/sphere.js';
35
export * from './src/engine/dataStructures/octree.js';
46
export * from './src/engine/geometries/geometry.js'
57
export * from './src/core/shaders/tsl/functions.js'
68
export * from './src/core/shaders/tsl/noise.js'
79
export * from './src/core/shaders/tsl/utils.js'
810
export * from './src/core/shaders/wgsl/noise.js'
9-
11+
1012
export * from './src/components/bodies/planet.js'
13+
export * from './src/components/materials/planetMaterial.js'
1114

1215
export * from './src/engine/utils/boundingBoxUtils.js'
1316

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as THREE from 'three'
2+
3+
export class PlanetMaterial extends THREE.ShaderMaterial {
4+
5+
constructor(parameters) {
6+
super();
7+
8+
this.uniforms = {
9+
resolution: { value: parameters.resolution || 256 },
10+
// textures
11+
cubeNormal: { value: parameters.cubeNormal },
12+
cubeDisplacements: { value: parameters.cubeDisplacements },
13+
cubeColor: { value: parameters.cubeColor },
14+
// check if textures are provided
15+
hasNormals: { value: parameters.cubeNormal ? true : false },
16+
hasColors: { value: parameters.cubeColor ? true : false },
17+
};
18+
19+
this.vertexShader =`
20+
varying vec3 vPosition;
21+
uniform samplerCube cubeDisplacements;
22+
23+
void main() {
24+
vPosition = position;
25+
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
26+
}`
27+
28+
this.fragmentShader = `
29+
varying vec3 vPosition;
30+
uniform samplerCube cubeNormal;
31+
uniform samplerCube cubeDisplacements;
32+
uniform samplerCube cubeColor;
33+
34+
uniform bool hasNormals;
35+
uniform bool hasColors;
36+
37+
vec4 baseColor;
38+
vec4 baseNormal;
39+
40+
void main() {
41+
baseColor = vec4(1.0);
42+
baseNormal = vec4(1.0);
43+
if(hasNormals){ baseNormal = textureCube(cubeNormal, vPosition );}
44+
if(hasColors){ baseColor = textureCube(cubeColor, vPosition );}
45+
gl_FragColor = baseColor;
46+
}`
47+
}
48+
49+
onBeforeRender( ){
50+
this.uniforms.hasNormals.value = this.uniforms.cubeNormal ? true : false
51+
this.uniforms.hasColors.value = this.uniforms.cubeColor ? true : false
52+
}
53+
}

src/core/postProcess/postProcesser.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,11 @@ export class postProcesser {
88
this.camera = camera
99
this.renderer = renderer
1010

11-
const depthTexture = new THREE.DepthTexture();
12-
depthTexture.type = THREE.FloatType;
13-
14-
this.renderTarget = new THREE.RenderTarget( window.innerWidth , window.innerHeight );
15-
this.renderTarget.depthTexture = depthTexture;
16-
17-
const materialFX = new THREE.MeshBasicNodeMaterial();
18-
materialFX.colorNode = texture( this.renderTarget.texture );
19-
20-
this.quad = new THREE.QuadMesh( materialFX );
11+
2112
}
2213

2314
render(){
24-
this.renderer.setRenderTarget( this.renderTarget );
25-
this.renderer.render( this.scene, this.camera );
26-
27-
this.renderer.setRenderTarget( null );
28-
this.quad.render( this.renderer );
15+
2916
}
3017
}
3118

src/engine/primitives/batched.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import * as THREE from 'three'
22
import { Primitive } from "./primitive.js"
3+
import { isSphere } from './../utils/primitiveUtils.js'
34

45
export class BatchedPrimitive extends Primitive{
56
static __type = 'BatchedPrimitive'
67

7-
constructor(primitive,params){
8+
constructor(type,params){
89

910
super(params)
10-
11-
this.constructor.__type = primitive.__type // dont like the idea this redefines what the actual type is
11+
12+
this.constructor.__type = type // dont like the idea this redefines what the actual type is / hardcode for now
1213

1314
if(isSphere(this)) this.infrastructure.config.radius = params.radius
1415

1516
}
1617

1718
createDimensions(){
1819

19-
let batchedMesh = new THREE.BatchedMesh(0,0,0, new THREE.MeshStandardMaterial())
20+
let batchedMesh = new THREE.BatchedMesh(1,1,1, this.infrastructure.config.material)
2021

2122
this._transferGeometry(batchedMesh)
2223

@@ -40,14 +41,16 @@ export class BatchedPrimitive extends Primitive{
4041

4142
let polyPerLevel = this.infrastructure.config.levels.polyPerLevel
4243

43-
this.infrastructure.on('afterMeshCreation', ( node, payload ) => {
44+
this.infrastructure.events.on('afterMeshCreation', ( node, payload ) => {
4445

4546
const parent = node.parent;
4647

4748
parent.remove( node );
4849

4950
const geometry = node.mesh().geometry
50-
51+
52+
const material = node.mesh().material
53+
5154
const depth = node.params.depth
5255

5356
const vertex = polyPerLevel[depth]
@@ -77,10 +80,29 @@ export class BatchedPrimitive extends Primitive{
7780
batchedMesh.setMatrixAt( id, matrix );
7881

7982
batchedMesh.setColorAt ( id, new THREE.Color( Math.random() * 0xffffff ) )
83+
84+
geometry.dispose() // might need this when phyics engine works
85+
86+
material.dispose()
8087

8188
})
82-
8389
}
84-
8590
}
86-
91+
92+
//example usage
93+
/*
94+
let planet2 = new BatchedPrimitive(122,{
95+
offset:1/0.5 ,
96+
levels:1,
97+
size:1,
98+
radius:10.0,
99+
resolution:50,
100+
dimension:10
101+
})
102+
planet2.infrastructure.config.lodDistanceOffset = 1
103+
planet2.createQuadTree({levels:1})
104+
105+
planet2.createMeshNodes()
106+
107+
planet2.createDimensions()
108+
*/

src/engine/system/infrastructure.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ export class Infrastructure {
2929
displacmentScale:1,
3030
visibilityLayer:{hiddenLayer:1,visibleLayer:0},
3131
material: new THREE.MeshPhongMaterial({
32-
3332
shininess: 10,
3433
flatShading: false,
35-
emissive: new THREE.Color(0x0a0a0a), // Add slight emissive to prevent pure black
34+
emissive: new THREE.Color(0x0a0a0a),
3635
emissiveIntensity: 1.0
3736
})
3837
}

0 commit comments

Comments
 (0)