Browse Source

Adding RenderToTexture example

Josh Engebretson 10 years ago
parent
commit
9bcc295435

+ 9 - 0
RenderToTexture/RenderToTexture.atomic

@@ -0,0 +1,9 @@
+{
+   "version": 1,
+   "project": {
+      "version": "1.0.0"
+   },
+   "platforms": [
+      "mac", "windows"
+   ]
+}

+ 94 - 0
RenderToTexture/Resources/Components/RenderBox.js

@@ -0,0 +1,94 @@
+
+// Render Box which has a
+
+
+var game = Atomic.game;
+var node = self.node;
+
+var chestScene;
+var chestNode;
+var chestCamera;
+
+
+// Create a scene which get's rendered to texture
+function createChestScene() {
+
+  chestScene = new Atomic.Scene();
+
+  chestScene.createComponent("Octree");
+
+  // zone
+  var zoneNode = chestScene.createChild("Zone")
+  var zone = zoneNode.createComponent("Zone");
+
+  zone.boundingBox = [-1000, -1000, -1000, 1000, 1000 , 1000];
+  zone.ambientColor = [0.0, 0.0, 0.0];
+  zone.fogColor = [.2, .2, .2, 1.0];
+  zone.fogStart = 10;
+  zone.fogEnd = 100;
+
+  chestNode = chestScene.createChild("Chest");
+  chestNode.pitch(-90);
+
+  var model = chestNode.createComponent("StaticModel");
+  model.setModel(game.cache.getResource("Model", "Models/Chest.mdl"));
+  model.setMaterial(game.cache.getResource("Material", "Materials/Chest.xml"));
+
+  // Create a camera for the render-to-texture scene. Simply leave it at the world origin and let it observe the scene
+  var cameraNode = chestScene.createChild("Camera");
+  chestCamera = cameraNode.createComponent("Camera");
+
+  cameraNode.position = [0, .5, -4];
+  cameraNode.pitch(0);
+
+  chestCamera.farClip = 100;
+
+  // Create a point light to the camera scene node
+  var light = cameraNode.createComponent("Light");
+  light.lightType = Atomic.LIGHT_POINT;
+  light.range = 30;
+
+}
+
+
+function start() {
+
+    createChestScene();
+
+    var cache = game.cache;
+
+    var model = node.createComponent("StaticModel");
+    model.setModel(cache.getResource("Model", "Models/Box.mdl"));
+
+    // Create a renderable texture (1024x768, RGB format), enable bilinear filtering on it
+    var renderTexture = new Atomic.Texture2D();
+
+    renderTexture.setSize(1024, 1024, game.graphics.getRGBFormat(), Atomic.TEXTURE_RENDERTARGET);
+    renderTexture.filterMode = Atomic.FILTER_BILINEAR;
+
+    // Create a new material from scratch, use the diffuse unlit technique, assign the render texture
+    // as its diffuse texture, then assign the material to the screen plane object
+    var renderMaterial = new Atomic.Material();
+    renderMaterial.setTechnique(0, game.cache.getResource("Technique", "Techniques/Diff.xml"));
+    renderMaterial.setTexture(Atomic.TU_DIFFUSE, renderTexture);
+    model.setMaterial(renderMaterial);
+
+    // Get the texture's RenderSurface object (exists when the texture has been created in rendertarget mode)
+    // and define the viewport for rendering the second scene, similarly as how backbuffer viewports are defined
+    // to the Renderer subsystem. By default the texture viewport will be updated when the texture is visible
+    // in the main view
+    var surface = renderTexture.getRenderSurface();
+    var rttViewport = new Atomic.Viewport(chestScene, chestCamera);
+    surface.setViewport(0, rttViewport);
+
+    node.pitch(-90);
+
+}
+
+
+function update(timeStep) {
+
+   node.roll(timeStep * 15);
+   chestNode.roll(timeStep * 75);
+
+}

+ 5 - 0
RenderToTexture/Resources/Materials/Chest.xml

@@ -0,0 +1,5 @@
+<material>
+    <technique name="Techniques/Diff.xml" quality="0" />
+    <texture unit="diffuse" name="Textures/chest.png" />
+    <parameter name="MatSpecColor" value="0.3 0.3 0.3 16" />
+</material>

BIN
RenderToTexture/Resources/Models/Box.mdl


BIN
RenderToTexture/Resources/Models/Chest.mdl


+ 45 - 0
RenderToTexture/Resources/Scripts/main.js

@@ -0,0 +1,45 @@
+
+// This script is the main entry point of the game
+
+require("AtomicGame");
+
+Atomic.game.init(start, update);
+
+
+// called at the start of play
+function start() {
+
+	var game = Atomic.game;
+
+	// create a 2D scene
+	game.createScene3D();
+
+	var scene = game.scene;
+
+	// zone
+	var zoneNode = scene.createChild("Zone")
+	var zone = zoneNode.createComponent("Zone");
+	zone.boundingBox = [-1000, -1000, -1000, 1000, 1000 , 1000];
+	zone.ambientColor = [0.35, 0.35, 0.35];
+	zone.fogColor = [0.1, 0.1, 0.1, 1.0];
+	zone.fogStart = 10;
+	zone.fogEnd = 100;
+
+	game.cameraNode.position = [0, 1.0, -3];
+	game.cameraNode.pitch(20);
+
+	var lightNode = scene.createChild("Directional Light");
+	lightNode.direction = [0.6, -1.0, 0.8];
+	var light = lightNode.createComponent("Light")
+	light.lightType = Atomic.LIGHT_DIRECTIONAL;
+
+	var node = game.scene.createChild("RenderBox");
+	node.createJSComponent("RenderBox");
+
+}
+
+// called per frame
+function update(timeStep) {
+
+
+}

BIN
RenderToTexture/Resources/Textures/chest.png