@@ -195,7 +195,7 @@ final class MoleculeRenderer {
195
195
sphereAmbientOcclusionTexture. label = " Ambient occlustion texture "
196
196
197
197
// TODO: Extend ambient occlusion to ball-and-stick rendering modes.
198
- if visualizationStyle == . ballAndStick {
198
+ if ( visualizationStyle == . ballAndStick) || ( molecule . atoms . count < 100 ) {
199
199
// Set the ambient lighting texture to all-white for uniform illumination.
200
200
if let commandBuffer = sharedMetalRenderingDevice. commandQueue. makeCommandBuffer ( ) {
201
201
commandBuffer. clear ( texture: sphereAmbientOcclusionTexture, with: MTLClearColorMake ( 1.0 , 1.0 , 1.0 , 1.0 ) )
@@ -206,25 +206,25 @@ final class MoleculeRenderer {
206
206
}
207
207
208
208
guard let commandBuffer = sharedMetalRenderingDevice. commandQueue. makeCommandBuffer ( ) else { return }
209
- commandBuffer. clear ( texture: sphereAmbientOcclusionTexture, with: MTLClearColorMake ( 0.0 , 0.0 , 0.0 , 0 .0) )
209
+ commandBuffer. clear ( texture: sphereAmbientOcclusionTexture, with: MTLClearColorMake ( 0.0 , 0.0 , 0.0 , 1 .0) )
210
210
let sphereDepthTextureWidth = 1024
211
211
212
- let intensityFactor : Float = 0.5 / Float( ambientOcclusionSamplingAngles. count)
213
- // let intensityFactor: Float = 1.0
212
+ let depthTextureDescriptor = MTLTextureDescriptor . texture2DDescriptor ( pixelFormat: . bgra8Unorm,
213
+ width: sphereDepthTextureWidth,
214
+ height: sphereDepthTextureWidth,
215
+ mipmapped: false )
216
+ depthTextureDescriptor. usage = [ . renderTarget, . shaderRead, . shaderWrite]
217
+ guard let depthTexture = sharedMetalRenderingDevice. device. makeTexture ( descriptor: depthTextureDescriptor) else {
218
+ fatalError ( " Could not create depth texture of size: ( \( sphereDepthTextureWidth) , \( sphereDepthTextureWidth) ) " )
219
+ }
220
+ depthTexture. label = " Depth texture "
221
+
222
+
223
+ let intensityFactor : Float = 2.0 / Float( ambientOcclusionSamplingAngles. count)
214
224
for (theta, phi) in ambientOcclusionSamplingAngles {
215
225
var rotationMatrix = Matrix4x4 . identity. rotated ( angle: theta, x: 1.0 , y: 0.0 , z: 0.0 )
216
226
rotationMatrix = rotationMatrix. rotated ( angle: phi, x: 0.0 , y: 1.0 , z: 0.0 )
217
227
218
- let depthTextureDescriptor = MTLTextureDescriptor . texture2DDescriptor ( pixelFormat: . bgra8Unorm,
219
- width: sphereDepthTextureWidth,
220
- height: sphereDepthTextureWidth,
221
- mipmapped: false )
222
- depthTextureDescriptor. usage = [ . renderTarget, . shaderRead, . shaderWrite]
223
- guard let depthTexture = sharedMetalRenderingDevice. device. makeTexture ( descriptor: depthTextureDescriptor) else {
224
- fatalError ( " Could not create depth texture of size: ( \( sphereDepthTextureWidth) , \( sphereDepthTextureWidth) ) " )
225
- }
226
- depthTexture. label = " Depth texture "
227
-
228
228
renderDepthTexture ( targetModelViewProjMatrix: rotationMatrix, buffer: commandBuffer, depthTexture: depthTexture)
229
229
renderAmbientOcclusion ( targetModelViewProjMatrix: rotationMatrix, buffer: commandBuffer, depthTexture: depthTexture, intensityFactor: intensityFactor)
230
230
@@ -239,7 +239,7 @@ final class MoleculeRenderer {
239
239
}
240
240
241
241
func renderDepthTexture( targetModelViewProjMatrix: Matrix4x4 , buffer: MTLCommandBuffer , depthTexture: MTLTexture ) {
242
- let orthographicMatrix = orthographicMatrix ( left: - 1.0 , right: 1.0 , bottom: Float ( - 1.0 * 1024 / 1024 ) , top: Float ( 1024 / 1024 ) , near: - 1.0 , far: 1.0 )
242
+ let orthographicMatrix = orthographicMatrix ( left: - 1.0 , right: 1.0 , bottom: - 1.0 , top: 1.0 , near: - 1.0 , far: 1.0 )
243
243
244
244
let depthStencilDescriptor1 = MTLTextureDescriptor . texture2DDescriptor ( pixelFormat: . depth32Float,
245
245
width: 1024 ,
@@ -314,7 +314,7 @@ final class MoleculeRenderer {
314
314
}
315
315
316
316
func renderAmbientOcclusion( targetModelViewProjMatrix: Matrix4x4 , buffer: MTLCommandBuffer , depthTexture: MTLTexture , intensityFactor: Float ) {
317
- let orthographicMatrix = orthographicMatrix ( left: - 1.0 , right: 1.0 , bottom: Float ( - 1.0 * 1024 / 1024 ) , top: Float ( 1024 / 1024 ) , near: - 1.0 , far: 1.0 )
317
+ let orthographicMatrix = orthographicMatrix ( left: - 1.0 , right: 1.0 , bottom: - 1.0 , top: 1.0 , near: - 1.0 , far: 1.0 )
318
318
319
319
let renderPass = MTLRenderPassDescriptor ( )
320
320
renderPass. colorAttachments [ 0 ] . texture = sphereAmbientOcclusionTexture
@@ -375,20 +375,6 @@ final class MoleculeRenderer {
375
375
}
376
376
377
377
func renderMoleculeFrame( width: CGFloat , height: CGFloat , buffer: MTLCommandBuffer , renderPass: MTLRenderPassDescriptor ) {
378
- let sphereDepthTextureWidth = 1024
379
- let depthTextureDescriptor = MTLTextureDescriptor . texture2DDescriptor ( pixelFormat: . bgra8Unorm,
380
- width: sphereDepthTextureWidth,
381
- height: sphereDepthTextureWidth,
382
- mipmapped: false )
383
- depthTextureDescriptor. usage = [ . renderTarget, . shaderRead, . shaderWrite]
384
- guard let depthTexture = sharedMetalRenderingDevice. device. makeTexture ( descriptor: depthTextureDescriptor) else {
385
- fatalError ( " Could not create depth texture of size: ( \( sphereDepthTextureWidth) , \( sphereDepthTextureWidth) ) " )
386
- }
387
- depthTexture. label = " Depth texture "
388
- // renderDepthTexture(buffer: buffer, depthTexture: depthTexture)
389
- // renderAmbientOcclusion(buffer: buffer, depthTexture: depthTexture, intensityFactor: 1.0)
390
-
391
-
392
378
let orthographicMatrix = orthographicMatrix ( left: - 1.0 , right: 1.0 , bottom: Float ( - 1.0 * height / width) , top: Float ( height / width) , near: - 1.0 , far: 1.0 )
393
379
394
380
renderPass. colorAttachments [ 0 ] . clearColor = MTLClearColorMake ( 0.0 , 0.0 , 0.0 , 1.0 )
0 commit comments