浏览代码

Raytracing Workers: Attempt to pass scene and camera information

- in json format.
- unfortunately object3d.toJSON doesn't seem to serialize positions
Joshua Koo 9 年之前
父节点
当前提交
237e834a9e

+ 0 - 1
examples/js/renderers/RaytracingRenderer.js

@@ -38,7 +38,6 @@ THREE.RaytracingRenderer = function ( parameters ) {
 	var objects;
 	var lights = [];
 	var cache = {};
-	var timeRendering = 0;
 
 	var animationFrameId = null;
 

+ 20 - 15
examples/js/renderers/RaytracingWorker.js

@@ -2,6 +2,11 @@ var workers, worker;
 var BLOCK = 128;
 var startX, startY, division, completed = 0;
 
+var scene, camera, renderer, loader;
+
+importScripts('../../../build/three.min.js');
+
+
 self.onmessage = function(e) {
 	var data = e.data;
 	if (!data) return;
@@ -12,27 +17,31 @@ self.onmessage = function(e) {
 			width = data.init[0],
 			height = data.init[1];
 
-		eval(data.initScene);
-		initScene(width, height);
 		worker = data.worker;
 		workers = data.workers;
 		BLOCK = data.blockSize;
+		eval(data.initScene);
+		initScene(width, height);
 
-		if (data.maxRecursionDepth) maxRecursionDepth = data.maxRecursionDepth;
+		renderer = new THREE.RaytracingRendererWorker();
+		loader = new THREE.ObjectLoader();
+		renderer.setSize( width, height );
 
-		var xblocks = Math.ceil(width / BLOCK);
-		var yblocks = Math.ceil(height / BLOCK);
+		// TODO fix passing maxRecursionDepth as parameter.
+		// if (data.maxRecursionDepth) maxRecursionDepth = data.maxRecursionDepth;
 
-		division = Math.ceil(xblocks * yblocks / workers);
+		completed = 0;
+	}
 
-		var start = division * worker;
-		startX = (start % xblocks) * BLOCK;
-		startY = (start / xblocks | 0) * BLOCK;
+	if (data.scene) {
+
+		// console.log('scene!!!', scene, camera)
+		// scene = loader.parse(data.scene);
+		// camera = loader.parse(data.camera);
 
-		completed = 0;
 	}
 
-	if (data.render) {
+	if (data.render && scene && camera) {
 		startX = data.x;
 		startY = data.y;
 		renderer.render(scene, camera);
@@ -40,8 +49,6 @@ self.onmessage = function(e) {
 
 }
 
-importScripts('../../../build/three.min.js');
-
 /**
  * DOM-less version of Raytracing Renderer
  * @author mrdoob / http://mrdoob.com/
@@ -49,8 +56,6 @@ importScripts('../../../build/three.min.js');
  * @author zz95 / http://github.com/zz85
  */
 
-THREE.RaytracingRenderer =
-
 THREE.RaytracingRendererWorker = function ( parameters ) {
 
 	console.log( 'THREE.RaytracingRenderer', THREE.REVISION );

+ 10 - 0
examples/js/renderers/RaytracingWorkerRenderer.js

@@ -137,6 +137,16 @@ THREE.RaytracingWorkerRenderer = function ( parameters ) {
 
 	this.render = function ( scene, camera ) {
 
+		var sceneJSON = scene.toJSON();
+		var cameraJSON = camera.toJSON();
+
+		pool.forEach(function(worker) {
+			worker.postMessage({
+				scene: sceneJSON,
+				camera: cameraJSON
+			});
+		});
+
 		context.clearRect( 0, 0, canvasWidth, canvasHeight );
 		reallyThen = Date.now();
 

+ 6 - 7
examples/raytracing_sandbox_workers.html

@@ -19,9 +19,9 @@
 
 		<script>
 
-			var hash = location.hash ? location.hash.substring(1) : '4';
+			var hash = location.hash ? location.hash.substring(1) : '3';
 
-			var WORKERS = +hash || navigator.hardwareConcurrency || 4;
+			var WORKERS = +hash || navigator.hardwareConcurrency || 3;
 
 			var container;
 
@@ -32,9 +32,7 @@
 			init();
 			render();
 
-			// this will be converted to a string to be passed to the workers
 			function initScene(width, height) {
-				console.log('initScene', width, height);
 
 				camera = new THREE.PerspectiveCamera( 60, width / height, 1, 1000 );
 				camera.position.z = 600;
@@ -231,9 +229,6 @@
 				light.physicalAttenuation = true;
 				scene.add( light );
 
-				renderer = new THREE.RaytracingRenderer();
-				renderer.setSize( width, height );
-
 			}
 
 			function init() {
@@ -252,6 +247,10 @@
 
 				//
 
+				initScene( window.innerWidth, window.innerHeight );
+
+				//
+
 				renderer = new THREE.RaytracingWorkerRenderer({
 					workers: WORKERS
 				});