1+ /*
2+ * Copyright (c) 2009-2025 jMonkeyEngine
3+ * All rights reserved.
4+ *
5+ * Redistribution and use in source and binary forms, with or without
6+ * modification, are permitted provided that the following conditions are
7+ * met:
8+ *
9+ * * Redistributions of source code must retain the above copyright
10+ * notice, this list of conditions and the following disclaimer.
11+ *
12+ * * Redistributions in binary form must reproduce the above copyright
13+ * notice, this list of conditions and the following disclaimer in the
14+ * documentation and/or other materials provided with the distribution.
15+ *
16+ * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+ * may be used to endorse or promote products derived from this software
18+ * without specific prior written permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ */
132package com .jme3 .material .plugin .export .material ;
233
334import com .jme3 .export .OutputCapsule ;
839import java .util .HashMap ;
940
1041/**
42+ * The `J3MRootOutputCapsule` class extends `J3MOutputCapsule` and serves as the
43+ * root output capsule for exporting jME materials (`.j3m` files).
44+ *
1145 * @author tsr
1246 */
1347public class J3MRootOutputCapsule extends J3MOutputCapsule {
1448
15- private final HashMap <Savable , J3MOutputCapsule > outCapsules ;
49+ /**
50+ * Stores a map of `Savable` objects to their corresponding `J3MOutputCapsule` instances.
51+ * This allows for managing and exporting different components (e.g., render states)
52+ * of a material.
53+ */
54+ private final HashMap <Savable , J3MOutputCapsule > outCapsules = new HashMap <>();
55+ // The name of the material.
1656 private String name ;
17- private String materialDefinition ;
57+ // The material definition string (e.g., "Common/MatDefs/Light.j3md").
58+ private String materialDef ;
59+ // Indicates whether the material is transparent
1860 private Boolean isTransparent ;
19-
61+ // Indicates whether the material receives shadows
62+ private Boolean receivesShadows ;
63+
64+ /**
65+ * Constructs a new `J3MRootOutputCapsule`.
66+ *
67+ * @param exporter The `J3MExporter` instance used for exporting savable objects.
68+ */
2069 public J3MRootOutputCapsule (J3MExporter exporter ) {
2170 super (exporter );
22- outCapsules = new HashMap <>();
2371 }
2472
73+ /**
74+ * Clears all data within this capsule and its superclass.
75+ * Resets material properties to their default or null values and clears
76+ * the map of savable capsules.
77+ */
2578 @ Override
2679 public void clear () {
2780 super .clear ();
2881 isTransparent = null ;
82+ receivesShadows = null ;
2983 name = "" ;
30- materialDefinition = "" ;
84+ materialDef = "" ;
3185 outCapsules .clear ();
32-
3386 }
3487
88+ /**
89+ * Retrieves an `OutputCapsule` for a given `Savable` object.
90+ * If a capsule for the object does not exist, a new `J3MRenderStateOutputCapsule`
91+ * is created and associated with the object.
92+ *
93+ * @param object The `Savable` object for which to retrieve or create a capsule.
94+ * @return The `OutputCapsule` associated with the given savable object.
95+ */
3596 public OutputCapsule getCapsule (Savable object ) {
3697 if (!outCapsules .containsKey (object )) {
3798 outCapsules .put (object , new J3MRenderStateOutputCapsule (exporter ));
3899 }
39-
40100 return outCapsules .get (object );
41101 }
42102
43103 @ Override
44104 public void writeToStream (Writer out ) throws IOException {
45- out .write ("Material " + name + " : " + materialDefinition + " {\n \n " );
105+ out .write ("Material " + name + " : " + materialDef + " {\n \n " );
106+
46107 if (isTransparent != null )
47- out .write (" Transparent " + ((isTransparent ) ? "On" : "Off" ) + "\n \n " );
108+ out .write (" Transparent " + (isTransparent ? "On" : "Off" ) + "\n \n " );
109+ if (receivesShadows != null )
110+ out .write (" ReceivesShadows " + (receivesShadows ? "On" : "Off" ) + "\n \n " );
48111
49112 out .write (" MaterialParameters {\n " );
50- super .writeToStream (out );
113+ super .writeToStream (out ); // Writes parameters from the superclass
51114 out .write (" }\n \n " );
52115
116+ // Write out encapsulated savable object data
53117 for (J3MOutputCapsule c : outCapsules .values ()) {
54118 c .writeToStream (out );
55119 }
@@ -60,7 +124,7 @@ public void writeToStream(Writer out) throws IOException {
60124 public void write (String value , String name , String defVal ) throws IOException {
61125 switch (name ) {
62126 case "material_def" :
63- materialDefinition = value ;
127+ materialDef = value ;
64128 break ;
65129 case "name" :
66130 this .name = value ;
@@ -72,21 +136,26 @@ public void write(String value, String name, String defVal) throws IOException {
72136
73137 @ Override
74138 public void write (boolean value , String name , boolean defVal ) throws IOException {
75- if ( value == defVal )
139+ // No need to write if the value is the same as the default.
140+ if (value == defVal ) {
76141 return ;
142+ }
77143
78144 switch (name ) {
79145 case "is_transparent" :
80146 isTransparent = value ;
81147 break ;
148+ case "receives_shadows" :
149+ receivesShadows = value ;
150+ break ;
82151 default :
83152 throw new UnsupportedOperationException (name + " boolean material parameter not supported yet" );
84153 }
85154 }
86155
87156 @ Override
88157 public void write (Savable object , String name , Savable defVal ) throws IOException {
89- if (object != null && !object .equals (defVal )) {
158+ if (object != null && !object .equals (defVal )) {
90159 object .write (exporter );
91160 }
92161 }
0 commit comments